Skip to content

Instantly share code, notes, and snippets.

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import coil.compose.rememberImagePainter
import kotlinx.serialization.Serializable
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
function signedIn() {
return request.auth.uid != null;
}
match /users/{user} {
allow read, write: if (signedIn() == true);
}
match /posts/{post} {
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
function signedIn() {
return request.auth.uid != null;
}
match /users/{user} {
allow read, write: if (signedIn() == true);
}
match /posts/{post} {
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write
}
}
}
fun changeLike(postItem: PostItem, onCompleted: (Result<Boolean>) -> Unit) {
val currentUser = FirebaseAuthHelper.instance.currentUser
val document = FirebaseFirestore.getInstance().collection("posts").document(postItem.uuid)
if (currentUser != null) {
// Подготавливаем здесь контент
FirebaseFirestore.getInstance().runTransaction { transition ->
//Обновляем поля
transition.update(document, "likeItems", newLikes)
newLikes
}.addOnSuccessListener { result ->
suspend fun sendComment(commentItem: CommentItem) {
val currentUser = FirebaseAuthHelper.instance.currentUser
commentItem.userId = currentUser?.uid.orEmpty()
commentItem.userName = currentUser?.displayName.orEmpty()
val collection =
FirebaseFirestore.getInstance().collection("posts").document(commentItem.postId)
.collection("comments")
val document = collection.document(commentItem.uuid)
document.set(commentItem).await()
fun startListenToComments(postId: String, result: (List<CommentItem>) -> Unit) {
val collection = FirebaseFirestore.getInstance().collection("posts").document(postId)
.collection("comments")
commentListener = collection.orderBy("date", Query.Direction.DESCENDING)
.addSnapshotListener(MetadataChanges.INCLUDE) { data, firebaseFirestoreExceptioor ->
if (data != null) {
val comments = data.toObjects(CommentItem::class.java)
result(comments)
}
}
val storage = Firebase.storage
suspend fun uploadImage(bytes: ByteArray): Uri? {
//Ссылка на объект
val reference = storage.reference.child("image-${Date()}.jpg")
var uploadTask = reference.putBytes(bytes)
// Ожидаем загрузку
uploadTask.await()
//Обрабатываем конечный url
return reference.downloadUrl.await()
suspend fun deletePost(id: String) {
val collection = FirebaseFirestore.getInstance().collection("posts")
val document = collection.document(id)
document.delete().await()
}
//Создание
suspend fun createPost(postItem: PostItem) {
val currentUser = FirebaseAuthHelper.instance.currentUser
postItem.userId = currentUser?.uid.orEmpty()
postItem.userName = currentUser?.displayName.orEmpty()
val collection = FirebaseFirestore.getInstance().collection("posts")
val document = collection.document(postItem.uuid)
document.set(postItem).await()
}