Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start migration to Compose Navigation #1211

Open
wants to merge 8 commits into
base: main-ose
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ plugins {
alias(libs.plugins.compose.compiler)
alias(libs.plugins.hilt)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.serialization)
alias(libs.plugins.ksp)
}

Expand Down Expand Up @@ -163,6 +164,7 @@ dependencies {
implementation(platform(libs.compose.bom))
implementation(libs.compose.material3)
implementation(libs.compose.materialIconsExtended)
implementation(libs.compose.navigation)
implementation(libs.compose.runtime.livedata)
debugImplementation(libs.compose.ui.tooling)
implementation(libs.compose.ui.toolingPreview)
Expand All @@ -189,6 +191,7 @@ dependencies {
@Suppress("RedundantSuppression")
implementation(libs.dnsjava)
implementation(libs.guava)
implementation(libs.kotlinx.serialization)
implementation(libs.mikepenz.aboutLibraries)
implementation(libs.nsk90.kstatemachine)
implementation(libs.okhttp.base)
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

<activity android:name=".ui.intro.IntroActivity" />
<activity
android:name=".ui.AccountsActivity"
android:name=".ui.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
Expand All @@ -73,12 +73,12 @@
<activity
android:name=".ui.AboutActivity"
android:label="@string/navigation_drawer_about"
android:parentActivityName=".ui.AccountsActivity"/>
android:parentActivityName=".ui.MainActivity"/>

<activity
android:name=".ui.AppSettingsActivity"
android:label="@string/app_settings"
android:parentActivityName=".ui.AccountsActivity"
android:parentActivityName=".ui.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.APPLICATION_PREFERENCES"/>
Expand Down Expand Up @@ -106,7 +106,7 @@

<activity
android:name=".ui.setup.LoginActivity"
android:parentActivityName=".ui.AccountsActivity"
android:parentActivityName=".ui.MainActivity"
android:windowSoftInputMode="adjustResize"
android:exported="true">
<intent-filter>
Expand Down Expand Up @@ -134,7 +134,7 @@

<activity
android:name=".ui.account.AccountActivity"
android:parentActivityName=".ui.AccountsActivity"
android:parentActivityName=".ui.MainActivity"
android:exported="true">
</activity>
<activity
Expand All @@ -156,7 +156,7 @@
<activity
android:name=".ui.webdav.WebdavMountsActivity"
android:exported="true"
android:parentActivityName=".ui.AccountsActivity" />
android:parentActivityName=".ui.MainActivity" />
<activity
android:name=".ui.webdav.AddWebdavMountActivity"
android:parentActivityName=".ui.webdav.WebdavMountsActivity"
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/kotlin/at/bitfire/davdroid/db/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import at.bitfire.davdroid.R
import at.bitfire.davdroid.TextTable
import at.bitfire.davdroid.db.migration.*
import at.bitfire.davdroid.ui.AccountsActivity
import at.bitfire.davdroid.db.migration.AutoMigration12
import at.bitfire.davdroid.db.migration.AutoMigration16
import at.bitfire.davdroid.ui.MainActivity
import at.bitfire.davdroid.ui.NotificationRegistry
import dagger.Module
import dagger.Provides
Expand Down Expand Up @@ -74,7 +75,7 @@ abstract class AppDatabase: RoomDatabase() {
.addCallback(object: Callback() {
override fun onDestructiveMigration(db: SupportSQLiteDatabase) {
notificationRegistry.notifyIfPossible(NotificationRegistry.NOTIFY_DATABASE_CORRUPTED) {
val launcherIntent = Intent(context, AccountsActivity::class.java)
val launcherIntent = Intent(context, MainActivity::class.java)
NotificationCompat.Builder(context, notificationRegistry.CHANNEL_GENERAL)
.setSmallIcon(R.drawable.ic_warning_notify)
.setContentTitle(context.getString(R.string.database_destructive_migration_title))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.core.os.bundleOf
import at.bitfire.davdroid.ui.AccountsActivity
import at.bitfire.davdroid.ui.MainActivity

class AddressBookAuthenticatorService: Service() {

Expand All @@ -31,7 +31,7 @@ class AddressBookAuthenticatorService: Service() {
): AbstractAccountAuthenticator(context) {

override fun addAccount(response: AccountAuthenticatorResponse?, accountType: String?, authTokenType: String?, requiredFeatures: Array<String>?, options: Bundle?): Bundle {
val intent = Intent(context, AccountsActivity::class.java)
val intent = Intent(context, MainActivity::class.java)
intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response)
return bundleOf(AccountManager.KEY_INTENT to intent)
}
Expand Down
58 changes: 0 additions & 58 deletions app/src/main/kotlin/at/bitfire/davdroid/ui/AccountsActivity.kt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleting the AccountsActivity breaks a reference from jtxBoard: "Drawermenu > Synchronization > Go to collections".

For now, since we don't want to break external references we would need to keep an AccountsActivity and have it redirect to the new MainActivity. See #1172 (comment)

This file was deleted.

3 changes: 2 additions & 1 deletion app/src/main/kotlin/at/bitfire/davdroid/ui/AccountsModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class AccountsModel @AssistedInject constructor(
private val db: AppDatabase,
introPageFactory: IntroPageFactory,
private val logger: Logger,
private val syncWorkerManager: SyncWorkerManager
private val syncWorkerManager: SyncWorkerManager,
val accountsDrawerHandler: AccountsDrawerHandler
): ViewModel() {

@AssistedFactory
Expand Down
43 changes: 41 additions & 2 deletions app/src/main/kotlin/at/bitfire/davdroid/ui/AccountsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package at.bitfire.davdroid.ui

import android.Manifest
import android.accounts.Account
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings
import androidx.activity.compose.BackHandler
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
Expand Down Expand Up @@ -67,22 +69,59 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavBackStackEntry
import androidx.navigation.toRoute
import at.bitfire.davdroid.BuildConfig
import at.bitfire.davdroid.R
import at.bitfire.davdroid.ui.account.AccountActivity
import at.bitfire.davdroid.ui.account.AccountProgress
import at.bitfire.davdroid.ui.composable.ActionCard
import at.bitfire.davdroid.ui.composable.ProgressBar
import at.bitfire.davdroid.ui.intro.IntroActivity
import at.bitfire.davdroid.ui.navigation.Routes
import at.bitfire.davdroid.ui.setup.LoginActivity
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.isGranted
import com.google.accompanist.permissions.rememberPermissionState
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@Composable
fun AccountsScreen(backStackEntry: NavBackStackEntry) {
val route = backStackEntry.toRoute<Routes.Accounts>()
val context = LocalContext.current
val activity = context as? Activity

val introActivityLauncher = rememberLauncherForActivityResult(IntroActivity.Contract) { cancelled ->
if (cancelled) activity?.finish()
}

AccountsScreen(
initialSyncAccounts = route.syncAccounts,
onShowAppIntro = {
introActivityLauncher.launch(null)
},
onAddAccount = {
// eventually this will become a navigation
context.startActivity(Intent(context, LoginActivity::class.java))
},
onShowAccount = { account ->
// eventually this will become a navigation
val intent = Intent(context, AccountActivity::class.java)
intent.putExtra(AccountActivity.EXTRA_ACCOUNT, account)
context.startActivity(intent)
},
onManagePermissions = {
// eventually this will become a navigation
context.startActivity(Intent(context, PermissionsActivity::class.java))
}
)
}

@Composable
fun AccountsScreen(
initialSyncAccounts: Boolean,
onShowAppIntro: () -> Unit,
accountsDrawerHandler: AccountsDrawerHandler,
onAddAccount: () -> Unit,
onShowAccount: (Account) -> Unit,
onManagePermissions: () -> Unit,
Expand All @@ -107,7 +146,7 @@ fun AccountsScreen(
}

AccountsScreen(
accountsDrawerHandler = accountsDrawerHandler,
accountsDrawerHandler = model.accountsDrawerHandler,
accounts = accounts,
showSyncAll = showSyncAll,
onSyncAll = { model.syncAllAccounts() },
Expand Down
49 changes: 49 additions & 0 deletions app/src/main/kotlin/at/bitfire/davdroid/ui/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details.
*/

package at.bitfire.davdroid.ui

import android.content.Intent
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.CompositionLocalProvider
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import at.bitfire.davdroid.ui.composition.LocalNavController
import at.bitfire.davdroid.ui.navigation.Routes
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class MainActivity: AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContent {
val navController = rememberNavController()

CompositionLocalProvider(LocalNavController provides navController) {
NavHost(
navController = navController,
startDestination = accountsFromIntent()
) {
composable<Routes.Accounts> { AccountsScreen(it) }
}
}
}
}

/**
* Initializes the accounts route from the current intent data.
* Checks whether the action is [Intent.ACTION_SYNC].
*/
private fun accountsFromIntent() = Routes.Accounts(
// handle "Sync all" intent from launcher shortcut
syncAccounts = intent.action == Intent.ACTION_SYNC
)

}
2 changes: 1 addition & 1 deletion app/src/main/kotlin/at/bitfire/davdroid/ui/UiUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ object UiUtils {
ShortcutInfo.Builder(context, SHORTCUT_SYNC_ALL)
.setIcon(Icon.createWithResource(context, R.drawable.ic_sync_shortcut))
.setShortLabel(context.getString(R.string.accounts_sync_all))
.setIntent(Intent(Intent.ACTION_SYNC, null, context, AccountsActivity::class.java))
.setIntent(Intent(Intent.ACTION_SYNC, null, context, MainActivity::class.java))
.build()
)
} catch(e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package at.bitfire.davdroid.ui.composition

import androidx.compose.runtime.compositionLocalOf
import androidx.navigation.NavController

val LocalNavController = compositionLocalOf<NavController> { error("No NavController attached.") }
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package at.bitfire.davdroid.ui.navigation

import kotlinx.serialization.Serializable

object Routes {
@Serializable
data class Accounts(val syncAccounts: Boolean)
}
2 changes: 1 addition & 1 deletion app/src/main/res/xml/sync_prefs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PreferenceScreen android:title="@string/manage_accounts">
<intent
android:targetPackage="at.bitfire.davdroid"
android:targetClass="at.bitfire.davdroid.ui.AccountsActivity"
android:targetClass="at.bitfire.davdroid.ui.MainActivity"
android:action="ACTION_VIEW" />
</PreferenceScreen>

Expand Down
6 changes: 3 additions & 3 deletions app/src/ose/kotlin/at/bitfire/davdroid/OseFlavorModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ interface OseFlavorModules {
@Module
@InstallIn(ActivityComponent::class)
interface ForActivities {
@Binds
fun accountsDrawerHandler(impl: OseAccountsDrawerHandler): AccountsDrawerHandler

@Binds
fun loginTypesProvider(impl: StandardLoginTypesProvider): LoginTypesProvider
}

@Module
@InstallIn(ViewModelComponent::class)
interface ForViewModels {
@Binds
fun accountsDrawerHandler(impl: OseAccountsDrawerHandler): AccountsDrawerHandler

@Binds
fun appLicenseInfoProvider(impl: OpenSourceLicenseInfoProvider): AboutActivity.AppLicenseInfoProvider

Expand Down
Loading
Loading