Skip to content

Commit

Permalink
Issues boostcampwm-2022#287 feat: SnackBar 동작을 MainActivity 로 이동 및 To…
Browse files Browse the repository at this point in the history
…ast 추가
  • Loading branch information
audxo112 committed Mar 2, 2023
1 parent 43de333 commit 06fff2f
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 31 deletions.
29 changes: 29 additions & 0 deletions app/src/main/java/com/lighthouse/ui/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
@@ -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<MessageEvent>()

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))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,6 +28,8 @@ class IntroFragment : Fragment(R.layout.fragment_intro) {

private val binding by viewBindings<FragmentIntroBinding>()

private val messageViewModel: MessageViewModel by activityViewModels()

private val googleAuthViewModel: GoogleAuthViewModel by viewModels()

@Inject
Expand Down Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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()
}
Expand All @@ -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()
}
Expand All @@ -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())
}
}

0 comments on commit 06fff2f

Please sign in to comment.