-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Small layout changes, code deduplication
- Add extensions for duplicated code in dialogs with a text input. - Remove "Show password" checkbox, instead use the builtin password toggle icon. - Change IME action in import password dialog. - Remove deprecated LayoutInflater.fromContext. - Shorten encrypted export summary (the encryption algorithm doesn't matter much to the average user).
- Loading branch information
Showing
11 changed files
with
202 additions
and
148 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
app/src/main/kotlin/com/maltaisn/notes/DialogExtensions.kt
This file contains 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,75 @@ | ||
/* | ||
* Copyright 2023 Nicolas Maltais | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.maltaisn.notes | ||
|
||
import android.annotation.SuppressLint | ||
import android.app.Dialog | ||
import android.graphics.Rect | ||
import android.view.View | ||
import android.widget.FrameLayout | ||
import android.widget.TextView | ||
import androidx.annotation.StringRes | ||
import androidx.fragment.app.DialogFragment | ||
import com.google.android.material.dialog.MaterialAlertDialogBuilder | ||
|
||
/** | ||
* Set [this] dialog maximum width to [maxWidth]. | ||
* @param view The dialog's content view. | ||
*/ | ||
fun Dialog.setMaxWidth(maxWidth: Int, view: View) { | ||
// Get current dialog's width and padding | ||
val fgPadding = Rect() | ||
val window = this.window!! | ||
window.decorView.background.getPadding(fgPadding) | ||
val padding = fgPadding.left + fgPadding.right | ||
var width = this.context.resources.displayMetrics.widthPixels - padding | ||
|
||
// Set dialog's dimensions, with maximum width. | ||
if (width > maxWidth) { | ||
width = maxWidth | ||
} | ||
window.setLayout(width + padding, FrameLayout.LayoutParams.WRAP_CONTENT) | ||
view.layoutParams = FrameLayout.LayoutParams(width, FrameLayout.LayoutParams.WRAP_CONTENT) | ||
} | ||
|
||
/** | ||
* Set title on dialog, but only if the device vertical size is large enough. | ||
* Otherwise, it becomes much harder or even impossible to type text (see #53). | ||
*/ | ||
fun MaterialAlertDialogBuilder.setTitleIfEnoughSpace(@StringRes title: Int): MaterialAlertDialogBuilder { | ||
val dimen = context.resources.getDimension(R.dimen.input_dialog_title_min_height) / | ||
context.resources.displayMetrics.density | ||
if (context.resources.configuration.screenHeightDp >= dimen) { | ||
setTitle(title) | ||
} | ||
return this | ||
} | ||
|
||
/** | ||
* In dialogs with an EditText, the cursor must be hidden when dialog is dismissed to prevent memory leak. | ||
* See [https://stackoverflow.com/questions/36842805/dialogfragment-leaking-memory]. | ||
* This should be called in [DialogFragment.onDismiss]. | ||
*/ | ||
@SuppressLint("WrongConstant") | ||
fun DialogFragment.hideCursorInAllViews() { | ||
val view = dialog?.window?.decorView ?: return | ||
for (focusableView in view.getFocusables(View.FOCUSABLES_ALL)) { | ||
if (focusableView is TextView) { | ||
focusableView.isCursorVisible = false | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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.