Skip to content

Commit

Permalink
feat:TOP-87 모듈간 이동을 core:navigation 모듈을 통해 진행하도록 변경
Browse files Browse the repository at this point in the history
 - feature모듈은 core:navigation 모듈에 의존해서 모듈간 이동 진행
 - core:navigation 모듈에 feature별 navigation interface를 정의, 각 feature에서 구현체 구현
 - app 모듈에 navigation di 코드 작성
 - 불필요한 코드 제거
  • Loading branch information
cwj-c committed Nov 26, 2023
1 parent da9318a commit 5b35358
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 39 deletions.
10 changes: 9 additions & 1 deletion app/src/main/java/com/tht/tht/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -28,6 +30,9 @@ class HomeActivity : BaseActivity<HomeViewModel, ActivityHomeBinding>(), Fragmen
override val vm by viewModels<HomeViewModel>()
override val binding by viewBinding(ActivityHomeBinding::inflate)

@Inject
lateinit var toHotNavigation: ToHotNavigation

override fun initViews() {
initNavigationBar()
}
Expand All @@ -42,7 +47,10 @@ class HomeActivity : BaseActivity<HomeViewModel, ActivityHomeBinding>(), 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 -> {
Expand Down
28 changes: 11 additions & 17 deletions app/src/main/java/com/tht/tht/SplashActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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()
}
}
Expand Down
11 changes: 0 additions & 11 deletions app/src/main/java/com/tht/tht/SplashViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ class SplashViewModel @Inject constructor(
checkAutoLogin()
}

fun signupSuccessEvent() {
checkAutoLogin()
}

private fun checkAutoLogin() {
viewModelScope.launch {
fetchThtTokenUseCase()
Expand All @@ -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()
}
13 changes: 13 additions & 0 deletions app/src/main/java/com/tht/tht/navigation/HomeNavigationImpl.kt
Original file line number Diff line number Diff line change
@@ -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))
}
}
25 changes: 25 additions & 0 deletions app/src/main/java/com/tht/tht/navigation/NavigationModule.kt
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package tht.core.navigation

import android.content.Context

interface HomeNavigation {
fun navigateHome(context: Context)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package tht.core.navigation

import android.content.Context

interface SignupNavigation {
fun navigatePreLogin(context: Context)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package tht.core.navigation

import androidx.fragment.app.FragmentManager

interface ToHotNavigation {
fun navigateToHot(
fragmentManager: FragmentManager,
fragmentContainerResourceId: Int
)
}
Original file line number Diff line number Diff line change
@@ -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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -30,13 +29,6 @@ class PreloginActivity : BaseStateActivity<PreloginViewModel, ActivityPreloginBi

override val binding by viewBinding(ActivityPreloginBinding::inflate)

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
Log.d("TAG", "onNewIntent PreLoginActivity")
setResult(RESULT_OK)
finish()
}

override fun initViews() = with(binding) {
btnKakaoLogin.setOnClickListener {
vm.requestKakaoLogin()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@ import androidx.core.view.isVisible
import androidx.navigation.findNavController
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import tht.core.navigation.HomeNavigation
import tht.core.ui.delegate.viewBinding
import tht.core.ui.extension.repeatOnStarted
import tht.core.ui.extension.showToast
import tht.feature.signin.R
import tht.feature.signin.databinding.ActivitySignupRootBinding
import tht.feature.signin.prelogin.PreloginActivity
import javax.inject.Inject

@AndroidEntryPoint
class SignupRootActivity : AppCompatActivity() {

private val viewModel: SignupRootViewModel by viewModels()
private val binding: ActivitySignupRootBinding by viewBinding(ActivitySignupRootBinding::inflate)

@Inject
lateinit var homeNavigation: HomeNavigation

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setListener()
Expand Down Expand Up @@ -78,7 +82,7 @@ class SignupRootActivity : AppCompatActivity() {
}
}
is SignupRootViewModel.SignupRootSideEffect.FinishSignup -> {
startActivity(PreloginActivity.getIntent(this@SignupRootActivity))
homeNavigation.navigateHome(this@SignupRootActivity)
finish()
}
is SignupRootViewModel.SignupRootSideEffect.ShowToast -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
}
}

0 comments on commit 5b35358

Please sign in to comment.