Skip to content

Commit

Permalink
Merge pull request #9 from deckameron/main
Browse files Browse the repository at this point in the history
New Android array serialization and creating a document with ID
  • Loading branch information
hansemannn authored Nov 24, 2024
2 parents 778b2f1 + 2d14e2e commit 8405d33
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 8 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ repositories {
}

dependencies {
implementation 'com.google.firebase:firebase-firestore-ktx:24.3.1'
implementation 'com.google.firebase:firebase-firestore-ktx:25.1.1'
}
2 changes: 1 addition & 1 deletion android/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 2.0.0
version: 2.0.2
apiversion: 4
architectures: arm64-v8a armeabi-v7a x86 x86_64
description: titanium-firebase-firestore
Expand Down
82 changes: 79 additions & 3 deletions android/src/firebase/firestore/TitaniumFirebaseFirestoreModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

package firebase.firestore

import android.util.Log
import com.google.firebase.firestore.FirebaseFirestoreException
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
import org.appcelerator.kroll.KrollDict
Expand All @@ -20,16 +22,61 @@ import org.appcelerator.kroll.annotations.Kroll
@Kroll.module(name = "TitaniumFirebaseFirestore", id = "firebase.firestore")
class TitaniumFirebaseFirestoreModule: KrollModule() {

private fun convertToFirestoreMap(input: KrollDict): Map<String, Any?> {
val output = mutableMapOf<String, Any?>()
input.keys.forEach { key ->
val value = input[key]
when (value) {
is KrollDict -> output[key] = convertToFirestoreMap(value) // Substituir objetos aninhados
is Array<*> -> output[key] = value.toList() // Converter array para List
is List<*> -> output[key] = value // Já é uma lista compatível
else -> output[key] = value // Outros tipos são mantidos
}
}
return output
}

// Methods
@Kroll.method
fun addDocumentWithId(params: KrollDict) {
val callback = params["callback"] as KrollFunction
val collection = params["collection"] as String
val customDocumentId = params["id"] as String
val data = params.getKrollDict("data")

// Converte KrollDict para Map<String, Any?> compatível com Firestore
val firestoreData = convertToFirestoreMap(data)

Firebase.firestore.collection(collection)
.document(customDocumentId)
.set(firestoreData)
.addOnSuccessListener {
val event = KrollDict()
event["success"] = true
event["documentID"] = customDocumentId

callback.callAsync(getKrollObject(), event)
}
.addOnFailureListener { error ->
val event = KrollDict()
event["success"] = false
event["error"] = error.localizedMessage

callback.callAsync(getKrollObject(), event)
}
}

@Kroll.method
fun addDocument(params: KrollDict) {
val callback = params["callback"] as KrollFunction
val collection = params["collection"] as String
val data = params.getKrollDict("data")

// Converte KrollDict para Map<String, Any?> compatível com Firestore
val firestoreData = convertToFirestoreMap(data)

Firebase.firestore.collection(collection)
.add(data)
.add(firestoreData)
.addOnSuccessListener {
val event = KrollDict()
event["success"] = true
Expand Down Expand Up @@ -77,20 +124,49 @@ class TitaniumFirebaseFirestoreModule: KrollModule() {
}
}

@Kroll.method
fun updateOneFieldInDocument(params: KrollDict) {
val callback = params["callback"] as KrollFunction
val collection = params["collection"] as String
val fieldName = params["field_name"] as String
val fieldValue = params["field_value"]
val document = params["document"] as String

Firebase.firestore.collection(collection)
.document(document)
.update(fieldName, fieldValue)
.addOnSuccessListener {
val event = KrollDict()
event["success"] = true
event["field"] = fieldName
event["value"] = fieldValue
callback.callAsync(getKrollObject(), event)
}
.addOnFailureListener { error ->
val event = KrollDict()
event["success"] = false
event["error"] = error.localizedMessage

callback.callAsync(getKrollObject(), event)
}
}

@Kroll.method
fun updateDocument(params: KrollDict) {
val callback = params["callback"] as KrollFunction
val collection = params["collection"] as String
val data = params.getKrollDict("data")
val document = params["document"] as String

// Converte KrollDict para Map<String, Any?> compatível com Firestore
val firestoreData = convertToFirestoreMap(data)

Firebase.firestore.collection(collection)
.document(document)
.update(data)
.set(firestoreData)
.addOnSuccessListener {
val event = KrollDict()
event["success"] = true

callback.callAsync(getKrollObject(), event)
}
.addOnFailureListener { error ->
Expand Down
4 changes: 2 additions & 2 deletions ios/Classes/FirebaseFirestoreModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ - (void)updateDocument:(id)params
NSDictionary *data = params[@"data"]; // TODO: Parse "FIRFieldValue" proxy types
NSString *document = params[@"document"];

[[[FIRFirestore.firestore collectionWithPath:collection] documentWithPath:document] updateData:data
completion:^(NSError *_Nullable error) {
[[[FIRFirestore.firestore collectionWithPath:collection] documentWithPath:document] setData:data
completion:^(NSError * _Nullable error) {
if (error != nil) {
[callback call:@[ @{@"success" : @(NO),
@"error" : error.localizedDescription} ]
Expand Down
2 changes: 1 addition & 1 deletion ios/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 1.0.2
version: 1.0.3
apiversion: 2
architectures: arm64 x86_64
mac: false
Expand Down

0 comments on commit 8405d33

Please sign in to comment.