This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@AndroidEntryPoint | |
class CounterFragment : Fragment() { | |
@Inject | |
internal lateinit var analyticsModelProvider: AnalyticsModelProvider | |
private var analyticsModel: CounterAnalyticsModel? = null | |
private val viewModel: CounterViewModel by viewModels() | |
override fun onCreate(savedInstanceState: Bundle?) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AnalyticsModelProvider(private val analyticsModelMap: Map<Class<out AnalyticsModel<*>>, Provider<AnalyticsModel<*>>>) { | |
@Suppress("UNCHECKED_CAST") | |
operator fun <T : AnalyticsModel<*>> get(modelClass: Class<T>): T? { | |
val analyticsModel = analyticsModelMap[modelClass]?.get() ?: return null | |
return analyticsModel as T | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module | |
@InstallIn(ApplicationComponent::class) | |
abstract class AnalyticsModule { | |
@Binds | |
@IntoMap | |
@AnalyticsModelKey(CounterAnalyticsModel::class) | |
internal abstract fun provideCounterScreenAnalyticsModel( | |
analyticsModel: CounterAnalyticsModel | |
): AnalyticsModel<*> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@MapKey | |
annotation class AnalyticsModelKey(val value: KClass<out AnalyticsModel<*>>) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CounterAnalyticsModel @Inject constructor( | |
private val analyticsTracker: AnalyticsTracker | |
) : AnalyticsModel<CounterViewModel>() { | |
override fun onScreenViewModelAttached() { | |
viewModel.viewModelScope.launch { | |
flowOf( | |
viewModel.decrementValue | |
.getExecutedEvents() | |
.onEach { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class AnalyticsModel<T : ViewModel> { | |
protected lateinit var viewModel: T | |
fun getScreenViewModel(): T = viewModel | |
fun setScreenViewModel(model: T) { | |
viewModel = model | |
onScreenViewModelAttached() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FunctionCommand( | |
private val function: () -> Unit | |
) : Command { | |
private val eventChannel = BroadcastChannel<Any>(Channel.BUFFERED) | |
override fun execute() { | |
function.invoke() | |
eventChannel.offer(Any()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Command { | |
fun execute() | |
fun getExecutedEvents(): Flow<Any> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
<data> | |
<import type="io.supercharge.hiltexample.io.supercharge.hiltexample.analytics.AnalyticsConstants.Event" /> | |
<variable | |
name="vm" | |
type="io.supercharge.hiltexample.main.model.CounterViewModel" /> | |
</data> | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@BindingAdapter(value = ["command", "analyticsEvent"], requireAll = false) | |
fun View.bindAnalyticsCommandToButton(command: Command?, event: AnalyticsConstants.Event?) { | |
setOnClickListener { | |
command?.execute() | |
event?.let { | |
(context.applicationContext as HiltApplication?)?.tracker?.logEvent(event) | |
} | |
} | |
} |
NewerOlder