Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement roles for volume buttons: do nothing, change text size, navigate sent history #554

Merged
merged 1 commit into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,8 @@ import com.ubergeek42.WeechatAndroid.upload.i
import com.ubergeek42.WeechatAndroid.upload.insertAddingSpacesAsNeeded
import com.ubergeek42.WeechatAndroid.upload.suppress
import com.ubergeek42.WeechatAndroid.upload.validateUploadConfig
import com.ubergeek42.WeechatAndroid.utils.*
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: not a fan of wildcard imports, would rather prefer smaller diff

import com.ubergeek42.WeechatAndroid.utils.Assert.assertThat
import com.ubergeek42.WeechatAndroid.utils.FriendlyExceptions
import com.ubergeek42.WeechatAndroid.utils.Toaster
import com.ubergeek42.WeechatAndroid.utils.Utils
import com.ubergeek42.WeechatAndroid.utils.afterTextChanged
import com.ubergeek42.WeechatAndroid.utils.equalsIgnoringUselessSpans
import com.ubergeek42.WeechatAndroid.utils.makeCopyWithoutUselessSpans
import com.ubergeek42.WeechatAndroid.utils.indexOfOrElse
import com.ubergeek42.WeechatAndroid.utils.ulet
import com.ubergeek42.WeechatAndroid.views.BufferFragmentFullScreenController
import com.ubergeek42.WeechatAndroid.views.OnBackGestureListener
import com.ubergeek42.WeechatAndroid.views.OnJumpedUpWhileScrollingListener
Expand Down Expand Up @@ -155,6 +148,7 @@ class BufferFragment : Fragment(), BufferEye {
savedInstanceState?.let {
restoreRecyclerViewState(it)
restoreSearchState(it)
restoreHistoryState(it)
}
}

Expand Down Expand Up @@ -287,6 +281,7 @@ class BufferFragment : Fragment(), BufferEye {
super.onSaveInstanceState(outState)
saveRecyclerViewState(outState)
saveSearchState(outState)
saveHistoryState(outState)
}

////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -521,11 +516,21 @@ class BufferFragment : Fragment(), BufferEye {
return@OnKeyListener true
}
KeyEvent.KEYCODE_VOLUME_DOWN, KeyEvent.KEYCODE_VOLUME_UP -> {
if (P.volumeBtnSize) {
val change = if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) 1f else -1f
val textSize = (P.textSize + change).coerceIn(5f, 30f)
P.setTextSizeColorAndLetterWidth(textSize)
return@OnKeyListener true
val up = keyCode == KeyEvent.KEYCODE_VOLUME_UP
when (P.volumeRole) {
P.VolumeRole.TEXT_SIZE -> {
val change = if (up) 1f else -1f
val textSize = (P.textSize + change).coerceIn(5f, 30f)
P.setTextSizeColorAndLetterWidth(textSize)
return@OnKeyListener true
}
P.VolumeRole.SEND_HISTORY -> {
ulet(ui?.chatInput) {
zopieux marked this conversation as resolved.
Show resolved Hide resolved
history.navigateOffset(it, if (up) 1 else -1)
}
return@OnKeyListener true
}
else -> {}
}
}
}
Expand Down Expand Up @@ -951,6 +956,20 @@ class BufferFragment : Fragment(), BufferEye {
}
}

////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////// history
////////////////////////////////////////////////////////////////////////////////////////////////

private val history = History()

private fun saveHistoryState(outState: Bundle) {
outState.putBundle(KEY_HISTORY, history.save())
}

private fun restoreHistoryState(savedInstanceState: Bundle) {
savedInstanceState.getBundle(KEY_HISTORY)?.let { history.restore(it) }
}

////////////////////////////////////////////////////////////////////////////////////////////////

private fun setPendingInputForParallelFragments() = ulet(buffer, ui) { buffer, ui ->
Expand Down Expand Up @@ -994,7 +1013,7 @@ private const val KEY_LAST_FOCUSED_MATCH = "lastFocusedMatch"
private const val KEY_REGEX = "regex"
private const val KEY_CASE_SENSITIVE = "caseSensitive"
private const val KEY_SOURCE = "source"

private const val KEY_HISTORY = "history"

private enum class ConnectivityState(
val displayBadge: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ public static void storeThemeOrColorSchemeColors(Context context) {
public static boolean notificationVibrate;
public static String notificationSound;

public static boolean showSend, showTab, showPaperclip, hotlistSync, volumeBtnSize;
public static boolean showSend, showTab, showPaperclip, hotlistSync;

public enum VolumeRole {NONE, TEXT_SIZE, SEND_HISTORY};
zopieux marked this conversation as resolved.
Show resolved Hide resolved
public static VolumeRole volumeRole;

public static boolean showBufferFilter;

Expand Down Expand Up @@ -167,7 +170,7 @@ public static void storeThemeOrColorSchemeColors(Context context) {
showTab = p.getBoolean(PREF_SHOW_TAB, PREF_SHOW_TAB_D);
showPaperclip = p.getBoolean(PREF_SHOW_PAPERCLIP, PREF_SHOW_PAPERCLIP_D);
hotlistSync = p.getBoolean(PREF_HOTLIST_SYNC, PREF_HOTLIST_SYNC_D);
volumeBtnSize = p.getBoolean(PREF_VOLUME_BTN_SIZE, PREF_VOLUME_BTN_SIZE_D);
volumeRole = VolumeRole.values()[Integer.parseInt(p.getString(PREF_VOLUME_ROLE, PREF_VOLUME_ROLE_D))];
zopieux marked this conversation as resolved.
Show resolved Hide resolved

// buffer list filter
showBufferFilter = p.getBoolean(PREF_SHOW_BUFFER_FILTER, PREF_SHOW_BUFFER_FILTER_D);
Expand Down Expand Up @@ -365,7 +368,7 @@ public static void loadServerKeyVerifier() {
case PREF_SHOW_TAB: showTab = p.getBoolean(key, PREF_SHOW_TAB_D); break;
case PREF_SHOW_PAPERCLIP: showPaperclip = p.getBoolean(key, PREF_SHOW_PAPERCLIP_D); break;
case PREF_HOTLIST_SYNC: hotlistSync = p.getBoolean(key, PREF_HOTLIST_SYNC_D); break;
case PREF_VOLUME_BTN_SIZE: volumeBtnSize = p.getBoolean(key, PREF_VOLUME_BTN_SIZE_D); break;
case PREF_VOLUME_ROLE: volumeRole = VolumeRole.values()[Integer.parseInt(p.getString(PREF_VOLUME_ROLE, PREF_VOLUME_ROLE_D))]; break;

// buffer list fragment
case PREF_SHOW_BUFFER_FILTER: showBufferFilter = p.getBoolean(key, PREF_SHOW_BUFFER_FILTER_D); break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.ubergeek42.WeechatAndroid.R;
import com.ubergeek42.WeechatAndroid.Weechat;
import com.ubergeek42.WeechatAndroid.service.P;

import java.util.Set;

Expand Down Expand Up @@ -123,8 +124,8 @@ public class Constants {
final public static boolean PREF_SHOW_SEND_D = true;
public final static String PREF_SHOW_TAB = "tabbtn_show";
final public static boolean PREF_SHOW_TAB_D = true;
public final static String PREF_VOLUME_BTN_SIZE = "volumebtn_size";
final public static boolean PREF_VOLUME_BTN_SIZE_D = true;
public final static String PREF_VOLUME_ROLE = "buttons__volume";
final public static String PREF_VOLUME_ROLE_D = String.valueOf(P.VolumeRole.TEXT_SIZE.ordinal());

final public static String PREF_SHOW_PAPERCLIP = "buttons__show_paperclip";
final public static boolean PREF_SHOW_PAPERCLIP_D = true;
Expand Down Expand Up @@ -256,5 +257,6 @@ static public class Deprecated {
final static public String PREF_SSH_KEY = "ssh_key"; final public static String PREF_SSH_KEY_D = null;
final static public String PREF_SSH_KEY_PASSPHRASE = "ssh_key_passphrase"; final public static String PREF_SSH_KEY_PASSPHRASE_D = null;
final static public String PREF_SSH_KNOWN_HOSTS = "ssh_known_hosts"; final public static String PREF_SSH_KNOWN_HOSTS_D = "";
final static public String PREF_VOLUME_BTN_SIZE = "volumebtn_size"; final public static boolean PREF_VOLUME_BTN_SIZE_D = true;
}
}
54 changes: 54 additions & 0 deletions app/src/main/java/com/ubergeek42/WeechatAndroid/utils/History.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.ubergeek42.WeechatAndroid.utils

import android.os.Bundle
import android.text.Editable
import android.widget.EditText
import androidx.core.os.bundleOf
import com.ubergeek42.WeechatAndroid.service.P

class History {
private var index = -1
zopieux marked this conversation as resolved.
Show resolved Hide resolved
private var userInput: Editable? = null
private var selectionStart: Int? = null
private var selectionEnd: Int? = null

fun navigateOffset(editText: EditText, offset: Int) {
zopieux marked this conversation as resolved.
Show resolved Hide resolved
if (index == -1) {
userInput = editText.text
selectionStart = editText.selectionStart
selectionEnd = editText.selectionEnd
}
val newIndex = index + offset
messageAt(newIndex)?.let {
editText.text = it
editText.post {
zopieux marked this conversation as resolved.
Show resolved Hide resolved
if (newIndex == -1) {
// Restore user selection.
editText.setSelection(selectionStart ?: 0, selectionEnd ?: editText.length())
zopieux marked this conversation as resolved.
Show resolved Hide resolved
} else {
// Go to end for sent messages.
editText.setSelection(editText.length())
}
}
index = newIndex
}
}

fun save(): Bundle = bundleOf(
Pair("index", index),
Pair("userInput", userInput),
Pair("selStart", selectionStart),
Pair("selEnd", selectionEnd),
)

fun restore(bundle: Bundle) {
index = bundle.getInt("index", -1)
userInput = bundle.get("userInput") as Editable?
selectionStart = bundle.getInt("selStart", -1).let { if (it == -1) null else it }
selectionEnd = bundle.getInt("selEnd", -1).let { if (it == -1) null else it }
}

private fun messageAt(index: Int): Editable? =
if (index == -1) userInput else P.sentMessages.getOrNull(P.sentMessages.size - 1 - index)
?.let { Editable.Factory.getInstance().newEditable(it) }
zopieux marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.ubergeek42.WeechatAndroid.utils

import android.content.Context
import androidx.core.content.edit
import androidx.preference.PreferenceManager
import androidx.preference.PrivateKeyPickerPreference
import com.ubergeek42.WeechatAndroid.service.P.VolumeRole
import com.ubergeek42.WeechatAndroid.upload.applicationContext
import com.ubergeek42.WeechatAndroid.utils.AndroidKeyStoreUtils.InsideSecurityHardware
import com.ubergeek42.cats.Kitty
import com.ubergeek42.cats.Root
import com.ubergeek42.weechat.relay.connection.SSHConnection
import java.util.*


class MigratePreferences(val context: Context) {
Expand Down Expand Up @@ -177,6 +178,14 @@ class MigratePreferences(val context: Context) {
"you may have to import them in settings.")
}
}

add(5, 6) {
val volumeChangesSize = preferences.getBoolean(Constants.Deprecated.PREF_VOLUME_BTN_SIZE, Constants.Deprecated.PREF_VOLUME_BTN_SIZE_D)
preferences.edit {
this.remove(Constants.Deprecated.PREF_VOLUME_BTN_SIZE)
this.putString(Constants.PREF_VOLUME_ROLE, (if (volumeChangesSize) VolumeRole.TEXT_SIZE else VolumeRole.NONE).ordinal.toString(10))
zopieux marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}

Expand Down
8 changes: 3 additions & 5 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -703,11 +703,9 @@
wenn der Büroklammer-Taste versteckt wurde,
um mehr Platz für das Eingabefeld zu schaffen.</string>

<string name="pref__buttons__volume_buttons_change_text_size">
Lautstärketasten ändern die Textgröße</string>
<string name="pref__buttons__volume_buttons_change_text_size_summary">
Wenn diese Option aktiviert ist,
ändern die Lautstärketasten die Textgröße, anstelle der Lautstärke</string>
<string name="pref_buttons_volume__actions__none">Nichts tun</string>
<string name="pref_buttons_volume__actions__text_size">Lautstärketasten ändern die Textgröße</string>
<string name="pref_buttons_volume__actions__history">Navigieren im Sendeverlauf</string>
Comment on lines +706 to +708
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Machine-translated, probably wrong.


<!-- ####################################################################################### -->
<!-- #################################### notifications #################################### -->
Expand Down
9 changes: 3 additions & 6 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -691,12 +691,9 @@
Quand le bouton «&#8239;trombone&#8239;» devient invisible pour laisser de la place au champ de saisie,
il reste possible de joindre des fichiers via le menu déroulant.</string>

<string name="pref__buttons__volume_buttons_change_text_size">
Taille du texte via le volume</string>
<string name="pref__buttons__volume_buttons_change_text_size_summary">
Si coché, les boutons du volume audio changent la taille du texte
lorsque Weechat-Android est au premier plan</string>

<string name="pref_buttons_volume__actions__none">Ne rien faire</string>
<string name="pref_buttons_volume__actions__text_size">Changer la taille du texte</string>
<string name="pref_buttons_volume__actions__history">Naviguer dans les messages envoyés</string>
<!-- ####################################################################################### -->
<!-- #################################### notifications #################################### -->
<!-- ####################################################################################### -->
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -699,10 +699,9 @@
Когда кнопка «Скрепка» скрыта, чтобы оставить больше места для поля ввода,
выбирать файлы можно через меню.</string>

<string name="pref__buttons__volume_buttons_change_text_size">
Кнопки громкости меняют размер текста</string>
<string name="pref__buttons__volume_buttons_change_text_size_summary">
Если настройка включена, кнопки громкости будут менять размер текста вместо громкости</string>
<string name="pref_buttons_volume__actions__none">Ничего не делать</string>
<string name="pref_buttons_volume__actions__text_size">Кнопки громкости меняют размер текста</string>
<string name="pref_buttons_volume__actions__history">Навигация по истории отправлений</string>
Comment on lines +702 to +704
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Machine-translated, probably wrong.


<!-- ####################################################################################### -->
<!-- #################################### notifications #################################### -->
Expand Down
14 changes: 10 additions & 4 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -714,10 +714,16 @@
When the paperclip button gets hidden to provide more space for the input field,
you can still attach files via overflow menu.</string>

<string name="pref__buttons__volume_buttons_change_text_size">
Volume buttons change text size</string>
<string name="pref__buttons__volume_buttons_change_text_size_summary">
If set, volume buttons will change text size instead of volume</string>
<string name="pref__buttons__volume__title">Volume button role</string>
zopieux marked this conversation as resolved.
Show resolved Hide resolved
<string-array name="pref__buttons__volume__entries">
<item>@string/pref_buttons_volume__actions__none</item>
<item>@string/pref_buttons_volume__actions__text_size</item>
<item>@string/pref_buttons_volume__actions__history</item>
zopieux marked this conversation as resolved.
Show resolved Hide resolved
</string-array>

<string name="pref_buttons_volume__actions__none">Do nothing</string>
<string name="pref_buttons_volume__actions__text_size">Change text size</string>
<string name="pref_buttons_volume__actions__history">Navigate sent history</string>

<!-- ####################################################################################### -->
<!-- #################################### notifications #################################### -->
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values/values.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
<item>4</item>
</string-array>

<string-array name="pref__buttons__volume__values">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>

<string-array name="pref__buttons__paperclip__action_1_values">
<item>content_images</item>
<item>content_media</item>
Expand Down
12 changes: 7 additions & 5 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,13 @@
android:persistent="false"
android:selectable="false"
android:dependency="buttons__show_paperclip" />
<CheckBoxPreference
android:key="volumebtn_size"
android:title="@string/pref__buttons__volume_buttons_change_text_size"
android:summary="@string/pref__buttons__volume_buttons_change_text_size_summary"
android:defaultValue="true" />
<ListPreference
android:key="buttons__volume"
android:title="@string/pref__buttons__volume__title"
android:entries="@array/pref__buttons__volume__entries"
android:entryValues="@array/pref__buttons__volume__values"
android:defaultValue="1"
app:useSimpleSummaryProvider="true" />
</PreferenceScreen>

<!-- notifications -->
Expand Down