Skip to content

Commit

Permalink
Merge pull request #207 from boostcampwm-2022/feature/#202
Browse files Browse the repository at this point in the history
[์œ ์ € ์ •๋ณด ๋ณต์› ๋กœ๋”ฉ] ๋กœ๊ทธ์ธ ์‹œ ์œ ์ € ์ •๋ณด ๋ณต์› ๋กœ๋”ฉํ™”๋ฉด
  • Loading branch information
Yoon6 authored Dec 12, 2022
2 parents 8e2eff7 + a0d5635 commit 5957d97
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 10 deletions.
29 changes: 29 additions & 0 deletions app/src/main/java/com/lateinit/rightweight/ui/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lateinit.rightweight.ui

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.graphics.Rect
Expand All @@ -9,6 +10,7 @@ import android.view.MotionEvent
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.TextView
import androidx.activity.viewModels
import androidx.annotation.IdRes
import androidx.appcompat.app.AppCompatActivity
Expand Down Expand Up @@ -36,8 +38,10 @@ import com.lateinit.rightweight.ui.dialog.CommonDialogFragment
import com.lateinit.rightweight.ui.dialog.CommonDialogFragment.Companion.BACKUP_USER_INFO_TAG
import com.lateinit.rightweight.ui.dialog.CommonDialogFragment.Companion.LOGOUT_DIALOG_TAG
import com.lateinit.rightweight.ui.dialog.CommonDialogFragment.Companion.WITHDRAW_DIALOG_TAG
import com.lateinit.rightweight.ui.dialog.LoadingDialogProvider
import com.lateinit.rightweight.ui.login.LoginActivity
import com.lateinit.rightweight.ui.login.NetworkState
import com.lateinit.rightweight.ui.model.LoadingState
import com.lateinit.rightweight.util.collectOnLifecycle
import dagger.hilt.android.AndroidEntryPoint

Expand All @@ -62,6 +66,11 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
}
}
}

private val loadingDialog: Dialog by lazy {
LoadingDialogProvider().provideLoadingDialog(this, R.layout.dialog_loading)
}

private val viewModel: MainViewModel by viewModels()
private val client: GoogleSignInClient by lazy {
val options = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
Expand All @@ -78,6 +87,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
binding = DataBindingUtil.setContentView(this@MainActivity, R.layout.activity_main)
isLoginBefore = intent.extras?.getBoolean("isLoginBefore") ?: false

setCollectLoadingState()
setActionBar()
setNavController()
restore()
Expand Down Expand Up @@ -255,6 +265,25 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
}
}

private fun setCollectLoadingState() {
collectOnLifecycle {
val textViewLoading = loadingDialog.findViewById<TextView>(R.id.text_vitw_loading)
viewModel.loadingState.collect { state ->
when (state) {
LoadingState.RESTORE -> {
textViewLoading.setText(R.string.restore_loading_message)
loadingDialog.show()
}
LoadingState.BACKUP -> {
textViewLoading.setText(R.string.backup_loading_message)
loadingDialog.show()
}
else -> loadingDialog.cancel()
}
}
}
}

override fun dispatchTouchEvent(event: MotionEvent?): Boolean {
when (event?.action) {
MotionEvent.ACTION_DOWN -> {
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/lateinit/rightweight/ui/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.lateinit.rightweight.data.repository.UserRepository
import com.lateinit.rightweight.ui.login.NetworkState
import com.lateinit.rightweight.ui.mapper.toDayUiModel
import com.lateinit.rightweight.ui.mapper.toRoutineUiModel
import com.lateinit.rightweight.ui.model.LoadingState
import com.lateinit.rightweight.ui.model.history.HistoryExerciseSetUiModel
import com.lateinit.rightweight.ui.model.history.HistoryExerciseUiModel
import com.lateinit.rightweight.ui.model.history.HistoryUiModel
Expand Down Expand Up @@ -50,6 +51,9 @@ class MainViewModel @Inject constructor(
private val _networkState = MutableSharedFlow<NetworkState>()
val networkState = _networkState.asSharedFlow()

private val _loadingState = MutableSharedFlow<LoadingState>()
val loadingState = _loadingState.asSharedFlow()

private val _deleteEvent = MutableSharedFlow<Boolean>()
val deleteEvent = _deleteEvent.asSharedFlow()

Expand Down Expand Up @@ -85,14 +89,19 @@ class MainViewModel @Inject constructor(

private suspend fun sendNetworkResultEvent(state: NetworkState) {
_networkState.emit(state)
if (state != NetworkState.SUCCESS) {
_loadingState.emit(LoadingState.FAIL)
}
}

fun backup() {
viewModelScope.launch(networkExceptionHandler) {
_loadingState.emit(LoadingState.BACKUP)
backupUserInfo()
backupMyRoutine()
backupHistory()
sendNetworkResultEvent(NetworkState.SUCCESS)
_loadingState.emit(LoadingState.NONE)
}
}

Expand All @@ -101,13 +110,15 @@ class MainViewModel @Inject constructor(
viewModelScope.launch(networkExceptionHandler) {
val userInfoInServer = userRepository.restoreUserInfo(userId)
if (userInfoInServer != null) {
_loadingState.emit(LoadingState.RESTORE)
restoreRoutine()
restoreHistory(userId)
restoreUserInfo(
userInfoInServer.routineId.value,
userInfoInServer.dayId.value,
userInfoInServer.completedDayId.value
)
_loadingState.emit(LoadingState.NONE)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.lateinit.rightweight.ui.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Window


class LoadingDialogProvider {

fun provideLoadingDialog(context: Context, loadingLayout: Int): Dialog {
return Dialog(context).apply {
requestWindowFeature(Window.FEATURE_NO_TITLE)
setCanceledOnTouchOutside(false)
setCancelable(false)
window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
setContentView(loadingLayout)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.lateinit.rightweight.ui.model

enum class LoadingState {
NONE,
RESTORE,
GET,
BACKUP,
FAIL,
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.lateinit.rightweight.ui.share.detail

import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.setFragmentResult
Expand All @@ -12,6 +14,8 @@ import androidx.navigation.fragment.findNavController
import com.google.android.material.snackbar.Snackbar
import com.lateinit.rightweight.R
import com.lateinit.rightweight.databinding.FragmentSharedRoutineDetailBinding
import com.lateinit.rightweight.ui.dialog.LoadingDialogProvider
import com.lateinit.rightweight.ui.model.LoadingState
import com.lateinit.rightweight.ui.model.routine.DayUiModel
import com.lateinit.rightweight.ui.routine.detail.DetailExerciseAdapter
import com.lateinit.rightweight.ui.routine.editor.RoutineDayAdapter
Expand All @@ -27,6 +31,10 @@ class SharedRoutineDetailFragment : Fragment() {

private val viewModel: SharedRoutineDetailViewModel by viewModels()

private val loadingDialog: Dialog by lazy {
LoadingDialogProvider().provideLoadingDialog(requireContext(), R.layout.dialog_loading)
}

private lateinit var routineDayAdapter: RoutineDayAdapter
private lateinit var exerciseAdapter: DetailExerciseAdapter

Expand All @@ -51,7 +59,9 @@ class SharedRoutineDetailFragment : Fragment() {
setExerciseAdapter()
setSharedRoutineDetailCollect()
setButtonRoutineImportOnClickListener()
setCollectLoadingState()
handleNavigationEvent()

}

private fun setRoutineDayAdapter() {
Expand Down Expand Up @@ -130,4 +140,19 @@ class SharedRoutineDetailFragment : Fragment() {
}
}
}

private fun setCollectLoadingState() {
viewLifecycleOwner.collectOnLifecycle {
val textViewLoading = loadingDialog.findViewById<TextView>(R.id.text_vitw_loading)
viewModel.loadingState.collect { state ->
when (state) {
LoadingState.GET -> {
textViewLoading.setText(R.string.get_loading_message)
loadingDialog.show()
}
else -> loadingDialog.cancel()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.lateinit.rightweight.data.repository.UserRepository
import com.lateinit.rightweight.ui.login.NetworkState
import com.lateinit.rightweight.ui.mapper.toDayUiModel
import com.lateinit.rightweight.ui.mapper.toSharedRoutineUiModel
import com.lateinit.rightweight.ui.model.LoadingState
import com.lateinit.rightweight.ui.model.routine.DayUiModel
import com.lateinit.rightweight.ui.model.shared.SharedRoutineUiModel
import com.lateinit.rightweight.util.FIRST_DAY_POSITION
Expand Down Expand Up @@ -55,27 +56,35 @@ class SharedRoutineDetailViewModel @Inject constructor(
private val _navigationEvent = MutableSharedFlow<String>()
val navigationEvent = _navigationEvent.asSharedFlow()

val networkExceptionHandler = CoroutineExceptionHandler { _, throwable ->
when (throwable) {
is SocketException -> sendNetworkResultEvent(NetworkState.BAD_INTERNET)
is HttpException -> sendNetworkResultEvent(NetworkState.PARSE_ERROR)
is UnknownHostException -> sendNetworkResultEvent(NetworkState.WRONG_CONNECTION)
else -> sendNetworkResultEvent(NetworkState.OTHER_ERROR)
private val _loadingState = MutableSharedFlow<LoadingState>()
val loadingState = _loadingState.asSharedFlow()

private val networkExceptionHandler = CoroutineExceptionHandler { _, throwable ->
viewModelScope.launch{
when (throwable) {
is SocketException -> sendNetworkResultEvent(NetworkState.BAD_INTERNET)
is HttpException -> sendNetworkResultEvent(NetworkState.PARSE_ERROR)
is UnknownHostException -> sendNetworkResultEvent(NetworkState.WRONG_CONNECTION)
else -> sendNetworkResultEvent(NetworkState.OTHER_ERROR)
}
}
}

private fun sendNetworkResultEvent(state: NetworkState) {
private suspend fun sendNetworkResultEvent(state: NetworkState) {
_uiState.value = LatestSharedRoutineDetailUiState.Error(state)
if(state != NetworkState.SUCCESS){
_loadingState.emit(LoadingState.FAIL)
}
}


fun getSharedRoutineDetail(routineId: String) {
viewModelScope.launch(networkExceptionHandler) {
sharedRoutineRepository.getSharedRoutineDetail(routineId)
.collect { sharedRoutineWithDays ->

if (sharedRoutineWithDays.days.isEmpty()) {
_loadingState.emit(LoadingState.GET)
sharedRoutineRepository.requestSharedRoutineDetail(routineId)
_loadingState.emit(LoadingState.NONE)
}

_uiState.value = LatestSharedRoutineDetailUiState.Success(
Expand All @@ -91,7 +100,6 @@ class SharedRoutineDetailViewModel @Inject constructor(
}
}
}

}
}

Expand Down
29 changes: 29 additions & 0 deletions app/src/main/res/layout/dialog_loading.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ProgressBar
android:id="@+id/progress_bar_loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@+id/text_vitw_loading"/>

<TextView
android:id="@+id/text_vitw_loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Text.Medium.Bold"
android:textColor="@color/white"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@id/progress_bar_loading"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,7 @@
<string name="backup_message">"์ง€๊ธˆ ์ƒํƒœ์˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?"</string>
<string name="modified_date_sort">"์ตœ์‹ ์ˆœ"</string>
<string name="shared_count_sort">"๊ณต์œ  ํšŸ์ˆ˜์ˆœ"</string>
<string name="restore_loading_message">๋ฐ์ดํ„ฐ๋ฅผ ๋ณต์›ํ•˜๋Š” ์ค‘ ์ž…๋‹ˆ๋‹ค.</string>
<string name="backup_loading_message">๋ฐ์ดํ„ฐ๋ฅผ ๋ฐฑ์—…ํ•˜๋Š” ์ค‘ ์ž…๋‹ˆ๋‹ค.</string>
<string name="get_loading_message">๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ค‘ ์ž…๋‹ˆ๋‹ค</string>
</resources>

0 comments on commit 5957d97

Please sign in to comment.