Skip to content

Commit

Permalink
Recorder: Rework edit name dialog
Browse files Browse the repository at this point in the history
Change-Id: I13a9e87b0c0ca804f6125515189337e673db1994
  • Loading branch information
luca020400 authored and SebaUbuntu committed Sep 9, 2024
1 parent 029fc80 commit 73e00e2
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 38 deletions.
66 changes: 48 additions & 18 deletions app/src/main/java/org/lineageos/recorder/ListActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import android.view.ActionMode
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.ProgressBar
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.view.ViewCompat
Expand All @@ -26,6 +29,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.AdapterDataObserver
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import org.lineageos.recorder.ext.scheduleShowSoftInput
import org.lineageos.recorder.list.ListActionModeCallback
import org.lineageos.recorder.list.RecordingData
import org.lineageos.recorder.list.RecordingListCallbacks
Expand All @@ -48,6 +52,9 @@ class ListActivity : AppCompatActivity(), RecordingListCallbacks {
private val listRecyclerView by lazy { findViewById<RecyclerView>(R.id.listRecyclerView) }
private val toolbar by lazy { findViewById<Toolbar>(R.id.toolbar) }

// System services
private val inputMethodManager by lazy { getSystemService(InputMethodManager::class.java) }

// Adapters
private val adapter by lazy {
RecordingsAdapter(this)
Expand Down Expand Up @@ -127,31 +134,54 @@ class ListActivity : AppCompatActivity(), RecordingListCallbacks {
}

override fun onRename(index: Int, uri: Uri, currentName: String) {
val view = layoutInflater.inflate(R.layout.dialog_content_rename, null)

val editText = view.findViewById<EditText>(R.id.nameEditText)
editText.setText(currentName)
editText.requestFocus()
Utils.showKeyboard(this)
lateinit var alertDialog: AlertDialog
lateinit var editText: EditText

MaterialAlertDialogBuilder(this)
.setTitle(R.string.list_edit_title)
.setView(view)
.setPositiveButton(R.string.list_edit_confirm) { _: DialogInterface?, _: Int ->
val editable = editText.text ?: return@setPositiveButton
if (editable.isEmpty()) {
return@setPositiveButton
}
val onConfirm = {
editText.text?.takeIf { it.isNotEmpty() }?.let { editable ->
val newTitle = editable.toString()
if (newTitle != currentName) {
renameRecording(uri, newTitle, index)
}
Utils.closeKeyboard(this)
}
.setNegativeButton(R.string.cancel) { _: DialogInterface?, _: Int ->
Utils.closeKeyboard(this)

true
} ?: false
}

val view = layoutInflater.inflate(
R.layout.dialog_content_rename,
null,
false
)
editText = view.findViewById<EditText>(R.id.nameEditText).apply {
setText(currentName)
setSelection(0, currentName.length)
setOnEditorActionListener { _, actionId, _ ->
when (actionId) {
EditorInfo.IME_ACTION_UNSPECIFIED,
EditorInfo.IME_ACTION_DONE -> {
onConfirm().also {
if (it) {
alertDialog.dismiss()
}
}
}

else -> false
}
}
}

alertDialog = MaterialAlertDialogBuilder(this)
.setTitle(R.string.list_edit_title)
.setView(view)
.setPositiveButton(R.string.list_edit_confirm) { _, _ -> onConfirm() }
.setNegativeButton(R.string.cancel, null)
.show()
.also {
editText.requestFocus()
inputMethodManager.scheduleShowSoftInput(editText, 0)
}
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
Expand Down
44 changes: 44 additions & 0 deletions app/src/main/java/org/lineageos/recorder/ext/InputMethodManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* SPDX-FileCopyrightText: 2024 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/

package org.lineageos.recorder.ext

import android.view.View
import android.view.inputmethod.InputMethodManager

private const val SHOW_REQUEST_TIMEOUT = 1000

private fun InputMethodManager.scheduleShowSoftInput(
view: View,
flags: Int,
runnable: Runnable,
showRequestTime: Long,
) {
if (!view.hasFocus()
|| (showRequestTime + SHOW_REQUEST_TIMEOUT) <= System.currentTimeMillis()
) {
return
}

if (showSoftInput(view, flags)) {
return
} else {
view.removeCallbacks(runnable)
view.postDelayed(runnable, 50)
}
}

/**
* @see InputMethodManager.showSoftInput
*/
fun InputMethodManager.scheduleShowSoftInput(view: View, flags: Int) {
val runnable = object : Runnable {
override fun run() {
scheduleShowSoftInput(view, flags, this, System.currentTimeMillis())
}
}

runnable.run()
}
15 changes: 0 additions & 15 deletions app/src/main/java/org/lineageos/recorder/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,9 @@ package org.lineageos.recorder.utils

import android.app.NotificationManager
import android.content.Context
import android.view.inputmethod.InputMethodManager
import org.lineageos.recorder.service.SoundRecorderService

object Utils {
fun showKeyboard(context: Context) {
val inputMethodManager = context.getSystemService(
InputMethodManager::class.java
)
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
}

fun closeKeyboard(context: Context) {
val inputMethodManager = context.getSystemService(
InputMethodManager::class.java
)
inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}

fun cancelShareNotification(context: Context) {
val nm = context.getSystemService(
NotificationManager::class.java
Expand Down
9 changes: 4 additions & 5 deletions app/src/main/res/layout/dialog_content_rename.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
SPDX-License-Identifier: Apache-2.0
-->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingHorizontal="?dialogPreferredPadding">
android:paddingHorizontal="?attr/dialogPreferredPadding">

<androidx.appcompat.widget.AppCompatEditText
<EditText
android:id="@+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:importantForAutofill="no"
android:maxLines="1"
android:singleLine="true"
tools:text="Name" />
android:singleLine="true" />

</FrameLayout>
8 changes: 8 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
<item name="android:windowLightNavigationBar">?attr/isLightTheme</item>
<item name="android:windowLightStatusBar">?attr/isLightTheme</item>
<item name="alertDialogTheme">@style/Theme.Recorder.AlertDialog</item>
<item name="materialAlertDialogTheme">@style/Theme.Recorder.AlertDialog</item>
<item name="windowActionModeOverlay">true</item>
</style>

Expand Down Expand Up @@ -52,4 +54,10 @@
<item name="android:iconTint">?attr/colorOnSurface</item>
<item name="iconTint">?attr/colorOnSurface</item>
</style>

<!-- Alert dialog theme. -->
<style name="Theme.Recorder.AlertDialog" parent="ThemeOverlay.Material3.MaterialAlertDialog">
<item name="android:colorBackground">?attr/colorSurface</item>
<item name="dialogCornerRadius">16dp</item>
</style>
</resources>

0 comments on commit 73e00e2

Please sign in to comment.