create event tracker class to inject into view models

This commit is contained in:
Satindar Dhillon
2022-12-13 17:13:18 -08:00
parent 80e039dca4
commit df7242e5dd
4 changed files with 14 additions and 16 deletions

View File

@ -8,8 +8,6 @@ import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
import com.segment.analytics.kotlin.android.Analytics
import com.segment.analytics.kotlin.core.*
@Module
@InstallIn(SingletonComponent::class)
@ -27,16 +25,5 @@ object AppModule {
@Singleton
@Provides
fun provideAnalytics(
@ApplicationContext app: Context
): Analytics {
val writeKey = app.getString(R.string.segment_write_key)
// TODO: abstract analytics to custom class
return Analytics(writeKey, app.applicationContext) {
trackApplicationLifecycleEvents = true
application = app.applicationContext
useLifecycleObserver = true
}
}
fun provideAnalytics(@ApplicationContext app: Context) = EventTracker(app)
}

View File

@ -36,7 +36,8 @@ data class PendingEmailUserCreds(
@HiltViewModel
class LoginViewModel @Inject constructor(
private val datastoreRepo: DatastoreRepository
private val datastoreRepo: DatastoreRepository,
private val eventTracker: EventTracker
): ViewModel() {
private var validateUsernameJob: Job? = null
@ -90,6 +91,10 @@ class LoginViewModel @Inject constructor(
showSocialLogin()
}
fun registerUser() {
// TODO: eventTracker.registerUser()
}
private fun resetState() {
validateUsernameJob = null
isLoading = false

View File

@ -19,7 +19,6 @@ import javax.inject.Inject
@HiltViewModel
class HomeViewModel @Inject constructor(
private val networker: Networker,
private val analytics: com.segment.analytics.kotlin.core.Analytics
): ViewModel() {
private var cursor: String? = null
private var items: List<LinkedItem> = listOf()

View File

@ -53,6 +53,13 @@ fun RootView(
} else {
WelcomeScreen(viewModel = loginViewModel)
}
DisposableEffect(hasAuthToken) {
if (hasAuthToken) {
loginViewModel.registerUser()
}
onDispose {}
}
}
}