remote conversations

This commit is contained in:
Thomas 2023-07-14 11:11:27 +02:00
parent 6d983a74de
commit ce607226d9
3 changed files with 83 additions and 7 deletions

View file

@ -16,6 +16,7 @@ package app.fedilab.android.mastodon.activities;
import static app.fedilab.android.BaseMainActivity.currentAccount;
import static app.fedilab.android.BaseMainActivity.currentInstance;
import android.content.Intent;
import android.content.SharedPreferences;
@ -60,6 +61,8 @@ public class ContextActivity extends BaseActivity implements FragmentMastodonCon
Fragment currentFragment;
private Status firstMessage;
private String remote_instance;
private Status focusedStatus;
private boolean checkRemotely;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -84,7 +87,7 @@ public class ContextActivity extends BaseActivity implements FragmentMastodonCon
}
Bundle b = getIntent().getExtras();
displayCW = sharedpreferences.getBoolean(getString(R.string.SET_EXPAND_CW), false);
Status focusedStatus = null; // or other values
focusedStatus = null; // or other values
if (b != null) {
focusedStatus = (Status) b.getSerializable(Helper.ARG_STATUS);
remote_instance = b.getString(Helper.ARG_REMOTE_INSTANCE, null);
@ -94,12 +97,24 @@ public class ContextActivity extends BaseActivity implements FragmentMastodonCon
return;
}
MastodonHelper.loadPPMastodon(binding.profilePicture, currentAccount.mastodon_account);
checkRemotely = sharedpreferences.getBoolean(getString(R.string.SET_CONVERSATION_REMOTELY), false);
if (!checkRemotely) {
loadLocalConversation();
} else {
loadRemotelyConversation(true);
invalidateOptionsMenu();
}
}
private void loadLocalConversation() {
Bundle bundle = new Bundle();
bundle.putSerializable(Helper.ARG_STATUS, focusedStatus);
bundle.putString(Helper.ARG_REMOTE_INSTANCE, remote_instance);
FragmentMastodonContext fragmentMastodonContext = new FragmentMastodonContext();
fragmentMastodonContext.firstMessage = this;
currentFragment = Helper.addFragment(getSupportFragmentManager(), R.id.nav_host_fragment_content_main, fragmentMastodonContext, bundle, null, null);
//Update the status
if (remote_instance == null) {
StatusesVM timelinesVM = new ViewModelProvider(ContextActivity.this).get(StatusesVM.class);
timelinesVM.getStatus(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, focusedStatus.id).observe(ContextActivity.this, status -> {
@ -126,7 +141,6 @@ public class ContextActivity extends BaseActivity implements FragmentMastodonCon
}
}
@Override
public boolean onCreateOptionsMenu(@NonNull Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
@ -144,7 +158,7 @@ public class ContextActivity extends BaseActivity implements FragmentMastodonCon
itemDisplayCW.setIcon(R.drawable.ic_outline_remove_red_eye_24);
}
MenuItem action_remote = menu.findItem(R.id.action_remote);
if (remote_instance != null) {
if (remote_instance != null || checkRemotely) {
action_remote.setVisible(false);
} else {
if (firstMessage != null && !firstMessage.visibility.equalsIgnoreCase("direct") && !firstMessage.visibility.equalsIgnoreCase("private")) {
@ -181,10 +195,62 @@ public class ContextActivity extends BaseActivity implements FragmentMastodonCon
}
invalidateOptionsMenu();
} else if (item.getItemId() == R.id.action_remote) {
loadRemotelyConversation(false);
}
return true;
}
private void loadRemotelyConversation(boolean fallback) {
if (fallback) {
StatusesVM statusesVM;
statusesVM = new ViewModelProvider(this).get(StatusesVM.class);
statusesVM.getContext(currentInstance, null, focusedStatus.id)
.observe(this, result -> {
if (result != null && result.ancestors != null && result.ancestors.size() > 0) {
firstMessage = result.ancestors.get(0);
String instance = null;
try {
URL url = new URL(firstMessage.uri);
instance = url.getHost();
} catch (MalformedURLException e) {
e.printStackTrace();
}
if (instance == null) {
loadLocalConversation();
return;
}
Pattern pattern = Helper.statusIdInUrl;
Matcher matcher = pattern.matcher(firstMessage.uri);
String remoteId = null;
if (matcher.find()) {
remoteId = matcher.group(1);
}
if (remoteId == null) {
loadLocalConversation();
return;
}
String finalInstance = instance;
statusesVM.getStatus(instance, null, remoteId).observe(ContextActivity.this, status -> {
if (status != null) {
Bundle bundle = new Bundle();
bundle.putSerializable(Helper.ARG_STATUS, status);
bundle.putString(Helper.ARG_REMOTE_INSTANCE, finalInstance);
FragmentMastodonContext fragmentMastodonContext = new FragmentMastodonContext();
fragmentMastodonContext.firstMessage = ContextActivity.this;
currentFragment = Helper.addFragment(getSupportFragmentManager(), R.id.nav_host_fragment_content_main, fragmentMastodonContext, bundle, null, null);
} else {
loadLocalConversation();
}
});
} else {
loadLocalConversation();
}
});
} else {
if (firstMessage == null) {
Toasty.warning(ContextActivity.this, getString(R.string.toast_try_later), Toasty.LENGTH_SHORT).show();
return true;
return;
}
if (firstMessage.account.acct != null) {
String instance = null;
@ -196,11 +262,11 @@ public class ContextActivity extends BaseActivity implements FragmentMastodonCon
}
if (instance == null) {
Toasty.info(ContextActivity.this, getString(R.string.toast_error_fetch_message), Toasty.LENGTH_SHORT).show();
return true;
return;
}
if (instance.equalsIgnoreCase(MainActivity.currentInstance)) {
Toasty.info(ContextActivity.this, getString(R.string.toast_on_your_instance), Toasty.LENGTH_SHORT).show();
return true;
return;
}
Pattern pattern = Helper.statusIdInUrl;
Matcher matcher = pattern.matcher(firstMessage.uri);
@ -229,7 +295,6 @@ public class ContextActivity extends BaseActivity implements FragmentMastodonCon
Toasty.warning(ContextActivity.this, getString(R.string.toast_error_fetch_message), Toasty.LENGTH_SHORT).show();
}
}
return true;
}
@Override

View file

@ -1127,6 +1127,7 @@
<string name="SET_REMOVE_LEFT_MARGIN" translatable="false">SET_REMOVE_LEFT_MARGIN</string>
<string name="SET_PROFILE_REMOTELY" translatable="false">SET_PROFILE_REMOTELY</string>
<string name="SET_CONVERSATION_REMOTELY" translatable="false">SET_CONVERSATION_REMOTELY</string>
<string name="SET_EXTAND_EXTRA_FEATURES" translatable="false">SET_EXTAND_EXTRA_FEATURES</string>
<string name="SET_DISPLAY_LOCAL_ONLY" translatable="false">SET_DISPLAY_LOCAL_ONLY</string>
<string name="SET_INNER_MARKER" translatable="false">SET_INNER_MARKER</string>
@ -1881,7 +1882,9 @@
<string name="following">Following</string>
<string name="self">Self</string>
<string name="set_remote_profile_title">Remote profiles</string>
<string name="set_remote_conversation_title">Remote conversations</string>
<string name="set_remote_profile">The app will display publicly profiles to get all messages. Interactions will need an extra step to federate messages.</string>
<string name="set_remote_conversation">The app will display publicly conversations to get all messages. Interactions will need an extra step to federate messages.</string>
<string name="local_only">Local only</string>
<string name="set_display_local_only">Display \"Local only\" button</string>
<string name="set_pixelfed_presentation">Pixelfed presentation for media</string>

View file

@ -28,6 +28,14 @@
app:summary="@string/set_remove_left_margin"
app:title="@string/set_remove_left_margin_title" />
<SwitchPreferenceCompat
app:defaultValue="false"
app:iconSpaceReserved="false"
app:key="@string/SET_CONVERSATION_REMOTELY"
app:singleLineTitle="false"
app:summary="@string/set_remote_conversation"
app:title="@string/set_remote_conversation_title" />
<SwitchPreferenceCompat
app:defaultValue="false"
app:iconSpaceReserved="false"