Skip to content

Commit

Permalink
refactor(data): completely remove methods/variables should can only h…
Browse files Browse the repository at this point in the history
…andle by AppPrefs from Config
  • Loading branch information
WhiredPlanck committed Nov 11, 2022
1 parent da4f6ab commit 1efe8ab
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 47 deletions.
6 changes: 3 additions & 3 deletions app/src/main/java/com/osfans/trime/data/AppPrefs.kt
Expand Up @@ -14,7 +14,7 @@ import java.lang.ref.WeakReference
* Helper class for an organized access to the shared preferences.
*/
class AppPrefs(
private val shared : SharedPreferences
private val shared: SharedPreferences
) {
private val applicationContext: WeakReference<Context> = WeakReference(appContext)

Expand Down Expand Up @@ -257,10 +257,10 @@ class AppPrefs(
get() = prefs.getPref(SWIPE_TIME_HI, 80)
private set
var longPressTimeout: Int = 0
get() = prefs.getPref(LONG_PRESS_TIMEOUT, 20)
get() = prefs.getPref(LONG_PRESS_TIMEOUT, 400)
private set
var repeatInterval: Int = 0
get() = prefs.getPref(REPEAT_INTERVAL, 4)
get() = prefs.getPref(REPEAT_INTERVAL, 50)
private set
var deleteCandidateTimeout: Int = 0
get() = prefs.getPref(DELETE_CANDIDATE_TIMEOUT, 2000)
Expand Down
20 changes: 0 additions & 20 deletions app/src/main/java/com/osfans/trime/data/Config.java
Expand Up @@ -759,26 +759,6 @@ public PositionType getWinPos() {
return PositionType.Companion.fromString(getString("layout/position"));
}

public int getLongTimeout() {
int progress = appPrefs.getKeyboard().getLongPressTimeout();
if (progress > 60) progress = 60;
return progress * 10 + 100;
}

public int getRepeatInterval() {
int progress = appPrefs.getKeyboard().getRepeatInterval();
if (progress > 9) progress = 9;
return progress * 10 + 10;
}

public static int getDeleteCandidateTimeout() {
return appPrefs.getKeyboard().getDeleteCandidateTimeout();
}

public static boolean getShouldLongClickDeleteCandidate() {
return appPrefs.getKeyboard().getShouldLongClickDeleteCandidate();
}

public int getLiquidPixel(String key) {
if (liquidKeyboard != null) {
if (liquidKeyboard.containsKey(key)) {
Expand Down
27 changes: 9 additions & 18 deletions app/src/main/java/com/osfans/trime/ime/keyboard/KeyboardView.java
Expand Up @@ -38,7 +38,6 @@
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.PopupWindow;
Expand Down Expand Up @@ -202,17 +201,12 @@ public interface OnKeyboardActionListener {
private int mComboCount = 0;
private boolean mComboMode = false;

private static int REPEAT_INTERVAL = 50; // ~20 keys per second
private static int REPEAT_START_DELAY = 400;
private static int LONG_PRESS_TIMEOUT = ViewConfiguration.getLongPressTimeout();

private static final int MAX_NEARBY_KEYS = 12;
private final int[] mDistances = new int[MAX_NEARBY_KEYS];

// For multi-tap
private int mLastSentIndex;
private long mLastTapTime;
private static int MULTI_TAP_INTERVAL = 800; // milliseconds
private final StringBuilder mPreviewLabel = new StringBuilder(1);

/** Whether the keyboard bitmap needs to be redrawn before it's blitted. * */
Expand Down Expand Up @@ -298,8 +292,8 @@ public void setEnterLabel(int action, CharSequence actionLabel) {
}

@NonNull
private AppPrefs getPrefs() {
return AppPrefs.Companion.defaultInstance();
private static AppPrefs getPrefs() {
return AppPrefs.defaultInstance();
}

private final MyHandler mHandler = new MyHandler(this);
Expand All @@ -324,7 +318,7 @@ public void handleMessage(Message msg) {
case MSG_REPEAT:
if (mKeyboardView.repeatKey()) {
Message repeat = Message.obtain(this, MSG_REPEAT);
sendMessageDelayed(repeat, REPEAT_INTERVAL);
sendMessageDelayed(repeat, getPrefs().getKeyboard().getRepeatInterval());
}
break;
case MSG_LONGPRESS:
Expand Down Expand Up @@ -403,11 +397,6 @@ public void reset() {
mPaintSymbol.setTextSize(mSymbolSize);
mPreviewText.setTypeface(config.getFont("preview_font"));

REPEAT_INTERVAL = config.getRepeatInterval();
REPEAT_START_DELAY = config.getLongTimeout() + 1;
LONG_PRESS_TIMEOUT = config.getLongTimeout();
MULTI_TAP_INTERVAL = config.getLongTimeout();

mEnterLabels = config.getmEnterLabels();
enterLabelMode = config.getInt("enter_label_mode");
invalidateAllKeys();
Expand Down Expand Up @@ -1576,7 +1565,8 @@ else if (action == MotionEvent.ACTION_UP)
if (mCurrentKey >= 0 && mKeys[mCurrentKey].getClick().isRepeatable()) {
mRepeatKeyIndex = mCurrentKey;
final Message msg = mHandler.obtainMessage(MSG_REPEAT);
mHandler.sendMessageDelayed(msg, REPEAT_START_DELAY);
final int repeatStartDelay = getPrefs().getKeyboard().getLongPressTimeout() + 1;
mHandler.sendMessageDelayed(msg, repeatStartDelay);
// Delivering the key could have caused an abort
if (mAbortKey) {
mRepeatKeyIndex = NOT_A_KEY;
Expand All @@ -1585,7 +1575,7 @@ else if (action == MotionEvent.ACTION_UP)
}
if (mCurrentKey != NOT_A_KEY) {
final Message msg = mHandler.obtainMessage(MSG_LONGPRESS, me);
mHandler.sendMessageDelayed(msg, LONG_PRESS_TIMEOUT);
mHandler.sendMessageDelayed(msg, getPrefs().getKeyboard().getLongPressTimeout());
}
showPreview(keyIndex, 0);
break;
Expand Down Expand Up @@ -1617,7 +1607,7 @@ else if (action == MotionEvent.ACTION_UP)
// Start new long press if key has changed
if (keyIndex != NOT_A_KEY) {
final Message msg = mHandler.obtainMessage(MSG_LONGPRESS, me);
mHandler.sendMessageDelayed(msg, LONG_PRESS_TIMEOUT);
mHandler.sendMessageDelayed(msg, getPrefs().getKeyboard().getLongPressTimeout());
}
}
showPreview(mCurrentKey);
Expand Down Expand Up @@ -1768,7 +1758,8 @@ private void resetMultiTap() {
private void checkMultiTap(long eventTime, int keyIndex) {
if (keyIndex == NOT_A_KEY) return;
// final Key key = mKeys[keyIndex];
if (eventTime > mLastTapTime + MULTI_TAP_INTERVAL || keyIndex != mLastSentIndex) {
final int multiTabInterval = getPrefs().getKeyboard().getLongPressTimeout();
if (eventTime > mLastTapTime + multiTabInterval || keyIndex != mLastSentIndex) {
resetMultiTap();
}
}
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/java/com/osfans/trime/ime/text/Candidate.java
Expand Up @@ -80,6 +80,11 @@ public interface EventListener {
private int candidateViewHeight, commentHeight, candidateSpacing, candidatePadding;
private boolean shouldShowComment = true, isCommentOnTop, candidateUseCursor;

@NonNull
private AppPrefs getAppPrefs() {
return AppPrefs.defaultInstance();
}

public void reset() {
Config config = Config.get();
candidateHighlight = new PaintDrawable(config.getColor("hilited_candidate_back_color"));
Expand Down Expand Up @@ -179,7 +184,7 @@ private void onCandidateClick(int index, boolean isLongClick) {
if (candidate != null) {
if (candidate instanceof ComputedCandidate.Word) {
if (listener.get() != null) {
if (isLongClick && Config.getShouldLongClickDeleteCandidate()) {
if (isLongClick && getAppPrefs().getKeyboard().getShouldLongClickDeleteCandidate()) {
listener.get().onCandidateLongClicked(index + startNum);
} else {
listener.get().onCandidatePressed(index + startNum);
Expand Down Expand Up @@ -393,7 +398,7 @@ public boolean onTouchEvent(@NonNull MotionEvent me) {
long durationMs = timeMove - timeDown;
setPressed(false);
if (me.getActionMasked() == MotionEvent.ACTION_UP) {
onCandidateClick(highlightIndex, durationMs >= Config.getDeleteCandidateTimeout());
onCandidateClick(highlightIndex, durationMs >= getAppPrefs().getKeyboard().getDeleteCandidateTimeout());
}
highlightIndex = -1;
invalidate();
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/res/xml/keyboard_preference.xml
Expand Up @@ -258,19 +258,19 @@
app:unit="@string/unit__time_ms"
app:useSimpleSummaryProvider="true" />
<com.osfans.trime.ui.components.DialogSeekBarPreference
android:defaultValue="20"
android:defaultValue="400"
android:key="keyboard__key_long_press_timeout"
android:title="@string/keyboard__long_press_timeout_title"
android:widgetLayout="@layout/seek_bar_dialog"
app:allowDividerAbove="false"
app:iconSpaceReserved="false"
app:max="60"
app:min="10"
app:max="800"
app:min="100"
app:seekBarIncrement="10"
app:unit="@string/unit__time_ms"
app:useSimpleSummaryProvider="true" />
<com.osfans.trime.ui.components.DialogSeekBarPreference
android:defaultValue="40"
android:defaultValue="30"
android:key="keyboard__key_repeat_interval"
android:title="@string/keyboard__key_repeat_interval_title"
app:allowDividerAbove="false"
Expand Down

0 comments on commit 1efe8ab

Please sign in to comment.