From 06fff2f76894d6084bad7d9f8b8bd41d8224efa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AA=85=EC=84=9D?= Date: Fri, 3 Mar 2023 07:03:29 +0900 Subject: [PATCH] =?UTF-8?q?Issues=20#287=20feat:=20SnackBar=20=EB=8F=99?= =?UTF-8?q?=EC=9E=91=EC=9D=84=20MainActivity=20=EB=A1=9C=20=EC=9D=B4?= =?UTF-8?q?=EB=8F=99=20=EB=B0=8F=20Toast=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/lighthouse/ui/MainActivity.kt | 29 ++++++++++ .../features/common/model/MessageEvent.kt | 10 ++++ .../features/common/ui/MessageViewModel.kt | 56 +++++++++++++++++++ .../features/intro/ui/IntroFragment.kt | 16 ++---- .../features/setting/ui/SettingFragment.kt | 29 +++------- 5 files changed, 109 insertions(+), 31 deletions(-) create mode 100644 features/ui-common/src/main/java/com/lighthouse/features/common/model/MessageEvent.kt create mode 100644 features/ui-common/src/main/java/com/lighthouse/features/common/ui/MessageViewModel.kt diff --git a/app/src/main/java/com/lighthouse/ui/MainActivity.kt b/app/src/main/java/com/lighthouse/ui/MainActivity.kt index d9598ae1f..778396355 100644 --- a/app/src/main/java/com/lighthouse/ui/MainActivity.kt +++ b/app/src/main/java/com/lighthouse/ui/MainActivity.kt @@ -1,15 +1,19 @@ package com.lighthouse.ui import android.os.Bundle +import android.widget.Toast import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen import androidx.databinding.DataBindingUtil import androidx.navigation.NavController import androidx.navigation.fragment.NavHostFragment +import com.google.android.material.snackbar.Snackbar import com.lighthouse.beep.R import com.lighthouse.beep.databinding.ActivityMainBinding import com.lighthouse.features.common.ext.repeatOnStarted +import com.lighthouse.features.common.model.MessageEvent +import com.lighthouse.features.common.ui.MessageViewModel import com.lighthouse.navs.app.model.AppNavigationItem import com.lighthouse.navs.app.navigator.AppNavigationViewModel import dagger.hilt.android.AndroidEntryPoint @@ -21,6 +25,8 @@ class MainActivity : AppCompatActivity() { private val viewModel: MainViewModel by viewModels() + private val messageViewModel: MessageViewModel by viewModels() + private val appNavigationViewModel: AppNavigationViewModel by viewModels() private lateinit var navController: NavController @@ -30,11 +36,34 @@ class MainActivity : AppCompatActivity() { super.onCreate(savedInstanceState) binding = DataBindingUtil.setContentView(this, R.layout.activity_main) + setUpMessage() setUpNavController() setUpNavigation() setUpIsLogin() } + private fun setUpMessage() { + repeatOnStarted { + messageViewModel.messageFlow.collect { event -> + when (event) { + is MessageEvent.SnackBar -> + showSnackBar(event.uiText.asString(this).toString()) + + is MessageEvent.Toast -> + showToast(event.uiText.asString(this).toString()) + } + } + } + } + + private fun showSnackBar(message: String) { + Snackbar.make(binding.root, message, Snackbar.LENGTH_SHORT).show() + } + + private fun showToast(message: String) { + Toast.makeText(this, message, Toast.LENGTH_SHORT).show() + } + private fun setUpNavController() { val navHostFragment = supportFragmentManager.findFragmentById(R.id.fcv) as NavHostFragment navController = navHostFragment.navController diff --git a/features/ui-common/src/main/java/com/lighthouse/features/common/model/MessageEvent.kt b/features/ui-common/src/main/java/com/lighthouse/features/common/model/MessageEvent.kt new file mode 100644 index 000000000..4822231cc --- /dev/null +++ b/features/ui-common/src/main/java/com/lighthouse/features/common/model/MessageEvent.kt @@ -0,0 +1,10 @@ +package com.lighthouse.features.common.model + +import com.lighthouse.core.android.utils.resource.UIText + +sealed class MessageEvent { + + data class SnackBar(val uiText: UIText) : MessageEvent() + + data class Toast(val uiText: UIText) : MessageEvent() +} diff --git a/features/ui-common/src/main/java/com/lighthouse/features/common/ui/MessageViewModel.kt b/features/ui-common/src/main/java/com/lighthouse/features/common/ui/MessageViewModel.kt new file mode 100644 index 000000000..3ba29a7c9 --- /dev/null +++ b/features/ui-common/src/main/java/com/lighthouse/features/common/ui/MessageViewModel.kt @@ -0,0 +1,56 @@ +package com.lighthouse.features.common.ui + +import androidx.annotation.StringRes +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.lighthouse.core.android.utils.resource.UIText +import com.lighthouse.core.utils.flow.MutableEventFlow +import com.lighthouse.core.utils.flow.asEventFlow +import com.lighthouse.features.common.model.MessageEvent +import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.launch +import javax.inject.Inject + +@HiltViewModel +class MessageViewModel @Inject constructor() : ViewModel() { + + private val _messageFlow = MutableEventFlow() + + val messageFlow = _messageFlow.asEventFlow() + + fun sendSnackBar(@StringRes resId: Int) { + viewModelScope.launch { + _messageFlow.emit(MessageEvent.SnackBar(UIText.StringResource(resId))) + } + } + + fun sendSnackBar(message: String) { + viewModelScope.launch { + _messageFlow.emit(MessageEvent.SnackBar(UIText.DynamicString(message))) + } + } + + fun sendSnackBar(text: UIText) { + viewModelScope.launch { + _messageFlow.emit(MessageEvent.SnackBar(text)) + } + } + + fun sendToast(@StringRes resId: Int) { + viewModelScope.launch { + _messageFlow.emit(MessageEvent.Toast(UIText.StringResource(resId))) + } + } + + fun sendToast(message: String) { + viewModelScope.launch { + _messageFlow.emit(MessageEvent.Toast(UIText.DynamicString(message))) + } + } + + fun sendToast(text: UIText) { + viewModelScope.launch { + _messageFlow.emit(MessageEvent.Toast(text)) + } + } +} diff --git a/features/ui-intro/src/main/java/com/lighthouse/features/intro/ui/IntroFragment.kt b/features/ui-intro/src/main/java/com/lighthouse/features/intro/ui/IntroFragment.kt index 697fdbd7b..c15989a5c 100644 --- a/features/ui-intro/src/main/java/com/lighthouse/features/intro/ui/IntroFragment.kt +++ b/features/ui-intro/src/main/java/com/lighthouse/features/intro/ui/IntroFragment.kt @@ -5,17 +5,17 @@ import android.os.Bundle import android.view.View import androidx.activity.result.contract.ActivityResultContracts import androidx.fragment.app.Fragment +import androidx.fragment.app.activityViewModels import androidx.fragment.app.viewModels import androidx.lifecycle.lifecycleScope -import com.google.android.material.snackbar.Snackbar import com.lighthouse.auth.google.model.GoogleAuthEvent import com.lighthouse.auth.google.repository.GoogleClient import com.lighthouse.auth.google.ui.GoogleAuthViewModel -import com.lighthouse.core.android.utils.resource.UIText import com.lighthouse.features.common.binding.viewBindings import com.lighthouse.features.common.dialog.progress.ProgressDialog import com.lighthouse.features.common.ext.repeatOnStarted import com.lighthouse.features.common.ext.show +import com.lighthouse.features.common.ui.MessageViewModel import com.lighthouse.features.common.utils.throttle.onThrottleClick import com.lighthouse.features.intro.R import com.lighthouse.features.intro.databinding.FragmentIntroBinding @@ -28,6 +28,8 @@ class IntroFragment : Fragment(R.layout.fragment_intro) { private val binding by viewBindings() + private val messageViewModel: MessageViewModel by activityViewModels() + private val googleAuthViewModel: GoogleAuthViewModel by viewModels() @Inject @@ -66,7 +68,7 @@ class IntroFragment : Fragment(R.layout.fragment_intro) { repeatOnStarted { googleAuthViewModel.eventFlow.collect { event -> when (event) { - is GoogleAuthEvent.SnackBar -> showSnackBar(event.text) + is GoogleAuthEvent.SnackBar -> messageViewModel.sendToast(event.text) } } } @@ -101,12 +103,4 @@ class IntroFragment : Fragment(R.layout.fragment_intro) { googleAuthViewModel.login() } } - - private fun showSnackBar(string: String) { - Snackbar.make(binding.root, string, Snackbar.LENGTH_SHORT).show() - } - - private fun showSnackBar(text: UIText) { - showSnackBar(text.asString(requireContext()).toString()) - } } diff --git a/features/ui-setting/src/main/java/com/lighthouse/features/setting/ui/SettingFragment.kt b/features/ui-setting/src/main/java/com/lighthouse/features/setting/ui/SettingFragment.kt index 8542194f4..0e51e7fc5 100644 --- a/features/ui-setting/src/main/java/com/lighthouse/features/setting/ui/SettingFragment.kt +++ b/features/ui-setting/src/main/java/com/lighthouse/features/setting/ui/SettingFragment.kt @@ -3,21 +3,19 @@ package com.lighthouse.features.setting.ui import android.os.Bundle import android.view.View import androidx.activity.result.contract.ActivityResultContracts -import androidx.annotation.StringRes import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import androidx.fragment.app.viewModels import androidx.lifecycle.lifecycleScope -import com.google.android.material.snackbar.Snackbar import com.lighthouse.auth.google.model.GoogleAuthEvent import com.lighthouse.auth.google.repository.GoogleClient import com.lighthouse.auth.google.ui.GoogleAuthViewModel import com.lighthouse.core.android.utils.permission.LocationPermissionManager -import com.lighthouse.core.android.utils.resource.UIText import com.lighthouse.features.common.binding.viewBindings import com.lighthouse.features.common.dialog.progress.ProgressDialog import com.lighthouse.features.common.ext.repeatOnStarted import com.lighthouse.features.common.ext.show +import com.lighthouse.features.common.ui.MessageViewModel import com.lighthouse.features.setting.R import com.lighthouse.features.setting.adapter.SettingAdapter import com.lighthouse.features.setting.databinding.FragmentSettingBinding @@ -35,6 +33,8 @@ class SettingFragment : Fragment(R.layout.fragment_setting) { private val viewModel: SettingViewModel by viewModels() + private val messageViewModel: MessageViewModel by activityViewModels() + private val appNavigationViewModel: AppNavigationViewModel by activityViewModels() private val settingAdapter = SettingAdapter( @@ -69,7 +69,7 @@ class SettingFragment : Fragment(R.layout.fragment_setting) { registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> val isGrant = locationPermissionManager.isGrant() if (!isGrant) { - showSnackBar(R.string.error_permission_not_allowed) + messageViewModel.sendSnackBar(R.string.error_permission_not_allowed) } viewModel.setLocationEnable(isGrant) } @@ -78,7 +78,7 @@ class SettingFragment : Fragment(R.layout.fragment_setting) { registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { results -> val isGrant = results.all { it.value } if (!isGrant) { - showSnackBar(R.string.error_permission_not_allowed) + messageViewModel.sendSnackBar(R.string.error_permission_not_allowed) } viewModel.setLocationEnable(isGrant) } @@ -113,7 +113,8 @@ class SettingFragment : Fragment(R.layout.fragment_setting) { viewLifecycleOwner.repeatOnStarted { googleAuthViewModel.eventFlow.collect { event -> when (event) { - is GoogleAuthEvent.SnackBar -> showSnackBar(event.text) + is GoogleAuthEvent.SnackBar -> + messageViewModel.sendSnackBar(event.text) } } } @@ -185,7 +186,7 @@ class SettingFragment : Fragment(R.layout.fragment_setting) { lifecycleScope.launch { val exception = googleClient.signOut().exceptionOrNull() if (exception != null) { - showSnackBar(getString(R.string.error_sign_out_google_client)) + messageViewModel.sendSnackBar(R.string.error_sign_out_google_client) } else { googleAuthViewModel.signOut() } @@ -196,7 +197,7 @@ class SettingFragment : Fragment(R.layout.fragment_setting) { lifecycleScope.launch { val exception = googleClient.signOut().exceptionOrNull() if (exception != null) { - showSnackBar(getString(R.string.error_sign_out_google_client)) + messageViewModel.sendSnackBar(R.string.error_sign_out_google_client) } else { googleAuthViewModel.withdrawal() } @@ -209,16 +210,4 @@ class SettingFragment : Fragment(R.layout.fragment_setting) { else -> Unit } } - - private fun showSnackBar(string: String) { - Snackbar.make(binding.root, string, Snackbar.LENGTH_SHORT).show() - } - - private fun showSnackBar(@StringRes resId: Int) { - Snackbar.make(binding.root, getString(resId), Snackbar.LENGTH_SHORT).show() - } - - private fun showSnackBar(uiText: UIText) { - showSnackBar(uiText.asString(requireContext()).toString()) - } }