Skip to content

Commit

Permalink
[MIN-22] feature : MainViewModel 프래그먼트 전환에 따른 툴바 텍스트 옵저빙 구현(#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
KDW03 committed Dec 31, 2022
1 parent 65f7ce5 commit b5fc0b9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
49 changes: 42 additions & 7 deletions app/src/main/java/com/najudoryeong/mineme/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -38,4 +61,16 @@ class MainActivity : AppCompatActivity() {
setDisplayHomeAsUpEnabled(true)
}
}

/** [MainViewModelUtil] */
override fun setToolbarTitle(newTitle: String) {
model.updateToolbarTitle(newTitle)
}

//toolbar title 옵저버
private val toolbarTitleObserver = Observer<String> {
binding.toolbar.title = it
}


}
13 changes: 13 additions & 0 deletions app/src/main/java/com/najudoryeong/mineme/MainViewModel.kt
Original file line number Diff line number Diff line change
@@ -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<String>()

val toolbarTitle: LiveData<String>
get() = _toolbarTitle

fun updateToolbarTitle(newTitle: String) {
_toolbarTitle.value = newTitle
}

}
12 changes: 10 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<data>

<variable
name="viewModel"
type="com.najudoryeong.mineme.MainViewModel" />
</data>

<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -12,10 +19,11 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">

<androidx.appcompat.widget.Toolbar
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"/>
android:layout_height="?attr/actionBarSize"
app:titleCentered="true" />

</com.google.android.material.appbar.AppBarLayout>

Expand Down

0 comments on commit b5fc0b9

Please sign in to comment.