Skip to content

Commit

Permalink
Merge pull request #190 from LachlanMcKee/use-hilt-in-sample-app
Browse files Browse the repository at this point in the history
Use hilt in demo app
  • Loading branch information
LachlanMcKee authored Feb 12, 2023
2 parents 9e61216 + 658a8b4 commit 0c81cb4
Show file tree
Hide file tree
Showing 30 changed files with 168 additions and 382 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ buildscript {
plugins {
id("mvi-core-collect-sarif")
id("com.autonomousapps.dependency-analysis") version libs.versions.dependencyAnalysis.get()
id("com.google.dagger.hilt.android") version("2.45") apply false
}

dependencyAnalysis {
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ rxandroid = { module = "io.reactivex.rxjava2:rxandroid", version.ref = "rxAndroi
# DI
dagger-runtime = { module = "com.google.dagger:dagger", version.ref = "daggerVersion" }
dagger-compiler = { module = "com.google.dagger:dagger-compiler", version.ref = "daggerVersion" }
hilt-runtime = { module = "com.google.dagger:hilt-android", version.ref = "daggerVersion" }
hilt-compiler = { module = "com.google.dagger:hilt-compiler", version.ref = "daggerVersion" }

# DebugDrawer
debugdrawer-impl = { module = "com.github.lenguyenthanh.debugdrawer:debugdrawer", version.ref = "debugDrawerVersion" }
Expand Down
3 changes: 3 additions & 0 deletions mvicore-demo/mvicore-demo-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id("com.android.application")
id("kotlin-android")
id("kotlin-kapt")
id("dagger.hilt.android.plugin")
id("mvi-core-lint")
id("mvi-core-detekt")
}
Expand Down Expand Up @@ -58,7 +59,9 @@ dependencies {

// DI
implementation(libs.dagger.runtime)
implementation(libs.hilt.runtime)
kapt(libs.dagger.compiler)
kapt(libs.hilt.compiler)

// DebugDrawer
debugImplementation(libs.debugdrawer.impl)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.badoo.mvicoredemo

import android.annotation.SuppressLint
import android.app.Application
import com.badoo.binder.middleware.config.MiddlewareConfiguration
import com.badoo.binder.middleware.config.Middlewares
Expand All @@ -10,36 +9,24 @@ import com.badoo.mvicore.consumer.middleware.PlaybackMiddleware
import com.badoo.mvicore.consumer.middleware.PlaybackMiddleware.RecordStore
import com.badoo.mvicore.middleware.DefaultPluginStore
import com.badoo.mvicore.middleware.IdeaPluginMiddleware
import com.badoo.mvicoredemo.di.appscope.component.AppScopedComponent
import dagger.hilt.android.HiltAndroidApp
import io.palaima.debugdrawer.timber.data.LumberYard
import timber.log.Timber
import javax.inject.Inject


@HiltAndroidApp
class App : Application() {

@Inject lateinit var recordStore: RecordStore

private val defaultStore = DefaultPluginStore(BuildConfig.APPLICATION_ID)

companion object {
@SuppressLint("StaticFieldLeak")
lateinit var component: AppScopedComponent
private set
}

override fun onCreate() {
super.onCreate()
dagger()
middlewares()
timber()
}

private fun dagger() {
component = AppScopedComponent(this)
component.get().inject(this)
}

private fun middlewares() {
Middlewares.configurations.add(
MiddlewareConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.badoo.mvicoredemo.di.usersessionscope.component.UserSessionScopedComponent
import com.badoo.mvicoredemo.di.usersessionscope.UserManager
import com.badoo.mvicoredemo.ui.login.LoginActivity
import com.badoo.mvicoredemo.ui.main.MainActivity


fun AppCompatActivity.login() {
storeIsLoggedIn(true)
UserSessionScopedComponent.get()
UserManager.getUserManager(application).userLoggedIn()

ContextCompat.startActivity(
this,
Intent(this, MainActivity::class.java).apply {
Expand All @@ -24,7 +24,8 @@ fun AppCompatActivity.login() {

fun AppCompatActivity.logout() {
storeIsLoggedIn(false)
UserSessionScopedComponent.destroy()
UserManager.getUserManager(application).logout()

ContextCompat.startActivity(
this,
Intent(this, LoginActivity::class.java).apply {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ package com.badoo.mvicoredemo.di.appscope.module
import com.badoo.mvicore.consumer.middleware.PlaybackMiddleware.RecordStore
import com.badoo.mvicore.consumer.playback.MemoryRecordStore
import com.badoo.mvicore.debugdrawer.MviCoreControlsModule
import com.badoo.mvicoredemo.di.appscope.scope.AppScope
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import io.reactivex.android.schedulers.AndroidSchedulers
import timber.log.Timber
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
class MviCoreModule {

@Provides
@AppScope
@Singleton
fun recordStore(): RecordStore =
MemoryRecordStore(
playbackScheduler = AndroidSchedulers.mainThread(),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.badoo.mvicoredemo.di.usersessionscope;

import android.app.Application
import com.badoo.mvicoredemo.di.usersessionscope.component.UserComponentEntryPoint
import com.badoo.mvicoredemo.di.usersessionscope.component.UserSessionComponent
import dagger.hilt.EntryPoints
import io.reactivex.disposables.CompositeDisposable
import javax.inject.Inject
import javax.inject.Provider
import javax.inject.Singleton

@Singleton
class UserManager @Inject constructor(
private val userSessionComponentBuilderProvider: Provider<UserSessionComponent.Builder>
) {
var userComponent: UserSessionComponent? = null
private set

fun userLoggedIn() {
userComponent = userSessionComponentBuilderProvider.get().build()
}

fun logout() {
userComponent?.apply {
val userPartsEntryPoint = UserComponentEntryPoint.get(this)
CompositeDisposable()
.apply {
addAll(
userPartsEntryPoint.feature1(),
userPartsEntryPoint.feature2(),
)
}
.dispose()
}
userComponent = null
}

companion object {
fun getUserManager(application: Application): UserManager =
EntryPoints
.get(application, UserManagerEntryPoint::class.java)
.userManager()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.badoo.mvicoredemo.di.usersessionscope

import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent

@InstallIn(SingletonComponent::class)
@EntryPoint
interface UserManagerEntryPoint {
fun userManager(): UserManager
}
Loading

0 comments on commit 0c81cb4

Please sign in to comment.