EtmViewModel.kt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Chris Narkiewicz
  5. * Copyright (C) 2020 Chris Narkiewicz <hello@ezaquarii.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.nextcloud.client.etm
  21. import android.accounts.Account
  22. import android.accounts.AccountManager
  23. import android.content.SharedPreferences
  24. import android.content.res.Resources
  25. import androidx.lifecycle.LiveData
  26. import androidx.lifecycle.MutableLiveData
  27. import androidx.lifecycle.ViewModel
  28. import com.nextcloud.client.etm.pages.EtmAccountsFragment
  29. import com.nextcloud.client.etm.pages.EtmBackgroundJobsFragment
  30. import com.nextcloud.client.etm.pages.EtmMigrations
  31. import com.nextcloud.client.etm.pages.EtmPreferencesFragment
  32. import com.nextcloud.client.jobs.BackgroundJobManager
  33. import com.nextcloud.client.jobs.JobInfo
  34. import com.nextcloud.client.migrations.MigrationInfo
  35. import com.nextcloud.client.migrations.MigrationsDb
  36. import com.nextcloud.client.migrations.MigrationsManager
  37. import com.owncloud.android.R
  38. import com.owncloud.android.lib.common.accounts.AccountUtils
  39. import javax.inject.Inject
  40. @Suppress("LongParameterList") // Dependencies Injection
  41. class EtmViewModel @Inject constructor(
  42. private val defaultPreferences: SharedPreferences,
  43. private val platformAccountManager: AccountManager,
  44. private val resources: Resources,
  45. private val backgroundJobManager: BackgroundJobManager,
  46. private val migrationsManager: MigrationsManager,
  47. private val migrationsDb: MigrationsDb
  48. ) : ViewModel() {
  49. companion object {
  50. val ACCOUNT_USER_DATA_KEYS = listOf(
  51. // AccountUtils.Constants.KEY_COOKIES, is disabled
  52. AccountUtils.Constants.KEY_DISPLAY_NAME,
  53. AccountUtils.Constants.KEY_OC_ACCOUNT_VERSION,
  54. AccountUtils.Constants.KEY_OC_BASE_URL,
  55. AccountUtils.Constants.KEY_OC_VERSION,
  56. AccountUtils.Constants.KEY_USER_ID
  57. )
  58. const val PAGE_SETTINGS = 0
  59. const val PAGE_ACCOUNTS = 1
  60. const val PAGE_JOBS = 2
  61. const val PAGE_MIGRATIONS = 3
  62. }
  63. /**
  64. * This data class holds all relevant account information that is
  65. * otherwise kept in separate collections.
  66. */
  67. data class AccountData(val account: Account, val userData: Map<String, String?>)
  68. val currentPage: LiveData<EtmMenuEntry?> = MutableLiveData()
  69. val pages: List<EtmMenuEntry> = listOf(
  70. EtmMenuEntry(
  71. iconRes = R.drawable.ic_settings,
  72. titleRes = R.string.etm_preferences,
  73. pageClass = EtmPreferencesFragment::class
  74. ),
  75. EtmMenuEntry(
  76. iconRes = R.drawable.ic_user,
  77. titleRes = R.string.etm_accounts,
  78. pageClass = EtmAccountsFragment::class
  79. ),
  80. EtmMenuEntry(
  81. iconRes = R.drawable.ic_clock,
  82. titleRes = R.string.etm_background_jobs,
  83. pageClass = EtmBackgroundJobsFragment::class
  84. ),
  85. EtmMenuEntry(
  86. iconRes = R.drawable.ic_arrow_up,
  87. titleRes = R.string.etm_migrations,
  88. pageClass = EtmMigrations::class
  89. )
  90. )
  91. val preferences: Map<String, String> get() {
  92. return defaultPreferences.all
  93. .map { it.key to "${it.value}" }
  94. .sortedBy { it.first }
  95. .toMap()
  96. }
  97. val accounts: List<AccountData> get() {
  98. val accountType = resources.getString(R.string.account_type)
  99. return platformAccountManager.getAccountsByType(accountType).map { account ->
  100. val userData: Map<String, String?> = ACCOUNT_USER_DATA_KEYS.map { key ->
  101. key to platformAccountManager.getUserData(account, key)
  102. }.toMap()
  103. AccountData(account, userData)
  104. }
  105. }
  106. val backgroundJobs: LiveData<List<JobInfo>> get() {
  107. return backgroundJobManager.jobs
  108. }
  109. val migrationsInfo: List<MigrationInfo> get() {
  110. return migrationsManager.info
  111. }
  112. val migrationsStatus: MigrationsManager.Status get() {
  113. return migrationsManager.status.value ?: MigrationsManager.Status.UNKNOWN
  114. }
  115. val lastMigratedVersion: Int get() {
  116. return migrationsDb.lastMigratedVersion
  117. }
  118. init {
  119. (currentPage as MutableLiveData).apply {
  120. value = null
  121. }
  122. }
  123. fun onPageSelected(index: Int) {
  124. if (index < pages.size) {
  125. currentPage as MutableLiveData
  126. currentPage.value = pages[index]
  127. }
  128. }
  129. fun onBackPressed(): Boolean {
  130. (currentPage as MutableLiveData)
  131. return if (currentPage.value != null) {
  132. currentPage.value = null
  133. true
  134. } else {
  135. false
  136. }
  137. }
  138. fun pruneJobs() {
  139. backgroundJobManager.pruneJobs()
  140. }
  141. fun cancelAllJobs() {
  142. backgroundJobManager.cancelAllJobs()
  143. }
  144. fun startTestJob(periodic: Boolean) {
  145. if (periodic) {
  146. backgroundJobManager.scheduleTestJob()
  147. } else {
  148. backgroundJobManager.startImmediateTestJob()
  149. }
  150. }
  151. fun cancelTestJob() {
  152. backgroundJobManager.cancelTestJob()
  153. }
  154. fun clearMigrations() {
  155. migrationsDb.clearMigrations()
  156. }
  157. }