Skip to content

Commit

Permalink
Theme support
Browse files Browse the repository at this point in the history
  • Loading branch information
ElishaAz committed Oct 24, 2022
1 parent 327306f commit c3f08ed
Show file tree
Hide file tree
Showing 26 changed files with 188 additions and 37 deletions.
4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ apply plugin: 'com.android.application'
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" }
}

android {
Expand Down Expand Up @@ -45,5 +46,6 @@ dependencies {
implementation 'androidx.navigation:navigation-ui:2.5.2'
implementation 'androidx.preference:preference:1.2.0'

implementation "org.greenrobot:eventbus:3.3.1"
implementation 'org.greenrobot:eventbus:3.3.1'
implementation 'com.github.martin-stone:hsv-alpha-color-picker-android:3.0.1'
}
28 changes: 28 additions & 0 deletions app/src/main/java/com/elishaazaria/sayboard/AppCtx.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.elishaazaria.sayboard;

import android.annotation.SuppressLint;
import android.content.Context;

import androidx.annotation.IntegerRes;
import androidx.annotation.StringRes;

public class AppCtx {
@SuppressLint("StaticFieldLeak") // App context
private static Context appCtx;

public static void setAppCtx(Context context) {
appCtx = context.getApplicationContext();
}

public static Context getAppCtx() {
return appCtx;
}

public static String getStringRes(@StringRes int res) {
return appCtx.getString(res);
}

public static int getIntegerRes(@IntegerRes int res){
return appCtx.getResources().getInteger(res);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.elishaazaria.sayboard;

import android.app.Application;
import android.content.Context;

import androidx.annotation.StringRes;

import com.elishaazaria.sayboard.preferences.MyPreferences;

import org.greenrobot.eventbus.EventBus;

Expand All @@ -9,5 +14,6 @@ public class SayboardApplication extends Application {
public void onCreate() {
super.onCreate();
EventBus.builder().logNoSubscriberMessages(false).sendNoSubscriberEvent(false).installDefaultEventBus();
AppCtx.setAppCtx(this);
}
}
1 change: 1 addition & 0 deletions app/src/main/java/com/elishaazaria/sayboard/ime/IME.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public void onStartInputView(EditorInfo info, boolean restarting) {
}

modelManager.reloadModels();
viewManager.refresh();
}

@Override
Expand Down
60 changes: 44 additions & 16 deletions app/src/main/java/com/elishaazaria/sayboard/ime/ViewManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.elishaazaria.sayboard.ime;

import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
Expand All @@ -9,8 +13,10 @@

import androidx.appcompat.content.res.AppCompatResources;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.widget.TextViewCompat;

import com.elishaazaria.sayboard.R;
import com.elishaazaria.sayboard.preferences.ThemePreferences;

public class ViewManager {
private final IME ime;
Expand Down Expand Up @@ -56,17 +62,14 @@ public void init() {
resultView.setMovementMethod(new ScrollingMovementMethod());

micButton.setOnClickListener(v -> {
if (listener != null)
listener.micClick();
if (listener != null) listener.micClick();
});
micButton.setOnLongClickListener(v -> listener != null && listener.micLongClick());
backButton.setOnClickListener(v -> {
if (listener != null)
listener.backClicked();
if (listener != null) listener.backClicked();
});
backspaceButton.setOnClickListener(v -> {
if (listener != null)
listener.backspaceClicked();
if (listener != null) listener.backspaceClicked();
});
backspaceButton.setOnTouchListener((v, event) -> {
if (listener == null) {
Expand All @@ -76,23 +79,50 @@ public void init() {
return listener.backspaceTouched(v, event);
});
returnButton.setOnClickListener(v -> {
if (listener != null)
listener.returnClicked();
if (listener != null) listener.returnClicked();
});
modelButton.setOnClickListener(v -> {
if (listener != null)
listener.modelClicked();
if (listener != null) listener.modelClicked();
});


initialized = true;

if (currentState == STATE_ERROR && !currentErrorMessage.isEmpty())
setErrorState(currentErrorMessage);
else
setUiState(currentState);
else setUiState(currentState);

setModelName(modelName);

setUpTheme();
}

private int currentForeground = Integer.MAX_VALUE;
private int currentBackground = Integer.MAX_VALUE;

private void setUpTheme() {
int foreground = ThemePreferences.getForegroundColor();
int background = ThemePreferences.getBackgroundColor();

if (currentForeground == foreground && currentBackground == background) return;

currentForeground = foreground;
currentBackground = background;

overlayView.setBackgroundColor(background);

ColorStateList foregroundTint = ColorStateList.valueOf(foreground);
micButton.setImageTintList(foregroundTint);
backButton.setImageTintList(foregroundTint);
backspaceButton.setImageTintList(foregroundTint);
returnButton.setImageTintList(foregroundTint);
TextViewCompat.setCompoundDrawableTintList(modelButton, foregroundTint);
modelButton.setTextColor(foreground);
resultView.setTextColor(foreground);
}

public void refresh() {
setUpTheme();
}

public void setUiState(int state) {
Expand Down Expand Up @@ -134,15 +164,13 @@ public void setUiState(int state) {

public void setErrorState(String message) {
setUiState(STATE_ERROR);
if (initialized)
resultView.setText(message);
if (initialized) resultView.setText(message);
currentErrorMessage = message;
}

public void setModelName(String modelName) {
this.modelName = modelName;
if (initialized)
modelButton.setText(modelName);
if (initialized) modelButton.setText(modelName);
}

public ConstraintLayout getRoot() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.elishaazaria.sayboard.preferences;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import com.elishaazaria.sayboard.AppCtx;
import com.elishaazaria.sayboard.R;

public class MyPreferences {
private static SharedPreferences sharedPref;

public static SharedPreferences getSharedPref() {
if (sharedPref == null) {
sharedPref = AppCtx.getAppCtx().getSharedPreferences(
AppCtx.getStringRes(R.string.main_shared_pref), Context.MODE_PRIVATE);
}
return sharedPref;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.elishaazaria.sayboard.preferences;

import android.graphics.Color;
import android.os.Build;

import com.elishaazaria.sayboard.AppCtx;
import com.elishaazaria.sayboard.R;

public class ThemePreferences {
public static int getForegroundColor() {
// TODO: if min-api 26, switch to Color objects
return MyPreferences.getSharedPref()
.getInt(AppCtx.getStringRes(R.string.pref_theme_foreground_c),
AppCtx.getIntegerRes(R.integer.pref_foreground_color_default));
}

public static int getBackgroundColor() {
// TODO: if min-api 26, switch to Color objects
return MyPreferences.getSharedPref()
.getInt(AppCtx.getStringRes(R.string.pref_theme_background_c),
AppCtx.getIntegerRes(R.integer.pref_background_color_default));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@

import android.os.Bundle;

import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;

import com.elishaazaria.sayboard.AppCtx;
import com.elishaazaria.sayboard.R;
import com.rarepebble.colorpicker.ColorPreference;

public class ThemeFragment extends PreferenceFragmentCompat {

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
PreferenceManager preferenceManager = getPreferenceManager();
preferenceManager.setSharedPreferencesName(AppCtx.getStringRes(R.string.main_shared_pref));
setPreferencesFromResource(R.xml.theme_preferences, rootKey);
}

@Override
public void onDisplayPreferenceDialog(Preference preference) {
if (preference instanceof ColorPreference) {
((ColorPreference) preference).showDialog(this, 0);
} else super.onDisplayPreferenceDialog(preference);
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_arrow_back.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:tint="@color/colorOnPrimary"
android:tint="#000000"
android:viewportWidth="24"
android:viewportHeight="24">
<path
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_backspace.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<vector android:height="24dp" android:tint="@color/colorOnPrimary"
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M22,3L7,3c-0.69,0 -1.23,0.35 -1.59,0.88L0,12l5.41,8.11c0.36,0.53 0.9,0.89 1.59,0.89h15c1.1,0 2,-0.9 2,-2L24,5c0,-1.1 -0.9,-2 -2,-2zM19,15.59L17.59,17 14,13.41 10.41,17 9,15.59 12.59,12 9,8.41 10.41,7 14,10.59 17.59,7 19,8.41 15.41,12 19,15.59z"/>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_keyboard_arrow_left.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<vector android:height="24dp" android:tint="@color/colorOnPrimary"
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M15.41,16.59L10.83,12l4.58,-4.59L14,6l-6,6 6,6 1.41,-1.41z"/>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_keyboard_arrow_right.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<vector android:height="24dp" android:tint="@color/colorOnPrimary"
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M8.59,16.59L13.17,12 8.59,7.41 10,6l6,6 -6,6 -1.41,-1.41z"/>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_keyboard_backspace.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<vector android:height="24dp" android:tint="@color/colorOnPrimary"
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M21,11H6.83l3.58,-3.59L9,6l-6,6 6,6 1.41,-1.41L6.83,13H21z"/>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_keyboard_return.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<vector android:height="24dp" android:tint="@color/colorOnPrimary"
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M19,7v4H5.83l3.58,-3.59L8,6l-6,6 6,6 1.41,-1.41L5.83,13H21V7z"/>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_keyboard_tab.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<vector android:height="24dp" android:tint="@color/colorOnPrimary"
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M11.59,7.41L15.17,11H1v2h14.17l-3.59,3.59L13,18l6,-6 -6,-6 -1.41,1.41zM20,6v12h2V6h-2z"/>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_language.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="@color/colorOnPrimary"
android:tint="#000000"
android:viewportWidth="24"
android:viewportHeight="24">
<path
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_mic.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<vector android:height="24dp" android:tint="@color/colorOnPrimary"
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,14c1.66,0 2.99,-1.34 2.99,-3L15,5c0,-1.66 -1.34,-3 -3,-3S9,3.34 9,5v6c0,1.66 1.34,3 3,3zM17.3,11c0,3 -2.54,5.1 -5.3,5.1S6.7,14 6.7,11L5,11c0,3.41 2.72,6.23 6,6.72L11,21h2v-3.28c3.28,-0.48 6,-3.3 6,-6.72h-1.7z"/>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_mic_none.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="@color/colorOnPrimary"
android:tint="#000000"
android:viewportWidth="24"
android:viewportHeight="24">
<path
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_mic_off.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<vector android:height="24dp" android:tint="@color/colorOnPrimary"
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M19,11h-1.7c0,0.74 -0.16,1.43 -0.43,2.05l1.23,1.23c0.56,-0.98 0.9,-2.09 0.9,-3.28zM14.98,11.17c0,-0.06 0.02,-0.11 0.02,-0.17L15,5c0,-1.66 -1.34,-3 -3,-3S9,3.34 9,5v0.18l5.98,5.99zM4.27,3L3,4.27l6.01,6.01L9.01,11c0,1.66 1.33,3 2.99,3 0.22,0 0.44,-0.03 0.65,-0.08l1.66,1.66c-0.71,0.33 -1.5,0.52 -2.31,0.52 -2.76,0 -5.3,-2.1 -5.3,-5.1L5,11c0,3.41 2.72,6.23 6,6.72L11,21h2v-3.28c0.91,-0.13 1.77,-0.45 2.54,-0.9L19.73,21 21,19.73 4.27,3z"/>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_settings_voice.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="@color/colorOnPrimary"
android:tint="#000000"
android:viewportWidth="24"
android:viewportHeight="24">
<path
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/ime.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableStart="@drawable/ic_language"
android:text="Button"
android:textColor="@color/colorOnPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
app:layout_constraintStart_toStartOf="parent"
tools:text="English (US)" />

<ImageButton
android:id="@+id/return_button"
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/integers.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="pref_foreground_color_default">0xFF377A00</integer>
<integer name="pref_background_color_default">0xFFFFFFFF</integer>
</resources>
7 changes: 7 additions & 0 deletions app/src/main/res/values/keys.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="main_shared_pref" translatable="false">main_shared_pref</string>
<string name="pref_theme_foreground_c" translatable="false">c_foreground</string>
<string name="pref_theme_background_c" translatable="false">c_background</string>

</resources>
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
<string name="themes_header">Theme</string>

<!-- Theme Preferences -->
<string name="signature_title">Your signature</string>
<string name="theme_foreground_color_title">Foreground color</string>
<string name="theme_background_color_title">Background color</string>
<string name="theme_reset_color">Reset</string>

<!-- Notifications -->
<string name="notification_download_channel_name">Model download</string>
Expand Down
Loading

0 comments on commit c3f08ed

Please sign in to comment.