-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#6 feat/메인화면 : 메인화면의 BottomNavigation 아이템 선택 여부에 따른 Fragment 노출 로직 추가
- Loading branch information
1 parent
bb7c698
commit 21bfcdb
Showing
3 changed files
with
93 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 53 additions & 5 deletions
58
app/src/main/java/com/ftw/hometerview/ui/main/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
app/src/main/java/com/ftw/hometerview/ui/main/MainViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |