From: Damyan Ivanov Date: Sun, 11 Oct 2020 19:37:03 +0000 (+0300) Subject: better version detection progress X-Git-Tag: v0.16.0~33 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=1059279ca52a15efff01c00a67a2a647fef4a1ba;p=mobile-ledger.git better version detection progress --- diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java index 9b0b60b7..01c0143d 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java @@ -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); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailModel.java index 92d1505f..4cbbfe3d 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailModel.java @@ -61,6 +61,7 @@ public class ProfileDetailModel extends ViewModel { private final MutableLiveData preferredAccountsFilter = new MutableLiveData<>(null); private final MutableLiveData themeId = new MutableLiveData<>(-1); private final MutableLiveData detectedVersion = new MutableLiveData<>(null); + private final MutableLiveData 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 o) { themeId.observe(lfo, o); } + void observeDetectingHledgerVersion(LifecycleOwner lfo, Observer 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); } } } diff --git a/app/src/main/res/layout/profile_detail.xml b/app/src/main/res/layout/profile_detail.xml index 2667a520..9fa89479 100644 --- a/app/src/main/res/layout/profile_detail.xml +++ b/app/src/main/res/layout/profile_detail.xml @@ -152,10 +152,13 @@ @@ -166,41 +169,32 @@ 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" /> - -