From 564e0c682cdda765d0f559a257ee3757ba34f2e0 Mon Sep 17 00:00:00 2001 From: Thomas Date: Wed, 14 Sep 2022 18:35:22 +0200 Subject: [PATCH] Allow to edit Nitter accounts with a long click on tabs --- .../app/fedilab/android/helper/Helper.java | 3 + .../android/helper/PinnedTimelineHelper.java | 69 ++++++++++++++++++- .../viewmodel/mastodon/TimelinesVM.java | 5 +- 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/helper/Helper.java b/app/src/main/java/app/fedilab/android/helper/Helper.java index 63a31d89..f578af3b 100644 --- a/app/src/main/java/app/fedilab/android/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/helper/Helper.java @@ -538,6 +538,9 @@ public class Helper { * @return String */ public static String dateDiff(Context context, Date date) { + if (date == null) { + date = new Date(); + } Date now = new Date(); long diff = now.getTime() - date.getTime(); long seconds = diff / 1000; diff --git a/app/src/main/java/app/fedilab/android/helper/PinnedTimelineHelper.java b/app/src/main/java/app/fedilab/android/helper/PinnedTimelineHelper.java index c70008f6..748d2087 100644 --- a/app/src/main/java/app/fedilab/android/helper/PinnedTimelineHelper.java +++ b/app/src/main/java/app/fedilab/android/helper/PinnedTimelineHelper.java @@ -412,7 +412,8 @@ public class PinnedTimelineHelper { // Set LongClick listener to each Tab int finalI = i; tabStrip.getChildAt(i).setOnLongClickListener(v -> { - switch (pinnedTimelineVisibleList.get(finalI - (BOTTOM_TIMELINE_COUNT - finalToRemove)).type) { + int position = finalI - (BOTTOM_TIMELINE_COUNT - finalToRemove); + switch (pinnedTimelineVisibleList.get(position).type) { case LIST: break; @@ -420,12 +421,16 @@ public class PinnedTimelineHelper { tagClick(activity, finalPinned, v, activityMainBinding, finalI); break; case REMOTE: - instanceClick(activity, finalPinned, v, activityMainBinding, finalI); + if (pinnedTimelineVisibleList.get(position).remoteInstance.type != RemoteInstance.InstanceType.NITTER) { + instanceClick(activity, finalPinned, v, activityMainBinding, finalI); + } else { + nitterClick(activity, finalPinned, activityMainBinding, finalI); + } break; case HOME: case LOCAL: case PUBLIC: - defaultClick(activity, pinnedTimelineVisibleList.get(finalI - (BOTTOM_TIMELINE_COUNT - finalToRemove)).type, v, activityMainBinding, finalI); + defaultClick(activity, pinnedTimelineVisibleList.get(position).type, v, activityMainBinding, finalI); break; } return true; @@ -1042,4 +1047,62 @@ public class PinnedTimelineHelper { popup.show(); } + + /** + * Manage long clicks on Nitter instances + * + * @param activity - BaseMainActivity activity + * @param pinned - {@link Pinned} + * @param position - int position of the tab + */ + public static void nitterClick(BaseMainActivity activity, Pinned pinned, ActivityMainBinding activityMainBinding, int position) { + + int toRemove = itemToRemoveInBottomMenu(activity); + int offSetPosition = position - (BOTTOM_TIMELINE_COUNT - toRemove); + RemoteInstance remoteInstance = pinned.pinnedTimelines.get(offSetPosition).remoteInstance; + if (remoteInstance == null) + return; + String accounts = remoteInstance.host; + AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity, Helper.dialogStyle()); + LayoutInflater inflater = activity.getLayoutInflater(); + View dialogView = inflater.inflate(R.layout.tags_any, new LinearLayout(activity), false); + dialogBuilder.setView(dialogView); + final EditText editText = dialogView.findViewById(R.id.filter_any); + editText.setHint(R.string.list_of_twitter_accounts); + if (accounts != null) { + editText.setText(accounts); + editText.setSelection(editText.getText().toString().length()); + } + dialogBuilder.setPositiveButton(R.string.validate, (dialog, id) -> { + pinned.pinnedTimelines.get(offSetPosition).remoteInstance.host = editText.getText().toString().trim(); + try { + new Pinned(activity).updatePinned(pinned); + } catch (DBException e) { + e.printStackTrace(); + } + FragmentMastodonTimeline fragmentMastodonTimeline = null; + if (activityMainBinding.viewPager.getAdapter() != null) { + Fragment fragment = (Fragment) activityMainBinding.viewPager.getAdapter().instantiateItem(activityMainBinding.viewPager, activityMainBinding.tabLayout.getSelectedTabPosition()); + if (fragment instanceof FragmentMastodonTimeline && fragment.isVisible()) { + fragmentMastodonTimeline = ((FragmentMastodonTimeline) fragment); + fragmentMastodonTimeline.refreshAllAdapters(); + } + } + FragmentTransaction fragTransaction1 = activity.getSupportFragmentManager().beginTransaction(); + if (fragmentMastodonTimeline == null) + return; + fragTransaction1.detach(fragmentMastodonTimeline).commit(); + Bundle bundle = new Bundle(); + bundle.putSerializable(Helper.ARG_REMOTE_INSTANCE, pinned.pinnedTimelines.get(offSetPosition)); + bundle.putSerializable(Helper.ARG_TIMELINE_TYPE, Timeline.TimeLineEnum.REMOTE); + fragmentMastodonTimeline.setArguments(bundle); + FragmentTransaction fragTransaction2 = activity.getSupportFragmentManager().beginTransaction(); + fragTransaction2.attach(fragmentMastodonTimeline); + fragTransaction2.commit(); + }); + AlertDialog alertDialog = dialogBuilder.create(); + alertDialog.show(); + } + + } diff --git a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/TimelinesVM.java b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/TimelinesVM.java index 80ed5300..c6c5097f 100644 --- a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/TimelinesVM.java +++ b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/TimelinesVM.java @@ -214,13 +214,16 @@ public class TimelinesVM extends AndroidViewModel { String accountsStr, String max_position) { MastodonTimelinesService mastodonTimelinesService = initInstanceXMLOnly(instance); + accountsStr = accountsStr.replaceAll("\\s", ","); statusesMutableLiveData = new MutableLiveData<>(); + String finalAccountsStr = accountsStr; new Thread(() -> { - Call publicTlCall = mastodonTimelinesService.getNitter(accountsStr, max_position); + Call publicTlCall = mastodonTimelinesService.getNitter(finalAccountsStr, max_position); Statuses statuses = new Statuses(); if (publicTlCall != null) { try { Response publicTlResponse = publicTlCall.execute(); + if (publicTlResponse.isSuccessful()) { Nitter rssResponse = publicTlResponse.body(); List statusList = new ArrayList<>();