Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
Add analitics (#94)
Browse files Browse the repository at this point in the history
* fix android release build (proguard)

* Add firebase logs and analytics for android side
  • Loading branch information
VovaStelmashchuk authored Aug 23, 2023
1 parent 95e10b4 commit e7b5e1b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 32 deletions.
70 changes: 48 additions & 22 deletions androidApp/src/androidMain/kotlin/org/mixdrinks/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.tasks.Task
import com.google.firebase.FirebaseApp
import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.auth.GoogleAuthProvider
Expand All @@ -35,6 +36,10 @@ class MainActivity : AppCompatActivity() {
private lateinit var register: ActivityResultLauncher<Intent>
private lateinit var firebaseAuth: FirebaseAuth

enum class AuthProvider(val trackName: String) {
GOOGLE("google"), APPLE("apple")
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val deepLink = intent?.data?.toString()
Expand All @@ -60,22 +65,7 @@ class MainActivity : AppCompatActivity() {
val appleProvider = OAuthProvider.newBuilder("apple.com")

setAppleAuthStart {
val pending = firebaseAuth.pendingAuthResult
if (pending != null) {
pending.addOnSuccessListener { authResult ->
authResult.user.sendNewToken()
}.addOnFailureListener { e ->
Firebase.crashlytics.recordException(e)
}
} else {
firebaseAuth.startActivityForSignInWithProvider(this, appleProvider.build())
.addOnSuccessListener { authResult ->
authResult.user.sendNewToken()
}
.addOnFailureListener { e ->
Firebase.crashlytics.recordException(e)
}
}
signInApple(appleProvider)
}

setLogout {
Expand All @@ -102,6 +92,26 @@ class MainActivity : AppCompatActivity() {
}
}

private fun signInApple(appleProvider: OAuthProvider.Builder) {
authStart(AuthProvider.APPLE)
val pending = firebaseAuth.pendingAuthResult
if (pending != null) {
pending.addOnSuccessListener { authResult ->
authResult.user.sendNewToken(AuthProvider.APPLE)
}.addOnFailureListener { e ->
authFail(AuthProvider.APPLE, e)
}
} else {
firebaseAuth.startActivityForSignInWithProvider(this, appleProvider.build())
.addOnSuccessListener { authResult ->
authResult.user.sendNewToken(AuthProvider.APPLE)
}
.addOnFailureListener { e ->
authFail(AuthProvider.APPLE, e)
}
}
}

private fun signInGoogle() {
val signInIntent: Intent = googleSignInClient.signInIntent
register.launch(signInIntent)
Expand All @@ -114,24 +124,25 @@ class MainActivity : AppCompatActivity() {
update(account)
}
} catch (e: ApiException) {
authFail(AuthProvider.GOOGLE, e)
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show()
}
}

private fun update(account: GoogleSignInAccount) {
val credential = GoogleAuthProvider.getCredential(account.idToken, null)
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(this) { task ->
FirebaseAuth.getInstance().currentUser?.sendNewToken()
.addOnCompleteListener(this) { _ ->
FirebaseAuth.getInstance().currentUser?.sendNewToken(AuthProvider.GOOGLE)
}
.addOnFailureListener { e ->
Firebase.crashlytics.recordException(e)
authFail(AuthProvider.GOOGLE, e)
}
}

private fun FirebaseUser?.sendNewToken() {
private fun FirebaseUser?.sendNewToken(provider: AuthProvider) {
if (this == null) {
Firebase.crashlytics.recordException(Exception("User is null"))
authFail(provider, Exception("User is null"))
return
}

Expand All @@ -142,7 +153,22 @@ class MainActivity : AppCompatActivity() {
}
}
.addOnFailureListener {
Firebase.crashlytics.recordException(it)
authFail(provider, it)
}
}

private fun authStart(provider: AuthProvider) {
Firebase.analytics.logEvent("auth_start", Bundle().apply {
putString("provider", provider.trackName)
})
}

private fun authFail(provider: AuthProvider, exception: Exception? = null) {
Firebase.analytics.logEvent("auth_fail", Bundle().apply {
putString("provider", provider.trackName)
})
if (exception != null) {
Firebase.crashlytics.recordException(exception)
}
}
}
2 changes: 1 addition & 1 deletion shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ kotlin {
podfile = project.file("../iosApp/Podfile")
framework {
baseName = "shared"
isStatic = true
isStatic = false
}
extraSpecAttributes["resources"] =
"['src/commonMain/resources/**', 'src/iosMain/resources/**']"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.mixdrinks.app.utils.undomain

import kotlin.concurrent.Volatile
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlin.jvm.Volatile

internal class LazySuspend<T>(
private val block: suspend () -> T,
Expand Down
11 changes: 5 additions & 6 deletions shared/src/commonMain/kotlin/org/mixdrinks/di/Graph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal class Graph {

val tokenStorage = TokenStorage(settings)

val httpClient = HttpClient {
private val httpClient = HttpClient {
install(Logging)
install(ContentNegotiation) {
json(json)
Expand All @@ -57,16 +57,15 @@ internal class Graph {
}
}

val visitedCocktailsService = Ktorfit.Builder()
private val ktorfit = Ktorfit.Builder()
.httpClient(httpClient)
.baseUrl(baseUrl)
.build()

val visitedCocktailsService = ktorfit
.create<UserVisitedCocktailsService>()

val deleteAccountService = Ktorfit.Builder()
.httpClient(httpClient)
.baseUrl(baseUrl)
.build()
val deleteAccountService = ktorfit
.create<DeleteAccountService>()

val snapshotRepository: SnapshotRepository = SnapshotRepository(snapshotService, settings, json)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import org.mixdrinks.data.SnapshotRepository
import org.mixdrinks.data.TagsRepository
import org.mixdrinks.domain.ImageUrlCreators
import org.mixdrinks.dto.CocktailId
import org.mixdrinks.ui.auth.AuthBus
import org.mixdrinks.ui.list.CocktailListMapper
import org.mixdrinks.ui.list.CocktailsListState
import org.mixdrinks.ui.widgets.undomain.UiState
Expand Down Expand Up @@ -64,7 +63,7 @@ internal class VisitedCocktailsComponent(
sealed class VisitedCocktailList {
data class Cocktails(val cocktails: List<CocktailsListState.Cocktails.Cocktail>) : VisitedCocktailList()

object Empty : VisitedCocktailList()
data object Empty : VisitedCocktailList()
}

}

0 comments on commit e7b5e1b

Please sign in to comment.