Add firebase storage
This commit is contained in:
parent
e985be8604
commit
4afac7eece
4 changed files with 123 additions and 5 deletions
4
external/src/main/java/dev/lucasnlm/external/ICloudStorage.kt
vendored
Normal file
4
external/src/main/java/dev/lucasnlm/external/ICloudStorage.kt
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
package dev.lucasnlm.external
|
||||
|
||||
interface ICloudStorage {
|
||||
}
|
57
external/src/main/java/dev/lucasnlm/external/model/CloudSave.kt
vendored
Normal file
57
external/src/main/java/dev/lucasnlm/external/model/CloudSave.kt
vendored
Normal file
|
@ -0,0 +1,57 @@
|
|||
package dev.lucasnlm.external.model
|
||||
|
||||
data class CloudSave(
|
||||
val playId: String,
|
||||
val lastWidth: Int,
|
||||
val lastHeight: Int,
|
||||
val lastMines: Int,
|
||||
val completeTutorial: Int,
|
||||
val selectedTheme: Int,
|
||||
val squareRadius: Int,
|
||||
val squareSize: Int,
|
||||
val touchTiming: Int,
|
||||
val questionMark: Int,
|
||||
val gameAssistance: Int,
|
||||
val help: Int,
|
||||
val hapticFeedback: Int,
|
||||
val soundEffects: Int,
|
||||
val stats: List<Map<String, String>>
|
||||
)
|
||||
|
||||
fun CloudSave.toHashMap(): HashMap<String, Any> = hashMapOf(
|
||||
"lastWidth" to lastWidth,
|
||||
"lastHeight" to lastHeight,
|
||||
"lastMines" to lastMines,
|
||||
"completeTutorial" to completeTutorial,
|
||||
"selectedTheme" to selectedTheme,
|
||||
"squareRadius" to squareRadius,
|
||||
"squareSize" to squareSize,
|
||||
"touchTiming" to touchTiming,
|
||||
"questionMark" to questionMark,
|
||||
"gameAssistance" to gameAssistance,
|
||||
"help" to help,
|
||||
"hapticFeedback" to hapticFeedback,
|
||||
"soundEffects" to soundEffects,
|
||||
"stats" to stats,
|
||||
)
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun cloudSaveOf(id: String, data: Map<String, Any>) =
|
||||
CloudSave(
|
||||
id,
|
||||
data["lastWidth"].toString().toInt(),
|
||||
data["lastHeight"].toString().toInt(),
|
||||
data["lastMines"].toString().toInt(),
|
||||
data["completeTutorial"].toString().toInt(),
|
||||
data["selectedTheme"].toString().toInt(),
|
||||
data["squareRadius"].toString().toInt(),
|
||||
data["squareSize"].toString().toInt(),
|
||||
data["touchTiming"].toString().toInt(),
|
||||
data["questionMark"].toString().toInt(),
|
||||
data["gameAssistance"].toString().toInt(),
|
||||
data["help"].toString().toInt(),
|
||||
data["hapticFeedback"].toString().toInt(),
|
||||
data["soundEffects"].toString().toInt(),
|
||||
data["stats"] as List<Map<String, String>>,
|
||||
)
|
||||
|
|
@ -38,16 +38,17 @@ dependencies {
|
|||
implementation project(':external')
|
||||
|
||||
// Google
|
||||
implementation 'com.android.billingclient:billing-ktx:3.0.0'
|
||||
implementation 'com.android.billingclient:billing-ktx:3.0.1'
|
||||
implementation 'com.google.android.gms:play-services-instantapps:17.0.0'
|
||||
implementation 'com.google.android.gms:play-services-games:20.0.0'
|
||||
implementation 'com.google.android.gms:play-services-games:20.0.1'
|
||||
implementation 'com.google.android.gms:play-services-auth:18.1.0'
|
||||
implementation 'com.google.android.gms:play-services-ads:19.3.0'
|
||||
implementation 'com.google.android.gms:play-services-ads:19.4.0'
|
||||
implementation 'com.google.android.play:core-ktx:1.8.1'
|
||||
|
||||
// Firebase
|
||||
implementation 'com.google.firebase:firebase-analytics-ktx:17.5.0'
|
||||
implementation 'com.google.firebase:firebase-crashlytics:17.2.1'
|
||||
implementation 'com.google.firebase:firebase-analytics-ktx:17.6.0'
|
||||
implementation 'com.google.firebase:firebase-crashlytics:17.2.2'
|
||||
implementation 'com.google.firebase:firebase-firestore-ktx:21.7.1'
|
||||
|
||||
// Kotlin
|
||||
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
|
||||
|
|
56
proprietary/src/main/java/dev/lucasnlm/external/CloudStorage.kt
vendored
Normal file
56
proprietary/src/main/java/dev/lucasnlm/external/CloudStorage.kt
vendored
Normal file
|
@ -0,0 +1,56 @@
|
|||
package dev.lucasnlm.external
|
||||
|
||||
import android.util.Log
|
||||
import com.google.android.gms.tasks.Tasks
|
||||
import com.google.firebase.firestore.ktx.firestore
|
||||
import com.google.firebase.ktx.Firebase
|
||||
import dev.lucasnlm.external.model.CloudSave
|
||||
import dev.lucasnlm.external.model.cloudSaveOf
|
||||
import dev.lucasnlm.external.model.toHashMap
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.util.concurrent.ExecutionException
|
||||
|
||||
class CloudStorage : ICloudStorage {
|
||||
private val db by lazy { Firebase.firestore }
|
||||
|
||||
fun uploadSave(cloudSave: CloudSave) {
|
||||
val data = cloudSave.toHashMap()
|
||||
db.collection(SAVES)
|
||||
.document(cloudSave.playId)
|
||||
.set(data)
|
||||
.addOnFailureListener {
|
||||
Log.e(TAG, "Fail to save on cloud", it)
|
||||
}
|
||||
.addOnSuccessListener {
|
||||
Log.v(TAG, "Saved on cloud")
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getSave(playId: String): CloudSave {
|
||||
try {
|
||||
GlobalScope.launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
val result = Tasks.await(
|
||||
db.collection(SAVES)
|
||||
.document(playId)
|
||||
.get()
|
||||
)
|
||||
|
||||
result.data?.let {
|
||||
cloudSaveOf(playId, it.toMap())
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Fail to load save on cloud", e)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val TAG = "CloudStorage"
|
||||
const val SAVES = "saves"
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue