Skip to content

Commit

Permalink
#6 feat/메인화면 : 메인화면의 BottomNavigation 아이템 선택 여부에 따른 Fragment 노출 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
HayleyKim0716 committed Aug 1, 2022
1 parent bb7c698 commit 21bfcdb
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,4 +21,10 @@ class ActivityViewModelModule {
): SplashViewModel {
return SplashViewModel(dispatcher, loginUseCase)
}

@Provides
@ActivityScoped
fun provideMainViewModel(): MainViewModel {
return MainViewModel()
}
}
58 changes: 53 additions & 5 deletions app/src/main/java/com/ftw/hometerview/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -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<ActivityMainBinding>(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()
}
}
33 changes: 33 additions & 0 deletions app/src/main/java/com/ftw/hometerview/ui/main/MainViewModel.kt
Original file line number Diff line number Diff line change
@@ -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<State> = MutableStateFlow(State.HomeItemSelected)
val state: StateFlow<State> 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
}
}

0 comments on commit 21bfcdb

Please sign in to comment.