EdricChan03 / EdricChan03/studybuddy-android

[Refactor] Migrate to GitLive's Firebase Kotlin SDK

Open
#413 0 comments 0 reactions 0 assignees View on GitHub
architecture code health enhancement epic firebase: auth :fire: firebase: firestore :fire: migration question
Dominant language
Kotlin
Stars
23
Forks
9
Avg merge
9h 1m
Merged PRs (30d)
1

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Feature type

Other

### Problem

The [Firebase Android SDK](https://firebase.google.com/docs/firestore/client/libraries#mobile_and_web_sdks) is built upon Java, requiring us to use certain extension functions for methods that return a [`Task` class from the Google Play Services library](https://developers.google.com/android/reference/com/google/android/gms/tasks/Task.html) (i.e. the [`await()` method](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-play-services/kotlinx.coroutines.tasks/await.html?query=suspend%20fun%20%3CT%3E%20Task%3CT%3E.await():%20T) from [kotlinx-coroutines-play-services](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-play-services/)):

https://github.com/EdricChan03/studybuddy-android/blob/011cb7d041628b6a66ec20e9fe078d0bd8cbf854/app/src/main/java/com/edricchan/studybuddy/extensions/firebase/auth/FirebaseUserExts.kt#L20-L107

### Description

Migrating to [GitLive's Firebase Kotlin SDK](https://github.com/GitLiveApp/firebase-kotlin-sdk) can remove the additional extension functions, and also allows for Kotlin syntax to be used:

```kt
// Without the Kotlin SDK
Firebase.auth.currentUser?.updateProfileAsync {
displayName = "John Doe"
photoUri = "..."
}
// With the Kotlin SDK from GitLive
Firebase.auth.currentUser?.updateProfile(displayName = "John Doe", photoUri = "...")
```

Additionally, we can also use [kotlinx.serialization](https://kotlinlang.org/docs/serialization.html) to serialize [Kotlin's data classes](https://kotlinlang.org/docs/data-classes.html) into [Firestore](https://firebase.google.com/docs/firestore/)/[Realtime Database](https://firebase.google.com/docs/database/) instead of needing to adhere to the [POJO requirements](https://firebase.google.com/docs/firestore/manage-data/add-data#custom_objects):

```kt
// Without kx.ser
data class TodoItem(
val title: String = "", // Default values are required, or there must be an empty constructor
val description: String, // May fail at runtime(?)
)

val myItem = TodoItem(...)
Firebase.firestore.collection("users/${userId}/todos").add(myItem)

// With kx.ser
import kotlinx.serializable.Serializable

@Serializable
data class TodoItem(
val title: String,
)

val myItem = TodoItem(...)
// See https://github.com/GitLiveApp/firebase-kotlin-sdk#serialization
Firebase.firestore.collection("users/${userId}/todos").add(TodoItem.serializer(), myItem, ...)
```

### Alternatives?

_No response_

### Additional info

_No response_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.