From 21bfcdb78d3e9699be9123e5dafc96b06059482b Mon Sep 17 00:00:00 2001 From: HayleyKim0716 Date: Mon, 1 Aug 2022 22:27:21 +0900 Subject: [PATCH] =?UTF-8?q?#6=20feat/=EB=A9=94=EC=9D=B8=ED=99=94=EB=A9=B4?= =?UTF-8?q?=20:=20=EB=A9=94=EC=9D=B8=ED=99=94=EB=A9=B4=EC=9D=98=20BottomNa?= =?UTF-8?q?vigation=20=EC=95=84=EC=9D=B4=ED=85=9C=20=EC=84=A0=ED=83=9D=20?= =?UTF-8?q?=EC=97=AC=EB=B6=80=EC=97=90=20=EB=94=B0=EB=A5=B8=20Fragment=20?= =?UTF-8?q?=EB=85=B8=EC=B6=9C=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../di/ui/ActivityViewModelModule.kt | 7 +++ .../ftw/hometerview/ui/main/MainActivity.kt | 58 +++++++++++++++++-- .../ftw/hometerview/ui/main/MainViewModel.kt | 33 +++++++++++ 3 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/com/ftw/hometerview/ui/main/MainViewModel.kt diff --git a/app/src/main/java/com/ftw/hometerview/di/ui/ActivityViewModelModule.kt b/app/src/main/java/com/ftw/hometerview/di/ui/ActivityViewModelModule.kt index 13cb4d7..3b60840 100644 --- a/app/src/main/java/com/ftw/hometerview/di/ui/ActivityViewModelModule.kt +++ b/app/src/main/java/com/ftw/hometerview/di/ui/ActivityViewModelModule.kt @@ -2,6 +2,7 @@ package com.ftw.hometerview.di.ui import com.ftw.domain.usecase.login.LoginUseCase import com.ftw.hometerview.dispatcher.Dispatcher +import com.ftw.hometerview.ui.main.MainViewModel import com.ftw.hometerview.ui.splash.SplashViewModel import dagger.Module import dagger.Provides @@ -20,4 +21,10 @@ class ActivityViewModelModule { ): SplashViewModel { return SplashViewModel(dispatcher, loginUseCase) } + + @Provides + @ActivityScoped + fun provideMainViewModel(): MainViewModel { + return MainViewModel() + } } diff --git a/app/src/main/java/com/ftw/hometerview/ui/main/MainActivity.kt b/app/src/main/java/com/ftw/hometerview/ui/main/MainActivity.kt index 730ed62..5bf1333 100644 --- a/app/src/main/java/com/ftw/hometerview/ui/main/MainActivity.kt +++ b/app/src/main/java/com/ftw/hometerview/ui/main/MainActivity.kt @@ -1,17 +1,65 @@ package com.ftw.hometerview.ui.main -import androidx.appcompat.app.AppCompatActivity +import android.content.Context +import android.content.Intent import android.os.Bundle -import android.util.Log -import com.ftw.domain.usecase.login.LoginUseCase -import com.ftw.domain.usecase.login.LoginUseCaseImpl +import androidx.appcompat.app.AppCompatActivity +import androidx.databinding.DataBindingUtil +import androidx.fragment.app.Fragment +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.lifecycleScope +import androidx.lifecycle.repeatOnLifecycle import com.ftw.hometerview.R +import com.ftw.hometerview.databinding.ActivityMainBinding +import com.ftw.hometerview.ui.main.favorite.FavoriteFragment +import com.ftw.hometerview.ui.main.home.HomeFragment +import com.ftw.hometerview.ui.main.map.MapFragment +import com.ftw.hometerview.ui.main.mypage.MyPageFragment +import dagger.hilt.android.AndroidEntryPoint +import javax.inject.Inject +import kotlinx.coroutines.launch +@AndroidEntryPoint class MainActivity : AppCompatActivity() { + + companion object { + fun newIntent(context: Context): Intent = Intent(context, MainActivity::class.java) + } + + @Inject + lateinit var viewModel: MainViewModel + + private val homeFragment by lazy { HomeFragment() } + private val mapFragment by lazy { MapFragment() } + private val favoriteFragment by lazy { FavoriteFragment() } + private val myPageFragment by lazy { MyPageFragment() } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_main) + DataBindingUtil.setContentView(this, R.layout.activity_main) + .apply { + viewModel = this@MainActivity.viewModel + lifecycleOwner = this@MainActivity + } + lifecycleScope.launch { + repeatOnLifecycle(Lifecycle.State.STARTED) { + viewModel.state.collect { state -> + when (state) { + MainViewModel.State.HomeItemSelected -> replaceFragment(homeFragment) + MainViewModel.State.MapItemSelected -> replaceFragment(mapFragment) + MainViewModel.State.FavoriteItemSelected -> replaceFragment(favoriteFragment) + MainViewModel.State.MyPageItemSelected -> replaceFragment(myPageFragment) + MainViewModel.State.None -> throw IllegalArgumentException() + } + } + } + } + } + private fun replaceFragment(fragment: Fragment) { + supportFragmentManager.beginTransaction() + .replace(R.id.frame_layout, fragment) + .commitAllowingStateLoss() } } diff --git a/app/src/main/java/com/ftw/hometerview/ui/main/MainViewModel.kt b/app/src/main/java/com/ftw/hometerview/ui/main/MainViewModel.kt new file mode 100644 index 0000000..5557a93 --- /dev/null +++ b/app/src/main/java/com/ftw/hometerview/ui/main/MainViewModel.kt @@ -0,0 +1,33 @@ +package com.ftw.hometerview.ui.main + +import android.view.MenuItem +import com.ftw.hometerview.R +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow + +class MainViewModel { + + sealed class State { + object HomeItemSelected : State() + object MapItemSelected : State() + object FavoriteItemSelected : State() + object MyPageItemSelected : State() + object None : State() + } + + private val _state: MutableStateFlow = MutableStateFlow(State.HomeItemSelected) + val state: StateFlow get() = _state.asStateFlow() + + fun onNavigationItemSelected(item: MenuItem): Boolean { + _state.value = when (item.itemId) { + R.id.home -> State.HomeItemSelected + R.id.map -> State.MapItemSelected + R.id.heart -> State.FavoriteItemSelected + R.id.user -> State.MyPageItemSelected + else -> State.HomeItemSelected + } + + return state.value != State.None + } +}