Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[Jetchat] Moved back to LiveData
Change-Id: I599817ddac91513c5e65c16e6a3c4838963fddc5
  • Loading branch information
JoseAlcerreca committed Dec 3, 2020
commit a8d2462137e56e43cfdc9256a54c9319282ff6c1
2 changes: 2 additions & 0 deletions Jetchat/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ dependencies {

implementation Libs.AndroidX.coreKtx
implementation Libs.AndroidX.appcompat
implementation Libs.AndroidX.Lifecycle.livedata
implementation Libs.AndroidX.Navigation.fragment
implementation Libs.AndroidX.Navigation.uiKtx
implementation Libs.material
Expand All @@ -105,6 +106,7 @@ dependencies {
implementation Libs.AndroidX.Compose.tooling
implementation Libs.AndroidX.Compose.uiUtil
implementation Libs.AndroidX.Compose.runtime
implementation Libs.AndroidX.Compose.runtimeLivedata
implementation Libs.AndroidX.Compose.viewBinding

androidTestImplementation Libs.junit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@

package com.example.compose.jetchat

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

/**
* Used to communicate between screens.
*/
class MainViewModel : ViewModel() {

private val _drawerShouldBeOpened = MutableStateFlow(false)
val drawerShouldBeOpened: StateFlow<Boolean> = _drawerShouldBeOpened
private val _drawerShouldBeOpened = MutableLiveData(false)
val drawerShouldBeOpened: LiveData<Boolean> = _drawerShouldBeOpened

fun openDrawer() {
_drawerShouldBeOpened.value = true
}
fun resetOpenDrawer() {
fun resetOpenDrawerAction() {
_drawerShouldBeOpened.value = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.compose.material.rememberScaffoldState
import androidx.compose.runtime.Providers
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.platform.setContent
import androidx.compose.ui.viewinterop.AndroidViewBinding
import androidx.core.os.bundleOf
Expand All @@ -45,11 +46,11 @@ class NavActivity : AppCompatActivity() {
Providers(AmbientBackPressedDispatcher provides this) {
val scaffoldState = rememberScaffoldState()

val openDrawerEvent = viewModel.drawerShouldBeOpened.collectAsState()
if (openDrawerEvent.value) {
val openDrawerEvent = viewModel.drawerShouldBeOpened.observeAsState()
if (openDrawerEvent.value == true) {
// Open drawer and reset state in VM.
scaffoldState.drawerState.open {
viewModel.resetOpenDrawer()
viewModel.resetOpenDrawerAction()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,14 @@ private fun ChatItem(text: String, selected: Boolean, onChatClicked: () -> Unit)
.clickable(onClick = onChatClicked),
verticalAlignment = CenterVertically
) {
val mediumEmphasisOnSurface =
val iconTint = if (selected) {
MaterialTheme.colors.primary
} else {
MaterialTheme.colors.onSurface.copy(alpha = ContentAlpha.medium)
}
Icon(
vectorResource(id = R.drawable.ic_jetchat),
tint = if (selected) MaterialTheme.colors.primary else mediumEmphasisOnSurface,
tint = iconTint,
modifier = Modifier.padding(8.dp)
)
Providers(AmbientContentAlpha provides ContentAlpha.medium) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ fun JetchatScaffold(
drawerContent = {
JetchatDrawer(
onProfileClicked = onProfileClicked,
onChatClicked = onChatClicked,

onChatClicked = onChatClicked
)
},
bodyContent = content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.platform.ComposeView
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
Expand Down Expand Up @@ -49,7 +49,7 @@ class ProfileFragment : Fragment() {

return ComposeView(context = requireContext()).apply {
setContent {
viewModel.userData.collectAsState().value.let { userData: ProfileScreenState? ->
viewModel.userData.observeAsState().value.let { userData ->
JetchatTheme {
if (userData == null) {
ProfileError()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ package com.example.compose.jetchat.profile

import androidx.annotation.DrawableRes
import androidx.compose.runtime.Immutable
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.compose.jetchat.data.colleagueProfile
import com.example.compose.jetchat.data.meProfile
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

class ProfileViewModel : ViewModel() {

Expand All @@ -35,8 +35,8 @@ class ProfileViewModel : ViewModel() {
_userData.value = if (userId == meProfile.userId) meProfile else colleagueProfile
}

private val _userData = MutableStateFlow<ProfileScreenState?>(null)
val userData: StateFlow<ProfileScreenState?> = _userData
private val _userData = MutableLiveData<ProfileScreenState>()
val userData: LiveData<ProfileScreenState> = _userData
}

@Immutable
Expand Down