Skip to content

Commit

Permalink
#178 [refact] presentation 모듈 내 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
murjune committed Aug 11, 2022
1 parent eebc8a9 commit 6402b5b
Show file tree
Hide file tree
Showing 19 changed files with 128 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import androidx.annotation.DrawableRes
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.hous.data.entity.Category
import com.hous.domain.model.CategoryInfo
import com.hous.hous_aos.R
import com.hous.hous_aos.databinding.ItemRulesRuleBinding

Expand All @@ -16,7 +16,7 @@ class HomeRulesCategoryAdapter(
private val onPlusClick: () -> Unit,
private val onChangeIsSelected: (Int) -> Unit
) :
ListAdapter<Category, RecyclerView.ViewHolder>(
ListAdapter<CategoryInfo, RecyclerView.ViewHolder>(
CategoryOfRuleDiffUtilCallback
) {
private val iconTypeHashMap: HashMap<String, CategoryIconType> = hashMapOf(
Expand Down Expand Up @@ -79,7 +79,7 @@ class HomeRulesCategoryAdapter(
private val binding: ItemRulesRuleBinding
) : RecyclerView.ViewHolder(binding.root) {

fun onBind(data: com.hous.data.entity.Category) {
fun onBind(data: CategoryInfo) {
binding.data = data
binding.iconType = iconTypeHashMap[data.categoryIcon]
/** 앱잼 내에서는 비활성화*/
Expand All @@ -100,7 +100,7 @@ class HomeRulesCategoryAdapter(
private val binding: ItemRulesRuleBinding
) : RecyclerView.ViewHolder(binding.root) {

fun onBind(data: com.hous.data.entity.Category) {
fun onBind(data: CategoryInfo) {
binding.data = data
binding.iconType = CategoryIconType.NONE
/** 앱잼 내에서는 비활성화*/
Expand All @@ -112,17 +112,17 @@ class HomeRulesCategoryAdapter(

companion object {
private val CategoryOfRuleDiffUtilCallback =
object : DiffUtil.ItemCallback<com.hous.data.entity.Category>() {
object : DiffUtil.ItemCallback<CategoryInfo>() {
override fun areItemsTheSame(
oldItem: com.hous.data.entity.Category,
newItem: com.hous.data.entity.Category
oldItem: CategoryInfo,
newItem: CategoryInfo
): Boolean {
return oldItem.id == newItem.id
}

override fun areContentsTheSame(
oldItem: com.hous.data.entity.Category,
newItem: com.hous.data.entity.Category
oldItem: CategoryInfo,
newItem: CategoryInfo
): Boolean {
return oldItem == newItem
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.commit
import androidx.fragment.app.replace
import com.hous.hous_aos.BuildConfig
import com.hous.hous_aos.R
import com.hous.hous_aos.databinding.FragmentRulesBinding
import com.hous.hous_aos.ui.rules.my_to_do.MyToDoFragment
Expand Down Expand Up @@ -37,7 +38,6 @@ class RulesFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

initTransaction()
initAdapter()
observeCategory()
Expand Down
173 changes: 53 additions & 120 deletions app/src/main/java/com/hous/hous_aos/ui/rules/RulesViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package com.hous.hous_aos.ui.rules

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.hous.data.entity.Category
import com.hous.data.entity.Homie
import com.hous.data.entity.Rule
import com.hous.data.model.response.TempManagerRequest
import com.hous.data.repository.RulesTodayRepository
import com.hous.domain.model.CategoryInfo
import com.hous.domain.model.HomieInfo
import com.hous.domain.model.RuleInfo
import com.hous.domain.model.TempManagerInfo
import com.hous.domain.model.rules.RulesTodayInfo
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject

@HiltViewModel
class RulesViewModel @Inject constructor(
Expand All @@ -25,26 +26,26 @@ class RulesViewModel @Inject constructor(
val isSelectedCategorySmile: LiveData<Boolean> get() = _isSelectedCategorySmile

private var _categoryOfRuleList =
MutableLiveData<List<Category>>()
MutableLiveData<List<CategoryInfo>>()
val categoryOfRuleList get() = _categoryOfRuleList

private var _todayTodoList =
MutableLiveData<List<Rule>>()
MutableLiveData<List<RuleInfo>>()
val todayTodoList get() = _todayTodoList

private var _myTodoList =
MutableLiveData<List<Rule>>()
MutableLiveData<List<RuleInfo>>()
val myTodoList get() = _myTodoList

private var _keyRulesTableList =
MutableLiveData<List<Rule>>()
MutableLiveData<List<RuleInfo>>()
val keyRulesTableList get() = _keyRulesTableList

private var _generalRulesTableList =
MutableLiveData<List<Rule>>()
MutableLiveData<List<RuleInfo>>()
val generalRulesTableList get() = _generalRulesTableList

private var _tmpManagerList = MutableLiveData<List<Homie>>()
private var _tmpManagerList = MutableLiveData<List<HomieInfo>>()
val tmpManagerList get() = _tmpManagerList

private var _tmpTodayToDoPosition = MutableLiveData<Int>(0)
Expand All @@ -61,25 +62,18 @@ class RulesViewModel @Inject constructor(

init {
viewModelScope.launch {
rulesTodayRepository.getTodayTodayInfoList("")
.onSuccess {
_todayTodoList.value = it.data!!.todayTodoRules
_categoryOfRuleList.value = it.data!!.homeRuleCategories
_categoryOfRuleList.value = (_categoryOfRuleList.value!!).plus(
Category(
id = "62d6b94e0e9be86f165d48db",
categoryName = "없음",
categoryIcon = "CLEAN"
)
val rulesTodayInfo: RulesTodayInfo? = rulesTodayRepository.getTodayTodayInfoList("")
rulesTodayInfo?.let {
Timber.d("RulesViewModel init")
_todayTodoList.value = it.todayTodoRules
_categoryOfRuleList.value = it.homeRuleCategories.plus(
CategoryInfo(
id = "62d6b94e0e9be86f165d48db",
categoryName = "없음",
categoryIcon = "CLEAN"
)
Log.d(
TAG,
"통신 시작 -- ${_todayTodoList?.value}"
)
}
.onFailure {
Log.d(TAG, "RulesViewModel - init - getRulesTodayList fail : ${it.message}")
}
)
}
}
}

Expand All @@ -92,24 +86,13 @@ class RulesViewModel @Inject constructor(
_tmpManagerList.value?.forEach {
if (it.isChecked) clickedTmpManagerList.add(it.id!!)
}
val tmp = TempManagerRequest(clickedTmpManagerList)
Log.d(
TAG,
"Put -- tmp.tmpRuleMembers: ${tmp.tmpRuleMembers} tmp.size : ${tmp.tmpRuleMembers.size}"
)

viewModelScope.launch {
rulesTodayRepository.putTempManagerInfoList(
"",
_todayTodoList.value!![tmpTodayToDoPosition.value!!].id,
tmp
)
.onSuccess {
fetchToTodayToDoList()
}
.onFailure {
Log.d(TAG, " result fail : $tmp")
Log.d(TAG, " result fail : ${it.message}")
}
clickedTmpManagerList
)?.let { fetchToTodayToDoList() }
}
}

Expand All @@ -124,54 +107,32 @@ class RulesViewModel @Inject constructor(
/** get
* 임시 담당자 다이얼로그 조회 */
fun fetchToTmpManagerList(position: Int) {
Log.d(
TAG,
"RulesViewModel - fetchToTmpManagerList() _todayTodoList.value!![position].id: ${_todayTodoList.value!![position].id}"
)
viewModelScope.launch {
rulesTodayRepository.getTempManagerInfoList("", _todayTodoList.value!![position].id)
.onSuccess {
_tmpManagerList.value = it.data!!.homies
_tmpTodayToDoPosition.postValue(position)
}
.onFailure {
Log.d(TAG, "RulesViewModel - fetchToTmpManagerList() - ${it.message}")
}
val tempManagerInfo: TempManagerInfo? =
rulesTodayRepository.getTempManagerInfoList("", _todayTodoList.value!![position].id)
tempManagerInfo?.let {
_tmpManagerList.value = it.homies
_tmpTodayToDoPosition.value = position
}

}
}

fun fetchToTodayToDoList() {
fun fetchToTodayToDoList() =
viewModelScope.launch {
rulesTodayRepository.getTodayTodayInfoList("")
.onSuccess {
_todayTodoList.value = it.data!!.todayTodoRules
// Log.d(
// TAG,
// "다시서버통신 -- Size: ${_todayTodoList?.value!![0].todayMembersWithTypeColor.size}"
// )
}
.onFailure {
Log.d(
TAG,
"RulesViewModel - init - getRulesTodayList fail : ${it.message}"
)
}
rulesTodayRepository.getTodayTodayInfoList("")?.let {
_todayTodoList.value = it.todayTodoRules
}
}
}


/** get
* My -To - DO 서버통신*/
fun fetchToMyTodayToDoList() {
viewModelScope.launch {
rulesTodayRepository.getMyTodoInfoList("")
.onSuccess {
_myTodoList.value = it.data!!
Log.d(TAG, "RulesViewModel - fetchToMyTodayToDoList() called")
Log.d("MYTODO", "Success ")
}
.onFailure {
Log.d(TAG, "fail : ${it.message}")
}
rulesTodayRepository.getMyTodoInfoList("")?.let {
_myTodoList.value = it
}
}
}

Expand All @@ -187,17 +148,8 @@ class RulesViewModel @Inject constructor(
rulesTodayRepository.putMyToDoCheckLust(
"",
id,
com.hous.data.model.request.MyToDoCheckRequest(checked)
checked
)
.onSuccess {
Log.d(TAG, "Success - id: $id, checked: $checked ")
}
.onFailure {
Log.d(
TAG,
"fail - $id, checked: $checked - :${it.message}"
)
}
}
}

Expand All @@ -208,33 +160,17 @@ class RulesViewModel @Inject constructor(
val categoryId = categoryOfRuleList.value!![position].id
_categoryName.value = categoryOfRuleList.value!![position].categoryName
_categoryId.value = categoryOfRuleList.value!![position].id
Log.d("RulesViewModel", "categoryId: $categoryId")
viewModelScope.launch {
rulesTodayRepository.getRuleTableInfoList("", categoryId)
.onSuccess {
Log.d("RulesViewModel", "Success - RulesTableList() ${it.message}")
val data = it.data
Log.d("RulesViewModel", "Success- data: $data")
val tmpGeneralRulesTableList = data!!.rules
val tmpKeyRulesTableList = data.keyRules
val totalRulesDataSize =
tmpGeneralRulesTableList.size + tmpKeyRulesTableList.size

_ruleTableSize.value = totalRulesDataSize
_generalRulesTableList.value = tmpGeneralRulesTableList
_keyRulesTableList.value = tmpKeyRulesTableList
Log.d(
"RulesViewModel",
"Success -keyRulesTableList: ${generalRulesTableList.value}"
)
Log.d(
"RulesViewModel",
"Success -tmpGeneralRulesTableList: ${keyRulesTableList.value}"
)
}
.onFailure {
Log.d("RulesViewModel", "Fail - RulesTableList() ${it.message}")
}
rulesTodayRepository.getRuleTableInfoList("", categoryId)?.let {
val tmpGeneralRulesTableList = it.rules
val tmpKeyRulesTableList = it.keyRules
val totalRulesDataSize =
tmpGeneralRulesTableList.size + tmpKeyRulesTableList.size

_ruleTableSize.value = totalRulesDataSize
_generalRulesTableList.value = tmpGeneralRulesTableList
_keyRulesTableList.value = tmpKeyRulesTableList
}
}
}

Expand Down Expand Up @@ -269,7 +205,4 @@ class RulesViewModel @Inject constructor(
_categoryOfRuleList.value = tmpCategoryOfRuleList.toList()
}

companion object {
private const val TAG = "RulesViewModel"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.hous.data.entity.Rule
import com.hous.domain.model.RuleInfo
import com.hous.hous_aos.databinding.ItemRulesMyToDoBinding

class MyToDoAdapter(private val onClickIcon: (Int) -> Unit) :
ListAdapter<Rule, MyToDoAdapter.MyToDoViewHolder>(
ListAdapter<RuleInfo, MyToDoAdapter.MyToDoViewHolder>(
MyTodoDiffUtilCallback
) {
private val iconTypeHashMap: HashMap<String, IconType> = hashMapOf(
Expand All @@ -28,7 +28,7 @@ class MyToDoAdapter(private val onClickIcon: (Int) -> Unit) :
private val iconTypeHashMap: HashMap<String, IconType>,
private val onClickIcon: (Int) -> Unit
) : RecyclerView.ViewHolder(binding.root) {
fun onBind(data: Rule) {
fun onBind(data: RuleInfo) {
binding.data = data
binding.iconType = iconTypeHashMap[data.categoryIcon]
binding.clMyToDo.setOnClickListener {
Expand All @@ -55,18 +55,18 @@ class MyToDoAdapter(private val onClickIcon: (Int) -> Unit) :

companion object {
private val MyTodoDiffUtilCallback =
object : DiffUtil.ItemCallback<Rule>() {
object : DiffUtil.ItemCallback<RuleInfo>() {
override fun areItemsTheSame(
oldItem: Rule,
newItem: Rule
oldItem: RuleInfo,
newItem: RuleInfo

): Boolean {
return oldItem.id == newItem.id
}

override fun areContentsTheSame(
oldItem: Rule,
newItem: Rule
oldItem: RuleInfo,
newItem: RuleInfo
): Boolean {
return oldItem == newItem
}
Expand Down
Loading

0 comments on commit 6402b5b

Please sign in to comment.