ComposeActivity.kt 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Alper Ozturk
  5. * Copyright (C) 2024 Alper Ozturk
  6. * Copyright (C) 2024 Nextcloud GmbH
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. package com.nextcloud.ui.composeActivity
  22. import android.content.Context
  23. import android.os.Bundle
  24. import android.view.MenuItem
  25. import androidx.compose.material3.ColorScheme
  26. import androidx.compose.material3.MaterialTheme
  27. import androidx.compose.runtime.Composable
  28. import androidx.compose.runtime.LaunchedEffect
  29. import androidx.compose.runtime.getValue
  30. import androidx.compose.runtime.mutableStateOf
  31. import androidx.compose.runtime.remember
  32. import androidx.compose.runtime.setValue
  33. import com.nextcloud.client.assistant.AssistantScreen
  34. import com.nextcloud.client.assistant.AssistantViewModel
  35. import com.nextcloud.common.NextcloudClient
  36. import com.nextcloud.common.User
  37. import com.nextcloud.utils.extensions.getSerializableArgument
  38. import com.nextcloud.utils.extensions.toColorScheme
  39. import com.owncloud.android.R
  40. import com.owncloud.android.databinding.ActivityComposeBinding
  41. import com.owncloud.android.lib.common.OwnCloudClientFactory
  42. import com.owncloud.android.lib.common.accounts.AccountUtils
  43. import com.owncloud.android.lib.common.utils.Log_OC
  44. import com.owncloud.android.ui.activity.DrawerActivity
  45. import kotlinx.coroutines.Dispatchers
  46. import kotlinx.coroutines.flow.MutableStateFlow
  47. import kotlinx.coroutines.withContext
  48. class ComposeActivity : DrawerActivity() {
  49. lateinit var binding: ActivityComposeBinding
  50. companion object {
  51. const val destinationKey = "destinationKey"
  52. const val titleKey = "titleKey"
  53. const val menuItemKey = "menuItemKey"
  54. lateinit var schemeFlow: MutableStateFlow<ColorScheme>
  55. }
  56. override fun onCreate(savedInstanceState: Bundle?) {
  57. super.onCreate(savedInstanceState)
  58. binding = ActivityComposeBinding.inflate(layoutInflater)
  59. setContentView(binding.root)
  60. val destination = intent.getSerializableArgument(destinationKey, ComposeDestination::class.java)
  61. val titleId = intent.getIntExtra(titleKey, R.string.empty)
  62. val menuItemId = intent.getIntExtra(menuItemKey, R.id.nav_assistant)
  63. setupToolbar()
  64. updateActionBarTitleAndHomeButtonByString(getString(titleId))
  65. setupDrawer(menuItemId)
  66. val colorScheme = viewThemeUtils.material.getScheme(this).toColorScheme()
  67. schemeFlow = MutableStateFlow(colorScheme)
  68. binding.composeView.setContent {
  69. MaterialTheme(
  70. colorScheme = colorScheme,
  71. content = {
  72. Content(destination, storageManager.user, this)
  73. }
  74. )
  75. }
  76. }
  77. override fun onResume() {
  78. super.onResume()
  79. setDrawerMenuItemChecked(R.id.nav_assistant)
  80. }
  81. override fun onOptionsItemSelected(item: MenuItem): Boolean {
  82. return when (item.itemId) {
  83. android.R.id.home -> {
  84. if (isDrawerOpen) closeDrawer() else openDrawer()
  85. true
  86. }
  87. else -> super.onOptionsItemSelected(item)
  88. }
  89. }
  90. @Composable
  91. private fun Content(destination: ComposeDestination?, user: User, context: Context) {
  92. var nextcloudClient by remember { mutableStateOf<NextcloudClient?>(null) }
  93. LaunchedEffect(Unit) {
  94. nextcloudClient = getNextcloudClient(user, context)
  95. }
  96. if (destination == ComposeDestination.AssistantScreen) {
  97. nextcloudClient?.let {
  98. AssistantScreen(
  99. viewModel = AssistantViewModel(
  100. client = it
  101. )
  102. )
  103. }
  104. }
  105. }
  106. private suspend fun getNextcloudClient(user: User, context: Context): NextcloudClient? {
  107. return withContext(Dispatchers.IO) {
  108. try {
  109. OwnCloudClientFactory.createNextcloudClient(user, context)
  110. } catch (e: AccountUtils.AccountNotFoundException) {
  111. Log_OC.e(this, "Error caught at init of AssistantRepository", e)
  112. null
  113. }
  114. }
  115. }
  116. }