我希望能够在我的 ViewModel 中使用 Kotlin 协程收听 Firebase 数据库中的实时更新。
问题是,每当在集合中创建新消息时,我的应用程序就会冻结并且无法从此状态恢复。我需要杀死它并重新启动应用程序。
它第一次通过,我可以在 UI 上看到以前的消息。SnapshotListener第二次调用时会发生此问题。
我的observer()功能
val channel = Channel<List<MessageEntity>>()
firestore.collection(path).addSnapshotListener { data, error ->
if (error != null) {
channel.close(error)
} else {
if (data != null) {
val messages = data.toObjects(MessageEntity::class.java)
//till this point it gets executed^^^^
channel.sendBlocking(messages)
} else {
channel.close(CancellationException("No data received"))
}
}
}
return channel
Run Code Online (Sandbox Code Playgroud)
这就是我想观察消息的方式
launch(Dispatchers.IO) {
val newMessages =
messageRepository
.observer()
.receive()
}
}
Run Code Online (Sandbox Code Playgroud)
在我替换sendBlocking()为之后,send()我仍然没有在频道中收到任何新消息。SnapshotListener边被执行
//channel.sendBlocking(messages) …Run Code Online (Sandbox Code Playgroud) 今天,我正在尝试安装新的材料组件,这是您需要更改应用程序的父级以从Theme.MaterialComponents继承。我之所以这样做,是因为我想使用底部导航更好的波纹。但是之后,几乎所有应用程序中的按钮都变得更加膨松。
我该怎么做才能恢复到之前的状态(右图)?
我想使用带有白色背景和彩色图标的“使用Google登录”按钮,但是当我使用此Google图标时,总是会被着色。
<android.support.design.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.Icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign in with Google"
app:icon="@drawable/ic_google_colored"/>
Run Code Online (Sandbox Code Playgroud)
如何使图标不着色?
android material-design material-components material-components-android
我有这个布局。
<data>
<import type="android.view.View" />
<variable
name="shouldBeVisible"
type="java.lang.Boolean" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="@={shouldBeVisible}" />
<LinearLayout
android:layout_width="40dp"
android:layout_height="30dp"
android:background="#dd0"
android:visibility="@{shouldBeVisible ? View.VISIBLE : View.GONE}" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
shouldBeVisible默认为 false。有办法shouldBeVisible实现吗?在这种情况下,我希望 LinearLayout 可见。
到目前为止我正在使用 binding.shouldBeVisible=true
约束布局版本:2.0.0-alpha3
所以我使用 MotionLayout 我想创建类似的东西。https://blog.stylingandroid.com/motionlayout-collapsing-toolbar-part-2/
我想实现当用户进入活动ProgressBar时,当我加载数据时(需要一些时间)我想ProgressBar隐藏。
我的问题是,当我开始与 UI 交互时,ProgressBar状态被重置并再次可见
我应该怎么做才能防止 progressBar 在用户开始与之交互后开始显示?
这是一个简化版
layout file
<androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutDescription="@xml/collapsing_toolbar"
tools:context=".MainActivity"
tools:showPaths="true">
<androidx.core.widget.NestedScrollView
android:id="@+id/scroll_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar_image">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="match_parent"
android:layout_height="2000dp"
android:background="#ff2"/>
</FrameLayout>
</androidx.core.widget.NestedScrollView>
<ImageView
android:id="@+id/toolbar_image"
android:layout_width="0dp"
android:layout_height="200dp"
android:background="@color/colorPrimary"
android:src="@color/colorAccent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/error"
android:text="ERROR"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.motion.widget.MotionLayout>
Run Code Online (Sandbox Code Playgroud)
这是布局说明 ( xml/collapsing_toolbar)
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Transition
app:constraintSetEnd="@id/collapsed"
app:constraintSetStart="@id/expanded">
<OnSwipe
app:dragDirection="dragUp"
app:touchAnchorId="@id/scroll_view"
app:touchAnchorSide="top"/> …Run Code Online (Sandbox Code Playgroud) 我希望开发人员能够使用该函数,但我希望他们知道它不适用于大多数情况。
有没有办法警告其他开发人员有关功能的信息。类似于 lint 的作用。@Warning("Prefer using #fooo() if possible)该方法有类似注释之类的东西吗?我想知道如何用java或kotlin来做。类似于@Deprecated,但在我的上下文中更有意义。
我希望我的 IDE 也能意识到这个警告,而不仅仅是编译器。
想象一下我们有聊天应用程序,在这个应用程序中,我们有很多房间,一些是私人的,一些是供所有人使用的。每个房间都有一个管理员可以管理用户(可以邀请和删除)。只有房间成员才能读取和写入消息。管理员是在此场景中创建房间的人。
我想在创建房间时创建安全规则,并在 MembersChange 上更新它,以便只有成员才能读取和写入留言板的内容。
在这种情况下,它可能是这样的:
databse/rooms/
private1
admin: memberX
members: member1, member2
//only admin can write into members fields
messages
message1...
message2...
message3...
//only members can write and read messages
private2
admin: memberXY
members: member1, member4
//only admin can write into members fields
messages
message1...
message2...
message3...
//only members can write and read messages
Run Code Online (Sandbox Code Playgroud)
那么是否可以从云功能创建和更新安全规则,而不是在 firebase 控制台中手动更新它们?或者有什么方法可以自动化这个过程吗?
我注意到我可以使用 CLI 部署安全规则。这里应该是一个什么样的流程呢?我什么时候调用它?如何从数据库中获取会员?
编辑: 对于任何想要更多信息的人,请查看如何在 Firebase 中构建安全应用程序
我想将地图转换为列表
例如我有
mapOf("a" to listOf(1,2),
"b" to listOf(3,4)
)
Run Code Online (Sandbox Code Playgroud)
我希望结果是
listOf("a", 1, 2, "b", 3, 4)
Run Code Online (Sandbox Code Playgroud)
order 必须是 Key 及其值,Key 及其值,...
kotlin 中是否有一些功能可以帮助我解决这个问题?