Skip to content

Commit

Permalink
Fixed constructors and got rid of utils
Browse files Browse the repository at this point in the history
Signed-off-by: Arnau Mora <[email protected]>
  • Loading branch information
ArnyminerZ committed Oct 19, 2023
1 parent c897563 commit 5919925
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 45 deletions.
5 changes: 2 additions & 3 deletions app/src/main/kotlin/at/bitfire/davdroid/ui/TasksFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,16 @@ 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
import at.bitfire.davdroid.databinding.ActivityTasksBinding
import at.bitfire.davdroid.resource.TaskUtils
import at.bitfire.davdroid.settings.SettingsManager
import at.bitfire.davdroid.util.context
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 @@ -120,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 @@ -15,16 +15,15 @@ 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.context
import at.bitfire.davdroid.util.DavUtils
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
Expand Down Expand Up @@ -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 @@ -6,7 +6,6 @@ package at.bitfire.davdroid.ui.account

import android.accounts.Account
import android.app.Application
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand All @@ -15,18 +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 at.bitfire.davdroid.util.context
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 @@ -116,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 @@ -37,7 +37,6 @@ import at.bitfire.davdroid.syncadapter.Syncer
import at.bitfire.davdroid.ui.UiUtils
import at.bitfire.davdroid.ui.setup.GoogleLoginFragment
import at.bitfire.davdroid.util.PermissionUtils
import at.bitfire.davdroid.util.context
import at.bitfire.ical4android.TaskProvider
import at.bitfire.vcard4android.GroupMethod
import com.google.android.material.snackbar.Snackbar
Expand Down Expand Up @@ -489,7 +488,9 @@ class SettingsActivity: AppCompatActivity() {
fun reload() {
val accountSettings = accountSettings ?: return

syncIntervalContacts.postValue(accountSettings.getSyncInterval(context.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 @@ -572,7 +573,10 @@ class SettingsActivity: AppCompatActivity() {
accountSettings?.setGroupMethod(groupMethod)
reload()

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

/**
Expand All @@ -599,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(context, account, authority, resync)
SyncWorker.enqueue(getApplication(), account, authority, resync)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ import at.bitfire.davdroid.db.AppDatabase
import at.bitfire.davdroid.db.Collection
import at.bitfire.davdroid.log.Logger
import at.bitfire.davdroid.util.PermissionUtils
import at.bitfire.davdroid.util.context
import com.google.android.material.snackbar.Snackbar
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 @@ -192,6 +190,8 @@ class WebcalFragment: CollectionsFragment() {
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 @@ -30,7 +30,6 @@ import at.bitfire.davdroid.settings.SettingsManager
import at.bitfire.davdroid.ui.UiUtils
import at.bitfire.davdroid.ui.intro.BatteryOptimizationsFragment.Model.Companion.HINT_AUTOSTART_PERMISSION
import at.bitfire.davdroid.ui.intro.BatteryOptimizationsFragment.Model.Companion.HINT_BATTERY_OPTIMIZATIONS
import at.bitfire.davdroid.util.context
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ 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 @@ -42,12 +41,10 @@ import at.bitfire.davdroid.settings.Settings
import at.bitfire.davdroid.settings.SettingsManager
import at.bitfire.davdroid.syncadapter.AccountUtils
import at.bitfire.davdroid.ui.account.AccountActivity
import at.bitfire.davdroid.util.context
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 @@ -157,7 +154,8 @@ class AccountDetailsFragment : Fragment() {
}


class Model(
@HiltViewModel
class Model @Inject constructor(
application: Application,
val db: AppDatabase,
val settingsManager: SettingsManager
Expand All @@ -167,6 +165,8 @@ class AccountDetailsFragment : Fragment() {
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 @@ -14,10 +14,9 @@ import android.view.View
import androidx.activity.viewModels
import androidx.annotation.WorkerThread
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.AndroidViewModel
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 @@ -30,13 +29,10 @@ import at.bitfire.davdroid.db.WebDavMount
import at.bitfire.davdroid.log.Logger
import at.bitfire.davdroid.network.HttpClient
import at.bitfire.davdroid.ui.UiUtils
import at.bitfire.davdroid.util.context
import at.bitfire.davdroid.webdav.CredentialsStore
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 @@ -171,6 +167,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 @@ -31,7 +31,6 @@ import at.bitfire.davdroid.db.WebDavDocument
import at.bitfire.davdroid.db.WebDavMount
import at.bitfire.davdroid.log.Logger
import at.bitfire.davdroid.ui.UiUtils
import at.bitfire.davdroid.util.context
import at.bitfire.davdroid.webdav.CredentialsStore
import at.bitfire.davdroid.webdav.DavDocumentsProvider
import dagger.hilt.android.AndroidEntryPoint
Expand Down Expand Up @@ -201,6 +200,8 @@ class WebdavMountsActivity: AppCompatActivity() {
val db: AppDatabase
): AndroidViewModel(application) {

val context: Context get() = getApplication()

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

val mountInfos = object: MediatorLiveData<List<MountInfo>>() {
Expand Down
15 changes: 0 additions & 15 deletions app/src/main/kotlin/at/bitfire/davdroid/util/ViewModelUtil.kt

This file was deleted.

0 comments on commit 5919925

Please sign in to comment.