Skip to content

Commit

Permalink
feature: add hilt to project (#316)
Browse files Browse the repository at this point in the history
* feat: add hilt dependency and setup

Add Hilt to the project, and set it up on the application object.

* feat: switch project to use Hilt

Construct the ViewModel and its dependencies using Hilt.

* refactor: remove unused extension file

ActivityExtensions is no longer used, so can safely be removed.

* refactor: remove unused consumer-rules files

* refactor: convert hilt file into an object

Convert into an object rather than a class to be more efficient.

* style: switch to use expression body syntax
  • Loading branch information
Chesire committed Jun 6, 2022
1 parent 6dd5c97 commit 971705c
Show file tree
Hide file tree
Showing 30 changed files with 93 additions and 73 deletions.
9 changes: 9 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
plugins {
id("com.android.application")
id("dagger.hilt.android.plugin")
kotlin("android")
kotlin("kapt")
}

android {
Expand Down Expand Up @@ -43,4 +45,11 @@ dependencies {
implementation(libs.androidx.compose.runtime)
implementation(libs.androidx.preference)
implementation(libs.google.material)
implementation(libs.hilt.android)
implementation(libs.okhttp)
kapt(libs.hilt.android.compiler)
}

kapt {
correctErrorTypes = true
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".PushieApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/chesire/pushie/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import androidx.fragment.app.FragmentManager
import com.chesire.pushie.pusher.PusherFragment
import com.chesire.pushie.settings.SettingsFragment
import com.google.android.material.appbar.MaterialToolbar
import dagger.hilt.android.AndroidEntryPoint

/**
* The main activity for the application.
*/
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/com/chesire/pushie/PushieApplication.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.chesire.pushie

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class PushieApplication : Application()
17 changes: 17 additions & 0 deletions app/src/main/java/com/chesire/pushie/di/NetworkModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.chesire.pushie.di

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
import okhttp3.OkHttpClient

@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {

@Provides
@Singleton
fun providesOkHttpClient(): OkHttpClient = OkHttpClient()
}
19 changes: 19 additions & 0 deletions app/src/main/java/com/chesire/pushie/di/SystemModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.chesire.pushie.di

import android.content.ClipboardManager
import android.content.Context
import androidx.core.content.getSystemService
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent

@Module
@InstallIn(SingletonComponent::class)
object SystemModule {

@Provides
fun providesClipboardManager(@ApplicationContext context: Context): ClipboardManager =
context.getSystemService<ClipboardManager>() as ClipboardManager
}
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ buildscript {
}
dependencies {
classpath("com.android.tools.build:gradle:7.2.1")
classpath("com.google.dagger:hilt-android-gradle-plugin:${libs.versions.hilt.get()}")
classpath(kotlin("gradle-plugin", libs.versions.kotlin.get()))
}
}
Expand Down
6 changes: 4 additions & 2 deletions feature/pusher/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
plugins {
id("com.android.library")
id("dagger.hilt.android.plugin")
id("kotlin-parcelize")
kotlin("android")
kotlin("kapt")
}

android {
compileSdk = 31
defaultConfig {
minSdk = 21
targetSdk = 31

consumerProguardFiles("consumer-rules.pro")
}
buildFeatures {
compose = true
Expand Down Expand Up @@ -41,8 +41,10 @@ dependencies {
implementation(libs.androidx.lifecycle.viewmodel)
implementation(libs.liveevent)
implementation(libs.google.material)
implementation(libs.hilt.android)
implementation(libs.kotlin.result)
implementation(libs.okhttp)
kapt(libs.hilt.android.compiler)

testImplementation(libs.junit)
}
Empty file removed feature/pusher/consumer-rules.pro
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package com.chesire.pushie.pusher

import android.content.ClipData
import android.content.ClipboardManager
import javax.inject.Inject

/**
* Interacts with the Clipboard to push new values into it.
*/
class ClipboardInteractor(private val clipboard: ClipboardManager) {
class ClipboardInteractor @Inject constructor(private val clipboard: ClipboardManager) {

/**
* Copies [value] directly into the Android clipboard.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,59 +1,25 @@
package com.chesire.pushie.pusher

import android.content.ClipboardManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.platform.ComposeView
import androidx.core.content.ContextCompat.getSystemService
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.AbstractSavedStateViewModelFactory
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.savedstate.SavedStateRegistryOwner
import com.chesire.pushie.compose.PushieTheme
import com.chesire.pushie.datasource.pwpush.PWPushRepository
import com.chesire.pushie.datasource.pwpush.remote.PusherApi
import com.chesire.pushie.datastore.PreferenceStore
import com.google.android.material.snackbar.Snackbar
import okhttp3.OkHttpClient
import dagger.hilt.android.AndroidEntryPoint

/**
* Fragment for the main screen of the application, that allows users to use the api service.
*/
@AndroidEntryPoint
class PusherFragment : Fragment() {

inner class PusherViewModelFactory(
owner: SavedStateRegistryOwner,
defaultArgs: Bundle? = null
) : AbstractSavedStateViewModelFactory(owner, defaultArgs) {
// TODO: Use Hilt instead here
private val okHttpClient = OkHttpClient()
private val preferenceStore = PreferenceStore(requireContext().applicationContext)
private val pusherApi = PusherApi(okHttpClient, preferenceStore)
private val pusherRepository = PWPushRepository(pusherApi)
private val pusherInteractor = PusherInteractor(pusherRepository)
private val clipboard = getSystemService(
requireContext(),
ClipboardManager::class.java
) as ClipboardManager
private val clipboardInteractor = ClipboardInteractor(clipboard)

override fun <T : ViewModel> create(
key: String,
modelClass: Class<T>,
handle: SavedStateHandle
): T {
@Suppress("UNCHECKED_CAST")
return PusherViewModel(handle, pusherInteractor, clipboardInteractor) as T
}
}

private val viewModel: PusherViewModel by viewModels { PusherViewModelFactory(this) }
private val viewModel by viewModels<PusherViewModel>()

override fun onCreateView(
inflater: LayoutInflater,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package com.chesire.pushie.pusher
import com.chesire.pushie.datasource.pwpush.PWPushRepository
import com.github.michaelbull.result.Result
import com.github.michaelbull.result.mapEither
import javax.inject.Inject

/**
* Interacts with the [PWPushRepository] to send up passwords and generate urls.
*/
class PusherInteractor(private val repository: PWPushRepository) {
class PusherInteractor @Inject constructor(private val repository: PWPushRepository) {

/**
* Sends a new password to the API, and returns a [SendPasswordResult].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import androidx.lifecycle.viewModelScope
import com.github.michaelbull.result.onFailure
import com.github.michaelbull.result.onSuccess
import com.hadilq.liveevent.LiveEvent
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.coroutines.launch
import kotlinx.parcelize.Parcelize

Expand All @@ -17,7 +19,8 @@ private const val VIEW_KEY = "PusherViewKey"
/**
* ViewModel to use with the [PusherFragment].
*/
class PusherViewModel(
@HiltViewModel
class PusherViewModel @Inject constructor(
state: SavedStateHandle,
private val pushInteractor: PusherInteractor,
private val clipboardInteractor: ClipboardInteractor
Expand Down
2 changes: 0 additions & 2 deletions feature/settings/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ android {
defaultConfig {
minSdk = 21
targetSdk = 31

consumerProguardFiles("consumer-rules.pro")
}

buildFeatures {
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[versions]
compose = "1.1.1"
coroutines = "1.6.2"
hilt = "2.38.1"
kotlin = "1.6.10"
lifecycle = "2.4.1"

Expand All @@ -17,6 +18,8 @@ androidx-lifecycle-livedata = { module = "androidx.lifecycle:lifecycle-livedata-
androidx-lifecycle-viewmodel = { module = "androidx.lifecycle:lifecycle-viewmodel-ktx", version.ref = "lifecycle" }
androidx-preference = { module = "androidx.preference:preference-ktx", version = "1.2.0" }
google-material = { module = "com.google.android.material:material", version = "1.6.1" }
hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hilt"}
hilt-android-compiler = { module = "com.google.dagger:hilt-android-compiler", version.ref = "hilt"}
junit = { module = "junit:junit", version = "4.13.2" }
kotlin-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines" }
kotlin-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }
Expand Down
2 changes: 0 additions & 2 deletions library/common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@ android {
defaultConfig {
minSdk = 21
targetSdk = 31

consumerProguardFiles("consumer-rules.pro")
}
}
Empty file removed library/common/consumer-rules.pro
Empty file.

This file was deleted.

2 changes: 0 additions & 2 deletions library/compose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ android {
defaultConfig {
minSdk = 21
targetSdk = 31

consumerProguardFiles("consumer-rules.pro")
}
buildFeatures {
compose = true
Expand Down
Empty file removed library/compose/consumer-rules.pro
Empty file.
6 changes: 4 additions & 2 deletions library/datasource/pwpush/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
plugins {
id("com.android.library")
id("dagger.hilt.android.plugin")
kotlin("android")
kotlin("kapt")
}

android {
compileSdk = 31
defaultConfig {
minSdk = 21
targetSdk = 31

consumerProguardFiles("consumer-rules.pro")
}
}

Expand All @@ -18,10 +18,12 @@ dependencies {
implementation(project(":library:datastore"))
implementation(libs.androidx.appcompat)
implementation(libs.androidx.core)
implementation(libs.hilt.android)
implementation(libs.kotlin.coroutines.android)
implementation(libs.kotlin.coroutines.core)
implementation(libs.kotlin.result)
implementation(libs.okhttp)
kapt(libs.hilt.android.compiler)

testImplementation(libs.junit)
}
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import com.chesire.pushie.datasource.pwpush.remote.PushedModel
import com.chesire.pushie.datasource.pwpush.remote.PusherApi
import com.github.michaelbull.result.Result
import com.github.michaelbull.result.onSuccess
import javax.inject.Inject

/**
* Repository to interact with the [PusherApi] remote data source, and to interact with any local
* data sources that store urls.
*/
class PWPushRepository(private val passwordAPI: PusherApi) {
class PWPushRepository @Inject constructor(private val pusherApi: PusherApi) {

/**
* Sends the [password] up to the API.
Expand All @@ -20,7 +21,7 @@ class PWPushRepository(private val passwordAPI: PusherApi) {
expiryDays: Int,
expiryViews: Int
): Result<PushedModel, ApiError> {
return passwordAPI
return pusherApi
.sendPassword(password, expiryDays, expiryViews)
.onSuccess {
// TODO: store in local db. Use a flow from there to return these urls
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.github.michaelbull.result.Err
import com.github.michaelbull.result.Ok
import com.github.michaelbull.result.Result
import java.io.IOException
import javax.inject.Inject
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
Expand All @@ -18,7 +19,7 @@ import org.json.JSONObject
/**
* Interacts with the `pwpush.com` API.
*/
class PusherApi(
class PusherApi @Inject constructor(
private val client: OkHttpClient,
private val preferenceStore: PreferenceStore
) {
Expand Down
9 changes: 7 additions & 2 deletions library/datastore/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
plugins {
id("com.android.library")
id("dagger.hilt.android.plugin")
kotlin("android")
kotlin("kapt")
}

android {
compileSdk = 31
defaultConfig {
minSdk = 21
targetSdk = 31

consumerProguardFiles("consumer-rules.pro")
}
}

dependencies {
implementation(libs.hilt.android)
kapt(libs.hilt.android.compiler)
}
Empty file.
Loading

0 comments on commit 971705c

Please sign in to comment.