Skip to content

Commit

Permalink
refactor(openfeedback): migrate firebase to firebase KMP.
Browse files Browse the repository at this point in the history
  • Loading branch information
GerardPaligot committed Nov 24, 2023
1 parent 50bbba3 commit 78e55e7
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 95 deletions.
1 change: 1 addition & 0 deletions build-logic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies {
implementation(libs.vespene)
implementation(libs.android.gradle.plugin)
implementation(libs.kotlin.gradle.plugin)
implementation(libs.kotlin.serialization.plugin)
}

gradlePlugin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class LibraryPlugin : Plugin<Project> {
with(target.pluginManager) {
apply("com.android.library")
apply("org.jetbrains.kotlin.android")
apply("org.jetbrains.kotlin.plugin.serialization")
apply("maven-publish")
apply("signing")
}
Expand Down
17 changes: 8 additions & 9 deletions libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
[versions]
kotlin_lang = "1.9.10"
kotlin_coroutines = "1.7.3"
kotlinx_serialization = "1.6.0"
androidx_core = "1.12.0"
androidx_compose_bom = "2023.10.01"
androidx_compose_compiler = "1.5.3"
androidx_lifecycle = "2.6.2"
androidx_savedstate = "1.2.1"
firebase_firestore = "24.0.1"
firebase_auth = "21.0.1"
gitlive_firebase = "1.10.4"

[libraries]
android-gradle-plugin = "com.android.tools.build:gradle:8.1.2"
vespene = "net.mbonnin.vespene:vespene-lib:0.5"

kotlin_gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin_lang" }
kotlin_serialization-plugin = { module = "org.jetbrains.kotlin:kotlin-serialization", version.ref = "kotlin_lang" }
kotlin-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlin_coroutines" }
kotlin-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "kotlin_coroutines" }
kotlin-coroutines-play-services = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-play-services", version.ref = "kotlin_coroutines" }

firebase_firestore = { module = "com.google.firebase:firebase-firestore-ktx", version.ref = "firebase_firestore" }
firebase_auth = { module = "com.google.firebase:firebase-auth", version.ref = "firebase_auth" }
kotlinx_serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx_serialization" }

gitlive_firestore = { module = "dev.gitlive:firebase-firestore", version.ref = "gitlive_firebase" }
gitlive_auth = { module = "dev.gitlive:firebase-auth", version.ref = "gitlive_firebase" }
gitlive_common = { module = "dev.gitlive:firebase-common", version.ref = "gitlive_firebase" }

androidx_core = { module = "androidx.core:core-ktx", version.ref = "androidx_core" }
androidx_savedstate = { module = "androidx.savedstate:savedstate-ktx", version.ref = "androidx_savedstate" }
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "androidx_compose_bom" }
androidx_compose_material = { module = "androidx.compose.material:material" }
androidx_compose_material3 = { module = "androidx.compose.material3:material3" }
androidx_compose_ui = { module = "androidx.compose.ui:ui" }
androidx_compose_runtime = { module = "androidx.compose.runtime:runtime" }
androidx_compose_foundation = { module = "androidx.compose.foundation:foundation" }
androidx_compose_uitooling = { module = "androidx.compose.ui:ui-tooling" }
androidx_lifecycle_viewmodel_compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "androidx_lifecycle" }
androidx_lifecycle_runtime = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "androidx_lifecycle" }
androidx_lifecycle_viewmodel_ktx = { module = "androidx.lifecycle:lifecycle-viewmodel-ktx", version.ref = "androidx_lifecycle" }
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package io.openfeedback.android.viewmodels
import android.content.Context
import com.google.firebase.FirebaseApp
import com.google.firebase.FirebaseOptions
import dev.gitlive.firebase.Firebase
import dev.gitlive.firebase.initialize

internal object FirebaseFactory {
fun create(
Expand All @@ -18,4 +20,21 @@ internal object FirebaseFactory {
.build()
return FirebaseApp.initializeApp(context, options, appName)
}

fun createKM(
context: Context,
config: OpenFeedbackFirebaseConfig,
appName: String = "openfeedback"
): dev.gitlive.firebase.FirebaseApp {
return Firebase.initialize(
context = context,
options = dev.gitlive.firebase.FirebaseOptions(
projectId = config.projectId,
applicationId = config.applicationId,
apiKey = config.apiKey,
databaseUrl = config.databaseUrl
),
name = appName
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ data class OpenFeedbackFirebaseConfig(
val databaseUrl: String,
val appName: String = "openfeedback"
) {
val firebaseApp = lazy { FirebaseFactory.create(context, this, appName) }
val firebaseApp = lazy { FirebaseFactory.createKM(context, this, appName) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.openfeedback.android.viewmodels
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.google.firebase.FirebaseApp
import dev.gitlive.firebase.FirebaseApp
import io.openfeedback.android.OpenFeedbackRepository
import io.openfeedback.android.caches.OptimisticVoteCaching
import io.openfeedback.android.model.VoteStatus
Expand Down
8 changes: 5 additions & 3 deletions openfeedback/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ openfeedback {
}

dependencies {
api(libs.kotlinx.serialization.json)
api(libs.kotlin.coroutines.core)
api(libs.kotlin.coroutines.android)
api(libs.kotlin.coroutines.play.services)

// Firestore
api(libs.firebase.firestore)
api(libs.firebase.auth)
// KMM Firestore
api(libs.gitlive.firestore)
api(libs.gitlive.auth)
implementation(libs.gitlive.common)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package io.openfeedback.android.model

import com.google.firebase.Timestamp
import kotlinx.serialization.Serializable

@Serializable
data class Project(
val chipColors: List<String> = emptyList(),
val voteItems: List<VoteItem> = emptyList()
)

@Serializable
data class VoteItem(
val id: String = "",
val languages: Map<String, String> = emptyMap(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package io.openfeedback.android.sources

import android.util.Log
import com.google.firebase.FirebaseApp
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import dev.gitlive.firebase.Firebase
import dev.gitlive.firebase.FirebaseApp
import dev.gitlive.firebase.auth.FirebaseAuth
import dev.gitlive.firebase.auth.FirebaseUser
import dev.gitlive.firebase.auth.auth
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.tasks.await

class OpenFeedbackAuth(private val auth: FirebaseAuth) {
suspend fun firebaseUser(): FirebaseUser? = Mutex().withLock {
if (auth.currentUser == null) {
val result = auth.signInAnonymously().await()
auth.signInAnonymously()
val result = auth.signInAnonymously()
if (result.user == null) {
Log.e("OpenFeedbackAuth", "Cannot signInAnonymously")
}
Expand All @@ -25,6 +27,6 @@ class OpenFeedbackAuth(private val auth: FirebaseAuth) {

companion object Factory {
fun create(app: FirebaseApp): OpenFeedbackAuth =
OpenFeedbackAuth(FirebaseAuth.getInstance(app))
OpenFeedbackAuth(Firebase.auth(app))
}
}
Loading

0 comments on commit 78e55e7

Please sign in to comment.