From 80023219b320e366a1ab4770e8bf320058f4c553 Mon Sep 17 00:00:00 2001 From: Thomas Date: Wed, 23 Nov 2022 12:13:24 +0100 Subject: [PATCH] Fix some crashes --- .../app/fedilab/android/BaseMainActivity.java | 4 ++-- .../android/activities/ProfileActivity.java | 2 +- .../app/fedilab/android/helper/Helper.java | 21 +++++++++---------- .../android/ui/drawer/ReorderTabAdapter.java | 6 ++++-- .../FragmentMastodonNotification.java | 3 +-- .../viewmodel/mastodon/TimelinesVM.java | 10 +++++---- 6 files changed, 24 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/BaseMainActivity.java b/app/src/main/java/app/fedilab/android/BaseMainActivity.java index a353434e1..49d407c9f 100644 --- a/app/src/main/java/app/fedilab/android/BaseMainActivity.java +++ b/app/src/main/java/app/fedilab/android/BaseMainActivity.java @@ -654,13 +654,13 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt regex_public = sharedpreferences.getString(getString(R.string.SET_FILTER_REGEX_PUBLIC) + currentUserID + currentInstance, null); show_art_nsfw = sharedpreferences.getBoolean(getString(R.string.SET_ART_WITH_NSFW) + currentUserID + currentInstance, false); binding.profilePicture.setOnClickListener(v -> binding.drawerLayout.openDrawer(GravityCompat.START)); - Helper.loadPP(binding.profilePicture, currentAccount); + Helper.loadPP(BaseMainActivity.this, binding.profilePicture, currentAccount); headerMainBinding.accountAcc.setText(String.format("%s@%s", currentAccount.mastodon_account.username, currentAccount.instance)); if (currentAccount.mastodon_account.display_name == null || currentAccount.mastodon_account.display_name.isEmpty()) { currentAccount.mastodon_account.display_name = currentAccount.mastodon_account.acct; } headerMainBinding.accountName.setText(currentAccount.mastodon_account.display_name); - Helper.loadPP(headerMainBinding.accountProfilePicture, currentAccount, false); + Helper.loadPP(BaseMainActivity.this, headerMainBinding.accountProfilePicture, currentAccount, false); MastodonHelper.loadProfileMediaMastodon(headerMainBinding.backgroundImage, currentAccount.mastodon_account, MastodonHelper.MediaAccountType.HEADER); /* * Some general data are loaded when the app starts such; diff --git a/app/src/main/java/app/fedilab/android/activities/ProfileActivity.java b/app/src/main/java/app/fedilab/android/activities/ProfileActivity.java index 4c5e37ad2..e2b97c1ce 100644 --- a/app/src/main/java/app/fedilab/android/activities/ProfileActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/ProfileActivity.java @@ -870,7 +870,7 @@ public class ProfileActivity extends BaseActivity { i++; } builderSingle.setMultiChoiceItems(listsArray, presentArray, (dialog, which, isChecked) -> { - if (!relationship.following) { + if (relationship == null || !relationship.following) { accountsVM.follow(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, account.id, true, false) .observe(ProfileActivity.this, newRelationShip -> { relationship = newRelationShip; 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 aee215ddc..df3b8c9fa 100644 --- a/app/src/main/java/app/fedilab/android/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/helper/Helper.java @@ -1132,8 +1132,8 @@ public class Helper { * @param view ImageView - the view where the image will be loaded * @param account - {@link Account} */ - public static void loadPP(ImageView view, BaseAccount account) { - loadPP(view, account, false); + public static void loadPP(Activity activity, ImageView view, BaseAccount account) { + loadPP(activity, view, account, false); } /** @@ -1142,15 +1142,14 @@ public class Helper { * @param view ImageView - the view where the image will be loaded * @param account - {@link Account} */ - public static void loadPP(ImageView view, BaseAccount account, boolean crop) { - Context context = view.getContext(); - SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context); - boolean disableGif = sharedpreferences.getBoolean(context.getString(R.string.SET_DISABLE_GIF), false); + public static void loadPP(Activity activity, ImageView view, BaseAccount account, boolean crop) { + SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(activity); + boolean disableGif = sharedpreferences.getBoolean(activity.getString(R.string.SET_DISABLE_GIF), false); String targetedUrl = disableGif ? account.mastodon_account.avatar_static : account.mastodon_account.avatar; - if (targetedUrl != null && Helper.isValidContextForGlide(context)) { + if (targetedUrl != null && Helper.isValidContextForGlide(activity)) { if (disableGif || (!targetedUrl.endsWith(".gif"))) { try { - RequestBuilder requestBuilder = Glide.with(context) + RequestBuilder requestBuilder = Glide.with(activity) .asDrawable() .load(targetedUrl) .thumbnail(0.1f); @@ -1161,7 +1160,7 @@ public class Helper { } catch (Exception ignored) { } } else { - RequestBuilder requestBuilder = Glide.with(context) + RequestBuilder requestBuilder = Glide.with(activity) .asGif() .load(targetedUrl) .thumbnail(0.1f); @@ -1170,8 +1169,8 @@ public class Helper { } requestBuilder.apply(new RequestOptions().transform(new CenterCrop(), new RoundedCorners(10))).into(view); } - } else if (Helper.isValidContextForGlide(context)) { - Glide.with(context) + } else if (Helper.isValidContextForGlide(activity)) { + Glide.with(activity) .asDrawable() .load(R.drawable.ic_person) .thumbnail(0.1f) diff --git a/app/src/main/java/app/fedilab/android/ui/drawer/ReorderTabAdapter.java b/app/src/main/java/app/fedilab/android/ui/drawer/ReorderTabAdapter.java index 4f0739105..8f92f0508 100644 --- a/app/src/main/java/app/fedilab/android/ui/drawer/ReorderTabAdapter.java +++ b/app/src/main/java/app/fedilab/android/ui/drawer/ReorderTabAdapter.java @@ -170,8 +170,10 @@ public class ReorderTabAdapter extends RecyclerView.Adapter { if (item.type == Timeline.TimeLineEnum.TAG || item.type == Timeline.TimeLineEnum.REMOTE || item.type == Timeline.TimeLineEnum.LIST) { mUndoListener.onUndo(item, position); - pinned.pinnedTimelines.remove(position); - notifyItemRemoved(position); + if (position < pinned.pinnedTimelines.size()) { + pinned.pinnedTimelines.remove(position); + notifyItemRemoved(position); + } } }); } diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java index f87a09cf2..65bb41639 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java @@ -622,8 +622,7 @@ public class FragmentMastodonNotification extends Fragment implements Notificati for (Notification notificationsAlreadyPresent : notificationList) { //We compare the date of each status and we only add status having a date greater than the another, it is inserted at this position //Pinned messages are ignored because their date can be older - //if (Helper.compareTo(notificationReceived.id, notificationsAlreadyPresent.id) > 0) { - if (notificationReceived.created_at.after(notificationsAlreadyPresent.created_at)) { + if (Helper.compareTo(notificationReceived.id, notificationsAlreadyPresent.id) > 0) { if (!notificationList.contains(notificationReceived)) { notificationList.add(position, notificationReceived); notificationAdapter.notifyItemInserted(position); 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 79fc7669f..d94d1ad08 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 @@ -661,22 +661,24 @@ public class TimelinesVM extends AndroidViewModel { public LiveData> getLists(@NonNull String instance, String token) { mastodonListListMutableLiveData = new MutableLiveData<>(); MastodonTimelinesService mastodonTimelinesService = init(instance); + List mastodonListList = new ArrayList<>(); new Thread(() -> { - List mastodonListList = null; Call> getListsCall = mastodonTimelinesService.getLists(token); if (getListsCall != null) { try { Response> getListsResponse = getListsCall.execute(); if (getListsResponse.isSuccessful()) { - mastodonListList = getListsResponse.body(); + List mastodonLists = getListsResponse.body(); + if (mastodonLists != null) { + mastodonListList.addAll(mastodonLists); + } } } catch (Exception e) { e.printStackTrace(); } } Handler mainHandler = new Handler(Looper.getMainLooper()); - List finalMastodonListList = mastodonListList; - Runnable myRunnable = () -> mastodonListListMutableLiveData.setValue(finalMastodonListList); + Runnable myRunnable = () -> mastodonListListMutableLiveData.setValue(mastodonListList); mainHandler.post(myRunnable); }).start(); return mastodonListListMutableLiveData;