]> git.ktnx.net Git - mobile-ledger-staging.git/commitdiff
splash: setup DB in the background
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 26 Sep 2020 14:24:09 +0000 (17:24 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 26 Sep 2020 14:24:09 +0000 (17:24 +0300)
this puts all initial DB work in the background - creation and upgrade

avoids empty screen when there is a ig DB rework on upgrade

app/src/main/java/net/ktnx/mobileledger/model/Data.java
app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/SplashActivity.java
app/src/main/java/net/ktnx/mobileledger/utils/MobileLedgerDatabase.java
app/src/main/res/anim/fade_in_slowly.xml [new file with mode: 0644]
app/src/main/res/anim/fade_out_slowly.xml [new file with mode: 0644]

index 0bd50ebb771f4338f3851ea8eb9e27258908536d..f0ba846ec05d9271a7c4f390631cde6a9906c4e5 100644 (file)
@@ -92,7 +92,7 @@ public final class Data {
     }
     public static void setCurrentProfile(@NonNull MobileLedgerProfile newProfile) {
         MLDB.setOption(MLDB.OPT_PROFILE_UUID, newProfile.getUuid());
-        profile.setValue(newProfile);
+        profile.postValue(newProfile);
     }
     public static int getProfileIndex(MobileLedgerProfile profile) {
         try (LockHolder ignored = profilesLocker.lockForReading()) {
index 6f6e51cbd6e5293dca8b9e562780e1086aa3ea3a..c9e358e292451c83468ddefc923c2393a9df3fa4 100644 (file)
@@ -122,7 +122,7 @@ public final class MobileLedgerProfile {
                     result = item;
             }
         }
-        Data.profiles.setValue(list);
+        Data.profiles.postValue(list);
         return result;
     }
     public static void storeProfilesOrder() {
index 32b71d653e038d9348747c89e716f7da3de4953f..921abb7512f7363ed0bca78ea6a140e28d45cd46 100644 (file)
 
 package net.ktnx.mobileledger.ui.activity;
 
-import android.app.Activity;
 import android.content.Intent;
+import android.os.AsyncTask;
 import android.os.Bundle;
 import android.os.Handler;
 
+import androidx.activity.ComponentActivity;
 import androidx.annotation.Nullable;
 
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
+import net.ktnx.mobileledger.utils.Logger;
 import net.ktnx.mobileledger.utils.MLDB;
+import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
 
-public class SplashActivity extends Activity {
+public class SplashActivity extends ComponentActivity {
+    private static final long keepActiveForMS = 500;
+    private long startupTime;
+    private boolean running = true;
     @Override
     protected void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setTheme(R.style.AppTheme_default);
         setContentView(R.layout.splash_activity_layout);
+        Logger.debug("splash", "onCreate()");
+
+        MobileLedgerDatabase.initComplete.setValue(false);
+        MobileLedgerDatabase.initComplete.observe(this, this::onDbInitDoneChanged);
     }
     @Override
     protected void onStart() {
         super.onStart();
+        Logger.debug("splash", "onStart()");
+        running = true;
 
-        MobileLedgerProfile.loadAllFromDB(null);
+        startupTime = System.currentTimeMillis();
 
-        String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
-        MobileLedgerProfile startupProfile = Data.getProfile(profileUUID);
-        if (startupProfile != null)
-            Data.setCurrentProfile(startupProfile);
+        AsyncTask<Void, Void, Void> dbInitTask = new DatabaseInitTask();
+        dbInitTask.execute();
+    }
+    @Override
+    protected void onPause() {
+        super.onPause();
+        Logger.debug("splash", "onPause()");
+        running = false;
+    }
+    @Override
+    protected void onResume() {
+        super.onResume();
+        Logger.debug("splash", "onResume()");
+        running = true;
+    }
+    private void onDbInitDoneChanged(Boolean done) {
+        if (!done) {
+            Logger.debug("splash", "DB not yet initialized");
+            return;
+        }
 
-        new Handler().postDelayed(() -> {
+        Logger.debug("splash", "DB init done");
+        long now = System.currentTimeMillis();
+        if (now > startupTime + keepActiveForMS)
+            startMainActivity();
+        else {
+            new Handler().postDelayed(this::startMainActivity,
+                    keepActiveForMS - (now - startupTime));
+        }
+    }
+    private void startMainActivity() {
+        if (running) {
+            Logger.debug("splash", "still running, launching main activity");
             Intent intent = new Intent(this, MainActivity.class);
             intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION |
                             Intent.FLAG_ACTIVITY_NEW_TASK);
             startActivity(intent);
-            overridePendingTransition(R.anim.fade_in, R.anim.dummy);
-        }, 250);
+            overridePendingTransition(R.anim.fade_in_slowly, R.anim.fade_out_slowly);
+        }
+        else {
+            Logger.debug("splash", "Not running, finish and go away");
+            finish();
+        }
+    }
+    private static class DatabaseInitTask extends AsyncTask<Void, Void, Void> {
+        @Override
+        protected Void doInBackground(Void... voids) {
+            MobileLedgerProfile.loadAllFromDB(null);
+
+            String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
+            MobileLedgerProfile startupProfile = Data.getProfile(profileUUID);
+            if (startupProfile != null)
+                Data.setCurrentProfile(startupProfile);
+            return null;
+        }
+        @Override
+        protected void onPostExecute(Void aVoid) {
+            Logger.debug("splash", "DatabaseInitTask::onPostExecute()");
+            super.onPostExecute(aVoid);
+            MobileLedgerDatabase.initComplete.setValue(true);
+        }
     }
 }
index 29d650a57ec7c46c7b8975900cda9f4faf64414f..307d4bc9f93425a2bdeb930f6476abb5f36ef5d2 100644 (file)
@@ -24,6 +24,8 @@ import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
 import android.util.Log;
 
+import androidx.lifecycle.MutableLiveData;
+
 import net.ktnx.mobileledger.BuildConfig;
 
 import java.io.BufferedReader;
@@ -35,10 +37,10 @@ import java.util.Locale;
 import static net.ktnx.mobileledger.utils.Logger.debug;
 
 public class MobileLedgerDatabase extends SQLiteOpenHelper {
+    public static final MutableLiveData<Boolean> initComplete = new MutableLiveData<>(false);
     private static final String DB_NAME = "MoLe.db";
     private static final int LATEST_REVISION = 40;
     private static final String CREATE_DB_SQL = "create_db";
-
     private final Application mContext;
 
     public MobileLedgerDatabase(Application context) {
diff --git a/app/src/main/res/anim/fade_in_slowly.xml b/app/src/main/res/anim/fade_in_slowly.xml
new file mode 100644 (file)
index 0000000..1c76bb1
--- /dev/null
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?><!--
+  ~ Copyright © 2020 Damyan Ivanov.
+  ~ 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.
+  ~
+  ~ 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 MoLe. If not, see <https://www.gnu.org/licenses/>.
+  -->
+
+<set xmlns:android="http://schemas.android.com/apk/res/android"
+    android:duration="500"
+    >
+    <alpha
+        android:fromAlpha="0.0"
+        android:toAlpha="1.0"
+        />
+</set>
\ No newline at end of file
diff --git a/app/src/main/res/anim/fade_out_slowly.xml b/app/src/main/res/anim/fade_out_slowly.xml
new file mode 100644 (file)
index 0000000..296c095
--- /dev/null
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?><!--
+  ~ Copyright © 2020 Damyan Ivanov.
+  ~ 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.
+  ~
+  ~ 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 MoLe. If not, see <https://www.gnu.org/licenses/>.
+  -->
+
+<set xmlns:android="http://schemas.android.com/apk/res/android"
+    android:duration="500"
+    >
+    <alpha
+        android:fromAlpha="1.0"
+        android:toAlpha="0.0"
+        />
+</set>
\ No newline at end of file