]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
whitepspace
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / MainActivity.java
index 6a290685636cbf04bb730638d7e5b2db3e545fb2..40bf49c6c0e782a9c45c8a51824e7069378c912f 100644 (file)
@@ -22,7 +22,6 @@ import android.content.pm.PackageInfo;
 import android.content.pm.ShortcutInfo;
 import android.content.pm.ShortcutManager;
 import android.content.res.ColorStateList;
-import android.content.res.Resources;
 import android.graphics.Color;
 import android.graphics.drawable.Icon;
 import android.os.AsyncTask;
@@ -103,7 +102,6 @@ public class MainActivity extends ProfileThemedActivity {
     private boolean mBackMeansToAccountList = false;
     private Observer profileObserver;
     private Observer profilesObserver;
-    private Observer lastUpdateDateObserver;
     private Toolbar mToolbar;
     private DrawerLayout.SimpleDrawerListener drawerListener;
     private ActionBarDrawerToggle barDrawerToggle;
@@ -133,8 +131,6 @@ public class MainActivity extends ProfileThemedActivity {
         profileObserver = null;
         Data.profiles.deleteObserver(profilesObserver);
         profilesObserver = null;
-        Data.lastUpdateDate.deleteObserver(lastUpdateDateObserver);
-        lastUpdateDateObserver = null;
         RecyclerView root = findViewById(R.id.nav_profile_list);
         if (root != null) root.setAdapter(null);
         if (drawer != null) drawer.removeDrawerListener(drawerListener);
@@ -143,7 +139,8 @@ public class MainActivity extends ProfileThemedActivity {
         barDrawerToggle = null;
         if (mViewPager != null) mViewPager.removeOnPageChangeListener(pageChangeListener);
         pageChangeListener = null;
-        if (mProfileListAdapter != null) mProfileListAdapter.deleteEditingProfilesObserver(editingProfilesObserver);
+        if (mProfileListAdapter != null)
+            mProfileListAdapter.deleteEditingProfilesObserver(editingProfilesObserver);
         editingProfilesObserver = null;
         super.onDestroy();
     }
@@ -189,8 +186,8 @@ public class MainActivity extends ProfileThemedActivity {
         }
 
         if (barDrawerToggle == null) {
-            barDrawerToggle = new ActionBarDrawerToggle(this, drawer, mToolbar, R.string.navigation_drawer_open,
-                    R.string.navigation_drawer_close);
+            barDrawerToggle = new ActionBarDrawerToggle(this, drawer, mToolbar,
+                    R.string.navigation_drawer_open, R.string.navigation_drawer_close);
             drawer.addDrawerListener(barDrawerToggle);
         }
         barDrawerToggle.syncState();
@@ -227,7 +224,8 @@ public class MainActivity extends ProfileThemedActivity {
                             markDrawerItemCurrent(R.id.nav_latest_transactions);
                             break;
                         default:
-                            Log.e("MainActivity", String.format("Unexpected page index %d", position));
+                            Log.e("MainActivity",
+                                    String.format("Unexpected page index %d", position));
                     }
 
                     super.onPageSelected(position);
@@ -246,15 +244,7 @@ public class MainActivity extends ProfileThemedActivity {
         }
         else mAccountFilter = null;
 
-        if (lastUpdateDateObserver == null) {
-            lastUpdateDateObserver = (o, arg) -> {
-                Log.d("main", "lastUpdateDate changed");
-                runOnUiThread(this::updateLastUpdateDisplay);
-            };
-            Data.lastUpdateDate.addObserver(lastUpdateDateObserver);
-        }
-
-        updateLastUpdateDisplay();
+        Data.lastUpdateDate.observe(this, this::updateLastUpdateDisplay);
 
         findViewById(R.id.btn_no_profiles_add)
                 .setOnClickListener(v -> startEditProfileActivity(null));
@@ -284,7 +274,8 @@ public class MainActivity extends ProfileThemedActivity {
                     profileListHeadArrow.setVisibility(View.VISIBLE);
                     profileListHeadCancel.setVisibility(View.GONE);
                     profileListHeadMore.setVisibility(View.GONE);
-                    profileListHeadMore.setVisibility(profileListExpanded ? View.VISIBLE : View.GONE);
+                    profileListHeadMore
+                            .setVisibility(profileListExpanded ? View.VISIBLE : View.GONE);
                 }
             };
             mProfileListAdapter.addEditingProfilesObserver(editingProfilesObserver);
@@ -311,12 +302,15 @@ public class MainActivity extends ProfileThemedActivity {
             drawer.addDrawerListener(drawerListener);
         }
 
+        findViewById(R.id.nav_profile_list_head_layout)
+                .setOnClickListener(this::navProfilesHeadClicked);
+        findViewById(R.id.nav_profiles_label).setOnClickListener(this::navProfilesHeadClicked);
         setupProfile();
         onProfileChanged(null);
 
         updateLastUpdateTextFromDB();
-        Date lastUpdate = Data.lastUpdateDate.get();
-
+    }
+    private void scheduleDataRetrievalIfStale(Date lastUpdate) {
         long now = new Date().getTime();
         if ((lastUpdate == null) || (now > (lastUpdate.getTime() + (24 * 3600 * 1000)))) {
             if (lastUpdate == null) Log.d("db::", "WEB data never fetched. scheduling a fetch");
@@ -328,7 +322,8 @@ public class MainActivity extends ProfileThemedActivity {
         }
     }
     private void createShortcuts() {
-        Resources rm = getResources();
+        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) return;
+
         List<ShortcutInfo> shortcuts = new ArrayList<>();
         try (LockHolder lh = Data.profiles.lockForReading()) {
             for (int i = 0; i < Data.profiles.size(); i++) {
@@ -340,8 +335,7 @@ public class MainActivity extends ProfileThemedActivity {
                         .setIcon(Icon.createWithResource(this, R.drawable.svg_thick_plus_white))
                         .setIntent(new Intent(Intent.ACTION_VIEW, null, this,
                                 NewTransactionActivity.class).putExtra("profile_uuid", p.getUuid()))
-                        .setRank(i)
-                        .build();
+                        .setRank(i).build();
                 shortcuts.add(si);
             }
         }
@@ -363,6 +357,12 @@ public class MainActivity extends ProfileThemedActivity {
         MobileLedgerProfile profile = Data.profile.get();
         MainActivity.this.runOnUiThread(() -> {
 
+            boolean haveProfile = profile != null;
+            findViewById(R.id.no_profiles_layout)
+                    .setVisibility(haveProfile ? View.GONE : View.VISIBLE);
+            findViewById(R.id.pager_layout)
+                    .setVisibility(haveProfile ? View.VISIBLE : View.VISIBLE);
+
             Data.transactions.clear();
             Log.d("transactions", "requesting list reload");
             TransactionListViewModel.scheduleTransactionListReload();
@@ -415,22 +415,25 @@ public class MainActivity extends ProfileThemedActivity {
                     fab.hide();
                 }
             }
+
+            updateLastUpdateTextFromDB();
         });
     }
-    private void updateLastUpdateDisplay() {
+    private void updateLastUpdateDisplay(Date newValue) {
         LinearLayout l = findViewById(R.id.transactions_last_update_layout);
         TextView v = findViewById(R.id.transactions_last_update);
-        Date date = Data.lastUpdateDate.get();
-        if (date == null) {
+        if (newValue == null) {
             l.setVisibility(View.INVISIBLE);
             Log.d("main", "no last update date :(");
         }
         else {
-            final String text = DateFormat.getDateTimeInstance().format(date);
+            final String text = DateFormat.getDateTimeInstance().format(newValue);
             v.setText(text);
             l.setVisibility(View.VISIBLE);
             Log.d("main", String.format("Date formatted: %s", text));
         }
+
+        scheduleDataRetrievalIfStale(newValue);
     }
     @Override
     public void finish() {
@@ -582,18 +585,15 @@ public class MainActivity extends ProfileThemedActivity {
         }
     }
     public void updateLastUpdateTextFromDB() {
-        {
-            final MobileLedgerProfile profile = Data.profile.get();
-            long last_update =
-                    (profile != null) ? profile.getLongOption(MLDB.OPT_LAST_SCRAPE, 0L) : 0;
+        final MobileLedgerProfile profile = Data.profile.get();
+        long last_update = (profile != null) ? profile.getLongOption(MLDB.OPT_LAST_SCRAPE, 0L) : 0;
 
-            Log.d("transactions", String.format("Last update = %d", last_update));
-            if (last_update == 0) {
-                Data.lastUpdateDate.set(null);
-            }
-            else {
-                Data.lastUpdateDate.set(new Date(last_update));
-            }
+        Log.d("transactions", String.format("Last update = %d", last_update));
+        if (last_update == 0) {
+            Data.lastUpdateDate.postValue(null);
+        }
+        else {
+            Data.lastUpdateDate.postValue(new Date(last_update));
         }
     }
     public void scheduleTransactionListRetrieval() {