Fix issue #241 - Store instance info in sharedpref

This commit is contained in:
Thomas 2022-07-11 07:34:33 +02:00
parent 1437976f38
commit 13d8876626
4 changed files with 38 additions and 2 deletions

View File

@ -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);

View File

@ -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<Status> statusDraftList = new ArrayList<>();

View File

@ -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;

View File

@ -895,7 +895,7 @@
<string name="SET_ACCOUNTS_PER_CALL" translatable="false">SET_ACCOUNTS_PER_CALL</string>
<string name="SET_STATUSES_PER_CALL" translatable="false">SET_STATUSES_PER_CALL</string>
<string name="SET_NOTIFICATIONS_PER_CALL" translatable="false">SET_NOTIFICATIONS_PER_CALL</string>
<string name="INSTANCE_INFO" translatable="false">INSTANCE_INFO</string>
<string name="SET_INVIDIOUS" translatable="false">SET_INVIDIOUS</string>
<string name="SET_INVIDIOUS_HOST" translatable="false">SET_INVIDIOUS_HOST</string>
<string name="DEFAULT_INVIDIOUS_HOST" translatable="false">invidious.snopyta.org</string>