Repository navigation
Improve sync error handling #3401
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
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2eaa581
improve sync error handling
alperozturk96 8a62652
use single event for sync errors
alperozturk96 3fdc17d
update lib and new translation key
alperozturk96 a6bffee
detect removed account and warn user
alperozturk96 28940ce
make sync snackbar class
alperozturk96 78118bf
make sync snackbar class
alperozturk96 398bbb8
Rename .java to .kt
alperozturk96 d6f5834
consolidate snackbar logic
alperozturk96 a3119fc
consolidate branded snackbar usage in mainactivity for sync errors
alperozturk96 b1af761
consolidate branded snackbar usage in mainactivity for sync errors
alperozturk96 9e1630f
update test
alperozturk96 fe05b1c
fix snackbar line appereance, pull to refresh error handling
alperozturk96 c6c5b42
fix snackbar line appereance, pull to refresh error handling
alperozturk96 9a3fc12
show username in snackbar for remove
alperozturk96 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
37 changes: 0 additions & 37 deletions
37
app/src/main/java/it/niedermann/owncloud/notes/branding/BrandedSnackbar.java
This file was deleted.
Oops, something went wrong.
126 changes: 126 additions & 0 deletions
126
app/src/main/java/it/niedermann/owncloud/notes/branding/BrandedSnackbar.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,126 @@ | ||
| /* | ||
| * Nextcloud - Android Client | ||
| * | ||
| * SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com> | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| package it.niedermann.owncloud.notes.branding | ||
|
|
||
| import android.content.Context | ||
| import android.view.View | ||
| import androidx.annotation.ColorInt | ||
| import androidx.annotation.StringRes | ||
| import androidx.fragment.app.FragmentActivity | ||
| import com.google.android.material.snackbar.BaseTransientBottomBar | ||
| import com.google.android.material.snackbar.Snackbar | ||
| import com.nextcloud.android.sso.AccountImporter | ||
| import com.nextcloud.android.sso.exceptions.SSOException | ||
| import com.nextcloud.android.sso.helper.SingleAccountHelper | ||
| import it.niedermann.owncloud.notes.R | ||
| import it.niedermann.owncloud.notes.exception.ExceptionDialogFragment | ||
| import it.niedermann.owncloud.notes.main.SyncErrorMessage | ||
| import it.niedermann.owncloud.notes.persistence.FilesAppAccount | ||
| import it.niedermann.owncloud.notes.persistence.SyncFailureReason | ||
| import it.niedermann.owncloud.notes.persistence.entity.Account | ||
| import java.lang.ref.WeakReference | ||
|
|
||
| object BrandedSnackbar { | ||
| private const val SYNC_ERROR_MAX_LINES = 4 | ||
|
|
||
| private var syncErrorSnackbar: WeakReference<Snackbar>? = null | ||
| private var currentAccount: Account? = null | ||
|
|
||
| @JvmStatic | ||
| fun make( | ||
| view: View, | ||
| text: CharSequence, | ||
| @BaseTransientBottomBar.Duration duration: Int | ||
| ): Snackbar { | ||
| @ColorInt val color = BrandingUtil.readBrandMainColor(view.context) | ||
| val snackbar = Snackbar.make(view, text, duration) | ||
| val utils = BrandingUtil.of(color, view.context) | ||
|
|
||
| utils.material.themeSnackbar(snackbar) | ||
|
|
||
| return snackbar | ||
| } | ||
|
|
||
| @JvmStatic | ||
| fun make( | ||
| view: View, | ||
| @StringRes resId: Int, | ||
| @BaseTransientBottomBar.Duration duration: Int | ||
| ): Snackbar { | ||
| return make(view, view.resources.getText(resId), duration) | ||
| } | ||
|
|
||
| @JvmStatic | ||
| fun showSyncError(activity: FragmentActivity, view: View, anchor: View, exceptions: Collection<Throwable>) { | ||
| val reason = SyncErrorMessage(exceptions).reasonOf() | ||
| if (reason == SyncFailureReason.ACCOUNT_REMOVED && isCurrentAccountAvailable(activity)) return | ||
|
|
||
| val snackbar = make(view, reason.text(view.context), reason.snackbarDuration()) | ||
| .setTextMaxLines(SYNC_ERROR_MAX_LINES) | ||
| .setAnchorView(anchor) | ||
| when (reason) { | ||
| SyncFailureReason.TOKEN_MISMATCH -> | ||
| snackbar.setAction(R.string.error_action_grant_access) { requestAccessAgain(activity) } | ||
|
|
||
| SyncFailureReason.ACCOUNT_REMOVED -> | ||
| snackbar.setAction(R.string.choose_account) { chooseAccount(activity) } | ||
|
|
||
| SyncFailureReason.NO_NETWORK, SyncFailureReason.MAINTENANCE -> Unit | ||
|
|
||
| else -> snackbar.setAction(R.string.simple_more) { showDetails(activity, exceptions) } | ||
| } | ||
|
|
||
| syncErrorSnackbar = WeakReference(snackbar) | ||
| snackbar.show() | ||
| } | ||
|
|
||
| @JvmStatic | ||
| fun onCurrentAccountChanged(account: Account) { | ||
| currentAccount = account | ||
| syncErrorSnackbar?.get()?.dismiss() | ||
| syncErrorSnackbar = null | ||
| } | ||
|
|
||
| @JvmStatic | ||
| fun showSyncError(activity: FragmentActivity, view: View, anchor: View, exception: Throwable) = | ||
| showSyncError(activity, view, anchor, listOf(exception)) | ||
|
|
||
| private fun SyncFailureReason.text(context: Context): CharSequence = when (this) { | ||
| SyncFailureReason.ACCOUNT_REMOVED -> context.getString(message, currentAccount?.userName) | ||
| else -> context.getText(message) | ||
| } | ||
|
|
||
| private fun SyncFailureReason.snackbarDuration(): Int = when (this) { | ||
| SyncFailureReason.TOKEN_MISMATCH, SyncFailureReason.ACCOUNT_REMOVED -> Snackbar.LENGTH_INDEFINITE | ||
| else -> Snackbar.LENGTH_LONG | ||
| } | ||
|
|
||
| private fun isCurrentAccountAvailable(activity: FragmentActivity): Boolean = | ||
| currentAccount?.let { FilesAppAccount.exists(activity, it.accountName) } == true | ||
|
|
||
| private fun requestAccessAgain(activity: FragmentActivity) { | ||
| try { | ||
| val ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(activity) | ||
| AccountImporter.authenticateSingleSignAccount(activity, ssoAccount) | ||
| } catch (e: SSOException) { | ||
| showDetails(activity, listOf(e)) | ||
| } | ||
| } | ||
|
|
||
| private fun chooseAccount(activity: FragmentActivity) { | ||
| try { | ||
| AccountImporter.pickNewAccount(activity) | ||
| } catch (e: SSOException) { | ||
| showDetails(activity, listOf(e)) | ||
| } | ||
| } | ||
|
|
||
| private fun showDetails(activity: FragmentActivity, exceptions: Collection<Throwable>) { | ||
| ExceptionDialogFragment.newInstance(ArrayList(exceptions)) | ||
| .show(activity.supportFragmentManager, ExceptionDialogFragment::class.java.simpleName) | ||
| } | ||
| } |
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
33 changes: 33 additions & 0 deletions
33
app/src/main/java/it/niedermann/owncloud/notes/persistence/FilesAppAccount.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,33 @@ | ||
| /* | ||
| * Nextcloud - Android Client | ||
| * | ||
| * SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com> | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| package it.niedermann.owncloud.notes.persistence | ||
|
|
||
| import android.content.Context | ||
| import com.nextcloud.android.sso.AccountImporter | ||
| import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException | ||
| import com.nextcloud.android.sso.model.SingleSignOnAccount | ||
|
|
||
| object FilesAppAccount { | ||
| @JvmStatic | ||
| @Throws(NextcloudFilesAppAccountNotFoundException::class) | ||
| fun require(context: Context, accountName: String): SingleSignOnAccount { | ||
| ensureExists(context, accountName) | ||
|
|
||
| return AccountImporter.getSingleSignOnAccount(context, accountName) | ||
| } | ||
|
|
||
| @JvmStatic | ||
| @Throws(NextcloudFilesAppAccountNotFoundException::class) | ||
| fun ensureExists(context: Context, accountName: String) { | ||
| if (!exists(context, accountName)) { | ||
| throw NextcloudFilesAppAccountNotFoundException(context, accountName) | ||
| } | ||
| } | ||
|
|
||
| fun exists(context: Context, accountName: String): Boolean = | ||
| AccountImporter.getAccountForName(context, accountName) != null | ||
| } |
Oops, something went wrong.
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.