-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Extract account logic from BaseActivity into a mixin #5064
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
Merged
tobiasKaminsky
merged 1 commit into
master
from
ezaquarii/extract-account-logic-from-base-activity-into-mixin
Feb 10, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/com/nextcloud/client/mixins/ActivityMixin.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * Nextcloud Android client application | ||
| * | ||
| * @author Chris Narkiewicz | ||
| * Copyright (C) 2020 Chris Narkiewicz <hello@ezaquarii.com> | ||
| * Copyright (C) 2020 Nextcloud GmbH | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package com.nextcloud.client.mixins | ||
|
|
||
| import android.content.Intent | ||
| import android.os.Bundle | ||
|
|
||
| /** | ||
| * Interface allowing to implement part of [android.app.Activity] logic as | ||
| * a mix-in. | ||
| */ | ||
| interface ActivityMixin { | ||
| fun onNewIntent(intent: Intent) { /* no-op */ } | ||
| fun onSaveInstanceState(outState: Bundle) { /* no-op */ } | ||
| fun onCreate(savedInstanceState: Bundle?) { /* no-op */ } | ||
| fun onRestart() { /* no-op */ } | ||
| fun onStart() { /* no-op */ } | ||
| fun onResume() { /* no-op */ } | ||
| fun onPause() { /* no-op */ } | ||
| fun onStop() { /* no-op */ } | ||
| fun onDestroy() { /* no-op */ } | ||
| } |
88 changes: 88 additions & 0 deletions
88
src/main/java/com/nextcloud/client/mixins/MixinRegistry.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| * Nextcloud Android client application | ||
| * | ||
| * @author Chris Narkiewicz | ||
| * Copyright (C) 2020 Chris Narkiewicz <hello@ezaquarii.com> | ||
| * Copyright (C) 2020 Nextcloud GmbH | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package com.nextcloud.client.mixins | ||
|
|
||
| import android.content.Intent | ||
| import android.os.Bundle | ||
|
|
||
| /** | ||
| * Mix-in registry allows forwards lifecycle calls to all | ||
| * registered mix-ins. | ||
| * | ||
| * Once instantiated, all [android.app.Activity] lifecycle methods | ||
| * must call relevant registry companion methods. | ||
| * | ||
| * Calling the registry from [android.app.Application.ActivityLifecycleCallbacks] is | ||
| * not possible as not all callbacks are supported by this interface. | ||
| */ | ||
| class MixinRegistry : ActivityMixin { | ||
|
|
||
| private val mixins = mutableListOf<ActivityMixin>() | ||
|
|
||
| fun add(vararg mixins: ActivityMixin) { | ||
| mixins.forEach { this.mixins.add(it) } | ||
| } | ||
|
|
||
| override fun onNewIntent(intent: Intent) { | ||
| super.onNewIntent(intent) | ||
| mixins.forEach { it.onNewIntent(intent) } | ||
| } | ||
|
|
||
| override fun onSaveInstanceState(outState: Bundle) { | ||
| super.onSaveInstanceState(outState) | ||
| mixins.forEach { it.onSaveInstanceState(outState) } | ||
| } | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| mixins.forEach { it.onCreate(savedInstanceState) } | ||
| } | ||
|
|
||
| override fun onRestart() { | ||
| super.onRestart() | ||
| mixins.forEach { it.onRestart() } | ||
| } | ||
|
|
||
| override fun onStart() { | ||
| super.onStart() | ||
| mixins.forEach { it.onStart() } | ||
| } | ||
|
|
||
| override fun onResume() { | ||
| super.onResume() | ||
| mixins.forEach { it.onResume() } | ||
| } | ||
|
|
||
| override fun onPause() { | ||
| super.onPause() | ||
| mixins.forEach { it.onPause() } | ||
| } | ||
|
|
||
| override fun onStop() { | ||
| super.onStop() | ||
| mixins.forEach { it.onStop() } | ||
| } | ||
|
|
||
| override fun onDestroy() { | ||
| super.onDestroy() | ||
| mixins.forEach { it.onDestroy() } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # Package com.nextcloud.client.mixins | ||
|
|
||
| This package provides utilities and interfaces | ||
| allowing implementation of UI logic as mix-ins. | ||
|
|
||
| Mix-ins allow encapsulation of non-visual logic | ||
| as classes facilitating composition over inheritance. | ||
|
|
||
| For more information about mix-in concept, please | ||
| refer to [article on Wikipedia](https://en.wikipedia.org/wiki/Mixin). |
133 changes: 133 additions & 0 deletions
133
src/main/java/com/nextcloud/client/mixins/SessionMixin.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| /* | ||
| * Nextcloud Android client application | ||
| * | ||
| * @author Chris Narkiewicz | ||
| * Copyright (C) 2020 Chris Narkiewicz <hello@ezaquarii.com> | ||
| * Copyright (C) 2020 Nextcloud GmbH | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package com.nextcloud.client.mixins | ||
|
|
||
| import android.accounts.Account | ||
| import android.app.Activity | ||
| import android.content.ContentResolver | ||
| import android.content.Intent | ||
| import android.os.Bundle | ||
| import com.nextcloud.client.account.User | ||
| import com.nextcloud.client.account.UserAccountManager | ||
| import com.nextcloud.java.util.Optional | ||
| import com.owncloud.android.datamodel.FileDataStorageManager | ||
| import com.owncloud.android.lib.resources.status.OCCapability | ||
| import com.owncloud.android.ui.activity.BaseActivity | ||
|
|
||
| /** | ||
| * Session mixin collects all account / user handling logic currently | ||
| * spread over various activities. | ||
| * | ||
| * It is an intermediary step facilitating comprehensive rework of | ||
| * account handling logic. | ||
| */ | ||
| class SessionMixin constructor( | ||
| private val activity: Activity, | ||
| private val contentResolver: ContentResolver, | ||
| private val accountManager: UserAccountManager | ||
| ) : ActivityMixin { | ||
|
|
||
| private companion object { | ||
| private val TAG = BaseActivity::class.java.simpleName | ||
| } | ||
|
|
||
| var currentAccount: Account? = null | ||
| private set | ||
| var storageManager: FileDataStorageManager? = null | ||
| private set | ||
| var capabilities: OCCapability? = null | ||
| private set | ||
|
|
||
| fun setAccount(account: Account?) { | ||
| val validAccount = account != null && accountManager.setCurrentOwnCloudAccount(account.name) | ||
| if (validAccount) { | ||
| currentAccount = account | ||
| } else { | ||
| swapToDefaultAccount() | ||
| } | ||
|
|
||
| currentAccount?.let { | ||
| val storageManager = FileDataStorageManager(currentAccount, contentResolver) | ||
| this.storageManager = storageManager | ||
| this.capabilities = storageManager.getCapability(it.name) | ||
| } | ||
| } | ||
|
|
||
| fun setUser(user: User) { | ||
| setAccount(user.toPlatformAccount()) | ||
| } | ||
|
|
||
| fun getUser(): Optional<User> = when (val it = this.currentAccount) { | ||
| null -> Optional.empty() | ||
| else -> accountManager.getUser(it.name) | ||
| } | ||
|
|
||
| /** | ||
| * Tries to swap the current ownCloud [Account] for other valid and existing. | ||
| * | ||
| * If no valid ownCloud [Account] exists, then the user is requested | ||
| * to create a new ownCloud [Account]. | ||
| */ | ||
| private fun swapToDefaultAccount() { | ||
| // default to the most recently used account | ||
| val newAccount = accountManager.currentAccount | ||
| if (newAccount == null) { | ||
| // no account available: force account creation | ||
| startAccountCreation() | ||
| } else { | ||
| currentAccount = newAccount | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Launches the account creation activity. | ||
| */ | ||
| fun startAccountCreation() { | ||
| accountManager.startAccountCreation(activity) | ||
| } | ||
|
|
||
| override fun onNewIntent(intent: Intent) { | ||
| super.onNewIntent(intent) | ||
| val current = accountManager.currentAccount | ||
| val currentAccount = this.currentAccount | ||
| if (current != null && currentAccount != null && !currentAccount.name.equals(current.name)) { | ||
| this.currentAccount = current | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Since ownCloud {@link Account} can be managed from the system setting menu, the existence of the {@link | ||
| * Account} associated to the instance must be checked every time it is restarted. | ||
| */ | ||
| override fun onRestart() { | ||
| super.onRestart() | ||
| val validAccount = currentAccount != null && accountManager.exists(currentAccount) | ||
| if (!validAccount) { | ||
| swapToDefaultAccount() | ||
| } | ||
| } | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| val account = accountManager.currentAccount | ||
| setAccount(account) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.