]> git.ktnx.net Git - mobile-ledger.git/commitdiff
better version detection progress
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 11 Oct 2020 19:37:03 +0000 (22:37 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 11 Oct 2020 19:37:03 +0000 (22:37 +0300)
app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailModel.java
app/src/main/res/layout/profile_detail.xml

index 9b0b60b70b00a874277a47418058c6d40068a627..01c0143dcc4171c66ef1eed09d532d9527e54889 100644 (file)
@@ -299,8 +299,11 @@ public class ProfileDetailFragment extends Fragment {
                 detectedApiVersion.setText(ver.toString());
         });
         detectedApiVersion.setOnClickListener(v -> model.triggerVersionDetection());
-        context.findViewById(R.id.api_version_detect_button)
-               .setOnClickListener(v -> model.triggerVersionDetection());
+        final View detectButton = context.findViewById(R.id.api_version_detect_button);
+        detectButton.setOnClickListener(v -> model.triggerVersionDetection());
+        model.observeDetectingHledgerVersion(viewLifecycleOwner, running -> {
+            detectButton.setVisibility(running ? View.VISIBLE : View.INVISIBLE);
+        });
 
         authParams = context.findViewById(R.id.auth_params);
 
index 92d1505fb0baa159bd38cf2e24b9f003d2ddfa4a..4cbbfe3d6fda715fa69b0668995aa51f0b61df97 100644 (file)
@@ -61,6 +61,7 @@ public class ProfileDetailModel extends ViewModel {
     private final MutableLiveData<String> preferredAccountsFilter = new MutableLiveData<>(null);
     private final MutableLiveData<Integer> themeId = new MutableLiveData<>(-1);
     private final MutableLiveData<HledgerVersion> detectedVersion = new MutableLiveData<>(null);
+    private final MutableLiveData<Boolean> detectingHledgerVersion = new MutableLiveData<>(false);
     public int initialThemeHue = Colors.DEFAULT_HUE_DEG;
     private VersionDetectionThread versionDetectionThread;
     public ProfileDetailModel() {
@@ -219,6 +220,9 @@ public class ProfileDetailModel extends ViewModel {
     void observeThemeId(LifecycleOwner lfo, Observer<Integer> o) {
         themeId.observe(lfo, o);
     }
+    void observeDetectingHledgerVersion(LifecycleOwner lfo, Observer<Boolean> o) {
+        detectingHledgerVersion.observe(lfo, o);
+    }
     void setValuesFromProfile(MobileLedgerProfile mProfile, int newProfileHue) {
         final int profileThemeId;
         if (mProfile != null) {
@@ -284,29 +288,28 @@ public class ProfileDetailModel extends ViewModel {
         versionDetectionThread.start();
     }
     static class VersionDetectionThread extends Thread {
+        static final int TARGET_PROCESS_DURATION = 1000;
         private final Pattern versionPattern =
                 Pattern.compile("^\"(\\d+)\\.(\\d+)(?:\\.(\\d+))?\"$");
         private final ProfileDetailModel model;
         public VersionDetectionThread(ProfileDetailModel model) {
             this.model = model;
         }
-        @Override
-        public void run() {
+        private HledgerVersion detectVersion() {
+            HttpURLConnection http = null;
             try {
-                HttpURLConnection http = NetworkUtil.prepareConnection(model.getUrl(), "version",
+                http = NetworkUtil.prepareConnection(model.getUrl(), "version",
                         model.getUseAuthentication());
                 switch (http.getResponseCode()) {
                     case 200:
                         break;
                     case 404:
-                        model.detectedVersion.postValue(new HledgerVersion(true));
-                        return;
+                        return new HledgerVersion(true);
                     default:
                         Logger.debug("profile", String.format(Locale.US,
                                 "HTTP error detecting hledger-web version: [%d] %s",
                                 http.getResponseCode(), http.getResponseMessage()));
-                        model.detectedVersion.postValue(null);
-                        return;
+                        return null;
                 }
                 InputStream stream = http.getInputStream();
                 BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
@@ -318,17 +321,42 @@ public class ProfileDetailModel extends ViewModel {
                     final boolean hasPatch = m.groupCount() >= 3;
                     int patch = hasPatch ? Integer.parseInt(Objects.requireNonNull(m.group(3))) : 0;
 
-                    model.detectedVersion.postValue(
-                            hasPatch ? new HledgerVersion(major, minor, patch)
-                                     : new HledgerVersion(major, minor));
+                    return hasPatch ? new HledgerVersion(major, minor, patch)
+                                    : new HledgerVersion(major, minor);
                 }
                 else {
                     Logger.debug("profile",
                             String.format("Unrecognised version string '%s'", version));
+                    return null;
                 }
             }
             catch (IOException e) {
                 e.printStackTrace();
+                return null;
+            }
+        }
+        @Override
+        public void run() {
+            model.detectingHledgerVersion.postValue(true);
+            try {
+                long startTime = System.currentTimeMillis();
+
+                final HledgerVersion version = detectVersion();
+
+                long elapsed = System.currentTimeMillis() - startTime;
+                Logger.debug("profile", "Detection duration " + elapsed);
+                if (elapsed < TARGET_PROCESS_DURATION) {
+                    try {
+                        Thread.sleep(TARGET_PROCESS_DURATION - elapsed);
+                    }
+                    catch (InterruptedException e) {
+                        e.printStackTrace();
+                    }
+                }
+                model.detectedVersion.postValue(version);
+            }
+            finally {
+                model.detectingHledgerVersion.postValue(false);
             }
         }
     }
index 2667a5200d6d2d066667ac3fa4704ab3d162101a..9fa89479cc2c1ba0ff39ef4f43fa63501a35ef03 100644 (file)
 
             <TextView
                 android:id="@+id/api_version_label"
-                android:layout_width="match_parent"
+                android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:text="@string/profile_api_version_title"
                 android:textAppearance="?android:textAppearanceListItem"
+                android:layout_marginEnd="24dp"
+                app:layout_constraintEnd_toStartOf="@id/detected_version_text"
+                app:layout_constraintStart_toStartOf="parent"
                 app:layout_constraintTop_toTopOf="parent"
                 />
 
                 android:textAppearance="?android:textAppearanceListItemSecondary"
                 android:textColor="?attr/textColor"
                 app:layout_constraintEnd_toStartOf="@id/detected_version_text"
+                android:layout_marginEnd="24dp"
                 app:layout_constraintStart_toStartOf="parent"
                 app:layout_constraintTop_toBottomOf="@id/api_version_label"
                 />
-            <TextView
-                android:id="@+id/detected_version_label"
-                android:layout_width="0dp"
-                android:layout_height="wrap_content"
-                android:layout_marginEnd="8dp"
-                android:gravity="end"
-                android:text="@string/detected_version_label"
-                android:textAppearance="?android:textAppearanceListItemSecondary"
-                app:layout_constraintEnd_toStartOf="@id/detected_version_text"
-                app:layout_constraintStart_toStartOf="parent"
-                app:layout_constraintTop_toBottomOf="@id/api_version_text"
-                />
             <TextView
                 android:id="@+id/detected_version_text"
                 android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
+                android:layout_height="0dp"
                 android:layout_marginEnd="8dp"
                 android:text="@string/api_version_unknown_label"
                 android:textAppearance="?android:textAppearanceListItemSecondary"
                 android:textColor="?attr/textColor"
                 app:layout_constraintEnd_toStartOf="@id/api_version_detect_button"
-                app:layout_constraintTop_toBottomOf="@id/api_version_text"
+                android:gravity="bottom"
+                app:layout_constraintBottom_toBottomOf="parent"
+                app:layout_constraintTop_toTopOf="parent"
                 />
-            <TextView
+            <ProgressBar
+                android:layout_height="24dp"
                 android:id="@+id/api_version_detect_button"
                 android:layout_width="24dp"
-                android:layout_height="0dp"
-                android:drawableBottom="@drawable/ic_refresh_primary_24dp"
+                android:indeterminate="true"
                 android:foregroundGravity="bottom"
                 app:layout_constraintBottom_toBottomOf="parent"
                 app:layout_constraintEnd_toEndOf="parent"
-                app:layout_constraintTop_toBottomOf="@id/api_version_label"
+                android:visibility="invisible"
                 />
         </androidx.constraintlayout.widget.ConstraintLayout>