]> 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 f662dabe367913fdeb2bf4deeb33a6bc6c7178b4..40bf49c6c0e782a9c45c8a51824e7069378c912f 100644 (file)
@@ -102,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;
@@ -132,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);
@@ -247,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));
@@ -313,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");
@@ -330,6 +322,8 @@ public class MainActivity extends ProfileThemedActivity {
         }
     }
     private void createShortcuts() {
+        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++) {
@@ -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() {