getInstance returns optional

This commit is contained in:
sk
2023-05-30 18:57:17 +02:00
parent 8df589c103
commit 0985a4c968
9 changed files with 32 additions and 24 deletions

View File

@@ -15,6 +15,7 @@ import org.joinmastodon.android.model.Token;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
public class AccountSession{ public class AccountSession{
public Token token; public Token token;
@@ -89,7 +90,7 @@ public class AccountSession{
return pushSubscriptionManager; return pushSubscriptionManager;
} }
public Instance getInstance() { public Optional<Instance> getInstance() {
return AccountSessionManager.getInstance().getInstanceInfo(domain); return Optional.ofNullable(AccountSessionManager.getInstance().getInstanceInfo(domain));
} }
} }

View File

@@ -263,9 +263,6 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr
Nav.finish(this); Nav.finish(this);
return; return;
} }
if(customEmojis.isEmpty()){
AccountSessionManager.getInstance().updateInstanceInfo(instanceDomain);
}
Bundle bundle = savedInstanceState != null ? savedInstanceState : getArguments(); Bundle bundle = savedInstanceState != null ? savedInstanceState : getArguments();
if (bundle.containsKey("scheduledStatus")) scheduledStatus=Parcels.unwrap(bundle.getParcelable("scheduledStatus")); if (bundle.containsKey("scheduledStatus")) scheduledStatus=Parcels.unwrap(bundle.getParcelable("scheduledStatus"));

View File

@@ -35,6 +35,7 @@ import org.parceler.Parcels;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List; import java.util.List;
import java.util.Optional;
import androidx.annotation.IdRes; import androidx.annotation.IdRes;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@@ -74,8 +75,9 @@ public class HomeFragment extends AppKitFragment implements OnBackPressedListene
E.register(this); E.register(this);
accountID=getArguments().getString("account"); accountID=getArguments().getString("account");
setTitle(R.string.sk_app_name); setTitle(R.string.sk_app_name);
Instance instance = AccountSessionManager.getInstance().getAccount(accountID).getInstance(); isPleroma = AccountSessionManager.getInstance().getAccount(accountID).getInstance()
isPleroma = instance.isPleroma(); .map(Instance::isPleroma)
.orElse(false);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N) if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N)
setRetainInstance(true); setRetainInstance(true);
@@ -296,10 +298,10 @@ public class HomeFragment extends AppKitFragment implements OnBackPressedListene
public void updateNotificationBadge() { public void updateNotificationBadge() {
AccountSession session = AccountSessionManager.getInstance().getAccount(accountID); AccountSession session = AccountSessionManager.getInstance().getAccount(accountID);
Instance instance = session.getInstance(); Optional<Instance> instance = session.getInstance();
if (instance == null) return; if (instance.isEmpty()) return; // avoiding incompatibility with akkoma
new GetNotifications(null, 1, EnumSet.allOf(Notification.Type.class), instance != null && instance.isPleroma()) new GetNotifications(null, 1, EnumSet.allOf(Notification.Type.class), instance.get().isPleroma())
.setCallback(new Callback<>() { .setCallback(new Callback<>() {
@Override @Override
public void onSuccess(List<Notification> notifications) { public void onSuccess(List<Notification> notifications) {

View File

@@ -164,7 +164,7 @@ public class NotificationsListFragment extends BaseStatusListFragment<Notificati
.notifications.lastReadId = result.items.get(0).id; .notifications.lastReadId = result.items.get(0).id;
AccountSessionManager.getInstance().writeAccountsFile(); AccountSessionManager.getInstance().writeAccountsFile();
if (AccountSessionManager.getInstance().getAccount(accountID).getInstance().isPleroma()) if (AccountSessionManager.getInstance().getAccount(accountID).getInstance().map(Instance::isPleroma).orElse(false))
new PleromaMarkNotificationsRead(result.items.get(0).id).exec(accountID); new PleromaMarkNotificationsRead(result.items.get(0).id).exec(accountID);
} }
} }

View File

@@ -184,7 +184,7 @@ public class ProfileFragment extends LoaderFragment implements OnBackPressedList
loadRelationship(); loadRelationship();
else { else {
Instance instance = AccountSessionManager.getInstance().getInstanceInfo(domain); Instance instance = AccountSessionManager.getInstance().getInstanceInfo(domain);
if (instance.isPleroma()) { if (instance != null && instance.isPleroma()) {
maxFields = instance.pleroma.metadata.fieldsLimits.maxFields; maxFields = instance.pleroma.metadata.fieldsLimits.maxFields;
} }
} }

View File

@@ -9,7 +9,6 @@ import android.graphics.Canvas;
import android.graphics.Rect; import android.graphics.Rect;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils;
import android.util.LruCache; import android.util.LruCache;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.Gravity; import android.view.Gravity;
@@ -62,6 +61,7 @@ import org.joinmastodon.android.updater.GithubSelfUpdater;
import org.parceler.Parcels; import org.parceler.Parcels;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import androidx.annotation.DrawableRes; import androidx.annotation.DrawableRes;
@@ -105,7 +105,7 @@ public class SettingsFragment extends MastodonToolbarFragment{
imageCache = ImageCache.getInstance(getActivity()); imageCache = ImageCache.getInstance(getActivity());
accountID=getArguments().getString("account"); accountID=getArguments().getString("account");
AccountSession session=AccountSessionManager.getInstance().getAccount(accountID); AccountSession session=AccountSessionManager.getInstance().getAccount(accountID);
Instance instance = session.getInstance(); Optional<Instance> instance = session.getInstance();
String instanceName = UiUtils.getInstanceName(accountID); String instanceName = UiUtils.getInstanceName(accountID);
if(GithubSelfUpdater.needSelfUpdating()){ if(GithubSelfUpdater.needSelfUpdating()){
@@ -223,7 +223,7 @@ public class SettingsFragment extends MastodonToolbarFragment{
GlobalUserPreferences.showReplies=i.checked; GlobalUserPreferences.showReplies=i.checked;
GlobalUserPreferences.save(); GlobalUserPreferences.save();
})); }));
if (instance.isPleroma()) { if (instance.map(Instance::isPleroma).orElse(false)) {
items.add(new ButtonItem(R.string.sk_settings_reply_visibility, R.drawable.ic_fluent_chat_24_regular, b->{ items.add(new ButtonItem(R.string.sk_settings_reply_visibility, R.drawable.ic_fluent_chat_24_regular, b->{
PopupMenu popupMenu=new PopupMenu(getActivity(), b, Gravity.CENTER_HORIZONTAL); PopupMenu popupMenu=new PopupMenu(getActivity(), b, Gravity.CENTER_HORIZONTAL);
popupMenu.inflate(R.menu.reply_visibility); popupMenu.inflate(R.menu.reply_visibility);
@@ -299,7 +299,9 @@ public class SettingsFragment extends MastodonToolbarFragment{
GlobalUserPreferences.save(); GlobalUserPreferences.save();
needAppRestart=true; needAppRestart=true;
})); }));
boolean translationAvailable = instance.v2 != null && instance.v2.configuration.translation != null && instance.v2.configuration.translation.enabled; boolean translationAvailable = instance
.map(i -> i.v2 != null && i.v2.configuration.translation != null && i.v2.configuration.translation.enabled)
.orElse(false);
items.add(new SmallTextItem(getString(translationAvailable ? items.add(new SmallTextItem(getString(translationAvailable ?
R.string.sk_settings_translation_availability_note_available : R.string.sk_settings_translation_availability_note_available :
R.string.sk_settings_translation_availability_note_unavailable, instanceName))); R.string.sk_settings_translation_availability_note_unavailable, instanceName)));
@@ -333,7 +335,9 @@ public class SettingsFragment extends MastodonToolbarFragment{
items.add(new TextItem(R.string.settings_tos, ()->UiUtils.launchWebBrowser(getActivity(), "https://"+session.domain+"/terms"), R.drawable.ic_fluent_open_24_regular)); items.add(new TextItem(R.string.settings_tos, ()->UiUtils.launchWebBrowser(getActivity(), "https://"+session.domain+"/terms"), R.drawable.ic_fluent_open_24_regular));
items.add(new TextItem(R.string.settings_privacy_policy, ()->UiUtils.launchWebBrowser(getActivity(), "https://"+session.domain+"/terms"), R.drawable.ic_fluent_open_24_regular)); items.add(new TextItem(R.string.settings_privacy_policy, ()->UiUtils.launchWebBrowser(getActivity(), "https://"+session.domain+"/terms"), R.drawable.ic_fluent_open_24_regular));
items.add(new TextItem(R.string.log_out, this::confirmLogOut, R.drawable.ic_fluent_sign_out_24_regular)); items.add(new TextItem(R.string.log_out, this::confirmLogOut, R.drawable.ic_fluent_sign_out_24_regular));
if (!TextUtils.isEmpty(instance.version)) items.add(new SmallTextItem(getString(R.string.sk_settings_server_version, instance.version))); items.add(new SmallTextItem(instance
.map(i -> getString(R.string.sk_settings_server_version, i.version))
.orElse(getString(R.string.sk_instance_info_unavailable))));
items.add(new HeaderItem(R.string.sk_instance_features)); items.add(new HeaderItem(R.string.sk_instance_features));
items.add(new SwitchItem(R.string.sk_settings_content_types, 0, GlobalUserPreferences.accountsWithContentTypesEnabled.contains(accountID), (i)->{ items.add(new SwitchItem(R.string.sk_settings_content_types, 0, GlobalUserPreferences.accountsWithContentTypesEnabled.contains(accountID), (i)->{
@@ -361,14 +365,16 @@ public class SettingsFragment extends MastodonToolbarFragment{
b.setText(getContentTypeString(contentType)); b.setText(getContentTypeString(contentType));
contentTypeMenu = popupMenu.getMenu(); contentTypeMenu = popupMenu.getMenu();
contentTypeMenu.findItem(ContentType.getContentTypeRes(contentType)).setChecked(true); contentTypeMenu.findItem(ContentType.getContentTypeRes(contentType)).setChecked(true);
ContentType.adaptMenuToInstance(contentTypeMenu, instance); instance.ifPresent(i -> ContentType.adaptMenuToInstance(contentTypeMenu, i));
})); }));
items.add(new SmallTextItem(getString(R.string.sk_settings_default_content_type_explanation))); items.add(new SmallTextItem(getString(R.string.sk_settings_default_content_type_explanation)));
items.add(new SwitchItem(R.string.sk_settings_support_local_only, 0, GlobalUserPreferences.accountsWithLocalOnlySupport.contains(accountID), i->{ items.add(new SwitchItem(R.string.sk_settings_support_local_only, 0, GlobalUserPreferences.accountsWithLocalOnlySupport.contains(accountID), i->{
glitchModeItem.enabled = i.checked; glitchModeItem.enabled = i.checked;
if (i.checked) { if (i.checked) {
GlobalUserPreferences.accountsWithLocalOnlySupport.add(accountID); GlobalUserPreferences.accountsWithLocalOnlySupport.add(accountID);
if (instance.pleroma == null) GlobalUserPreferences.accountsInGlitchMode.add(accountID); if (!instance.map(Instance::isPleroma).orElse(false)) {
GlobalUserPreferences.accountsInGlitchMode.add(accountID);
}
} else { } else {
GlobalUserPreferences.accountsWithLocalOnlySupport.remove(accountID); GlobalUserPreferences.accountsWithLocalOnlySupport.remove(accountID);
GlobalUserPreferences.accountsInGlitchMode.remove(accountID); GlobalUserPreferences.accountsInGlitchMode.remove(accountID);

View File

@@ -259,13 +259,14 @@ public class TimelineDefinition {
public boolean isCompatible(AccountSession session) { public boolean isCompatible(AccountSession session) {
// still enabling the bubble timeline for all pleroma/akkoma instances since i know of // still enabling the bubble timeline for all pleroma/akkoma instances since i know of
// at least one instance that supports it, but doesn't list "bubble_timeline" // at least one instance that supports it, but doesn't list "bubble_timeline"
return session.getInstance().isPleroma(); return session.getInstance().map(Instance::isPleroma).orElse(false);
} }
@Override @Override
public boolean wantsDefault(AccountSession session) { public boolean wantsDefault(AccountSession session) {
Instance instance = session.getInstance(); return session.getInstance()
return instance.isPleroma() && instance.pleroma.metadata.features.contains("bubble_timeline"); .map(i -> i.isPleroma() && i.pleroma.metadata.features.contains("bubble_timeline"))
.orElse(false);
} }
}; };

View File

@@ -948,8 +948,8 @@ public class UiUtils {
public static String getInstanceName(String accountID) { public static String getInstanceName(String accountID) {
AccountSession session = AccountSessionManager.getInstance().getAccount(accountID); AccountSession session = AccountSessionManager.getInstance().getAccount(accountID);
Instance instance = session.getInstance(); Optional<Instance> instance = session.getInstance();
return instance != null && !instance.title.isBlank() ? instance.title : session.domain; return instance.isPresent() && !instance.get().title.isBlank() ? instance.get().title : session.domain;
} }
public static void pickAccount(Context context, String exceptFor, @StringRes int titleRes, @DrawableRes int iconRes, Consumer<AccountSession> sessionConsumer, Consumer<AlertDialog.Builder> transformDialog) { public static void pickAccount(Context context, String exceptFor, @StringRes int titleRes, @DrawableRes int iconRes, Consumer<AccountSession> sessionConsumer, Consumer<AlertDialog.Builder> transformDialog) {

View File

@@ -288,4 +288,5 @@
<string name="sk_settings_content_types_explanation">Allows setting a content type like Markdown when creating a post. Keep in mind that not all instances support this.</string> <string name="sk_settings_content_types_explanation">Allows setting a content type like Markdown when creating a post. Keep in mind that not all instances support this.</string>
<string name="sk_settings_default_content_type">Default content type</string> <string name="sk_settings_default_content_type">Default content type</string>
<string name="sk_settings_default_content_type_explanation">This lets you have a content type be pre-selected when creating new posts, overriding the value set in “Posting preferences”.</string> <string name="sk_settings_default_content_type_explanation">This lets you have a content type be pre-selected when creating new posts, overriding the value set in “Posting preferences”.</string>
<string name="sk_instance_info_unavailable">Instance info temporarily unavailable</string>
</resources> </resources>