如何使用 Single Live Event 在 Kotlin 中显示 toast
How to use Single Live Event to show toast in Kotlin
我想使用单个直播活动 class 来展示祝酒词(如旗帜)
这是我尝试过的代码。
我不想像旗帜一样使用 peding。我该如何解决?
MainViewModel
class MainViewModel(private val movieRepository: MovieRepository) : ViewModel() {
val keyword = MutableLiveData<String>()
val movieList = MutableLiveData<List<Movie>>()
val msg = MutableLiveData<String>()
val pending: AtomicBoolean = AtomicBoolean(false)
fun findMovie() {
val keywordValue = keyword.value ?: return
pending.set(true)
if (keywordValue.isNullOrBlank()) {
msg.value = "emptyKeyword"
return
}
movieRepository.getMovieData(keyword = keywordValue, 30,
onSuccess = {
if (it.items!!.isEmpty()) {
msg.value = "emptyResult"
} else {
msg.value = "success"
movieList.value = it.items
}
},
onFailure = {
msg.value = "fail"
}
)
}
}
MainActivity
private fun viewModelCallback() {
mainViewModel.msg.observe(this, {
if (mainViewModel.pending.compareAndSet(true, false)) {
when (it) {
"success" -> toast(R.string.network_success)
"emptyKeyword" -> toast(R.string.keyword_empty)
"fail" -> toast(R.string.network_error)
"emptyResult" -> toast(R.string.keyword_result_empty)
}
}
})
}
SingleLiveEvent
扩展 MutableLiveData
。所以,你可以像普通的 MutableLiveData
.
一样使用它
首先,您需要包含 SingleLiveEvent.java
class (https://github.com/android/architecture-samples/blob/dev-todo-mvvm-live/todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/SingleLiveEvent.java)。复制此 class 文件并将其添加到您的项目中。
当你想显示 toast 时,你可以在你的 ViewModel 中这样设置,
SingleLiveEvent<String> toastMsg = new SingleLiveEvent<>(); //this goes in ViewModel constructor
toastMsg.setValue("hello"); //when you want to show toast
在您的 ViewModel 中创建一个函数来观察此 SingleLiveEvent toastMsg
并像观察您的 Activity
中的常规 LiveData
一样观察它
在视图模型中:
SingleLiveEvent getToastSLE() {
return toastMsg
}
在Activity中:
viewmodel.getToastSLE().observe(this, toastString -> {
Toast.makeText(this, toastString, Toast.LENGTH_LONG).show() //this will display toast "hello"
})
解决方案
步骤 1. 将 SingleLiveEvent.kt
复制到您的应用程序
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.myapp;
import android.util.Log;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* A lifecycle-aware observable that sends only new updates after subscription, used for events like
* navigation and Snackbar messages.
* <p>
* This avoids a common problem with events: on configuration change (like rotation) an update
* can be emitted if the observer is active. This LiveData only calls the observable if there's an
* explicit call to setValue() or call().
* <p>
* Note that only one observer is going to be notified of changes.
*/
public class SingleLiveEvent<T> extends MutableLiveData<T> {
private static final String TAG = "SingleLiveEvent";
private final AtomicBoolean mPending = new AtomicBoolean(false);
@MainThread
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) {
if (hasActiveObservers()) {
Log.w(TAG, "Multiple observers registered but only one will be notified of changes.");
}
// Observe the internal MutableLiveData
super.observe(owner, t -> {
if (mPending.compareAndSet(true, false)) {
observer.onChanged(t);
}
});
}
@MainThread
public void setValue(@Nullable T t) {
mPending.set(true);
super.setValue(t);
}
/**
* Used for cases where T is Void, to make calls cleaner.
*/
@MainThread
public void call() {
setValue(null);
}
}
第 2 步。 从您的代码中使用。
MainViewModel
class MainViewModel(private val movieRepository: MovieRepository) : ViewModel() {
val keyword = MutableLiveData<String>()
val movieList = MutableLiveData<List<Movie>>()
val msg = SingleLiveEvent<String>()
fun findMovie() {
val keywordValue = keyword.value ?: return
if (keywordValue.isNullOrBlank()) {
msg.value = "emptyKeyword"
return
}
movieRepository.getMovieData(keyword = keywordValue, 30,
onSuccess = {
if (it.items!!.isEmpty()) {
msg.value = "emptyResult"
} else {
msg.value = "success"
movieList.value = it.items
}
},
onFailure = {
msg.value = "fail"
}
)
}
}
MainActivity
private fun viewModelCallback() {
mainViewModel.msg.observe(this, {
when (it) {
"success" -> toast(R.string.network_success)
"emptyKeyword" -> toast(R.string.keyword_empty)
"fail" -> toast(R.string.network_error)
"emptyResult" -> toast(R.string.keyword_result_empty)
}
})
}
而不是 SingleLiveEvent,如果您使用的是 Kotlin 并且仅一次性触发 data/event,请使用 MutableSharedFlow
示例:
// init
val data = MutableSharedFlow<String>()
// set value
data.emit("hello world)
lifecycleScope.launchWhenStarted {
data.collectLatest {
// value only collect once unless a new trigger come
}
}
MutableSharedFlow
不会触发方向更改或返回上一个片段等
如 here 在 2021 年 12 月编辑 最后所述,您应该让视图告诉您的 viewModel 您的事件已被处理。它不是一个漂亮的解决方案,但它绝对是最容易理解和实施的解决方案之一。
基本上你是在你的 viewModel 中添加一个 StateFlow 来保存你的事件然后在你的视图收集它之后,你再次将该状态重置为空:
在你的 viewModel ->
private val _loadingPostVisibilityEvent = MutableStateFlow<Boolean?>(null)
val loadingPostVisibilityEvent: StateFlow<Boolean?> = _loadingPostVisibilityEvent
fun setLoadingPostVisibilityEvent(isVisible: Boolean?) {
_loadingPostVisibilityEvent.value = isVisible
}
那么在你看来->
viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
launch {
actionsViewModel.loadingPostVisibilityEvent.filterNotNull().collect {
// do your magic with the value $it
//then don't forget to reset the state.
actionsViewModel.setLoadingPostVisibilityEvent(null)
}
}
}
}
注意如果您没有将事件 stateFlow 重置为 null,如果再次重新创建视图,它可能会再次被收集。
如果你想使用扩展功能收集一次然后添加这个 ->
suspend fun <T> StateFlow<T?>.collectOnce(reset: (T?) -> Unit, action: (value: T) -> Unit) {
this.filterNotNull().onEach { reset.invoke(null) }.collect {
action.invoke(it)
}
}
并像这样使用它
viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
launch {
actionsViewModel.loadingPostVisibilityEvent.collectOnce(actionsViewModel::setLoadingPostVisibilityEvent) {
// do your magic with the value $it
}
}
}
}
我想使用单个直播活动 class 来展示祝酒词(如旗帜) 这是我尝试过的代码。 我不想像旗帜一样使用 peding。我该如何解决?
MainViewModel
class MainViewModel(private val movieRepository: MovieRepository) : ViewModel() {
val keyword = MutableLiveData<String>()
val movieList = MutableLiveData<List<Movie>>()
val msg = MutableLiveData<String>()
val pending: AtomicBoolean = AtomicBoolean(false)
fun findMovie() {
val keywordValue = keyword.value ?: return
pending.set(true)
if (keywordValue.isNullOrBlank()) {
msg.value = "emptyKeyword"
return
}
movieRepository.getMovieData(keyword = keywordValue, 30,
onSuccess = {
if (it.items!!.isEmpty()) {
msg.value = "emptyResult"
} else {
msg.value = "success"
movieList.value = it.items
}
},
onFailure = {
msg.value = "fail"
}
)
}
}
MainActivity
private fun viewModelCallback() {
mainViewModel.msg.observe(this, {
if (mainViewModel.pending.compareAndSet(true, false)) {
when (it) {
"success" -> toast(R.string.network_success)
"emptyKeyword" -> toast(R.string.keyword_empty)
"fail" -> toast(R.string.network_error)
"emptyResult" -> toast(R.string.keyword_result_empty)
}
}
})
}
SingleLiveEvent
扩展 MutableLiveData
。所以,你可以像普通的 MutableLiveData
.
首先,您需要包含 SingleLiveEvent.java
class (https://github.com/android/architecture-samples/blob/dev-todo-mvvm-live/todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/SingleLiveEvent.java)。复制此 class 文件并将其添加到您的项目中。
当你想显示 toast 时,你可以在你的 ViewModel 中这样设置,
SingleLiveEvent<String> toastMsg = new SingleLiveEvent<>(); //this goes in ViewModel constructor
toastMsg.setValue("hello"); //when you want to show toast
在您的 ViewModel 中创建一个函数来观察此 SingleLiveEvent toastMsg
并像观察您的 Activity
LiveData
一样观察它
在视图模型中:
SingleLiveEvent getToastSLE() {
return toastMsg
}
在Activity中:
viewmodel.getToastSLE().observe(this, toastString -> {
Toast.makeText(this, toastString, Toast.LENGTH_LONG).show() //this will display toast "hello"
})
解决方案
步骤 1. 将 SingleLiveEvent.kt
复制到您的应用程序
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.myapp;
import android.util.Log;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* A lifecycle-aware observable that sends only new updates after subscription, used for events like
* navigation and Snackbar messages.
* <p>
* This avoids a common problem with events: on configuration change (like rotation) an update
* can be emitted if the observer is active. This LiveData only calls the observable if there's an
* explicit call to setValue() or call().
* <p>
* Note that only one observer is going to be notified of changes.
*/
public class SingleLiveEvent<T> extends MutableLiveData<T> {
private static final String TAG = "SingleLiveEvent";
private final AtomicBoolean mPending = new AtomicBoolean(false);
@MainThread
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) {
if (hasActiveObservers()) {
Log.w(TAG, "Multiple observers registered but only one will be notified of changes.");
}
// Observe the internal MutableLiveData
super.observe(owner, t -> {
if (mPending.compareAndSet(true, false)) {
observer.onChanged(t);
}
});
}
@MainThread
public void setValue(@Nullable T t) {
mPending.set(true);
super.setValue(t);
}
/**
* Used for cases where T is Void, to make calls cleaner.
*/
@MainThread
public void call() {
setValue(null);
}
}
第 2 步。 从您的代码中使用。
MainViewModel
class MainViewModel(private val movieRepository: MovieRepository) : ViewModel() {
val keyword = MutableLiveData<String>()
val movieList = MutableLiveData<List<Movie>>()
val msg = SingleLiveEvent<String>()
fun findMovie() {
val keywordValue = keyword.value ?: return
if (keywordValue.isNullOrBlank()) {
msg.value = "emptyKeyword"
return
}
movieRepository.getMovieData(keyword = keywordValue, 30,
onSuccess = {
if (it.items!!.isEmpty()) {
msg.value = "emptyResult"
} else {
msg.value = "success"
movieList.value = it.items
}
},
onFailure = {
msg.value = "fail"
}
)
}
}
MainActivity
private fun viewModelCallback() {
mainViewModel.msg.observe(this, {
when (it) {
"success" -> toast(R.string.network_success)
"emptyKeyword" -> toast(R.string.keyword_empty)
"fail" -> toast(R.string.network_error)
"emptyResult" -> toast(R.string.keyword_result_empty)
}
})
}
而不是 SingleLiveEvent,如果您使用的是 Kotlin 并且仅一次性触发 data/event,请使用 MutableSharedFlow
示例:
// init
val data = MutableSharedFlow<String>()
// set value
data.emit("hello world)
lifecycleScope.launchWhenStarted {
data.collectLatest {
// value only collect once unless a new trigger come
}
}
MutableSharedFlow
不会触发方向更改或返回上一个片段等
如 here 在 2021 年 12 月编辑 最后所述,您应该让视图告诉您的 viewModel 您的事件已被处理。它不是一个漂亮的解决方案,但它绝对是最容易理解和实施的解决方案之一。
基本上你是在你的 viewModel 中添加一个 StateFlow 来保存你的事件然后在你的视图收集它之后,你再次将该状态重置为空:
在你的 viewModel ->
private val _loadingPostVisibilityEvent = MutableStateFlow<Boolean?>(null)
val loadingPostVisibilityEvent: StateFlow<Boolean?> = _loadingPostVisibilityEvent
fun setLoadingPostVisibilityEvent(isVisible: Boolean?) {
_loadingPostVisibilityEvent.value = isVisible
}
那么在你看来->
viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
launch {
actionsViewModel.loadingPostVisibilityEvent.filterNotNull().collect {
// do your magic with the value $it
//then don't forget to reset the state.
actionsViewModel.setLoadingPostVisibilityEvent(null)
}
}
}
}
注意如果您没有将事件 stateFlow 重置为 null,如果再次重新创建视图,它可能会再次被收集。
如果你想使用扩展功能收集一次然后添加这个 ->
suspend fun <T> StateFlow<T?>.collectOnce(reset: (T?) -> Unit, action: (value: T) -> Unit) {
this.filterNotNull().onEach { reset.invoke(null) }.collect {
action.invoke(it)
}
}
并像这样使用它
viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
launch {
actionsViewModel.loadingPostVisibilityEvent.collectOnce(actionsViewModel::setLoadingPostVisibilityEvent) {
// do your magic with the value $it
}
}
}
}