diff --git a/app/src/main/java/com/tht/tht/HomeActivity.kt b/app/src/main/java/com/tht/tht/HomeActivity.kt index 5c7ce9e6..34159a29 100644 --- a/app/src/main/java/com/tht/tht/HomeActivity.kt +++ b/app/src/main/java/com/tht/tht/HomeActivity.kt @@ -12,6 +12,7 @@ import androidx.lifecycle.repeatOnLifecycle import com.tht.tht.databinding.ActivityHomeBinding import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.launch +import tht.core.navigation.ToHotNavigation import tht.core.ui.base.BaseActivity import tht.core.ui.base.FragmentNavigator import tht.core.ui.delegate.viewBinding @@ -20,6 +21,7 @@ import tht.feature.chat.ChatFragment import tht.feature.heart.HeartFragment import tht.feature.setting.MyFragment import tht.feature.tohot.tohot.fragment.ToHotFragment +import javax.inject.Inject @SuppressLint("CommitTransaction") @AndroidEntryPoint @@ -28,6 +30,9 @@ class HomeActivity : BaseActivity(), Fragmen override val vm by viewModels() override val binding by viewBinding(ActivityHomeBinding::inflate) + @Inject + lateinit var toHotNavigation: ToHotNavigation + override fun initViews() { initNavigationBar() } @@ -42,7 +47,10 @@ class HomeActivity : BaseActivity(), Fragmen binding.bnvHome.setOnItemSelectedListener { item -> when (item.itemId) { R.id.menu_tohot -> { - showFragment(ToHotFragment.TAG) + toHotNavigation.navigateToHot( + supportFragmentManager, + R.id.fragment_container + ) true } R.id.menu_heart -> { diff --git a/app/src/main/java/com/tht/tht/SplashActivity.kt b/app/src/main/java/com/tht/tht/SplashActivity.kt index f1f57488..77dafd5b 100644 --- a/app/src/main/java/com/tht/tht/SplashActivity.kt +++ b/app/src/main/java/com/tht/tht/SplashActivity.kt @@ -3,17 +3,16 @@ package com.tht.tht import android.animation.Animator import android.annotation.SuppressLint import android.os.Bundle -import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen -import androidx.core.view.isVisible import androidx.lifecycle.lifecycleScope import com.tht.tht.databinding.ActivitySplashBinding import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.launch +import tht.core.navigation.SignupNavigation import tht.core.ui.delegate.viewBinding -import tht.feature.signin.prelogin.PreloginActivity +import javax.inject.Inject @SuppressLint("CustomSplashScreen") @AndroidEntryPoint @@ -22,6 +21,12 @@ class SplashActivity : AppCompatActivity() { private val viewModel: SplashViewModel by viewModels() private val binding: ActivitySplashBinding by viewBinding(ActivitySplashBinding::inflate) + @Inject + lateinit var signupNavigation: SignupNavigation + + @Inject + lateinit var homeNavigation: SignupNavigation + override fun onCreate(savedInstanceState: Bundle?) { installSplashScreen() super.onCreate(savedInstanceState) @@ -46,27 +51,16 @@ class SplashActivity : AppCompatActivity() { } private fun observeViewModel() { - val signupResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { - binding.lottieView.isVisible = false - when (it.resultCode) { - RESULT_OK -> viewModel.signupSuccessEvent() - else -> viewModel.signupCancelEvent() - } - } - lifecycleScope.launch { viewModel.sideEffect.collect { when (it) { is SplashSideEffect.Signup -> { - signupResult.launch(PreloginActivity.getIntent(this@SplashActivity)) - } - - is SplashSideEffect.Home -> { - startActivity(HomeActivity.newIntent(this@SplashActivity)) + signupNavigation.navigatePreLogin(this@SplashActivity) finish() } - is SplashSideEffect.Cancel -> { + is SplashSideEffect.Home -> { + homeNavigation.navigatePreLogin(this@SplashActivity) finish() } } diff --git a/app/src/main/java/com/tht/tht/SplashViewModel.kt b/app/src/main/java/com/tht/tht/SplashViewModel.kt index 0767434f..2c0cae37 100644 --- a/app/src/main/java/com/tht/tht/SplashViewModel.kt +++ b/app/src/main/java/com/tht/tht/SplashViewModel.kt @@ -21,10 +21,6 @@ class SplashViewModel @Inject constructor( checkAutoLogin() } - fun signupSuccessEvent() { - checkAutoLogin() - } - private fun checkAutoLogin() { viewModelScope.launch { fetchThtTokenUseCase() @@ -36,16 +32,9 @@ class SplashViewModel @Inject constructor( } } } - - fun signupCancelEvent() { - viewModelScope.launch { - _sideEffect.emit(SplashSideEffect.Cancel) - } - } } sealed class SplashSideEffect { object Signup : SplashSideEffect() object Home : SplashSideEffect() - object Cancel : SplashSideEffect() } diff --git a/app/src/main/java/com/tht/tht/navigation/HomeNavigationImpl.kt b/app/src/main/java/com/tht/tht/navigation/HomeNavigationImpl.kt new file mode 100644 index 00000000..2b525b8c --- /dev/null +++ b/app/src/main/java/com/tht/tht/navigation/HomeNavigationImpl.kt @@ -0,0 +1,13 @@ +package com.tht.tht.navigation + +import android.content.Context +import com.tht.tht.HomeActivity +import tht.core.navigation.HomeNavigation +import javax.inject.Inject + +class HomeNavigationImpl @Inject constructor() : HomeNavigation { + + override fun navigateHome(context: Context) { + context.startActivity(HomeActivity.newIntent(context)) + } +} diff --git a/app/src/main/java/com/tht/tht/navigation/NavigationModule.kt b/app/src/main/java/com/tht/tht/navigation/NavigationModule.kt new file mode 100644 index 00000000..42fc16f7 --- /dev/null +++ b/app/src/main/java/com/tht/tht/navigation/NavigationModule.kt @@ -0,0 +1,25 @@ +package com.tht.tht.navigation + +import dagger.Binds +import dagger.Module +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent +import tht.core.navigation.HomeNavigation +import tht.core.navigation.SignupNavigation +import tht.core.navigation.ToHotNavigation +import tht.feature.signin.navigation.SignupNavigationImpl +import tht.feature.tohot.navigation.ToHotNavigationImpl + +@Module +@InstallIn(SingletonComponent::class) +abstract class NavigationModule { + + @Binds + abstract fun bindHomeNavigation(impl: HomeNavigationImpl): HomeNavigation + + @Binds + abstract fun bindSignupNavigation(impl: SignupNavigationImpl): SignupNavigation + + @Binds + abstract fun bindToHotNavigation(impl: ToHotNavigationImpl): ToHotNavigation +} diff --git a/core/navigation/src/main/java/tht/core/navigation/HomeNavigation.kt b/core/navigation/src/main/java/tht/core/navigation/HomeNavigation.kt new file mode 100644 index 00000000..a729068e --- /dev/null +++ b/core/navigation/src/main/java/tht/core/navigation/HomeNavigation.kt @@ -0,0 +1,7 @@ +package tht.core.navigation + +import android.content.Context + +interface HomeNavigation { + fun navigateHome(context: Context) +} diff --git a/core/navigation/src/main/java/tht/core/navigation/SignupNavigation.kt b/core/navigation/src/main/java/tht/core/navigation/SignupNavigation.kt new file mode 100644 index 00000000..4f81ae07 --- /dev/null +++ b/core/navigation/src/main/java/tht/core/navigation/SignupNavigation.kt @@ -0,0 +1,7 @@ +package tht.core.navigation + +import android.content.Context + +interface SignupNavigation { + fun navigatePreLogin(context: Context) +} diff --git a/core/navigation/src/main/java/tht/core/navigation/ToHotNavigation.kt b/core/navigation/src/main/java/tht/core/navigation/ToHotNavigation.kt new file mode 100644 index 00000000..9f40e311 --- /dev/null +++ b/core/navigation/src/main/java/tht/core/navigation/ToHotNavigation.kt @@ -0,0 +1,10 @@ +package tht.core.navigation + +import androidx.fragment.app.FragmentManager + +interface ToHotNavigation { + fun navigateToHot( + fragmentManager: FragmentManager, + fragmentContainerResourceId: Int + ) +} diff --git a/feature/signin/src/main/java/tht/feature/signin/navigation/SignupNavigationImpl.kt b/feature/signin/src/main/java/tht/feature/signin/navigation/SignupNavigationImpl.kt new file mode 100644 index 00000000..58e1b2c9 --- /dev/null +++ b/feature/signin/src/main/java/tht/feature/signin/navigation/SignupNavigationImpl.kt @@ -0,0 +1,12 @@ +package tht.feature.signin.navigation + +import android.content.Context +import tht.core.navigation.SignupNavigation +import tht.feature.signin.prelogin.PreloginActivity +import javax.inject.Inject + +class SignupNavigationImpl @Inject constructor() : SignupNavigation { + override fun navigatePreLogin(context: Context) { + context.startActivity(PreloginActivity.getIntent(context)) + } +} diff --git a/feature/signin/src/main/java/tht/feature/signin/prelogin/PreloginActivity.kt b/feature/signin/src/main/java/tht/feature/signin/prelogin/PreloginActivity.kt index 2365f7a1..768487e4 100644 --- a/feature/signin/src/main/java/tht/feature/signin/prelogin/PreloginActivity.kt +++ b/feature/signin/src/main/java/tht/feature/signin/prelogin/PreloginActivity.kt @@ -2,7 +2,6 @@ package tht.feature.signin.prelogin import android.content.Context import android.content.Intent -import android.util.Log import androidx.activity.viewModels import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope @@ -30,13 +29,6 @@ class PreloginActivity : BaseStateActivity { - startActivity(PreloginActivity.getIntent(this@SignupRootActivity)) + homeNavigation.navigateHome(this@SignupRootActivity) finish() } is SignupRootViewModel.SignupRootSideEffect.ShowToast -> { diff --git a/feature/tohot/src/main/java/tht/feature/tohot/navigation/ToHotNavigationImpl.kt b/feature/tohot/src/main/java/tht/feature/tohot/navigation/ToHotNavigationImpl.kt new file mode 100644 index 00000000..43c2b904 --- /dev/null +++ b/feature/tohot/src/main/java/tht/feature/tohot/navigation/ToHotNavigationImpl.kt @@ -0,0 +1,25 @@ +package tht.feature.tohot.navigation + +import androidx.fragment.app.FragmentManager +import tht.core.navigation.ToHotNavigation +import tht.feature.tohot.tohot.fragment.ToHotFragment +import javax.inject.Inject + +class ToHotNavigationImpl @Inject constructor() : ToHotNavigation { + override fun navigateToHot( + fragmentManager: FragmentManager, + fragmentContainerResourceId: Int + ) { + val foundFragment = fragmentManager.findFragmentByTag(ToHotFragment.TAG) + fragmentManager.fragments.forEach { fm -> + fragmentManager.beginTransaction().hide(fm).commitAllowingStateLoss() + } + foundFragment?.let { + fragmentManager.beginTransaction().show(it).commitAllowingStateLoss() + } ?: run { + fragmentManager.beginTransaction() + .add(fragmentContainerResourceId, ToHotFragment.newInstance(), ToHotFragment.TAG) + .commitAllowingStateLoss() + } + } +}