From 688d466f8e70e4702cf88f89df0e177ec8aa188d Mon Sep 17 00:00:00 2001 From: sk Date: Fri, 4 Nov 2022 03:09:19 +0100 Subject: [PATCH] implement manual update check settings item --- .../updater/GithubSelfUpdaterImpl.java | 8 ++++ .../android/fragments/SettingsFragment.java | 43 +++++++++++++++---- .../android/updater/GithubSelfUpdater.java | 2 + .../main/res/layout/item_settings_text.xml | 33 ++++++++++---- .../src/main/res/values-de-rDE/strings.xml | 1 + mastodon/src/main/res/values/strings.xml | 1 + 6 files changed, 72 insertions(+), 16 deletions(-) diff --git a/mastodon/src/github/java/org/joinmastodon/android/updater/GithubSelfUpdaterImpl.java b/mastodon/src/github/java/org/joinmastodon/android/updater/GithubSelfUpdaterImpl.java index 314fee665..b5fd62826 100644 --- a/mastodon/src/github/java/org/joinmastodon/android/updater/GithubSelfUpdaterImpl.java +++ b/mastodon/src/github/java/org/joinmastodon/android/updater/GithubSelfUpdaterImpl.java @@ -26,6 +26,8 @@ import org.joinmastodon.android.api.MastodonAPIController; import org.joinmastodon.android.events.SelfUpdateStateChangedEvent; import java.io.File; +import java.time.Instant; +import java.time.LocalDateTime; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -101,6 +103,12 @@ public class GithubSelfUpdaterImpl extends GithubSelfUpdater{ } } + @Override + public void checkForUpdates() { + setState(UpdateState.CHECKING); + MastodonAPIController.runInBackground(this::actuallyCheckForUpdates); + } + private void actuallyCheckForUpdates(){ Request req=new Request.Builder() .url("https://api.github.com/repos/mastodon/mastodon-android/releases/latest") diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/SettingsFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/SettingsFragment.java index 8b1590595..843c98346 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/SettingsFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/SettingsFragment.java @@ -15,6 +15,7 @@ import android.view.View; import android.view.ViewGroup; import android.view.WindowInsets; import android.view.WindowManager; +import android.view.animation.AlphaAnimation; import android.view.animation.LinearInterpolator; import android.widget.Button; import android.widget.ImageButton; @@ -71,6 +72,7 @@ public class SettingsFragment extends MastodonToolbarFragment{ private PushSubscription pushSubscription; private ImageView themeTransitionWindowView; + private TextItem checkForUpdateItem; @Override public void onCreate(Bundle savedInstanceState){ @@ -118,6 +120,10 @@ public class SettingsFragment extends MastodonToolbarFragment{ items.add(new TextItem(R.string.settings_privacy_policy, ()->UiUtils.launchWebBrowser(getActivity(), "https://"+session.domain+"/terms"))); items.add(new RedHeaderItem(R.string.settings_spicy)); + if (GithubSelfUpdater.needSelfUpdating()) { + checkForUpdateItem = new TextItem(R.string.check_for_update, GithubSelfUpdater.getInstance()::checkForUpdates); + items.add(checkForUpdateItem); + } items.add(new TextItem(R.string.settings_clear_cache, this::clearImageCache)); items.add(new TextItem(R.string.log_out, this::confirmLogOut)); @@ -326,11 +332,21 @@ public class SettingsFragment extends MastodonToolbarFragment{ @Subscribe public void onSelfUpdateStateChanged(SelfUpdateStateChangedEvent ev){ - if(items.get(0) instanceof UpdateItem item){ - RecyclerView.ViewHolder holder=list.findViewHolderForAdapterPosition(0); - if(holder instanceof UpdateViewHolder uvh){ - uvh.bind(item); - } + checkForUpdateItem.loading = ev.state == GithubSelfUpdater.UpdateState.CHECKING; + if (list.findViewHolderForAdapterPosition(items.indexOf(checkForUpdateItem)) instanceof TextViewHolder tvh) tvh.rebind(); + + UpdateItem updateItem = null; + if(items.get(0) instanceof UpdateItem item0) { + updateItem = item0; + } else if (ev.state != GithubSelfUpdater.UpdateState.CHECKING + && ev.state != GithubSelfUpdater.UpdateState.NO_UPDATE) { + updateItem = new UpdateItem(); + items.add(0, updateItem); + list.setAdapter(new SettingsAdapter()); + } + + if(updateItem != null && list.findViewHolderForAdapterPosition(0) instanceof UpdateViewHolder uvh){ + uvh.bind(updateItem); } } @@ -398,10 +414,16 @@ public class SettingsFragment extends MastodonToolbarFragment{ private class TextItem extends Item{ private String text; private Runnable onClick; + private boolean loading; - public TextItem(@StringRes int text, Runnable onClick){ + public TextItem(@StringRes int text, Runnable onClick) { + this(text, onClick, false); + } + + public TextItem(@StringRes int text, Runnable onClick, boolean loading){ this.text=getString(text); this.onClick=onClick; + this.loading=loading; } @Override @@ -630,14 +652,18 @@ public class SettingsFragment extends MastodonToolbarFragment{ private class TextViewHolder extends BindableViewHolder implements UsableRecyclerView.Clickable{ private final TextView text; + private final ProgressBar progress; + public TextViewHolder(){ super(getActivity(), R.layout.item_settings_text, list); - text=(TextView) itemView; + text = itemView.findViewById(R.id.text); + progress = itemView.findViewById(R.id.progress); } @Override public void onBind(TextItem item){ text.setText(item.text); + progress.animate().alpha(item.loading ? 1 : 0); } @Override @@ -692,8 +718,9 @@ public class SettingsFragment extends MastodonToolbarFragment{ @Override public void onBind(UpdateItem item){ GithubSelfUpdater updater=GithubSelfUpdater.getInstance(); - GithubSelfUpdater.UpdateInfo info=updater.getUpdateInfo(); GithubSelfUpdater.UpdateState state=updater.getState(); + if (state == GithubSelfUpdater.UpdateState.CHECKING) return; + GithubSelfUpdater.UpdateInfo info=updater.getUpdateInfo(); if(state!=GithubSelfUpdater.UpdateState.DOWNLOADED){ text.setText(getString(R.string.update_available, info.version)); button.setText(getString(R.string.download_update, UiUtils.formatFileSize(getActivity(), info.size, false))); diff --git a/mastodon/src/main/java/org/joinmastodon/android/updater/GithubSelfUpdater.java b/mastodon/src/main/java/org/joinmastodon/android/updater/GithubSelfUpdater.java index 810c516dc..600c3ea6a 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/updater/GithubSelfUpdater.java +++ b/mastodon/src/main/java/org/joinmastodon/android/updater/GithubSelfUpdater.java @@ -23,6 +23,8 @@ public abstract class GithubSelfUpdater{ return BuildConfig.BUILD_TYPE.equals("githubRelease"); } + public abstract void checkForUpdates(); + public abstract void maybeCheckForUpdates(); public abstract GithubSelfUpdater.UpdateState getState(); diff --git a/mastodon/src/main/res/layout/item_settings_text.xml b/mastodon/src/main/res/layout/item_settings_text.xml index 45d35542e..affa2bb5f 100644 --- a/mastodon/src/main/res/layout/item_settings_text.xml +++ b/mastodon/src/main/res/layout/item_settings_text.xml @@ -1,13 +1,30 @@ - \ No newline at end of file + android:layoutDirection="locale"> + + + \ No newline at end of file diff --git a/mastodon/src/main/res/values-de-rDE/strings.xml b/mastodon/src/main/res/values-de-rDE/strings.xml index 0c19347fb..2f8c13b9e 100644 --- a/mastodon/src/main/res/values-de-rDE/strings.xml +++ b/mastodon/src/main/res/values-de-rDE/strings.xml @@ -377,4 +377,5 @@ Download (%s) Installieren + Auf Update prüfen diff --git a/mastodon/src/main/res/values/strings.xml b/mastodon/src/main/res/values/strings.xml index 2ee4d9975..9cc98cba2 100644 --- a/mastodon/src/main/res/values/strings.xml +++ b/mastodon/src/main/res/values/strings.xml @@ -384,6 +384,7 @@ Download (%s) Install + Check for update Mastodon and your privacy Although the Mastodon app does not collect any data, the server you sign up through may have a different policy. Take a minute to review and agree to the Mastodon app privacy policy and your server\'s privacy policy. I Agree