]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/AccountSummary.java
put a 30 seconds timeout on network operations
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / AccountSummary.java
index e34aa6b71d054c45bd838e598cd740f41eaa41ee..3a59003dda2d149801fe139a4b1b9abfa77484f7 100644 (file)
@@ -1,3 +1,20 @@
+/*
+ * 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 <https://www.gnu.org/licenses/>.
+ */
+
 package net.ktnx.mobileledger;
 
 import android.arch.lifecycle.ViewModelProviders;
@@ -24,6 +41,10 @@ import android.view.MenuItem;
 import android.view.View;
 import android.widget.LinearLayout;
 
+import net.ktnx.mobileledger.async.RetrieveAccountsTask;
+import net.ktnx.mobileledger.model.LedgerAccount;
+import net.ktnx.mobileledger.utils.MLDB;
+
 import java.lang.ref.WeakReference;
 import java.util.Date;
 import java.util.List;
@@ -31,13 +52,11 @@ import java.util.List;
 import static net.ktnx.mobileledger.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
 
 public class AccountSummary extends AppCompatActivity {
-    DrawerLayout drawer;
-
     private static long account_list_last_updated;
     private static boolean account_list_needs_update = true;
+    DrawerLayout drawer;
     MenuItem mShowHiddenAccounts;
     SharedPreferences.OnSharedPreferenceChangeListener sBindPreferenceSummaryToValueListener;
-    private MobileLedgerDatabase dbh;
     private AccountSummaryViewModel model;
     private AccountSummaryAdapter modelAdapter;
     private Menu optMenu;
@@ -53,25 +72,26 @@ public class AccountSummary extends AppCompatActivity {
         Toolbar toolbar = findViewById(R.id.toolbar);
         setSupportActionBar(toolbar);
 
-        dbh = new MobileLedgerDatabase(this);
-
         drawer = findViewById(R.id.drawer_layout);
-        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
-                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
+        ActionBarDrawerToggle toggle =
+                new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open,
+                        R.string.navigation_drawer_close);
         drawer.addDrawerListener(toggle);
         toggle.syncState();
 
         android.widget.TextView ver = drawer.findViewById(R.id.drawer_version_text);
 
         try {
-            PackageInfo pi = getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
+            PackageInfo pi =
+                    getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
             ver.setText(pi.versionName);
-        } catch (Exception e) {
+        }
+        catch (Exception e) {
             e.printStackTrace();
         }
 
         model = ViewModelProviders.of(this).get(AccountSummaryViewModel.class);
-        List<LedgerAccount> accounts = model.getAccounts();
+        List<LedgerAccount> accounts = model.getAccounts(getApplicationContext());
         modelAdapter = new AccountSummaryAdapter(accounts);
 
         RecyclerView root = findViewById(R.id.account_root);
@@ -81,30 +101,33 @@ public class AccountSummary extends AppCompatActivity {
         llm.setOrientation(LinearLayoutManager.VERTICAL);
         root.setLayoutManager(llm);
 
-        root.addOnItemTouchListener(new RecyclerItemListener(this, root, new RecyclerItemListener.RecyclerTouchListener() {
-            @Override
-            public void onClickItem(View v, int position) {
-                Log.d("list", String.format("item %d clicked", position));
-                if (modelAdapter.isSelectionActive()) {
-                    modelAdapter.selectItem(position);
-                }
-            }
-
-            @Override
-            public void onLongClickItem(View v, int position) {
-                Log.d("list", String.format("item %d long-clicked", position));
-                modelAdapter.startSelection();
-                if (optMenu != null) {
-                    optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(true);
-                    optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(true);
-                    optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
-                }
-                {
-                    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.btn_add_transaction);
-                    if (fab != null) fab.hide();
-                }
-            }
-        }));
+        root.addOnItemTouchListener(new RecyclerItemListener(this, root,
+                new RecyclerItemListener.RecyclerTouchListener() {
+                    @Override
+                    public void onClickItem(View v, int position) {
+                        Log.d("list", String.format("item %d clicked", position));
+                        if (modelAdapter.isSelectionActive()) {
+                            modelAdapter.selectItem(position);
+                        }
+                    }
+
+                    @Override
+                    public void onLongClickItem(View v, int position) {
+                        Log.d("list", String.format("item %d long-clicked", position));
+                        modelAdapter.startSelection();
+                        if (optMenu != null) {
+                            optMenu.findItem(R.id.menu_acc_summary_cancel_selection)
+                                    .setVisible(true);
+                            optMenu.findItem(R.id.menu_acc_summary_confirm_selection)
+                                    .setVisible(true);
+                            optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
+                        }
+                        {
+                            FloatingActionButton fab = findViewById(R.id.btn_add_transaction);
+                            if (fab != null) fab.hide();
+                        }
+                    }
+                }));
 
         root.addOnScrollListener(new RecyclerView.OnScrollListener() {
             @Override
@@ -113,7 +136,9 @@ public class AccountSummary extends AppCompatActivity {
                 if (dy > 0) ((FloatingActionButton) findViewById(R.id.btn_add_transaction)).hide();
             }
         });
-        ((SwipeRefreshLayout) findViewById(R.id.account_swiper)).setOnRefreshListener(() -> {
+        SwipeRefreshLayout swiper = findViewById(R.id.account_swiper);
+        swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
+        swiper.setOnRefreshListener(() -> {
             Log.d("ui", "refreshing accounts via swipe");
             update_accounts(true);
         });
@@ -161,13 +186,17 @@ public class AccountSummary extends AppCompatActivity {
         Intent intent = new Intent(this, SettingsActivity.class);
         startActivity(intent);
     }
-
+    public void onLatestTransactionsClicked(View view) {
+        Intent intent = new Intent(this, TransactionListActivity.class);
+        startActivity(intent);
+    }
     @Override
     public void onBackPressed() {
         DrawerLayout drawer = findViewById(R.id.drawer_layout);
         if (drawer.isDrawerOpen(GravityCompat.START)) {
             drawer.closeDrawer(GravityCompat.START);
-        } else {
+        }
+        else {
             super.onBackPressed();
         }
     }
@@ -210,8 +239,7 @@ public class AccountSummary extends AppCompatActivity {
         update_accounts(true);
     }
 
-    public
-    void onShowOnlyStarredClicked(MenuItem mi) {
+    public void onShowOnlyStarredClicked(MenuItem mi) {
         SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
         boolean flag = pref.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false);
 
@@ -224,13 +252,15 @@ public class AccountSummary extends AppCompatActivity {
     }
 
     private void prepare_db() {
-        account_list_last_updated = dbh.get_option_value("last_refresh", (long) 0);
+        account_list_last_updated = MLDB.get_option_value(this, "last_refresh", (long) 0);
     }
 
     private void update_accounts(boolean force) {
         long now = new Date().getTime();
-        if ((now > (account_list_last_updated + (24 * 3600*1000))) || force) {
-            Log.d("db", "accounts last updated at " + account_list_last_updated+" and now is " + now+". re-fetching");
+        if ((now > (account_list_last_updated + (24 * 3600 * 1000))) || force) {
+            Log.d("db",
+                    "accounts last updated at " + account_list_last_updated + " and now is " + now +
+                    ". re-fetching");
             update_accounts();
         }
     }
@@ -242,21 +272,21 @@ public class AccountSummary extends AppCompatActivity {
         task.execute();
 
     }
-    void onAccountRefreshDone(int error) {
+    public void onAccountRefreshDone(int error) {
         SwipeRefreshLayout srl = findViewById(R.id.account_swiper);
         srl.setRefreshing(false);
         if (error != 0) {
             String err_text = getResources().getString(error);
             Log.d("visual", String.format("showing snackbar: %s", err_text));
-            Snackbar.make(drawer, err_text, Snackbar.LENGTH_LONG ).show();
+            Snackbar.make(drawer, err_text, Snackbar.LENGTH_LONG).show();
         }
         else {
-            dbh.set_option_value("last_refresh", new Date().getTime() );
+            MLDB.set_option_value(this, "last_refresh", new Date().getTime());
             update_account_table();
         }
     }
     private void update_account_table() {
-        model.reloadAccounts();
+        model.reloadAccounts(getApplicationContext());
         modelAdapter.notifyDataSetChanged();
     }
     void stopSelection() {
@@ -275,7 +305,7 @@ public class AccountSummary extends AppCompatActivity {
         stopSelection();
     }
     public void onConfirmAccSelection(MenuItem item) {
-        model.commitSelections();
+        model.commitSelections(getApplicationContext());
         stopSelection();
     }
 }