Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 3981638

Browse files
authored
Use BottomSheet in place of Snackbar for longer messages (#1157)
1 parent 6c1e41b commit 3981638

File tree

6 files changed

+54
-24
lines changed

6 files changed

+54
-24
lines changed

app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ import androidx.recyclerview.widget.LinearLayoutManager
2424
import com.github.michaelbull.result.fold
2525
import com.github.michaelbull.result.runCatching
2626
import com.github.michaelbull.result.onFailure
27-
import com.google.android.material.snackbar.Snackbar
2827
import com.zeapo.pwdstore.databinding.PasswordRecyclerViewBinding
2928
import com.zeapo.pwdstore.git.BaseGitActivity
3029
import com.zeapo.pwdstore.git.GitServerConfigActivity
3130
import com.zeapo.pwdstore.git.config.AuthMode
3231
import com.zeapo.pwdstore.git.config.GitSettings
3332
import com.zeapo.pwdstore.ui.OnOffItemAnimator
3433
import com.zeapo.pwdstore.ui.adapters.PasswordItemRecyclerAdapter
34+
import com.zeapo.pwdstore.ui.dialogs.BasicBottomSheet
3535
import com.zeapo.pwdstore.ui.dialogs.ItemCreationBottomSheet
3636
import com.zeapo.pwdstore.utils.PasswordItem
3737
import com.zeapo.pwdstore.utils.PasswordRepository
@@ -87,11 +87,13 @@ class PasswordFragment : Fragment(R.layout.password_recycler_view) {
8787
requireStore().refreshPasswordList()
8888
binding.swipeRefresher.isRefreshing = false
8989
} else if (!PasswordRepository.isGitRepo()) {
90-
Snackbar.make(binding.root, getString(R.string.clone_git_repo), Snackbar.LENGTH_INDEFINITE)
91-
.setAction(R.string.clone_button) {
90+
BasicBottomSheet.Builder(requireContext())
91+
.setMessageRes(R.string.clone_git_repo)
92+
.setPositiveButtonClickListener(getString(R.string.clone_button)) {
9293
swipeResult.launch(GitServerConfigActivity.createCloneIntent(requireContext()))
9394
}
94-
.show()
95+
.build()
96+
.show(requireActivity().supportFragmentManager, "NOT_A_GIT_REPO")
9597
binding.swipeRefresher.isRefreshing = false
9698
} else {
9799
// When authentication is set to AuthMode.None then the only git operation we can

app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import com.zeapo.pwdstore.crypto.PasswordCreationActivity
4646
import com.zeapo.pwdstore.git.BaseGitActivity
4747
import com.zeapo.pwdstore.git.config.AuthMode
4848
import com.zeapo.pwdstore.git.config.GitSettings
49+
import com.zeapo.pwdstore.ui.dialogs.BasicBottomSheet
4950
import com.zeapo.pwdstore.ui.dialogs.FolderCreationDialogFragment
5051
import com.zeapo.pwdstore.ui.onboarding.activity.OnboardingActivity
5152
import com.zeapo.pwdstore.utils.PasswordItem
@@ -373,17 +374,13 @@ class PasswordStore : BaseGitActivity() {
373374
*/
374375
private fun hasRequiredStoragePermissions(): Boolean {
375376
return if (!isPermissionGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
376-
Snackbar.make(
377-
findViewById(R.id.main_layout),
378-
getString(R.string.access_sdcard_text),
379-
Snackbar.LENGTH_INDEFINITE
380-
).run {
381-
setAction(getString(R.string.snackbar_action_grant)) {
377+
BasicBottomSheet.Builder(this)
378+
.setMessageRes(R.string.access_sdcard_text)
379+
.setPositiveButtonClickListener(getString(R.string.snackbar_action_grant)) {
382380
storagePermissionRequest.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE)
383-
dismiss()
384381
}
385-
show()
386-
}
382+
.build()
383+
.show(supportFragmentManager, "STORAGE_PERMISSION_MISSING")
387384
false
388385
} else {
389386
checkLocalRepository()

app/src/main/java/com/zeapo/pwdstore/git/GitServerConfigActivity.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,25 @@ class GitServerConfigActivity : BaseGitActivity() {
115115
GitSettings.UpdateConnectionSettingsResult.FailedToParseUrl -> {
116116
Snackbar.make(binding.root, getString(R.string.git_server_config_save_error), Snackbar.LENGTH_LONG).show()
117117
}
118+
118119
is GitSettings.UpdateConnectionSettingsResult.MissingUsername -> {
119120
when (updateResult.newProtocol) {
120-
Protocol.Https -> Snackbar.make(binding.root, getString(R.string.git_server_config_save_missing_username_https), Snackbar.LENGTH_LONG).show()
121-
Protocol.Ssh -> Snackbar.make(binding.root, getString(R.string.git_server_config_save_missing_username_ssh), Snackbar.LENGTH_LONG).show()
121+
Protocol.Https ->
122+
BasicBottomSheet.Builder(this)
123+
.setTitleRes(R.string.ssh_scheme_needed_title)
124+
.setMessageRes(R.string.git_server_config_save_missing_username_https)
125+
.setPositiveButtonClickListener {
126+
}
127+
.build()
128+
.show(supportFragmentManager, "HTTPS_MISSING_USERNAME")
129+
Protocol.Ssh ->
130+
BasicBottomSheet.Builder(this)
131+
.setTitleRes(R.string.ssh_scheme_needed_title)
132+
.setMessageRes(R.string.git_server_config_save_missing_username_ssh)
133+
.setPositiveButtonClickListener {
134+
}
135+
.build()
136+
.show(supportFragmentManager, "SSH_MISSING_USERNAME")
122137
}
123138
}
124139
GitSettings.UpdateConnectionSettingsResult.Valid -> {
@@ -201,7 +216,6 @@ class GitServerConfigActivity : BaseGitActivity() {
201216
// Silently delete & replace the lone .git folder if it exists
202217
if (localDir.exists() && localDirFiles.size == 1 && localDirFiles[0].name == ".git") {
203218
localDir.deleteRecursively()
204-
205219
}
206220
}.onFailure { e ->
207221
e(e)

app/src/main/java/com/zeapo/pwdstore/ui/dialogs/BasicBottomSheet.kt

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ import com.zeapo.pwdstore.utils.viewBinding
2828
* API through [Builder] to create a similar UI, just at the bottom of the screen.
2929
*/
3030
class BasicBottomSheet private constructor(
31-
val title: String,
31+
val title: String?,
3232
val message: String,
33+
val positiveButtonLabel: String?,
34+
val negativeButtonLabel: String?,
3335
val positiveButtonClickListener: View.OnClickListener?,
3436
val negativeButtonClickListener: View.OnClickListener?,
3537
) : BottomSheetDialogFragment() {
@@ -65,9 +67,15 @@ class BasicBottomSheet private constructor(
6567
peekHeight = 0
6668
addBottomSheetCallback(bottomSheetCallback)
6769
}
68-
binding.bottomSheetTitle.text = title
70+
if (!title.isNullOrEmpty()) {
71+
binding.bottomSheetTitle.isVisible = true
72+
binding.bottomSheetTitle.text = title
73+
}
6974
binding.bottomSheetMessage.text = message
7075
if (positiveButtonClickListener != null) {
76+
positiveButtonLabel?.let { buttonLbl ->
77+
binding.bottomSheetOkButton.text = buttonLbl
78+
}
7179
binding.bottomSheetOkButton.isVisible = true
7280
binding.bottomSheetOkButton.setOnClickListener {
7381
positiveButtonClickListener.onClick(it)
@@ -76,6 +84,9 @@ class BasicBottomSheet private constructor(
7684
}
7785
if (negativeButtonClickListener != null) {
7886
binding.bottomSheetCancelButton.isVisible = true
87+
negativeButtonLabel?.let { buttonLbl ->
88+
binding.bottomSheetCancelButton.text = buttonLbl
89+
}
7990
binding.bottomSheetCancelButton.setOnClickListener {
8091
negativeButtonClickListener.onClick(it)
8192
dismiss()
@@ -98,6 +109,8 @@ class BasicBottomSheet private constructor(
98109

99110
private var title: String? = null
100111
private var message: String? = null
112+
private var positiveButtonLabel: String? = null
113+
private var negativeButtonLabel: String? = null
101114
private var positiveButtonClickListener: View.OnClickListener? = null
102115
private var negativeButtonClickListener: View.OnClickListener? = null
103116

@@ -121,22 +134,25 @@ class BasicBottomSheet private constructor(
121134
return this
122135
}
123136

124-
fun setPositiveButtonClickListener(listener: View.OnClickListener): Builder {
137+
fun setPositiveButtonClickListener(buttonLabel: String? = null, listener: View.OnClickListener): Builder {
125138
this.positiveButtonClickListener = listener
139+
this.positiveButtonLabel = buttonLabel
126140
return this
127141
}
128142

129-
fun setNegativeButtonClickListener(listener: View.OnClickListener): Builder {
143+
fun setNegativeButtonClickListener(buttonLabel: String? = null, listener: View.OnClickListener): Builder {
130144
this.negativeButtonClickListener = listener
145+
this.negativeButtonLabel = buttonLabel
131146
return this
132147
}
133148

134149
fun build(): BasicBottomSheet {
135-
require(title != null) { "Title needs to be set" }
136150
require(message != null) { "Message needs to be set" }
137151
return BasicBottomSheet(
138-
title!!,
152+
title,
139153
message!!,
154+
positiveButtonLabel,
155+
negativeButtonLabel,
140156
positiveButtonClickListener,
141157
negativeButtonClickListener
142158
)

app/src/main/res/layout/basic_bottom_sheet.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
android:layout_height="wrap_content"
1818
android:layout_marginStart="8dp"
1919
android:layout_marginTop="8dp"
20+
android:visibility="gone"
2021
app:layout_constraintStart_toStartOf="parent"
2122
app:layout_constraintTop_toTopOf="parent"
2223
tools:text="Bottom sheet title" />

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,8 @@
329329
<string name="connection_mode_openkeychain" translatable="false">OpenKeychain</string>
330330
<string name="git_server_config_save_success">Successfully saved configuration</string>
331331
<string name="git_server_config_save_error">The provided repository URL is not valid</string>
332-
<string name="git_server_config_save_missing_username_https">Please specify the HTTPS username in the form https://[email protected]/…</string>
333-
<string name="git_server_config_save_missing_username_ssh">Please specify the SSH username in the form [email protected]:…</string>
332+
<string name="git_server_config_save_missing_username_https">Please specify the HTTPS username in the form https://[email protected]/username/…</string>
333+
<string name="git_server_config_save_missing_username_ssh">Please specify the SSH username in the form [email protected]:username/…</string>
334334
<string name="git_server_config_save_auth_mode_mismatch">Valid authentication modes for %1$s: %2$s</string>
335335
<string name="git_operation_wrong_passphrase">Wrong passphrase</string>
336336
<string name="git_operation_wrong_password">Wrong password</string>

0 commit comments

Comments
 (0)