Skip to content

Commit

Permalink
Merge pull request #78 from Hyesung82/feature/group
Browse files Browse the repository at this point in the history
[develop] WriteViewModel 리팩토링
  • Loading branch information
Hyesung82 authored Mar 17, 2024
2 parents ac96c90 + b561e19 commit a17af5f
Show file tree
Hide file tree
Showing 36 changed files with 132 additions and 111 deletions.
16 changes: 11 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,25 @@
tools:ignore="Instantiatable">
<activity
android:name="com.cheocharm.presentation.ui.MainActivity"
android:exported="true" />
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name="com.cheocharm.presentation.ui.DiaryActivity" />

<activity
android:name="com.cheocharm.presentation.ui.login.SignActivity"
android:exported="true"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<!-- <intent-filter>-->
<!-- <action android:name="android.intent.action.MAIN" />-->

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- <category android:name="android.intent.category.LAUNCHER" />-->
<!-- </intent-filter>-->
</activity>
</application>

Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ plugins {
id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' version '2.0.1' apply false
id 'com.google.dagger.hilt.android' version '2.48.1' apply false
id 'androidx.navigation.safeargs.kotlin' version "${navVersion}" apply false
id 'com.google.devtools.ksp' version '1.9.20-1.0.14' apply false
}

tasks.register('clean', Delete) {
Expand Down
10 changes: 10 additions & 0 deletions data/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
id 'com.google.dagger.hilt.android'
id 'com.google.devtools.ksp'
}

Properties properties = new Properties()
Expand Down Expand Up @@ -61,4 +62,13 @@ dependencies {
implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"
implementation "com.squareup.okhttp3:logging-interceptor:4.9.3"

// Room
implementation "androidx.room:room-runtime:$roomVersion"
annotationProcessor "androidx.room:room-compiler:$roomVersion"
ksp "androidx.room:room-compiler:$roomVersion"
implementation "androidx.room:room-paging:$roomVersion"

// Gson
implementation 'com.google.code.gson:gson:2.10.1'
}
4 changes: 2 additions & 2 deletions data/src/main/java/com/cheocharm/data/di/DataSourceModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import javax.inject.Singleton
internal abstract class DataSourceModule {
@Binds
@Singleton
abstract fun bindAuthLocalDataSource(dataSourceImpl: AuthLocalDataSourceImpl): AuthLocalDataSource
abstract fun bindLoginRemoteDataSource(dataSource: LoginRemoteDataSourceImpl): LoginRemoteDataSource

@Binds
@Singleton
abstract fun bindLoginRemoteDataSource(dataSource: LoginRemoteDataSourceImpl): LoginRemoteDataSource
abstract fun bindAuthLocalDataSource(dataSourceImpl: AuthLocalDataSourceImpl): AuthLocalDataSource

@Binds
@Singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import javax.inject.Inject
class GroupRepositoryImpl @Inject constructor(
private val groupRemoteDataSource: GroupRemoteDataSource
) : GroupRepository {

override fun searchGroup(searchGroupName: String): Flow<PagingData<Group>> {
return groupRemoteDataSource.fetchGroupSearchList(searchGroupName)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import com.cheocharm.domain.repository.MyGroupsRepository
import javax.inject.Inject

class MyGroupsRepositoryImpl @Inject constructor(
private val dataSource: MyGroupsRemoteDataSource
private val remoteDataSource: MyGroupsRemoteDataSource
) : MyGroupsRepository {

override suspend fun getMyGroups(): Result<List<Group>> {
val result = dataSource.getMyGroups().mapCatching {
val result = remoteDataSource.getMyGroups().mapCatching {
it.map { data ->
data.toDomain()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.cheocharm.domain.model.Group
import kotlinx.coroutines.flow.Flow

interface GroupRepository {

fun searchGroup(searchGroupName: String): Flow<PagingData<Group>>

suspend fun joinGroup(groupName: String): Result<Unit>
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ coroutineVersion=1.6.3
glideVersion=4.12.0
pagingVersion=3.2.1
jvmTarget=17
roomVersion = 2.6.1
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import com.cheocharm.domain.model.Group
import com.cheocharm.presentation.databinding.ItemGroupBinding
import com.cheocharm.presentation.ui.write.GroupDiffCallback
import com.cheocharm.presentation.ui.write.GroupsAdapter
import com.cheocharm.presentation.ui.write.groups.GroupDiffCallback
import com.cheocharm.presentation.ui.write.groups.GroupsAdapter

class GroupsPagingAdapter(private val onClick: (Group) -> Unit) :
PagingDataAdapter<Group, GroupsAdapter.ViewHolder>(GroupDiffCallback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.cheocharm.presentation.common.GROUP_JOIN_REQUEST_BOTTOM
import com.cheocharm.presentation.databinding.ActivityMainBinding
import com.cheocharm.presentation.databinding.FragmentSearchGroupBinding
import com.cheocharm.presentation.ui.MainActivity
import com.cheocharm.presentation.ui.write.MembersAdapter
import com.cheocharm.presentation.ui.write.groups.MembersAdapter
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cheocharm.presentation.ui.write
package com.cheocharm.presentation.ui.write.diary

import android.os.Bundle
import android.view.View
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cheocharm.presentation.ui.write
package com.cheocharm.presentation.ui.write.diary

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cheocharm.presentation.ui.write
package com.cheocharm.presentation.ui.write.diary

import android.content.Intent
import android.os.Bundle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cheocharm.presentation.ui.write
package com.cheocharm.presentation.ui.write.diary

import android.view.LayoutInflater
import android.view.ViewGroup
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cheocharm.presentation.ui.write
package com.cheocharm.presentation.ui.write.diary

import android.graphics.Rect
import android.view.View
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cheocharm.presentation.ui.write
package com.cheocharm.presentation.ui.write.diary

import android.view.LayoutInflater
import android.view.ViewGroup
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.cheocharm.presentation.ui.write
package com.cheocharm.presentation.ui.write.diary

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.cheocharm.domain.model.Group
import com.cheocharm.domain.model.TempDiary
import com.cheocharm.domain.model.WriteDiaryRequest
import com.cheocharm.domain.usecase.write.GetMyGroupsUseCase
import com.cheocharm.domain.usecase.write.RequestWriteDiaryUseCase
import com.cheocharm.presentation.common.ErrorMessage
import com.cheocharm.presentation.common.Event
import com.cheocharm.presentation.model.Sticker
import com.cheocharm.presentation.model.TextAlign
Expand All @@ -22,15 +19,11 @@ import javax.inject.Inject

@HiltViewModel
class WriteViewModel @Inject constructor(
private val getMyGroupsUseCase: GetMyGroupsUseCase,
private val requestWriteDiaryUseCase: RequestWriteDiaryUseCase
) : ViewModel() {
lateinit var temp: TempDiary
lateinit var stickers: List<Sticker>

private val _groups = MutableLiveData<List<Group>>()
val groups: LiveData<List<Group>> = _groups

private val _openTool = MutableLiveData<TextEditTool?>()
val openTool: LiveData<TextEditTool?> = _openTool

Expand All @@ -49,30 +42,11 @@ class WriteViewModel @Inject constructor(
private val _underline = MutableLiveData<Boolean>()
val underline: LiveData<Boolean> = _underline

private val _toastText = MutableLiveData<Event<String>>()
val toastText: LiveData<Event<String>> = _toastText

private val _diaryWrittenEvent = MutableLiveData<Event<Long>?>()
val diaryWrittenEvent: LiveData<Event<Long>?> = _diaryWrittenEvent

init {
fetchMyGroups()
}

fun fetchMyGroups() {
viewModelScope.launch {
val result = getMyGroupsUseCase()

result
.onSuccess { groups ->
_groups.value = groups
}
.onFailure { throwable ->
_toastText.value =
Event(throwable.message ?: ErrorMessage.FETCH_MY_GROUPS_FAILED)
}
}
}
private val _toastText = MutableLiveData<Event<String>>()
val toastText: LiveData<Event<String>> = _toastText

fun onClickTool(tool: TextEditTool?) {
_openTool.value = if (openTool.value == tool) null else tool
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cheocharm.presentation.ui.write
package com.cheocharm.presentation.ui.write.groups

import android.view.LayoutInflater
import android.view.ViewGroup
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.cheocharm.presentation.ui.write
package com.cheocharm.presentation.ui.write.groups

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.fragment.app.viewModels
import androidx.hilt.navigation.fragment.hiltNavGraphViewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.DividerItemDecoration
Expand All @@ -20,8 +19,7 @@ import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class GroupsFragment : BaseFragment<FragmentGroupsBinding>(R.layout.fragment_groups) {
private val writeViewModel by hiltNavGraphViewModels<WriteViewModel>(R.id.write)
private val groupsViewModel: GroupsViewModel by viewModels()
private val groupsViewModel: GroupsViewModel by hiltNavGraphViewModels(R.id.write)

private lateinit var swipeRefreshLayout: SwipeRefreshLayout

Expand All @@ -41,7 +39,7 @@ class GroupsFragment : BaseFragment<FragmentGroupsBinding>(R.layout.fragment_gro
swipeRefreshLayout = binding.containerGroupsRefresh

swipeRefreshLayout.setOnRefreshListener {
writeViewModel.fetchMyGroups()
groupsViewModel.fetchMyGroups()
}
}

Expand All @@ -55,13 +53,16 @@ class GroupsFragment : BaseFragment<FragmentGroupsBinding>(R.layout.fragment_gro
adapter = groupsAdapter
}

writeViewModel.groups.observe(viewLifecycleOwner) {
groupsViewModel.groups.observe(viewLifecycleOwner) {
swipeRefreshLayout.isRefreshing = false

it?.let {
Log.d(logTag, it.toString())
groupsAdapter.submitList(it)
}
}

groupsViewModel.fetchMyGroups()
}

private fun adapterOnClick(group: Group) {
Expand All @@ -70,7 +71,7 @@ class GroupsFragment : BaseFragment<FragmentGroupsBinding>(R.layout.fragment_gro
}

private fun setupToast() {
writeViewModel.toastText.observe(viewLifecycleOwner, EventObserver {
groupsViewModel.toastText.observe(viewLifecycleOwner, EventObserver {
swipeRefreshLayout.isRefreshing = false

Toast.makeText(context, it, Toast.LENGTH_SHORT).show()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.cheocharm.presentation.ui.write.groups

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.cheocharm.domain.model.Group
import com.cheocharm.domain.usecase.write.GetMyGroupsUseCase
import com.cheocharm.presentation.common.ErrorMessage
import com.cheocharm.presentation.common.Event
import com.cheocharm.presentation.common.TestValues
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class GroupsViewModel @Inject constructor(
private val getMyGroupsUseCase: GetMyGroupsUseCase
) : ViewModel() {
private val _groups = MutableLiveData(TestValues.testGroups)
val groups: LiveData<List<Group>> = _groups

private val _toastText = MutableLiveData<Event<String>>()
val toastText: LiveData<Event<String>> = _toastText

fun fetchMyGroups() {
viewModelScope.launch(Dispatchers.IO) {
// val result = getMyGroupsUseCase()
//
// result
// .onSuccess { groups ->
// _groups.postValue(groups)
// }
// .onFailure { throwable ->
// _toastText.value =
// Event(throwable.message ?: ErrorMessage.FETCH_MY_GROUPS_FAILED)
// }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cheocharm.presentation.ui.write
package com.cheocharm.presentation.ui.write.groups

import android.view.LayoutInflater
import android.view.ViewGroup
Expand Down
Loading

0 comments on commit a17af5f

Please sign in to comment.