EtmViewModel.kt 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.annotation.SuppressLint
  24. import android.content.Context
  25. import android.content.SharedPreferences
  26. import android.content.res.Resources
  27. import androidx.lifecycle.LiveData
  28. import androidx.lifecycle.MutableLiveData
  29. import androidx.lifecycle.ViewModel
  30. import com.nextcloud.client.account.User
  31. import com.nextcloud.client.account.UserAccountManager
  32. import com.nextcloud.client.etm.pages.EtmAccountsFragment
  33. import com.nextcloud.client.etm.pages.EtmBackgroundJobsFragment
  34. import com.nextcloud.client.etm.pages.EtmDownloaderFragment
  35. import com.nextcloud.client.etm.pages.EtmMigrations
  36. import com.nextcloud.client.etm.pages.EtmPreferencesFragment
  37. import com.nextcloud.client.files.downloader.DownloaderConnection
  38. import com.nextcloud.client.jobs.BackgroundJobManager
  39. import com.nextcloud.client.jobs.JobInfo
  40. import com.nextcloud.client.migrations.MigrationInfo
  41. import com.nextcloud.client.migrations.MigrationsDb
  42. import com.nextcloud.client.migrations.MigrationsManager
  43. import com.owncloud.android.R
  44. import com.owncloud.android.lib.common.accounts.AccountUtils
  45. import javax.inject.Inject
  46. @Suppress("LongParameterList") // Dependencies Injection
  47. @SuppressLint("StaticFieldLeak")
  48. class EtmViewModel @Inject constructor(
  49. private val context: Context,
  50. private val defaultPreferences: SharedPreferences,
  51. private val platformAccountManager: AccountManager,
  52. private val accountManager: UserAccountManager,
  53. private val resources: Resources,
  54. private val backgroundJobManager: BackgroundJobManager,
  55. private val migrationsManager: MigrationsManager,
  56. private val migrationsDb: MigrationsDb
  57. ) : ViewModel() {
  58. companion object {
  59. val ACCOUNT_USER_DATA_KEYS = listOf(
  60. // AccountUtils.Constants.KEY_COOKIES, is disabled
  61. AccountUtils.Constants.KEY_DISPLAY_NAME,
  62. AccountUtils.Constants.KEY_OC_ACCOUNT_VERSION,
  63. AccountUtils.Constants.KEY_OC_BASE_URL,
  64. AccountUtils.Constants.KEY_OC_VERSION,
  65. AccountUtils.Constants.KEY_USER_ID
  66. )
  67. const val PAGE_SETTINGS = 0
  68. const val PAGE_ACCOUNTS = 1
  69. const val PAGE_JOBS = 2
  70. const val PAGE_MIGRATIONS = 3
  71. }
  72. /**
  73. * This data class holds all relevant account information that is
  74. * otherwise kept in separate collections.
  75. */
  76. data class AccountData(val account: Account, val userData: Map<String, String?>)
  77. val currentUser: User get() = accountManager.user
  78. val currentPage: LiveData<EtmMenuEntry?> = MutableLiveData()
  79. val pages: List<EtmMenuEntry> = listOf(
  80. EtmMenuEntry(
  81. iconRes = R.drawable.ic_settings,
  82. titleRes = R.string.etm_preferences,
  83. pageClass = EtmPreferencesFragment::class
  84. ),
  85. EtmMenuEntry(
  86. iconRes = R.drawable.ic_user,
  87. titleRes = R.string.etm_accounts,
  88. pageClass = EtmAccountsFragment::class
  89. ),
  90. EtmMenuEntry(
  91. iconRes = R.drawable.ic_clock,
  92. titleRes = R.string.etm_background_jobs,
  93. pageClass = EtmBackgroundJobsFragment::class
  94. ),
  95. EtmMenuEntry(
  96. iconRes = R.drawable.ic_arrow_up,
  97. titleRes = R.string.etm_migrations,
  98. pageClass = EtmMigrations::class
  99. ),
  100. EtmMenuEntry(
  101. iconRes = R.drawable.ic_download_grey600,
  102. titleRes = R.string.etm_downloader,
  103. pageClass = EtmDownloaderFragment::class
  104. )
  105. )
  106. val downloaderConnection = DownloaderConnection(context, accountManager.user)
  107. val preferences: Map<String, String> get() {
  108. return defaultPreferences.all
  109. .map { it.key to "${it.value}" }
  110. .sortedBy { it.first }
  111. .toMap()
  112. }
  113. val accounts: List<AccountData> get() {
  114. val accountType = resources.getString(R.string.account_type)
  115. return platformAccountManager.getAccountsByType(accountType).map { account ->
  116. val userData: Map<String, String?> = ACCOUNT_USER_DATA_KEYS.map { key ->
  117. key to platformAccountManager.getUserData(account, key)
  118. }.toMap()
  119. AccountData(account, userData)
  120. }
  121. }
  122. val backgroundJobs: LiveData<List<JobInfo>> get() {
  123. return backgroundJobManager.jobs
  124. }
  125. val migrationsInfo: List<MigrationInfo> get() {
  126. return migrationsManager.info
  127. }
  128. val migrationsStatus: MigrationsManager.Status get() {
  129. return migrationsManager.status.value ?: MigrationsManager.Status.UNKNOWN
  130. }
  131. val lastMigratedVersion: Int get() {
  132. return migrationsDb.lastMigratedVersion
  133. }
  134. init {
  135. (currentPage as MutableLiveData).apply {
  136. value = null
  137. }
  138. }
  139. fun onPageSelected(index: Int) {
  140. if (index < pages.size) {
  141. currentPage as MutableLiveData
  142. currentPage.value = pages[index]
  143. }
  144. }
  145. fun onBackPressed(): Boolean {
  146. (currentPage as MutableLiveData)
  147. return if (currentPage.value != null) {
  148. currentPage.value = null
  149. true
  150. } else {
  151. false
  152. }
  153. }
  154. fun pruneJobs() {
  155. backgroundJobManager.pruneJobs()
  156. }
  157. fun cancelAllJobs() {
  158. backgroundJobManager.cancelAllJobs()
  159. }
  160. fun startTestJob(periodic: Boolean) {
  161. if (periodic) {
  162. backgroundJobManager.scheduleTestJob()
  163. } else {
  164. backgroundJobManager.startImmediateTestJob()
  165. }
  166. }
  167. fun cancelTestJob() {
  168. backgroundJobManager.cancelTestJob()
  169. }
  170. fun clearMigrations() {
  171. migrationsDb.clearMigrations()
  172. }
  173. }