From 13d8876626404f349d2d2a40f32f67b3edeec39a Mon Sep 17 00:00:00 2001 From: Thomas Date: Mon, 11 Jul 2022 07:34:33 +0200 Subject: [PATCH] Fix issue #241 - Store instance info in sharedpref --- .../app/fedilab/android/BaseMainActivity.java | 7 ++++++- .../android/activities/ComposeActivity.java | 11 ++++++++++ .../android/client/entities/api/Instance.java | 20 +++++++++++++++++++ app/src/main/res/values/strings.xml | 2 +- 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/BaseMainActivity.java b/app/src/main/java/app/fedilab/android/BaseMainActivity.java index 343bbcc5..19532dd7 100644 --- a/app/src/main/java/app/fedilab/android/BaseMainActivity.java +++ b/app/src/main/java/app/fedilab/android/BaseMainActivity.java @@ -786,7 +786,12 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt new ViewModelProvider(BaseMainActivity.this).get(InstancesVM.class).getEmoji(currentInstance); //Retrieve instance info new ViewModelProvider(BaseMainActivity.this).get(InstancesVM.class).getInstance(currentInstance) - .observe(BaseMainActivity.this, instance -> instanceInfo = instance.info); + .observe(BaseMainActivity.this, instance -> { + instanceInfo = instance.info; + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putString(getString(R.string.INSTANCE_INFO) + MainActivity.currentInstance, Instance.serialize(instanceInfo)); + editor.apply(); + }); //Retrieve filters new ViewModelProvider(BaseMainActivity.this).get(AccountsVM.class).getFilters(currentInstance, currentToken) .observe(BaseMainActivity.this, filters -> mainFilters = filters); diff --git a/app/src/main/java/app/fedilab/android/activities/ComposeActivity.java b/app/src/main/java/app/fedilab/android/activities/ComposeActivity.java index 58c28c85..0a88e8d4 100644 --- a/app/src/main/java/app/fedilab/android/activities/ComposeActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/ComposeActivity.java @@ -26,6 +26,7 @@ import android.content.BroadcastReceiver; import android.content.ClipData; import android.content.Intent; import android.content.IntentFilter; +import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.graphics.drawable.ColorDrawable; import android.net.Uri; @@ -48,6 +49,7 @@ import androidx.appcompat.app.AlertDialog; import androidx.core.content.ContextCompat; import androidx.lifecycle.ViewModelProvider; import androidx.localbroadcastmanager.content.LocalBroadcastManager; +import androidx.preference.PreferenceManager; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.work.Data; import androidx.work.OneTimeWorkRequest; @@ -69,6 +71,7 @@ import app.fedilab.android.R; import app.fedilab.android.client.entities.api.Attachment; import app.fedilab.android.client.entities.api.Context; import app.fedilab.android.client.entities.api.EmojiInstance; +import app.fedilab.android.client.entities.api.Instance; import app.fedilab.android.client.entities.api.Mention; import app.fedilab.android.client.entities.api.ScheduledStatus; import app.fedilab.android.client.entities.api.Status; @@ -234,6 +237,14 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana } }).start(); } + final SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(ComposeActivity.this); + if (MainActivity.instanceInfo == null) { + String instanceInfo = sharedpreferences.getString(getString(R.string.INSTANCE_INFO) + instance, null); + if (instanceInfo != null) { + MainActivity.instanceInfo = Instance.restore(instanceInfo); + } + } + StatusesVM statusesVM = new ViewModelProvider(ComposeActivity.this).get(StatusesVM.class); //Empty compose List statusDraftList = new ArrayList<>(); diff --git a/app/src/main/java/app/fedilab/android/client/entities/api/Instance.java b/app/src/main/java/app/fedilab/android/client/entities/api/Instance.java index 3b9547ef..1ea2c726 100644 --- a/app/src/main/java/app/fedilab/android/client/entities/api/Instance.java +++ b/app/src/main/java/app/fedilab/android/client/entities/api/Instance.java @@ -1,5 +1,6 @@ package app.fedilab.android.client.entities.api; +import com.google.gson.Gson; import com.google.gson.annotations.SerializedName; import java.io.Serializable; @@ -113,6 +114,25 @@ public class Instance implements Serializable { return mimeTypes; } + + public static String serialize(Instance instance) { + Gson gson = new Gson(); + try { + return gson.toJson(instance); + } catch (Exception e) { + return null; + } + } + + public static Instance restore(String serialized) { + Gson gson = new Gson(); + try { + return gson.fromJson(serialized, Instance.class); + } catch (Exception e) { + return null; + } + } + public static class Configuration implements Serializable { @SerializedName("statuses") public StatusesConf statusesConf; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c50d3439..726ab01d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -895,7 +895,7 @@ SET_ACCOUNTS_PER_CALL SET_STATUSES_PER_CALL SET_NOTIFICATIONS_PER_CALL - + INSTANCE_INFO SET_INVIDIOUS SET_INVIDIOUS_HOST invidious.snopyta.org