Skip to content

Commit 52bd496

Browse files
committed
fix: anchor snackbars to the bottom bar only when necessary
Signed-off-by: Jorge Aguado Recio <jaguado@izertis.com>
1 parent 600f558 commit 52bd496

6 files changed

Lines changed: 74 additions & 45 deletions

File tree

‎owncloudApp/src/main/java/com/owncloud/android/extensions/ActivityExt.kt‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,23 @@ import timber.log.Timber
8484
import java.io.File
8585
import kotlin.math.min
8686

87-
fun Activity.showErrorInSnackbar(genericErrorMessageId: Int, throwable: Throwable?) =
87+
fun Activity.showErrorInSnackbar(genericErrorMessageId: Int, throwable: Throwable?, attachedToBottomBar: Boolean = false) =
8888
throwable?.let {
8989
showMessageInSnackbar(
90-
message = it.parseError(getString(genericErrorMessageId), resources)
90+
message = it.parseError(getString(genericErrorMessageId), resources),
91+
attachedToBottomBar = attachedToBottomBar
9192
)
9293
}
9394

9495
fun Activity.showMessageInSnackbar(
9596
layoutId: Int = android.R.id.content,
9697
message: CharSequence,
97-
duration: Int = Snackbar.LENGTH_LONG
98+
duration: Int = Snackbar.LENGTH_LONG,
99+
attachedToBottomBar: Boolean = false
98100
) {
99-
Snackbar.make(findViewById(layoutId), message, duration)
100-
.setAnchorView(findViewById(R.id.bottom_nav_view))
101-
.show()
101+
val snackbar = Snackbar.make(findViewById(layoutId), message, duration)
102+
if (attachedToBottomBar) { snackbar.setAnchorView(findViewById(R.id.bottom_nav_view)) }
103+
snackbar.show()
102104
}
103105

104106
fun Activity.showSnackbarWithAction(

‎owncloudApp/src/main/java/com/owncloud/android/extensions/FragmentExt.kt‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,20 @@ import kotlinx.coroutines.flow.Flow
3939
import kotlinx.coroutines.flow.collectLatest
4040
import kotlinx.coroutines.launch
4141

42-
fun Fragment.showErrorInSnackbar(genericErrorMessageId: Int, throwable: Throwable?) =
42+
fun Fragment.showErrorInSnackbar(genericErrorMessageId: Int, throwable: Throwable?, attachedToBottomBar: Boolean = false) =
4343
throwable?.let {
44-
showMessageInSnackbar(it.parseError(getString(genericErrorMessageId), resources))
44+
showMessageInSnackbar(it.parseError(getString(genericErrorMessageId), resources), attachedToBottomBar = attachedToBottomBar)
4545
}
4646

4747
fun Fragment.showMessageInSnackbar(
4848
message: CharSequence,
49-
duration: Int = Snackbar.LENGTH_LONG
49+
duration: Int = Snackbar.LENGTH_LONG,
50+
attachedToBottomBar: Boolean = false
5051
) {
5152
val requiredView = view?.rootView ?: return
52-
Snackbar.make(requiredView, message, duration)
53-
.setAnchorView(requiredView.findViewById(R.id.bottom_nav_view))
54-
.show()
53+
val snackbar = Snackbar.make(requiredView, message, duration)
54+
if (attachedToBottomBar) { snackbar.setAnchorView(requiredView.findViewById(R.id.bottom_nav_view)) }
55+
snackbar.show()
5556
}
5657

5758
fun Fragment.showSnackbarWithAction(
@@ -62,7 +63,6 @@ fun Fragment.showSnackbarWithAction(
6263
) {
6364
val requiredView = view?.rootView ?: return
6465
Snackbar.make(requiredView, message, duration)
65-
.setAnchorView(requiredView.findViewById(R.id.bottom_nav_view))
6666
.setAction(actionText) { action() }
6767
.show()
6868
}

‎owncloudApp/src/main/java/com/owncloud/android/presentation/files/filelist/MainFileListFragment.kt‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ class MainFileListFragment : Fragment(),
430430
// Set Refresh FAB and its listener
431431
binding.fabRefresh.setOnClickListener {
432432
if (fileOperationsViewModel.refreshFolderLiveData.value!!.peekContent().isLoading) {
433-
showMessageInSnackbar(message = getString(R.string.fab_refresh_sync_in_progress))
433+
showMessageInSnackbar(message = getString(R.string.fab_refresh_sync_in_progress), attachedToBottomBar = true)
434434
} else {
435435
fileOperationsViewModel.performOperation(
436436
FileOperation.RefreshFolderOperation(
@@ -572,13 +572,18 @@ class MainFileListFragment : Fragment(),
572572
val message =
573573
getString(R.string.open_in_web_error_generic) + " " + getString(R.string.error_reason) +
574574
" " + getString(R.string.open_in_web_error_not_supported)
575-
this.showMessageInSnackbar(message, Snackbar.LENGTH_LONG)
575+
this.showMessageInSnackbar(message = message, duration = Snackbar.LENGTH_LONG, attachedToBottomBar = true)
576576
} else if (uiResult.error is TooEarlyException) {
577-
this.showMessageInSnackbar(getString(R.string.open_in_web_error_too_early), Snackbar.LENGTH_LONG)
577+
this.showMessageInSnackbar(
578+
message = getString(R.string.open_in_web_error_too_early),
579+
duration = Snackbar.LENGTH_LONG,
580+
attachedToBottomBar = true
581+
)
578582
} else {
579583
this.showErrorInSnackbar(
580-
R.string.open_in_web_error_generic,
581-
uiResult.error
584+
genericErrorMessageId = R.string.open_in_web_error_generic,
585+
throwable = uiResult.error,
586+
attachedToBottomBar = true
582587
)
583588
}
584589
}
@@ -884,7 +889,8 @@ class MainFileListFragment : Fragment(),
884889
val errorMessage =
885890
uiResult.error?.parseError(resources.getString(R.string.create_file_fail_msg), resources, false)
886891
showMessageInSnackbar(
887-
message = errorMessage.toString()
892+
message = errorMessage.toString(),
893+
attachedToBottomBar = true
888894
)
889895
} else if (uiResult is UIResult.Success) {
890896
val fileId = uiResult.data
@@ -910,7 +916,7 @@ class MainFileListFragment : Fragment(),
910916

911917
is UIResult.Error -> {
912918
fileActivity.dismissLoadingDialog()
913-
showMessageInSnackbar(resources.getString(R.string.common_error_unknown))
919+
showMessageInSnackbar(message = resources.getString(R.string.common_error_unknown), attachedToBottomBar = true)
914920
}
915921
}
916922
}
@@ -1232,7 +1238,7 @@ class MainFileListFragment : Fragment(),
12321238
var error: String? = null
12331239

12341240
if (error != null) {
1235-
showMessageInSnackbar(error)
1241+
showMessageInSnackbar(message = error, attachedToBottomBar = true)
12361242
} else {
12371243
val filenameWithExtension = "$filename.$fileExtension"
12381244
fileOperationsViewModel.performOperation(
@@ -1306,7 +1312,8 @@ class MainFileListFragment : Fragment(),
13061312
val errorMessage =
13071313
uiResult.error?.parseError(resources.getString(R.string.create_dir_fail_msg), resources, false)
13081314
showMessageInSnackbar(
1309-
message = errorMessage.toString()
1315+
message = errorMessage.toString(),
1316+
attachedToBottomBar = true
13101317
)
13111318
}
13121319
})

‎owncloudApp/src/main/java/com/owncloud/android/presentation/spaces/SpacesListFragment.kt‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ class SpacesListFragment :
198198
spacesListAdapter.setData(spacesToListFiltered, isMultiPersonal)
199199
}
200200
binding.swipeRefreshSpacesList.isRefreshing = uiState.refreshing
201-
uiState.error?.let { showErrorInSnackbar(R.string.spaces_sync_failed, it) }
201+
uiState.error?.let { showErrorInSnackbar(genericErrorMessageId = R.string.spaces_sync_failed, throw it, attachedToBottomBar = true) }
202202

203203
uiState.rootFolderFromSelectedSpace?.let {
204204
setFragmentResult(REQUEST_KEY_CLICK_SPACE, bundleOf(BUNDLE_KEY_CLICK_SPACE to it))
@@ -270,7 +270,7 @@ class SpacesListFragment :
270270
}
271271
}
272272
TransferStatus.TRANSFER_FAILED -> {
273-
showMessageInSnackbar(getString(R.string.edit_space_image_failed))
273+
showMessageInSnackbar(message = getString(R.string.edit_space_image_failed), attachedToBottomBar = true)
274274
}
275275
else -> { }
276276
}
@@ -283,9 +283,15 @@ class SpacesListFragment :
283283
collectLatestLifecycleFlow(flow) { event ->
284284
event?.let {
285285
when (val uiResult = event.peekContent()) {
286-
is UIResult.Success -> { showMessageInSnackbar(getString(successMessage)) }
286+
is UIResult.Success -> { showMessageInSnackbar(message = getString(successMessage), attachedToBottomBar = true) }
287287
is UIResult.Loading -> { }
288-
is UIResult.Error -> { showErrorInSnackbar(errorMessage, uiResult.error) }
288+
is UIResult.Error -> {
289+
showErrorInSnackbar(
290+
genericErrorMessageId = errorMessage,
291+
throwable = uiResult.error,
292+
attachedToBottomBar = true
293+
)
294+
}
289295
}
290296
}
291297
}

‎owncloudApp/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.kt‎

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ class FileDisplayActivity : FileActivity(),
613613
// Handle calls form internal activities.
614614
if (requestCode == REQUEST_CODE__SELECT_CONTENT_FROM_APPS && (resultCode == RESULT_OK || resultCode == RESULT_OK_AND_MOVE)) {
615615
if (!manageAccountsViewModel.hasEnoughQuota(account.name)) {
616-
showMessageInSnackbar(message = getString(R.string.failed_upload_quota_exceeded_text))
616+
showMessageInSnackbar(message = getString(R.string.failed_upload_quota_exceeded_text), attachedToBottomBar = true)
617617
}
618618

619619
requestUploadOfContentFromApps(data)
@@ -631,7 +631,7 @@ class FileDisplayActivity : FileActivity(),
631631
) {
632632
if (hasEnoughSpace) {
633633
if (!manageAccountsViewModel.hasEnoughQuota(account.name)) {
634-
showMessageInSnackbar(message = getString(R.string.failed_upload_quota_exceeded_text))
634+
showMessageInSnackbar(message = getString(R.string.failed_upload_quota_exceeded_text), attachedToBottomBar = true)
635635
}
636636
requestUploadOfFilesFromFileSystem(capturedFilePaths)
637637
}
@@ -672,7 +672,7 @@ class FileDisplayActivity : FileActivity(),
672672

673673
} else {
674674
Timber.d("User clicked on 'Update' with no selection")
675-
showMessageInSnackbar(R.id.list_layout, getString(R.string.filedisplay_no_file_selected))
675+
showMessageInSnackbar(layoutId = R.id.list_layout, message = getString(R.string.filedisplay_no_file_selected), attachedToBottomBar = true)
676676
}
677677
}
678678

@@ -887,11 +887,12 @@ class FileDisplayActivity : FileActivity(),
887887
if (currentDir == null) {
888888
// current folder was removed from the server
889889
showMessageInSnackbar(
890-
R.id.list_layout,
891-
String.format(
890+
layoutId = R.id.list_layout,
891+
message = String.format(
892892
getString(R.string.sync_current_folder_was_removed),
893893
synchFolderRemotePath
894-
)
894+
),
895+
attachedToBottomBar = true
895896
)
896897
browseToRoot()
897898

@@ -924,7 +925,7 @@ class FileDisplayActivity : FileActivity(),
924925
if (synchResult.httpPhrase == "Error: Call to a member function getUID() on null") {
925926
showRequestAccountChangeNotice(getString(R.string.auth_failure_snackbar), false)
926927
} else {
927-
showMessageInSnackbar(R.id.list_layout, synchResult.httpPhrase)
928+
showMessageInSnackbar(layoutId = R.id.list_layout, message = synchResult.httpPhrase, attachedToBottomBar = true)
928929
}
929930
} else {
930931
showRequestAccountChangeNotice(getString(R.string.auth_failure_snackbar), false)
@@ -1026,7 +1027,7 @@ class FileDisplayActivity : FileActivity(),
10261027
val singleRemoval = listOfFilesRemoved.size == 1
10271028

10281029
if (singleRemoval) {
1029-
showMessageInSnackbar(message = getString(R.string.remove_success_msg))
1030+
showMessageInSnackbar(message = getString(R.string.remove_success_msg), attachedToBottomBar = true)
10301031
}
10311032

10321033
// Clean second fragment and refresh first one
@@ -1049,7 +1050,11 @@ class FileDisplayActivity : FileActivity(),
10491050

10501051
is UIResult.Error -> {
10511052
dismissLoadingDialog()
1052-
showErrorInSnackbar(R.string.remove_fail_msg, uiResult.getThrowableOrNull())
1053+
showErrorInSnackbar(
1054+
genericErrorMessageId = R.string.remove_fail_msg,
1055+
throwable = uiResult.getThrowableOrNull(),
1056+
attachedToBottomBar = true
1057+
)
10531058

10541059
if (uiResult.getThrowableOrNull() is SSLRecoverablePeerUnverifiedException) {
10551060
showUntrustedCertDialogForThrowable(uiResult.getThrowableOrNull())
@@ -1100,7 +1105,8 @@ class FileDisplayActivity : FileActivity(),
11001105

11011106
uiResult.error?.let {
11021107
showMessageInSnackbar(
1103-
message = it.parseError(getString(R.string.move_file_error), resources, true)
1108+
message = it.parseError(getString(R.string.move_file_error), resources, true),
1109+
attachedToBottomBar = true
11041110
)
11051111
}
11061112
}
@@ -1155,7 +1161,8 @@ class FileDisplayActivity : FileActivity(),
11551161
genericErrorMessage = getString(R.string.copy_file_error),
11561162
resources = resources,
11571163
showJustReason = true,
1158-
)
1164+
),
1165+
attachedToBottomBar = true
11591166
)
11601167
}
11611168
}
@@ -1180,7 +1187,7 @@ class FileDisplayActivity : FileActivity(),
11801187
},
11811188
)
11821189
} else {
1183-
showMessageInSnackbar(R.id.list_layout, message)
1190+
showMessageInSnackbar(layoutId = R.id.list_layout, message = message, attachedToBottomBar = true)
11841191
}
11851192
}
11861193

@@ -1377,7 +1384,11 @@ class FileDisplayActivity : FileActivity(),
13771384
is UIResult.Error -> {
13781385
dismissLoadingDialog()
13791386

1380-
showErrorInSnackbar(R.string.rename_server_fail_msg, uiResult.error)
1387+
showErrorInSnackbar(
1388+
genericErrorMessageId = R.string.rename_server_fail_msg,
1389+
throwable = uiResult.error,
1390+
attachedToBottomBar = true
1391+
)
13811392

13821393
if (uiResult.getThrowableOrNull() is SSLRecoverablePeerUnverifiedException) {
13831394
showUntrustedCertDialogForThrowable(uiResult.getThrowableOrNull())
@@ -1596,7 +1607,8 @@ class FileDisplayActivity : FileActivity(),
15961607
owner = this,
15971608
onWorkEnqueued = {
15981609
showMessageInSnackbar(
1599-
message = String.format(getString(R.string.downloader_download_enqueued_ticker), file.fileName)
1610+
message = String.format(getString(R.string.downloader_download_enqueued_ticker), file.fileName),
1611+
attachedToBottomBar = true
16001612
)
16011613
},
16021614
onWorkRunning = { progress -> Timber.d("Downloading - Progress $progress") },
@@ -1618,7 +1630,8 @@ class FileDisplayActivity : FileActivity(),
16181630
},
16191631
onWorkFailed = {
16201632
showMessageInSnackbar(
1621-
message = String.format(getString(R.string.downloader_download_failed_ticker), file.fileName)
1633+
message = String.format(getString(R.string.downloader_download_failed_ticker), file.fileName),
1634+
attachedToBottomBar = true
16221635
)
16231636
if (file.id == waitingToSend?.id) {
16241637
waitingToSend = null
@@ -1976,7 +1989,7 @@ class FileDisplayActivity : FileActivity(),
19761989

19771990
override fun uploadShortcutFileFromApp(shortcutFilePath: Array<String>) {
19781991
if (!manageAccountsViewModel.hasEnoughQuota(account.name)) {
1979-
showMessageInSnackbar(message = getString(R.string.failed_upload_quota_exceeded_text))
1992+
showMessageInSnackbar(message = getString(R.string.failed_upload_quota_exceeded_text), attachedToBottomBar = true)
19801993
}
19811994
requestUploadOfFilesFromFileSystem(shortcutFilePath)
19821995
}
@@ -2015,7 +2028,7 @@ class FileDisplayActivity : FileActivity(),
20152028
is UIResult.Error -> {
20162029
dismissLoadingDialog()
20172030
if (uiResult.error is FileNotFoundException) {
2018-
showMessageInSnackbar(message = getString(R.string.deep_link_user_no_access))
2031+
showMessageInSnackbar(message = getString(R.string.deep_link_user_no_access), attachedToBottomBar = true)
20192032
changeUser()
20202033
} else {
20212034
showMessageInSnackbar(
@@ -2025,7 +2038,8 @@ class FileDisplayActivity : FileActivity(),
20252038
} else {
20262039
R.string.default_error_msg
20272040
}
2028-
)
2041+
),
2042+
attachedToBottomBar = true
20292043
)
20302044
}
20312045
}

‎owncloudApp/src/main/java/com/owncloud/android/ui/activity/ReceiveExternalFilesActivity.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ protected void onAccountSet(boolean stateWasRecovered) {
361361
} else if (throwable instanceof CertificateCombinedException) {
362362
showUntrustedCertDialogForThrowable(throwable);
363363
} else {
364-
ActivityExtKt.showErrorInSnackbar(this, R.string.sync_fail_ticker, throwable);
364+
ActivityExtKt.showErrorInSnackbar(this, R.string.sync_fail_ticker, throwable, false);
365365
}
366366
}
367367
} else if (uiResult instanceof UIResult.Success) {

0 commit comments

Comments
 (0)