diff options
author | ashoksinghal <ashoksinghal1990@gmail.com> | 2020-10-17 22:09:34 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-17 22:09:34 +0530 |
commit | 3981638e0f8d714567f6a3c3a25dc54599263af2 (patch) | |
tree | 4148e109a1c0b7eb75b24e24be120a2a7edf8d36 | |
parent | 6c1e41ba1050c92f4b615f7e857e0d085120a242 (diff) |
Use BottomSheet in place of Snackbar for longer messages (#1157)
6 files changed, 54 insertions, 24 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt index 7c1dae9b..ee87f272 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt @@ -24,7 +24,6 @@ import androidx.recyclerview.widget.LinearLayoutManager import com.github.michaelbull.result.fold import com.github.michaelbull.result.runCatching import com.github.michaelbull.result.onFailure -import com.google.android.material.snackbar.Snackbar import com.zeapo.pwdstore.databinding.PasswordRecyclerViewBinding import com.zeapo.pwdstore.git.BaseGitActivity import com.zeapo.pwdstore.git.GitServerConfigActivity @@ -32,6 +31,7 @@ import com.zeapo.pwdstore.git.config.AuthMode import com.zeapo.pwdstore.git.config.GitSettings import com.zeapo.pwdstore.ui.OnOffItemAnimator import com.zeapo.pwdstore.ui.adapters.PasswordItemRecyclerAdapter +import com.zeapo.pwdstore.ui.dialogs.BasicBottomSheet import com.zeapo.pwdstore.ui.dialogs.ItemCreationBottomSheet import com.zeapo.pwdstore.utils.PasswordItem import com.zeapo.pwdstore.utils.PasswordRepository @@ -87,11 +87,13 @@ class PasswordFragment : Fragment(R.layout.password_recycler_view) { requireStore().refreshPasswordList() binding.swipeRefresher.isRefreshing = false } else if (!PasswordRepository.isGitRepo()) { - Snackbar.make(binding.root, getString(R.string.clone_git_repo), Snackbar.LENGTH_INDEFINITE) - .setAction(R.string.clone_button) { + BasicBottomSheet.Builder(requireContext()) + .setMessageRes(R.string.clone_git_repo) + .setPositiveButtonClickListener(getString(R.string.clone_button)) { swipeResult.launch(GitServerConfigActivity.createCloneIntent(requireContext())) } - .show() + .build() + .show(requireActivity().supportFragmentManager, "NOT_A_GIT_REPO") binding.swipeRefresher.isRefreshing = false } else { // When authentication is set to AuthMode.None then the only git operation we can diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt index b7ada287..edfd78b3 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt @@ -46,6 +46,7 @@ import com.zeapo.pwdstore.crypto.PasswordCreationActivity import com.zeapo.pwdstore.git.BaseGitActivity import com.zeapo.pwdstore.git.config.AuthMode import com.zeapo.pwdstore.git.config.GitSettings +import com.zeapo.pwdstore.ui.dialogs.BasicBottomSheet import com.zeapo.pwdstore.ui.dialogs.FolderCreationDialogFragment import com.zeapo.pwdstore.ui.onboarding.activity.OnboardingActivity import com.zeapo.pwdstore.utils.PasswordItem @@ -373,17 +374,13 @@ class PasswordStore : BaseGitActivity() { */ private fun hasRequiredStoragePermissions(): Boolean { return if (!isPermissionGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { - Snackbar.make( - findViewById(R.id.main_layout), - getString(R.string.access_sdcard_text), - Snackbar.LENGTH_INDEFINITE - ).run { - setAction(getString(R.string.snackbar_action_grant)) { + BasicBottomSheet.Builder(this) + .setMessageRes(R.string.access_sdcard_text) + .setPositiveButtonClickListener(getString(R.string.snackbar_action_grant)) { storagePermissionRequest.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE) - dismiss() } - show() - } + .build() + .show(supportFragmentManager, "STORAGE_PERMISSION_MISSING") false } else { checkLocalRepository() diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitServerConfigActivity.kt b/app/src/main/java/com/zeapo/pwdstore/git/GitServerConfigActivity.kt index 4b220be5..364dcc1a 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitServerConfigActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitServerConfigActivity.kt @@ -115,10 +115,25 @@ class GitServerConfigActivity : BaseGitActivity() { GitSettings.UpdateConnectionSettingsResult.FailedToParseUrl -> { Snackbar.make(binding.root, getString(R.string.git_server_config_save_error), Snackbar.LENGTH_LONG).show() } + is GitSettings.UpdateConnectionSettingsResult.MissingUsername -> { when (updateResult.newProtocol) { - Protocol.Https -> Snackbar.make(binding.root, getString(R.string.git_server_config_save_missing_username_https), Snackbar.LENGTH_LONG).show() - Protocol.Ssh -> Snackbar.make(binding.root, getString(R.string.git_server_config_save_missing_username_ssh), Snackbar.LENGTH_LONG).show() + Protocol.Https -> + BasicBottomSheet.Builder(this) + .setTitleRes(R.string.ssh_scheme_needed_title) + .setMessageRes(R.string.git_server_config_save_missing_username_https) + .setPositiveButtonClickListener { + } + .build() + .show(supportFragmentManager, "HTTPS_MISSING_USERNAME") + Protocol.Ssh -> + BasicBottomSheet.Builder(this) + .setTitleRes(R.string.ssh_scheme_needed_title) + .setMessageRes(R.string.git_server_config_save_missing_username_ssh) + .setPositiveButtonClickListener { + } + .build() + .show(supportFragmentManager, "SSH_MISSING_USERNAME") } } GitSettings.UpdateConnectionSettingsResult.Valid -> { @@ -201,7 +216,6 @@ class GitServerConfigActivity : BaseGitActivity() { // Silently delete & replace the lone .git folder if it exists if (localDir.exists() && localDirFiles.size == 1 && localDirFiles[0].name == ".git") { localDir.deleteRecursively() - } }.onFailure { e -> e(e) diff --git a/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/BasicBottomSheet.kt b/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/BasicBottomSheet.kt index 5f79baf8..6f028009 100644 --- a/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/BasicBottomSheet.kt +++ b/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/BasicBottomSheet.kt @@ -28,8 +28,10 @@ import com.zeapo.pwdstore.utils.viewBinding * API through [Builder] to create a similar UI, just at the bottom of the screen. */ class BasicBottomSheet private constructor( - val title: String, + val title: String?, val message: String, + val positiveButtonLabel: String?, + val negativeButtonLabel: String?, val positiveButtonClickListener: View.OnClickListener?, val negativeButtonClickListener: View.OnClickListener?, ) : BottomSheetDialogFragment() { @@ -65,9 +67,15 @@ class BasicBottomSheet private constructor( peekHeight = 0 addBottomSheetCallback(bottomSheetCallback) } - binding.bottomSheetTitle.text = title + if (!title.isNullOrEmpty()) { + binding.bottomSheetTitle.isVisible = true + binding.bottomSheetTitle.text = title + } binding.bottomSheetMessage.text = message if (positiveButtonClickListener != null) { + positiveButtonLabel?.let { buttonLbl -> + binding.bottomSheetOkButton.text = buttonLbl + } binding.bottomSheetOkButton.isVisible = true binding.bottomSheetOkButton.setOnClickListener { positiveButtonClickListener.onClick(it) @@ -76,6 +84,9 @@ class BasicBottomSheet private constructor( } if (negativeButtonClickListener != null) { binding.bottomSheetCancelButton.isVisible = true + negativeButtonLabel?.let { buttonLbl -> + binding.bottomSheetCancelButton.text = buttonLbl + } binding.bottomSheetCancelButton.setOnClickListener { negativeButtonClickListener.onClick(it) dismiss() @@ -98,6 +109,8 @@ class BasicBottomSheet private constructor( private var title: String? = null private var message: String? = null + private var positiveButtonLabel: String? = null + private var negativeButtonLabel: String? = null private var positiveButtonClickListener: View.OnClickListener? = null private var negativeButtonClickListener: View.OnClickListener? = null @@ -121,22 +134,25 @@ class BasicBottomSheet private constructor( return this } - fun setPositiveButtonClickListener(listener: View.OnClickListener): Builder { + fun setPositiveButtonClickListener(buttonLabel: String? = null, listener: View.OnClickListener): Builder { this.positiveButtonClickListener = listener + this.positiveButtonLabel = buttonLabel return this } - fun setNegativeButtonClickListener(listener: View.OnClickListener): Builder { + fun setNegativeButtonClickListener(buttonLabel: String? = null, listener: View.OnClickListener): Builder { this.negativeButtonClickListener = listener + this.negativeButtonLabel = buttonLabel return this } fun build(): BasicBottomSheet { - require(title != null) { "Title needs to be set" } require(message != null) { "Message needs to be set" } return BasicBottomSheet( - title!!, + title, message!!, + positiveButtonLabel, + negativeButtonLabel, positiveButtonClickListener, negativeButtonClickListener ) diff --git a/app/src/main/res/layout/basic_bottom_sheet.xml b/app/src/main/res/layout/basic_bottom_sheet.xml index 9e05aca4..0617fbc3 100644 --- a/app/src/main/res/layout/basic_bottom_sheet.xml +++ b/app/src/main/res/layout/basic_bottom_sheet.xml @@ -17,6 +17,7 @@ android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" + android:visibility="gone" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:text="Bottom sheet title" /> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f8be3859..7f4c9205 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -329,8 +329,8 @@ <string name="connection_mode_openkeychain" translatable="false">OpenKeychain</string> <string name="git_server_config_save_success">Successfully saved configuration</string> <string name="git_server_config_save_error">The provided repository URL is not valid</string> - <string name="git_server_config_save_missing_username_https">Please specify the HTTPS username in the form https://username@example.com/…</string> - <string name="git_server_config_save_missing_username_ssh">Please specify the SSH username in the form username@example.com:…</string> + <string name="git_server_config_save_missing_username_https">Please specify the HTTPS username in the form https://username@example.com/username/…</string> + <string name="git_server_config_save_missing_username_ssh">Please specify the SSH username in the form username@example.com:username/…</string> <string name="git_server_config_save_auth_mode_mismatch">Valid authentication modes for %1$s: %2$s</string> <string name="git_operation_wrong_passphrase">Wrong passphrase</string> <string name="git_operation_wrong_password">Wrong password</string> |