fix: fix updateItem not updating state after UpdateState change

This fixes the auto updater on the new settings redesign
This commit is contained in:
LucasGGamerM 2023-06-01 11:51:26 -03:00
parent 9478258caa
commit 8ea752fbf7
3 changed files with 64 additions and 5 deletions

View File

@ -7,9 +7,13 @@ import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.LruCache;
import android.view.View;
import android.widget.Toast;
import com.squareup.otto.Subscribe;
import org.joinmastodon.android.BuildConfig;
import org.joinmastodon.android.E;
import org.joinmastodon.android.GlobalUserPreferences;
import org.joinmastodon.android.MainActivity;
import org.joinmastodon.android.R;
@ -18,6 +22,7 @@ import org.joinmastodon.android.api.requests.oauth.RevokeOauthToken;
import org.joinmastodon.android.api.session.AccountActivationInfo;
import org.joinmastodon.android.api.session.AccountSession;
import org.joinmastodon.android.api.session.AccountSessionManager;
import org.joinmastodon.android.events.SelfUpdateStateChangedEvent;
import org.joinmastodon.android.fragments.onboarding.AccountActivationFragment;
import org.joinmastodon.android.fragments.onboarding.InstanceRulesFragment;
import org.joinmastodon.android.ui.M3AlertDialogBuilder;
@ -36,6 +41,9 @@ public class AboutFragment extends SettingsBaseFragment{
private TextItem clearImageCacheItem;
private ImageCache imageCache;
private TextItem checkForUpdateItem;
@Override
public void addItems(ArrayList<Item> items) {
@ -45,7 +53,7 @@ public class AboutFragment extends SettingsBaseFragment{
items.add(new TextItem(R.string.sk_settings_donate, ()->UiUtils.launchWebBrowser(getActivity(), "https://github.com/sponsors/LucasGGamerM"), R.drawable.ic_fluent_heart_24_regular));
if (GithubSelfUpdater.needSelfUpdating()) {
TextItem checkForUpdateItem = new TextItem(R.string.sk_check_for_update, GithubSelfUpdater.getInstance()::checkForUpdates);
checkForUpdateItem = new TextItem(R.string.sk_check_for_update, GithubSelfUpdater.getInstance()::checkForUpdates);
items.add(checkForUpdateItem);
items.add(new SwitchItem(R.string.sk_updater_enable_pre_releases, 0, GlobalUserPreferences.enablePreReleases, i->{
GlobalUserPreferences.enablePreReleases=i.checked;
@ -109,6 +117,27 @@ public class AboutFragment extends SettingsBaseFragment{
items.add(new FooterItem(version, () -> UiUtils.copyText(view, version)));
}
@Subscribe
public void onSelfUpdateStateChanged(SelfUpdateStateChangedEvent ev){
checkForUpdateItem.loading = ev.state == GithubSelfUpdater.UpdateState.CHECKING;
if (list.findViewHolderForAdapterPosition(items.indexOf(checkForUpdateItem)) instanceof TextViewHolder tvh) tvh.rebind();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (GithubSelfUpdater.needSelfUpdating()) {
E.register(this);
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (GithubSelfUpdater.needSelfUpdating())
E.unregister(this);
}
private void clearImageCache(){
MastodonAPIController.runInBackground(()->{
Activity activity=getActivity();

View File

@ -328,7 +328,7 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple
private String text;
protected String secondaryText;
private Runnable onClick;
private boolean loading;
protected boolean loading;
private int icon;
public TextItem(@StringRes int text, Runnable onClick) {
@ -423,7 +423,7 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple
}
private class SettingsAdapter extends RecyclerView.Adapter<BindableViewHolder<Item>>{
protected class SettingsAdapter extends RecyclerView.Adapter<BindableViewHolder<Item>>{
@NonNull
@Override
@ -871,7 +871,7 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple
}
}
private class UpdateViewHolder extends BindableViewHolder<UpdateItem>{
protected class UpdateViewHolder extends BindableViewHolder<UpdateItem>{
private final TextView text, changelog, changelogHeader;
private final Button button;

View File

@ -2,14 +2,20 @@ package org.joinmastodon.android.fragments.settings;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.squareup.otto.Subscribe;
import org.joinmastodon.android.E;
import org.joinmastodon.android.R;
import org.joinmastodon.android.events.SelfUpdateStateChangedEvent;
import org.joinmastodon.android.updater.GithubSelfUpdater;
import java.util.ArrayList;
public class SettingsMainFragment extends SettingsBaseFragment {
protected UpdateItem updateItem;
@Override
public void addItems(ArrayList<Item> items) {
@ -17,7 +23,7 @@ public class SettingsMainFragment extends SettingsBaseFragment {
GithubSelfUpdater updater = GithubSelfUpdater.getInstance();
GithubSelfUpdater.UpdateState state = updater.getState();
if (state != GithubSelfUpdater.UpdateState.NO_UPDATE && state != GithubSelfUpdater.UpdateState.CHECKING && updater.getUpdateInfo() != null) {
items.add(new SettingsBaseFragment.UpdateItem());
items.add(updateItem = new SettingsBaseFragment.UpdateItem());
}
}
@ -30,6 +36,30 @@ public class SettingsMainFragment extends SettingsBaseFragment {
}
@Subscribe
public void onSelfUpdateStateChanged(SelfUpdateStateChangedEvent ev){
// checkForUpdateItem.loading = ev.state == GithubSelfUpdater.UpdateState.CHECKING;
// if (list.findViewHolderForAdapterPosition(items.indexOf(checkForUpdateItem)) instanceof TextViewHolder tvh) tvh.rebind();
if (ev.state != GithubSelfUpdater.UpdateState.CHECKING
&& ev.state != GithubSelfUpdater.UpdateState.NO_UPDATE) {
updateItem = new UpdateItem();
items.remove(1);
items.add(1, updateItem);
list.setAdapter(new SettingsAdapter());
}
if(updateItem != null && list.findViewHolderForAdapterPosition(0) instanceof SettingsBaseFragment.UpdateViewHolder uvh){
uvh.bind(updateItem);
}
if (ev.state == GithubSelfUpdater.UpdateState.NO_UPDATE) {
Toast.makeText(getActivity(), R.string.sk_no_update_available, Toast.LENGTH_SHORT).show();
} else if (ev.state == GithubSelfUpdater.UpdateState.UPDATE_AVAILABLE){
Toast.makeText(getActivity(), getString(R.string.mo_update_available, GithubSelfUpdater.getInstance().getUpdateInfo().version), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);