From c7b957ab880ff947ef90e70e41c7bc1c19a1caa3 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Fri, 21 Dec 2018 05:59:22 +0000 Subject: [PATCH] store table colors in a global glass updated by a custom application class upon configuration change --- app/src/main/AndroidManifest.xml | 18 +++++++ .../mobileledger/MobileLedgerApplication.java | 51 +++++++++++++++++++ .../mobileledger/TransactionListAdapter.java | 13 ++--- .../net/ktnx/mobileledger/utils/Globals.java | 28 ++++++++++ 4 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/net/ktnx/mobileledger/MobileLedgerApplication.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/utils/Globals.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a41ff5d3..1cd74d14 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,4 +1,21 @@ + + @@ -11,6 +28,7 @@ android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" + android:name="net.ktnx.mobileledger.MobileLedgerApplication" android:theme="@style/AppTheme"> . + */ + +package net.ktnx.mobileledger; + +import android.app.Application; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.os.Build; + +import net.ktnx.mobileledger.utils.Globals; + +public class MobileLedgerApplication extends Application { + + @Override + public void onCreate() { + super.onCreate(); + updateColorValues(); + } + @Override + public void onConfigurationChanged(Configuration newConfig) { + super.onConfigurationChanged(newConfig); + updateColorValues(); + } + private void updateColorValues() { + Resources rm = getResources(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + Resources.Theme theme = getTheme(); + Globals.table_row_odd_bg = rm.getColor(R.color.table_row_odd_bg, theme); + Globals.table_row_even_bg = rm.getColor(R.color.table_row_even_bg, theme); + } + else { + Globals.table_row_odd_bg = rm.getColor(R.color.table_row_odd_bg); + Globals.table_row_even_bg = rm.getColor(R.color.table_row_even_bg); + } + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/TransactionListAdapter.java b/app/src/main/java/net/ktnx/mobileledger/TransactionListAdapter.java index bae5a536..c14d7890 100644 --- a/app/src/main/java/net/ktnx/mobileledger/TransactionListAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/TransactionListAdapter.java @@ -20,7 +20,6 @@ package net.ktnx.mobileledger; import android.content.Context; import android.content.res.Resources; import android.database.sqlite.SQLiteDatabase; -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.model.LedgerTransaction; import net.ktnx.mobileledger.model.LedgerTransactionAccount; +import net.ktnx.mobileledger.utils.Globals; import net.ktnx.mobileledger.utils.MLDB; import java.util.Iterator; @@ -49,7 +49,7 @@ class TransactionListAdapter public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) { LedgerTransaction tr = transactions.get(position); Context ctx = holder.row.getContext(); - Resources rm = ctx.getResources(); + try (SQLiteDatabase db = MLDB.getReadableDatabase(ctx.getApplicationContext())) { tr.loadData(db); @@ -70,15 +70,10 @@ class TransactionListAdapter } if (position % 2 == 0) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row - .setBackgroundColor(rm.getColor(R.color.table_row_even_bg, ctx.getTheme())); - else holder.row.setBackgroundColor(rm.getColor(R.color.table_row_even_bg)); + holder.row.setBackgroundColor(Globals.table_row_even_bg); } else { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row - .setBackgroundColor(rm.getColor(R.color.table_row_odd_bg, ctx - .getTheme())); - else holder.row.setBackgroundColor(rm.getColor(R.color.table_row_odd_bg)); + holder.row.setBackgroundColor(Globals.table_row_odd_bg); } holder.row.setTag(R.id.POS, position); diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/Globals.java b/app/src/main/java/net/ktnx/mobileledger/utils/Globals.java new file mode 100644 index 00000000..39884f05 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/utils/Globals.java @@ -0,0 +1,28 @@ +/* + * Copyright © 2018 Damyan Ivanov. + * This file is part of Mobile-Ledger. + * Mobile-Ledger is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * Mobile-Ledger is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with Mobile-Ledger. If not, see . + */ + +package net.ktnx.mobileledger.utils; + +import android.support.annotation.ColorInt; + +public final class Globals { + @ColorInt + public static int table_row_even_bg; + @ColorInt + public static int table_row_odd_bg; + +} \ No newline at end of file -- 2.39.2