Skip to content

Commit

Permalink
Refactor HiltViewModels ApplicationContext (#446)
Browse files Browse the repository at this point in the history
* Simplified ViewModels

Signed-off-by: Arnau Mora <[email protected]>

* Fixed injection

Signed-off-by: Arnau Mora <[email protected]>

* Fixed settings injection

Signed-off-by: Arnau Mora <[email protected]>

* Added missing import

Signed-off-by: Arnau Mora <[email protected]>

* Fixed application

Signed-off-by: Arnau Mora <[email protected]>

* Fixed constructors and got rid of utils

Signed-off-by: Arnau Mora <[email protected]>

* Optimized imports

Signed-off-by: Arnau Mora <[email protected]>

* Added missing annotation

Signed-off-by: Arnau Mora <[email protected]>

---------

Signed-off-by: Arnau Mora <[email protected]>
  • Loading branch information
ArnyminerZ authored Oct 19, 2023
1 parent f8330e8 commit fe679da
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package at.bitfire.davdroid.ui.webdav

import android.security.NetworkSecurityPolicy
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.core.app.ApplicationProvider
import at.bitfire.davdroid.db.AppDatabase
import at.bitfire.davdroid.db.WebDavMount
import dagger.hilt.android.testing.HiltAndroidRule
Expand Down Expand Up @@ -34,7 +34,7 @@ class AddWebdavMountActivityTest {
fun setUp() {
hiltRule.inject()

model = spyk(AddWebdavMountActivity.Model(InstrumentationRegistry.getInstrumentation().targetContext, db))
model = spyk(AddWebdavMountActivity.Model(ApplicationProvider.getApplicationContext(), db))

Assume.assumeTrue(NetworkSecurityPolicy.getInstance().isCleartextTrafficPermitted)
}
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/kotlin/at/bitfire/davdroid/ui/TasksFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package at.bitfire.davdroid.ui

import android.app.Application
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
Expand All @@ -16,8 +17,8 @@ import androidx.annotation.AnyThread
import androidx.databinding.ObservableBoolean
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import at.bitfire.davdroid.BuildConfig
import at.bitfire.davdroid.PackageChangedReceiver
import at.bitfire.davdroid.R
Expand All @@ -28,7 +29,6 @@ import at.bitfire.ical4android.TaskProvider.ProviderName
import com.google.android.material.snackbar.Snackbar
import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject

@AndroidEntryPoint
Expand Down Expand Up @@ -102,9 +102,9 @@ class TasksFragment: Fragment() {

@HiltViewModel
class Model @Inject constructor(
@ApplicationContext val context: Context,
application: Application,
val settings: SettingsManager
) : ViewModel(), SettingsManager.OnChangeListener {
) : AndroidViewModel(application), SettingsManager.OnChangeListener {

companion object {

Expand All @@ -117,6 +117,8 @@ class TasksFragment: Fragment() {

}

val context: Context get() = getApplication()

val currentProvider = MutableLiveData<ProviderName>()
val openTasksInstalled = MutableLiveData<Boolean>()
val openTasksRequested = MutableLiveData<Boolean>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package at.bitfire.davdroid.ui.account

import android.accounts.Account
import android.content.Context
import android.app.Application
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand All @@ -15,20 +15,19 @@ import androidx.fragment.app.viewModels
import androidx.lifecycle.*
import at.bitfire.dav4jvm.DavResource
import at.bitfire.dav4jvm.XmlUtils
import at.bitfire.davdroid.util.DavUtils
import at.bitfire.davdroid.network.HttpClient
import at.bitfire.davdroid.R
import at.bitfire.davdroid.db.AppDatabase
import at.bitfire.davdroid.db.Collection
import at.bitfire.davdroid.log.Logger
import at.bitfire.davdroid.network.HttpClient
import at.bitfire.davdroid.servicedetection.RefreshCollectionsWorker
import at.bitfire.davdroid.settings.AccountSettings
import at.bitfire.davdroid.ui.ExceptionInfoFragment
import at.bitfire.davdroid.util.DavUtils
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -117,12 +116,12 @@ class CreateCollectionFragment: DialogFragment() {


class Model @AssistedInject constructor(
@ApplicationContext val context: Context,
application: Application,
val db: AppDatabase,
@Assisted val account: Account,
@Assisted val serviceType: String,
@Assisted val collection: Collection
): ViewModel() {
): AndroidViewModel(application) {

@AssistedFactory
interface Factory {
Expand All @@ -133,7 +132,7 @@ class CreateCollectionFragment: DialogFragment() {

fun createCollection(): LiveData<Exception> {
viewModelScope.launch(Dispatchers.IO + NonCancellable) {
HttpClient.Builder(context, AccountSettings(context, account))
HttpClient.Builder(getApplication(), AccountSettings(getApplication(), account))
.setForeground(true)
.build().use { httpClient ->
try {
Expand All @@ -148,7 +147,7 @@ class CreateCollectionFragment: DialogFragment() {
db.collectionDao().insert(collection)

// trigger service detection (because the collection may have other properties than the ones we have inserted)
RefreshCollectionsWorker.refreshCollections(context, service.id)
RefreshCollectionsWorker.refreshCollections(getApplication(), service.id)
}

// post success
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package at.bitfire.davdroid.ui.account

import android.accounts.Account
import android.content.Context
import android.app.Application
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand All @@ -14,17 +14,16 @@ import androidx.fragment.app.DialogFragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.*
import at.bitfire.dav4jvm.DavResource
import at.bitfire.davdroid.network.HttpClient
import at.bitfire.davdroid.databinding.DeleteCollectionBinding
import at.bitfire.davdroid.db.AppDatabase
import at.bitfire.davdroid.db.Collection
import at.bitfire.davdroid.network.HttpClient
import at.bitfire.davdroid.settings.AccountSettings
import at.bitfire.davdroid.ui.ExceptionInfoFragment
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -88,11 +87,11 @@ class DeleteCollectionFragment: DialogFragment() {


class Model @AssistedInject constructor(
@ApplicationContext val context: Context,
application: Application,
val db: AppDatabase,
@Assisted var account: Account,
@Assisted val collectionId: Long
): ViewModel() {
): AndroidViewModel(application) {

@AssistedFactory
interface Factory {
Expand All @@ -114,7 +113,7 @@ class DeleteCollectionFragment: DialogFragment() {
viewModelScope.launch(Dispatchers.IO + NonCancellable) {
val collectionInfo = collectionInfo ?: return@launch

HttpClient.Builder(context, AccountSettings(context, account))
HttpClient.Builder(getApplication(), AccountSettings(getApplication(), account))
.setForeground(true)
.build().use { httpClient ->
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.TaskStackBuilder
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
Expand Down Expand Up @@ -425,10 +426,10 @@ class SettingsActivity: AppCompatActivity() {


class Model @AssistedInject constructor(
val application: Application,
application: Application,
val settings: SettingsManager,
@Assisted val account: Account
): ViewModel(), SettingsManager.OnChangeListener {
): AndroidViewModel(application), SettingsManager.OnChangeListener {

@AssistedFactory
interface Factory {
Expand Down Expand Up @@ -487,7 +488,9 @@ class SettingsActivity: AppCompatActivity() {
fun reload() {
val accountSettings = accountSettings ?: return

syncIntervalContacts.postValue(accountSettings.getSyncInterval(application.getString(R.string.address_books_authority)))
syncIntervalContacts.postValue(
accountSettings.getSyncInterval(getApplication<Application>().getString(R.string.address_books_authority))
)
syncIntervalCalendars.postValue(accountSettings.getSyncInterval(CalendarContract.AUTHORITY))
syncIntervalTasks.postValue(tasksProvider?.let { accountSettings.getSyncInterval(it.authority) })

Expand Down Expand Up @@ -570,7 +573,10 @@ class SettingsActivity: AppCompatActivity() {
accountSettings?.setGroupMethod(groupMethod)
reload()

resync(application.getString(R.string.address_books_authority), fullResync = true)
resync(
authority = getApplication<Application>().getString(R.string.address_books_authority),
fullResync = true
)
}

/**
Expand All @@ -597,7 +603,7 @@ class SettingsActivity: AppCompatActivity() {
*/
private fun resync(authority: String, fullResync: Boolean) {
val resync = if (fullResync) SyncWorker.FULL_RESYNC else SyncWorker.RESYNC
SyncWorker.enqueue(application, account, authority, resync)
SyncWorker.enqueue(getApplication(), account, authority, resync)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package at.bitfire.davdroid.ui.account

import android.Manifest
import android.app.Application
import android.content.ContentProviderClient
import android.content.Context
import android.content.Intent
Expand Down Expand Up @@ -33,7 +34,6 @@ import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.HttpUrl
Expand Down Expand Up @@ -180,16 +180,18 @@ class WebcalFragment: CollectionsFragment() {


class WebcalModel @AssistedInject constructor(
@ApplicationContext context: Context,
application: Application,
val db: AppDatabase,
@Assisted val serviceId: Long
): ViewModel() {
): AndroidViewModel(application) {

@AssistedFactory
interface Factory {
fun create(serviceId: Long): WebcalModel
}

val context: Context get() = getApplication()

private val resolver = context.contentResolver

private var calendarPermission = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package at.bitfire.davdroid.ui.setup

import android.accounts.Account
import android.accounts.AccountManager
import android.app.Application
import android.content.ContentResolver
import android.content.Context
import android.content.Intent
Expand All @@ -19,10 +20,10 @@ import android.widget.ArrayAdapter
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import at.bitfire.davdroid.InvalidAccountException
import at.bitfire.davdroid.R
Expand All @@ -44,7 +45,6 @@ import at.bitfire.vcard4android.GroupMethod
import com.google.android.material.snackbar.Snackbar
import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -156,15 +156,17 @@ class AccountDetailsFragment : Fragment() {

@HiltViewModel
class Model @Inject constructor(
@ApplicationContext val context: Context,
application: Application,
val db: AppDatabase,
val settingsManager: SettingsManager
) : ViewModel() {
) : AndroidViewModel(application) {

val name = MutableLiveData<String>()
val nameError = MutableLiveData<String>()
val showApostropheWarning = MutableLiveData<Boolean>(false)

val context: Context get() = getApplication()

fun validateAccountName(s: Editable) {
showApostropheWarning.value = s.toString().contains('\'')
nameError.value = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package at.bitfire.davdroid.ui.webdav

import android.app.Application
import android.content.Context
import android.os.Bundle
import android.view.Menu
Expand All @@ -14,8 +15,8 @@ import androidx.activity.viewModels
import androidx.annotation.WorkerThread
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.MenuProvider
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.lifecycleScope
import at.bitfire.dav4jvm.DavResource
import at.bitfire.dav4jvm.UrlUtils
Expand All @@ -33,7 +34,6 @@ import at.bitfire.davdroid.webdav.DavDocumentsProvider
import com.google.android.material.snackbar.Snackbar
import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.HttpUrl
Expand Down Expand Up @@ -156,9 +156,9 @@ class AddWebdavMountActivity: AppCompatActivity() {

@HiltViewModel
class Model @Inject constructor(
@ApplicationContext val context: Context,
application: Application,
val db: AppDatabase
) : ViewModel() {
) : AndroidViewModel(application) {

val displayName = MutableLiveData<String>()
val displayNameError = MutableLiveData<String>()
Expand All @@ -169,6 +169,8 @@ class AddWebdavMountActivity: AppCompatActivity() {

val error = MutableLiveData<String>()

val context: Context get() = getApplication()


@WorkerThread
fun addMount(mount: WebDavMount, credentials: Credentials?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package at.bitfire.davdroid.ui.webdav

import android.app.AlertDialog
import android.app.Application
import android.content.Context
import android.content.Intent
import android.os.Bundle
Expand Down Expand Up @@ -195,9 +196,11 @@ class WebdavMountsActivity: AppCompatActivity() {

@HiltViewModel
class Model @Inject constructor(
@ApplicationContext val context: Context,
application: Application,
val db: AppDatabase
): ViewModel() {
): AndroidViewModel(application) {

val context: Context get() = getApplication()

val authority = context.getString(R.string.webdav_authority)

Expand Down

0 comments on commit fe679da

Please sign in to comment.