diff --git a/app/src/main/java/com/najudoryeong/mineme/MainActivity.kt b/app/src/main/java/com/najudoryeong/mineme/MainActivity.kt index 7fa77e0..175ebb1 100644 --- a/app/src/main/java/com/najudoryeong/mineme/MainActivity.kt +++ b/app/src/main/java/com/najudoryeong/mineme/MainActivity.kt @@ -2,33 +2,56 @@ package com.najudoryeong.mineme import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import androidx.activity.viewModels import androidx.databinding.DataBindingUtil +import androidx.databinding.Observable +import androidx.lifecycle.Observer +import androidx.navigation.Navigation +import androidx.navigation.findNavController import androidx.navigation.fragment.NavHostFragment +import androidx.navigation.ui.AppBarConfiguration +import androidx.navigation.ui.NavigationUI import androidx.navigation.ui.setupWithNavController +import com.najudoryeong.mineme.common_ui.MainViewModelUtil import com.najudoryeong.mineme.databinding.ActivityMainBinding - -class MainActivity : AppCompatActivity() { +// 하위 모듈이 MainViewModel 코드에 접근할 수 있게 MainViewModelUtil 상속 구현 +class MainActivity : AppCompatActivity(), MainViewModelUtil { private lateinit var binding: ActivityMainBinding + private lateinit var appBarConfiguration: AppBarConfiguration + private val model: MainViewModel by viewModels() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = DataBindingUtil.setContentView(this, R.layout.activity_main) + + binding.viewModel = model + model.toolbarTitle.observe(this, toolbarTitleObserver) + initAppBar() initBottomNav() } private fun initBottomNav() { - val navHostFragment = supportFragmentManager.findFragmentById(R.id.fragment_containerView) as NavHostFragment + val navHostFragment = + supportFragmentManager.findFragmentById(R.id.fragment_containerView) as NavHostFragment val navController = navHostFragment.navController - binding.bottomNavigationView.let { - it.setupWithNavController(navController) - it.itemIconTintList = null - } + // appbar 구성 요소 설정 + // 바텀네비게이션과 연결하면 해당 프래그먼트에 네비게이션으로는 backButton x + appBarConfiguration = AppBarConfiguration( + setOf(R.id.storyFragment) + ) + + // navController와 actionbar 연결 + NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration) + binding.bottomNavigationView.run { + setupWithNavController(navController) + itemIconTintList = null + } } private fun initAppBar() { @@ -38,4 +61,16 @@ class MainActivity : AppCompatActivity() { setDisplayHomeAsUpEnabled(true) } } + + /** [MainViewModelUtil] */ + override fun setToolbarTitle(newTitle: String) { + model.updateToolbarTitle(newTitle) + } + + //toolbar title 옵저버 + private val toolbarTitleObserver = Observer { + binding.toolbar.title = it + } + + } \ No newline at end of file diff --git a/app/src/main/java/com/najudoryeong/mineme/MainViewModel.kt b/app/src/main/java/com/najudoryeong/mineme/MainViewModel.kt index 7c15204..8be94c0 100644 --- a/app/src/main/java/com/najudoryeong/mineme/MainViewModel.kt +++ b/app/src/main/java/com/najudoryeong/mineme/MainViewModel.kt @@ -1,7 +1,20 @@ package com.najudoryeong.mineme +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel +import com.najudoryeong.mineme.common_ui.MainViewModelUtil + class MainViewModel : ViewModel() { + private val _toolbarTitle = MutableLiveData() + + val toolbarTitle: LiveData + get() = _toolbarTitle + + fun updateToolbarTitle(newTitle: String) { + _toolbarTitle.value = newTitle + } + } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 18bf7ed..eb72f1b 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -3,6 +3,13 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> + + + + + - + android:layout_height="?attr/actionBarSize" + app:titleCentered="true" />