]> git.ktnx.net Git - mobile-ledger.git/commitdiff
show progress while loading accounts
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 2 Dec 2018 12:21:29 +0000 (12:21 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 2 Dec 2018 12:21:29 +0000 (12:21 +0000)
app/src/main/java/net/ktnx/mobileledger/LatestTransactions.java
app/src/main/java/net/ktnx/mobileledger/RetrieveAccountsTask.java
app/src/main/res/layout/content_latest_transactions.xml
app/src/main/res/values/strings.xml

index 8e0269f58d068a6223b51ffd6f8e6a2797e1a1ec..b3f2c90bb443665054cef5177d93536a837408af 100644 (file)
@@ -1,9 +1,11 @@
 package net.ktnx.mobileledger;
 
+import android.app.Activity;
 import android.content.Intent;
 import android.content.pm.PackageInfo;
 import android.os.Bundle;
 import android.preference.PreferenceManager;
+import android.support.design.widget.Snackbar;
 import android.support.v4.view.GravityCompat;
 import android.support.v4.widget.DrawerLayout;
 import android.support.v7.app.ActionBarDrawerToggle;
@@ -13,9 +15,12 @@ import android.util.Log;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
+import android.widget.ProgressBar;
+import android.widget.TextView;
 
 import java.util.Date;
 
+import static android.view.View.GONE;
 import static net.ktnx.mobileledger.MobileLedgerDB.db;
 
 public class LatestTransactions extends AppCompatActivity {
@@ -105,7 +110,29 @@ public class LatestTransactions extends AppCompatActivity {
     private void update_accounts() {
         prepare_db();
 
-        RetrieveAccountsTask task = new RetrieveAccountsTask();
+        Activity activity = this;
+
+        ProgressBar pb = findViewById(R.id.progressBar);
+        TextView pt = findViewById(R.id.textProgress);
+        pb.setIndeterminate(true);
+
+        RetrieveAccountsTask task = new RetrieveAccountsTask() {
+            @Override
+            protected void onProgressUpdate(Integer... values) {
+                if ( values[0] == 0 )
+                    pt.setText(R.string.progress_connecting);
+                else
+                    pt.setText(String.format(getResources().getString(R.string.progress_N_accounts_loaded), values[0]));
+            }
+
+            @Override
+            protected void onPostExecute(Void result) {
+                pb.setVisibility(GONE);
+                pt.setVisibility(GONE);
+                if (this.error != 0)
+                    Snackbar.make(drawer, activity.getResources().getString(this.error), Snackbar.LENGTH_LONG );
+            }
+        };
 
         task.setPref(PreferenceManager.getDefaultSharedPreferences(this));
         task.execute(db);
index 60e6c4564eace74d89facdad4f8667c509a6e70e..85f2306cc9b3a5bbe7c39263cfb50e6fb0dfeaa6 100644 (file)
@@ -17,8 +17,8 @@ import java.net.URLDecoder;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-public class RetrieveAccountsTask extends android.os.AsyncTask<SQLiteDatabase, Void, Void> {
-    private int error;
+abstract public class RetrieveAccountsTask extends android.os.AsyncTask<SQLiteDatabase, Integer, Void> {
+    protected int error;
 
     SharedPreferences pref;
     public void setPref(SharedPreferences pref) {
@@ -49,8 +49,9 @@ public class RetrieveAccountsTask extends android.os.AsyncTask<SQLiteDatabase, V
 //                http.setRequestProperty("Authorization", basic_auth);
                 Log.d("update_accounts", "Will auth as "+auth_user+" with password of "+auth_password.length()+" characters");
             }
-            http.setAllowUserInteraction(true);
+            http.setAllowUserInteraction(false);
             http.setRequestProperty("Accept-Charset", "UTF-8");
+            publishProgress(0);
             InputStream resp = http.getInputStream();
             try {
                 Log.d("update_accounts", String.valueOf(http.getResponseCode()));
@@ -66,6 +67,7 @@ public class RetrieveAccountsTask extends android.os.AsyncTask<SQLiteDatabase, V
                         BufferedReader buf = new BufferedReader(new InputStreamReader(resp, "UTF-8"));
                         // %3A is '='
                         Pattern re = Pattern.compile('"' + backend_url + "/register\\?q=inacct%3A([a-zA-Z0-9%]+)\\\"");
+                        int count = 0;
                         while ((line = buf.readLine()) != null) {
                             Matcher m = re.matcher(line);
                             while (m.find()) {
@@ -75,6 +77,7 @@ public class RetrieveAccountsTask extends android.os.AsyncTask<SQLiteDatabase, V
                                 Log.d("account-parser", acct_name);
 
                                 db.execSQL("insert into accounts(name) values(?)", new Object[]{acct_name} );
+                                publishProgress(++count);
                             }
                         }
 
@@ -104,10 +107,7 @@ public class RetrieveAccountsTask extends android.os.AsyncTask<SQLiteDatabase, V
         return null;
     }
 
-    protected void onPostExecute(Void result) {
-        if (error != 0)
-            Log.e("async-http", String.valueOf(error));
-        else
-            Log.d("async-http", "Accounts updated successfuly");
-    }
+    abstract protected void onProgressUpdate(Integer... values);
+
+    abstract protected void onPostExecute(Void result);
 }
index cb428e3678467629a8c8bbbecb26a835f8f17500..b2723206efcab9804201ab3f05ef12781586140a 100644 (file)
@@ -9,12 +9,25 @@
     tools:showIn="@layout/app_bar_latest_transactions">
 
     <TextView
+        android:id="@+id/textProgress"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
-        android:text="Hello World!"
+        android:text="TextView"
         app:layout_constraintBottom_toBottomOf="parent"
-        app:layout_constraintLeft_toLeftOf="parent"
-        app:layout_constraintRight_toRightOf="parent"
-        app:layout_constraintTop_toTopOf="parent" />
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent" />
+
+    <ProgressBar
+        android:id="@+id/progressBar"
+        style="?android:attr/progressBarStyle"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginStart="8dp"
+        android:layout_marginEnd="8dp"
+        android:layout_marginBottom="16dp"
+        android:visibility="visible"
+        app:layout_constraintBottom_toTopOf="@+id/textProgress"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent" />
 
 </android.support.constraint.ConstraintLayout>
\ No newline at end of file
index a06a809b61a5010698b4c94b05ba4cbf03a99586..0bd439e1cb3c73bea74cc38af647861919e997bd 100644 (file)
@@ -64,4 +64,6 @@
     <string name="err_net_io_error">Network I/O error</string>
     <string name="err_http_error">HTTP error</string>
     <string name="err_net_error">Network error</string>
+    <string name="progress_connecting">Connecting…</string>
+    <string name="progress_N_accounts_loaded">%d accounts loaded</string>
 </resources>