Skip to content

Commit

Permalink
질문 저장 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
kichan05 committed Feb 12, 2022
1 parent 263fc81 commit 386a0a2
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ fun bindSaveQnAItems(recyclerView: RecyclerView, items : ObservableArrayList<Que


adapter.submitList(items.toMutableList())
}

@BindingAdapter("bindVisibility")
fun bindVisibility(v : View, visibility : Boolean){
v.visibility = if(visibility) View.VISIBLE else View.GONE
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class ChatActivity : BaseActivity<ActivityGroupChatBinding>(R.layout.activity_gr
}

groupChat_RecyclerAdapter.submitList(chatList)
binding.recyclerGroupchat.smoothScrollToPosition(chatList.size)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ class QnASliderActivity : BaseActivity<ActivityQnaSliderBinding>(R.layout.activi
vpQna.adapter = ViewPagerAdapter(this@QnASliderActivity, mutableListOf())
vpQna.registerOnPageChangeCallback(viewModel!!.sliderCallback)

btnQnaShare.setOnClickListener{ shareQuestion(this@QnASliderActivity, getCurrentQuestion()) }
btnQnaShare.setOnClickListener{ shareQuestion(this@QnASliderActivity, getCurrentQuestion()!!) }

btnQnaSaveQuestion.setOnClickListener { saveQuestion(getCurrentQuestion()) }
btnQnaSaveQuestion.setOnClickListener { saveQuestion(getCurrentQuestion()!!) }
}
}


// viewModel.isCurrentQnASave.observe(this){
// Log.d("isCurrentQnASave", it.toString())
// }
}


fun saveQuestion(questionData : Question) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,47 @@ package com.edcan.howtosunrin.ui.qnaSlider

import android.util.Log
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableList
import androidx.fragment.app.Fragment
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.viewpager2.widget.ViewPager2
import com.edcan.howtosunrin.ui.all.bindSaveQnAItems
import com.edcan.howtosunrin.utill.qna.Question
import com.edcan.howtosunrin.ui.splash.qnaDB
import com.edcan.howtosunrin.ui.splash.saveQuestionDB
import com.edcan.howtosunrin.utill.qna.QuestionDatabase
import kotlinx.coroutines.*
import kotlin.math.log

class QnASliderViewModel : ViewModel() {
val questionList = ObservableArrayList<Fragment>()

val saveQnaList = ObservableArrayList<Question>()

var currentPage = 0

val isCurrentQnASave = MutableLiveData<Boolean>(false)

init {
addQuestions(15)
}

val sliderCallback = object : ViewPager2.OnPageChangeCallback(){
override fun onPageSelected(position: Int) {
super.onPageSelected(position)
viewModelScope.launch(Dispatchers.IO) {
CoroutineScope(Dispatchers.IO).async {
addQuestions(15)
}.await()

currentPage = position
val _questions = saveQuestionDB.questionDao().getAll().toList()
withContext(Dispatchers.Main) {
saveQnaList.addAll(_questions)

if(questionList.size - position <= 7){
addQuestions(10)
saveQuestionListIncludeCurrentQuestion()
}
}
}

private suspend fun getAllQuestion() : List<Question>{
return qnaDB.getAllQuestion()
}

private suspend fun getRandomQuestions(count : Int) : List<Question>{
return getAllQuestion().shuffled().slice(0 until count)
}

fun addQuestions(count : Int) = CoroutineScope(Dispatchers.IO).launch {
suspend fun addQuestions(count : Int) {
val fragments = mutableListOf<Fragment>()

val questions = getRandomQuestions(count)
Expand All @@ -52,11 +51,58 @@ class QnASliderViewModel : ViewModel() {
questionList.addAll(fragments)
}

private suspend fun getRandomQuestions(count : Int) : List<Question>{
return getAllQuestion().shuffled().slice(0 until count)
}

private suspend fun getAllQuestion() : List<Question>{
return qnaDB.getAllQuestion()
}


fun saveQuestion(questionData : Question) {
viewModelScope.launch(Dispatchers.IO){
saveQuestionDB.questionDao().insert(questionData)

saveQnaList.add(questionData)
withContext(Dispatchers.Main){ saveQuestionListIncludeCurrentQuestion() }
}
}

fun getCurrentQuestion() : Question? {
return if(0 <= currentPage && currentPage < questionList.size)
(questionList[currentPage] as QnAFragment).question
else null
}


private fun saveQuestionListIncludeCurrentQuestion() {
val currentQuestion = getCurrentQuestion()

if(currentQuestion == null){
isCurrentQnASave.value = false
return
}

isCurrentQnASave.value = saveQnaList.any {
it.checkEqualExcludeId(currentQuestion)
}
}

fun getCurrentQuestion() : Question = (questionList[currentPage] as QnAFragment).question

val sliderCallback = object : ViewPager2.OnPageChangeCallback(){
override fun onPageSelected(position: Int) {
super.onPageSelected(position)

currentPage = position

saveQuestionListIncludeCurrentQuestion()

if(questionList.size - position <= 7){
viewModelScope.launch(Dispatchers.IO) {
addQuestions(10)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ class SplashActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)

qnaDB = DB()
userDB = UserDB()
chatDB = ChatDB()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ data class Question(

@ColumnInfo(name = "answer")
var answer : String = "", //질문에 대한 답
) : Serializable
) : Serializable {
fun checkEqualExcludeId(diffQuestion: Question) : Boolean {

return question == diffQuestion.question && answer == diffQuestion.answer

}
}

// 질문 하나를 저장하는 클래스
73 changes: 54 additions & 19 deletions app/src/main/res/layout/activity_qna_slider.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@
app:layout_constraintEnd_toEndOf="parent"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:text="@{viewModel.isCurrentQnASave + ``}"

app:layout_constraintBottom_toTopOf="@id/txt_qna_msg"
app:layout_constraintStart_toStartOf="@id/guide_qna_line1"
app:layout_constraintEnd_toEndOf="@id/guide_qna_line2"
/>

<TextView
android:id="@+id/txt_qna_msg"
android:layout_width="wrap_content"
Expand All @@ -53,41 +64,65 @@

android:layout_marginBottom="10dp"

app:layout_constraintBottom_toTopOf="@id/btn_qna_share"
app:layout_constraintBottom_toTopOf="@id/linear_qna_buttons"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>


<Button
android:id="@+id/btn_qna_saveQuestion"
<LinearLayout
android:id="@+id/linear_qna_buttons"
android:layout_width="0dp"
android:layout_height="wrap_content"

android:text="질문 저장하기"
android:orientation="horizontal"

android:layout_marginHorizontal="12dp"
android:layout_marginBottom="30dp"
android:layout_marginBottom="32dp"

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@id/guide_qna_line1"
app:layout_constraintEnd_toStartOf="@id/btn_qna_share"
/>
app:layout_constraintEnd_toEndOf="@id/guide_qna_line2"
>
<Button
android:id="@+id/btn_qna_saveQuestion"
android:layout_width="0dp"
android:layout_height="wrap_content"

<Button
android:id="@+id/btn_qna_share"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"

android:text="질문을 공유하기"
android:text="질문 저장하기"

android:layout_marginHorizontal="12dp"
app:bindVisibility="@{!viewModel.isCurrentQnASave}"

app:layout_constraintTop_toTopOf="@id/btn_qna_saveQuestion"
app:layout_constraintBottom_toBottomOf="@id/btn_qna_saveQuestion"
app:layout_constraintStart_toEndOf="@id/btn_qna_saveQuestion"
app:layout_constraintEnd_toEndOf="@id/guide_qna_line2"
/>
android:layout_marginHorizontal="12dp"
/>

<Button
android:id="@+id/btn_qna_removeQuestion"
android:layout_width="0dp"
android:layout_height="wrap_content"

android:layout_weight="1"

android:text="저장 취소하기"

app:bindVisibility="@{viewModel.isCurrentQnASave}"

android:layout_marginHorizontal="12dp"
/>

<Button
android:id="@+id/btn_qna_share"
android:layout_width="0dp"
android:layout_height="wrap_content"

android:layout_weight="1"

android:text="질문을 공유하기"

android:layout_marginHorizontal="12dp"
/>
</LinearLayout>


<androidx.constraintlayout.widget.Guideline
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<item name="buttonBackgroundColor">@color/EDCAN</item>

<item name="editTextBackground">@color/gray1</item>
<item name="editTextTextColor">@color/white</item>
<item name="editTextTextColor">@color/black</item>

<item name="toolBarBackGroundColor">@color/white</item>

Expand Down

0 comments on commit 386a0a2

Please sign in to comment.