]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
whitespace
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / MainActivity.java
index f8bb6ce37c39727a9a8e958d11a11e06129d517b..9cb045d7a00ec44d69c6617d2730ba0f400fbef6 100644 (file)
@@ -1,27 +1,29 @@
 /*
  * Copyright © 2019 Damyan Ivanov.
- * This file is part of Mobile-Ledger.
- * Mobile-Ledger is free software: you can distribute it and/or modify it
+ * This file is part of MoLe.
+ * MoLe 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,
+ * MoLe 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 <https://www.gnu.org/licenses/>.
+ * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
  */
 
 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;
 import android.support.v4.app.FragmentPagerAdapter;
@@ -29,7 +31,6 @@ import android.support.v4.view.GravityCompat;
 import android.support.v4.view.ViewPager;
 import android.support.v4.widget.DrawerLayout;
 import android.support.v7.app.ActionBarDrawerToggle;
-import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.Toolbar;
 import android.util.Log;
 import android.view.View;
@@ -46,13 +47,14 @@ 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;
 import java.text.DateFormat;
 import java.util.Date;
 
-public class MainActivity extends AppCompatActivity {
+public class MainActivity extends CrashReportingActivity {
     DrawerLayout drawer;
     private FragmentManager fragmentManager;
     private TextView tvLastUpdate;
@@ -62,6 +64,7 @@ public class MainActivity extends AppCompatActivity {
     private LinearLayout progressLayout;
     private SectionsPagerAdapter mSectionsPagerAdapter;
     private ViewPager mViewPager;
+    private FloatingActionButton fab;
 
     @Override
     protected void onStart() {
@@ -84,9 +87,11 @@ public class MainActivity extends AppCompatActivity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+
         setContentView(R.layout.activity_main);
         Toolbar toolbar = findViewById(R.id.toolbar);
         setSupportActionBar(toolbar);
+        fab = findViewById(R.id.btn_add_transaction);
 
         Data.profile.addObserver((o, arg) -> {
             MobileLedgerProfile profile = Data.profile.get();
@@ -94,6 +99,22 @@ public class MainActivity extends AppCompatActivity {
                 if (profile == null) setTitle(R.string.app_name);
                 else setTitle(profile.getName());
                 updateLastUpdateTextFromDB();
+                if (profile.isPostingPermitted()) {
+                    toolbar.setSubtitle(null);
+                    fab.show();
+                }
+                else {
+                    toolbar.setSubtitle(R.string.profile_subitlte_read_only);
+                    fab.hide();
+                }
+
+                int newProfileTheme = profile.getThemeId();
+                if (newProfileTheme != Colors.profileThemeId) {
+                    Log.d("profiles", String.format("profile theme %d → %d", Colors.profileThemeId,
+                            newProfileTheme));
+                    profileThemeChanged();
+                    Colors.profileThemeId = newProfileTheme;
+                }
             });
         });
 
@@ -169,6 +190,14 @@ public class MainActivity extends AppCompatActivity {
 
         findViewById(R.id.btn_add_transaction).setOnClickListener(this::fabNewTransactionClicked);
     }
+    private void profileThemeChanged() {
+        setupProfileColors();
+
+        // restart activity to reflect theme change
+        finish();
+        Intent intent = new Intent(this, this.getClass());
+        startActivity(intent);
+    }
     @Override
     protected void onResume() {
         super.onResume();
@@ -215,20 +244,13 @@ public class MainActivity extends AppCompatActivity {
     }
     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);
             }
         }
     }
@@ -330,6 +352,8 @@ public class MainActivity extends AppCompatActivity {
         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);
@@ -358,6 +382,11 @@ public class MainActivity extends AppCompatActivity {
         Intent intent = new Intent(this, ProfileListActivity.class);
         startActivity(intent);
     }
+    public void fabShouldShow() {
+        MobileLedgerProfile profile = Data.profile.get();
+        if ((profile != null) && profile.isPostingPermitted()) fab.show();
+    }
+
     public class SectionsPagerAdapter extends FragmentPagerAdapter {
 
         public SectionsPagerAdapter(FragmentManager fm) {
@@ -383,5 +412,4 @@ public class MainActivity extends AppCompatActivity {
             return 2;
         }
     }
-
 }