use Colors.* for run-time color control
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 17 Feb 2019 20:12:02 +0000 (22:12 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 17 Feb 2019 20:12:02 +0000 (22:12 +0200)
app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java
app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/AppCompatPreferenceActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/CrashReportingActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java
app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java
app/src/main/java/net/ktnx/mobileledger/utils/Globals.java

index da678a6..52b2722 100644 (file)
@@ -20,7 +20,6 @@ package net.ktnx.mobileledger.ui.account_summary;
 import android.content.Context;
 import android.content.res.Resources;
 import android.graphics.Typeface;
-import android.os.Build;
 import android.support.annotation.NonNull;
 import android.support.v7.widget.RecyclerView;
 import android.view.LayoutInflater;
@@ -33,6 +32,7 @@ import android.widget.TextView;
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.LedgerAccount;
+import net.ktnx.mobileledger.utils.Colors;
 
 import java.util.List;
 
@@ -68,14 +68,10 @@ class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter.L
             }
 
             if (position % 2 == 0) {
-                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
-                        .setBackgroundColor(rm.getColor(R.color.table_row_dark_bg, ctx.getTheme()));
-                else holder.row.setBackgroundColor(rm.getColor(R.color.table_row_dark_bg));
+                holder.row.setBackgroundColor(Colors.tableRowDarkBG);
             }
             else {
-                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
-                        .setBackgroundColor(rm.getColor(R.color.drawer_background, ctx.getTheme()));
-                else holder.row.setBackgroundColor(rm.getColor(R.color.drawer_background));
+                holder.row.setBackgroundColor(Colors.tableRowLightBG);
             }
 
             holder.selectionCb.setVisibility(selectionActive ? View.VISIBLE : View.GONE);
index 2fcfe67..04cde35 100644 (file)
@@ -40,6 +40,7 @@ import net.ktnx.mobileledger.model.LedgerAccount;
 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
 import net.ktnx.mobileledger.ui.RecyclerItemListener;
 import net.ktnx.mobileledger.ui.activity.MainActivity;
+import net.ktnx.mobileledger.utils.Colors;
 
 import java.util.List;
 import java.util.Observer;
@@ -152,7 +153,9 @@ public class AccountSummaryFragment extends MobileLedgerListFragment {
             }
         });
         swiper = mActivity.findViewById(R.id.account_swiper);
-        swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
+        Colors.themeWatch.addObserver(
+                (o, arg) -> swiper.setColorSchemeColors(Colors.primary));
+        swiper.setColorSchemeColors(Colors.primary);
         swiper.setOnRefreshListener(() -> {
             Log.d("ui", "refreshing accounts via swipe");
             mActivity.scheduleTransactionListRetrieval();
@@ -160,8 +163,8 @@ public class AccountSummaryFragment extends MobileLedgerListFragment {
 
         Data.accounts.addObserver(
                 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
-        Data.profile.addObserver(
-                (o, arg) -> mActivity.runOnUiThread(() -> AccountSummaryViewModel.scheduleAccountListReload()));
+        Data.profile.addObserver((o, arg) -> mActivity.runOnUiThread(
+                AccountSummaryViewModel::scheduleAccountListReload));
         update_account_table();
     }
     private void update_account_table() {
index 932607e..65274df 100644 (file)
@@ -30,6 +30,8 @@ import android.view.MenuInflater;
 import android.view.View;
 import android.view.ViewGroup;
 
+import net.ktnx.mobileledger.utils.Colors;
+
 /**
  * A {@link android.preference.PreferenceActivity} which implements and proxies the necessary calls
  * to be used with AppCompat.
@@ -43,6 +45,7 @@ public abstract class AppCompatPreferenceActivity extends PreferenceActivity {
         getDelegate().installViewFactory();
         getDelegate().onCreate(savedInstanceState);
         super.onCreate(savedInstanceState);
+        Colors.setupTheme(this);
     }
 
     @Override
index 0d72499..72dec22 100644 (file)
@@ -6,14 +6,24 @@ import android.support.v7.app.AppCompatActivity;
 import android.util.Log;
 
 import net.ktnx.mobileledger.ui.CrashReportDialogFragment;
+import net.ktnx.mobileledger.utils.Colors;
 
 import java.io.PrintWriter;
 import java.io.StringWriter;
 
-public class CrashReportingActivity extends AppCompatActivity {
+public abstract class CrashReportingActivity extends AppCompatActivity {
+    protected void setupProfileColors() {
+        Colors.setupTheme(this);
+    }
+    @Override
+    protected void onStart() {
+        super.onStart();
+        Colors.refreshColors(getTheme());
+    }
     @Override
     protected void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        Colors.setupTheme(this);
 
         Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
             @Override
index 641d296..a96a0b9 100644 (file)
@@ -19,9 +19,10 @@ package net.ktnx.mobileledger.ui.activity;
 
 import android.content.Intent;
 import android.content.pm.PackageInfo;
+import android.content.res.ColorStateList;
+import android.graphics.Color;
 import android.os.Build;
 import android.os.Bundle;
-import android.support.annotation.ColorInt;
 import android.support.design.widget.FloatingActionButton;
 import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentManager;
@@ -46,6 +47,7 @@ import net.ktnx.mobileledger.model.LedgerAccount;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment;
 import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment;
+import net.ktnx.mobileledger.utils.Colors;
 import net.ktnx.mobileledger.utils.MLDB;
 
 import java.lang.ref.WeakReference;
@@ -225,20 +227,13 @@ public class MainActivity extends CrashReportingActivity {
     }
     public void markDrawerItemCurrent(int id) {
         TextView item = drawer.findViewById(id);
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
-            item.setBackgroundColor(getResources().getColor(R.color.table_row_dark_bg, getTheme()));
-        }
-        else {
-            item.setBackgroundColor(getResources().getColor(R.color.table_row_dark_bg));
-        }
-
-        @ColorInt int transparent = getResources().getColor(android.R.color.transparent);
+        item.setBackgroundColor(Colors.tableRowDarkBG);
 
         LinearLayout actions = drawer.findViewById(R.id.nav_actions);
         for (int i = 0; i < actions.getChildCount(); i++) {
             View view = actions.getChildAt(i);
             if (view.getId() != id) {
-                view.setBackgroundColor(transparent);
+                view.setBackgroundColor(Color.TRANSPARENT);
             }
         }
     }
@@ -340,6 +335,8 @@ public class MainActivity extends CrashReportingActivity {
         else Toast.makeText(this, error, Toast.LENGTH_LONG).show();
     }
     public void onRetrieveStart() {
+        progressBar.setIndeterminateTintList(ColorStateList.valueOf(Colors.primary));
+        progressBar.setProgressTintList(ColorStateList.valueOf(Colors.primary));
         progressBar.setIndeterminate(true);
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) progressBar.setProgress(0, false);
         else progressBar.setProgress(0);
index 0980eac..6cf729d 100644 (file)
@@ -38,6 +38,7 @@ import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.model.LedgerTransaction;
 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
 import net.ktnx.mobileledger.model.TransactionListItem;
+import net.ktnx.mobileledger.utils.Colors;
 import net.ktnx.mobileledger.utils.Globals;
 import net.ktnx.mobileledger.utils.MLDB;
 
@@ -162,8 +163,8 @@ public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowH
                 case HEAD:
                     holder.tvDescription.setText(step.getTransaction().getDescription());
 
-                    if (step.isOdd()) holder.row.setBackgroundColor(Globals.tableRowDarkBG);
-                    else holder.row.setBackgroundColor(Globals.tableRowLightBG);
+                    if (step.isOdd()) holder.row.setBackgroundColor(Colors.tableRowDarkBG);
+                    else holder.row.setBackgroundColor(Colors.tableRowLightBG);
 
                     break;
                 case ACCOUNTS:
@@ -209,8 +210,8 @@ public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowH
                     if ((boldAccountName != null) &&
                         acc.getAccountName().startsWith(boldAccountName))
                     {
-                        accName.setTextColor(Globals.primaryDark);
-                        accAmount.setTextColor(Globals.primaryDark);
+                        accName.setTextColor(Colors.accent);
+                        accAmount.setTextColor(Colors.accent);
 
                         SpannableString ss = new SpannableString(acc.getAccountName());
                         ss.setSpan(new StyleSpan(Typeface.BOLD), 0, boldAccountName.length(),
@@ -218,8 +219,8 @@ public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowH
                         accName.setText(ss);
                     }
                     else {
-                        accName.setTextColor(Globals.defaultTextColor);
-                        accAmount.setTextColor(Globals.defaultTextColor);
+                        accName.setTextColor(Colors.defaultTextColor);
+                        accAmount.setTextColor(Colors.defaultTextColor);
                         accName.setText(acc.getAccountName());
                     }
                     accAmount.setText(acc.toString());
index 6652120..62c4496 100644 (file)
@@ -40,6 +40,7 @@ import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
 import net.ktnx.mobileledger.ui.activity.MainActivity;
+import net.ktnx.mobileledger.utils.Colors;
 import net.ktnx.mobileledger.utils.Globals;
 import net.ktnx.mobileledger.utils.MLDB;
 import net.ktnx.mobileledger.utils.ObservableValue;
@@ -166,7 +167,9 @@ public class TransactionListFragment extends MobileLedgerListFragment {
             mActivity.scheduleTransactionListRetrieval();
         });
 
-        swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
+        Colors.themeWatch.addObserver(
+                (o, arg) -> swiper.setColorSchemeColors(Colors.primary));
+        swiper.setColorSchemeColors(Colors.primary);
 
         vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter);
         accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name);
index 8a2c836..7a9913d 100644 (file)
@@ -19,7 +19,6 @@ package net.ktnx.mobileledger.utils;
 
 import android.app.Activity;
 import android.content.Context;
-import android.support.annotation.ColorInt;
 import android.view.View;
 import android.view.inputmethod.InputMethodManager;
 
@@ -32,12 +31,6 @@ import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 public final class Globals {
-    @ColorInt
-    public static int tableRowLightBG;
-    @ColorInt
-    public static int tableRowDarkBG;
-    @ColorInt
-    public static int primaryDark, defaultTextColor;
     public static String[] monthNames;
     public static String developerEmail = "dam+mole-crash@ktnx.net";
     private static SimpleDateFormat ledgerDateFormatter =