Skip to content

Commit

Permalink
feat: Use an object instead of injection to use extension
Browse files Browse the repository at this point in the history
  • Loading branch information
tevincent committed Nov 7, 2024
1 parent eef205a commit b3631ef
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 24 deletions.
2 changes: 0 additions & 2 deletions Core2/Matomo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,4 @@ android {

dependencies {
api(core2.matomo)
api(core2.androidx.core)
api(core2.appcompat)
}
7 changes: 2 additions & 5 deletions Core2/Matomo/src/main/java/com/infomaniak/matomo/Matomo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import org.matomo.sdk.extra.TrackHelper

interface Matomo {

val tracker: Tracker
val Context.tracker: Tracker
val siteId: Int

fun Context.buildTracker(shouldOptOut: Boolean = false): Tracker {
Expand Down Expand Up @@ -62,19 +62,16 @@ interface Matomo {
}

//region Track screens

fun Activity.trackScreen() {
TrackHelper.track().screen(this).title(this::class.java.simpleName).with(tracker)
}

fun Context.trackScreen(path: String, title: String) {
TrackHelper.track().screen(path).title(title).with(tracker)
}

//endregion

//region Track events

fun Context.trackEvent(category: String, name: String, action: TrackerAction = TrackerAction.CLICK, value: Float? = null) {
TrackHelper.track()
.event(category, action.toString())
Expand All @@ -91,7 +88,7 @@ interface Matomo {
fun Boolean.toFloat() = if (this) 1.0f else 0.0f

fun shouldOptOut(context: Context, shouldOptOut: Boolean) {
tracker.isOptOut = shouldOptOut
context.tracker.isOptOut = shouldOptOut
}

enum class TrackerAction {
Expand Down
4 changes: 0 additions & 4 deletions Core2/gradle/core2.versions.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
[versions]
sentry-android = "7.15.0"
matomo = "4.1.4"
androidx-core = "1.13.1"
appcompatVersion = "1.7.0"

[libraries]
sentry-android = { module = "io.sentry:sentry-android", version.ref = "sentry-android" }
matomo = { module = "com.github.matomo-org:matomo-sdk-android", version.ref = "matomo" }
androidx-core = { module = "androidx.core:core-ktx", version.ref = "androidx-core" }
appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompatVersion" }
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
package com.infomaniak.swisstransfer.di

import android.app.Application
import com.infomaniak.multiplatform_swisstransfer.SwissTransferInjection
import com.infomaniak.swisstransfer.ui.MainActivity
import android.content.Context
import androidx.work.WorkManager
import com.infomaniak.swisstransfer.ui.MainApplication
import com.infomaniak.swisstransfer.ui.MatomoSwissTransfer
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
Expand All @@ -44,14 +43,6 @@ object ApplicationModule {
return CoroutineScope(defaultDispatcher)
}

@Provides
@Singleton
fun provideMatomoSwissTransfer(application: MainApplication) = MatomoSwissTransfer(application)

@Provides
@Singleton
fun provideMatomoTracker(matomo: MatomoSwissTransfer) = matomo.tracker

@Provides
@Singleton
fun providesWorkManager(@ApplicationContext context: Context) = WorkManager.getInstance(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import androidx.compose.runtime.getValue
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.infomaniak.multiplatform_swisstransfer.common.models.Theme
import com.infomaniak.swisstransfer.ui.MatomoSwissTransfer.trackScreen
import com.infomaniak.swisstransfer.ui.screen.main.MainScreen
import com.infomaniak.swisstransfer.ui.screen.main.settings.SettingsViewModel
import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme
Expand All @@ -48,6 +49,7 @@ class MainActivity : ComponentActivity() {
MainScreen()
}
}
trackScreen()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import android.content.Context
import com.infomaniak.matomo.Matomo
import org.matomo.sdk.Tracker

class MatomoSwissTransfer(private val context: Context) : Matomo {
object MatomoSwissTransfer : Matomo {

override val tracker: Tracker by lazy { context.buildTracker() }
override val Context.tracker: Tracker get() = (this as MainActivity).buildTracker() //TODO fetch appSettings for opt out
override val siteId: Int = -1 //TODO replace with the right site ID when we have one
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import com.infomaniak.swisstransfer.ui.MatomoSwissTransfer.trackScreen
import com.infomaniak.swisstransfer.ui.screen.newtransfer.NewTransferScreen
import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -36,5 +37,6 @@ class NewTransferActivity : ComponentActivity() {
NewTransferScreen(closeActivity = { finish() })
}
}
trackScreen()
}
}

0 comments on commit b3631ef

Please sign in to comment.