From 447ef0d660996acfce17be4dfcd175ca9aed270e Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Thu, 20 May 2021 15:07:09 -0400 Subject: [PATCH] replace CreateThreadAction --- .../asyncs/CreateThreadAction.java | 80 ------------------- .../fragments/StoryViewerFragment.java | 78 ++++++++++-------- .../fragments/main/ProfileFragment.java | 40 +++++++--- 3 files changed, 74 insertions(+), 124 deletions(-) delete mode 100644 app/src/main/java/awais/instagrabber/asyncs/CreateThreadAction.java diff --git a/app/src/main/java/awais/instagrabber/asyncs/CreateThreadAction.java b/app/src/main/java/awais/instagrabber/asyncs/CreateThreadAction.java deleted file mode 100644 index 384b3bbd..00000000 --- a/app/src/main/java/awais/instagrabber/asyncs/CreateThreadAction.java +++ /dev/null @@ -1,80 +0,0 @@ -package awais.instagrabber.asyncs; - -import android.os.AsyncTask; -import android.util.Log; - -import androidx.annotation.NonNull; - -import java.io.IOException; -import java.util.Collections; -import java.util.Locale; - -import awais.instagrabber.repositories.responses.directmessages.DirectThread; -import awais.instagrabber.utils.Constants; -import awais.instagrabber.utils.CookieUtils; -import awais.instagrabber.utils.Utils; -import awais.instagrabber.webservices.DirectMessagesService; -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Response; - -public class CreateThreadAction extends AsyncTask { - private static final String TAG = "CommentAction"; - - private final String cookie; - private final long userId; - private final OnTaskCompleteListener onTaskCompleteListener; - private final DirectMessagesService directMessagesService; - - public CreateThreadAction(final String cookie, final long userId, final OnTaskCompleteListener onTaskCompleteListener) { - this.cookie = cookie; - this.userId = userId; - this.onTaskCompleteListener = onTaskCompleteListener; - directMessagesService = DirectMessagesService.getInstance(CookieUtils.getCsrfTokenFromCookie(cookie), - CookieUtils.getUserIdFromCookie(cookie), - Utils.settingsHelper.getString(Constants.DEVICE_UUID)); - } - - protected Void doInBackground(Void... lmao) { - final Call createThreadRequest = directMessagesService.createThread(Collections.singletonList(userId), null); - createThreadRequest.enqueue(new Callback() { - @Override - public void onResponse(@NonNull final Call call, @NonNull final Response response) { - if (!response.isSuccessful()) { - if (response.errorBody() != null) { - try { - final String string = response.errorBody().string(); - final String msg = String.format(Locale.US, - "onResponse: url: %s, responseCode: %d, errorBody: %s", - call.request().url().toString(), - response.code(), - string); - Log.e(TAG, msg); - } catch (IOException e) { - Log.e(TAG, "onResponse: ", e); - } - } - Log.e(TAG, "onResponse: request was not successful and response error body was null"); - } - onTaskCompleteListener.onTaskComplete(response.body()); - if (response.body() == null) { - Log.e(TAG, "onResponse: thread is null"); - } - } - - @Override - public void onFailure(@NonNull final Call call, @NonNull final Throwable t) { - onTaskCompleteListener.onTaskComplete(null); - } - }); - return null; - } - -// @Override -// protected void onPostExecute() { -// } - - public interface OnTaskCompleteListener { - void onTaskComplete(final DirectThread thread); - } -} diff --git a/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.java b/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.java index c59caab6..5394c868 100644 --- a/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/StoryViewerFragment.java @@ -70,7 +70,6 @@ import java.util.regex.Pattern; import awais.instagrabber.BuildConfig; import awais.instagrabber.R; import awais.instagrabber.adapters.StoriesAdapter; -import awais.instagrabber.asyncs.CreateThreadAction; import awais.instagrabber.customviews.helpers.SwipeGestureListener; import awais.instagrabber.databinding.FragmentStoryViewerBinding; import awais.instagrabber.fragments.main.ProfileFragmentDirections; @@ -90,6 +89,7 @@ import awais.instagrabber.repositories.requests.StoryViewerOptions.Type; import awais.instagrabber.repositories.requests.directmessages.BroadcastOptions; import awais.instagrabber.repositories.responses.Media; import awais.instagrabber.repositories.responses.StoryStickerResponse; +import awais.instagrabber.repositories.responses.directmessages.DirectThread; import awais.instagrabber.repositories.responses.directmessages.DirectThreadBroadcastResponse; import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.CookieUtils; @@ -222,40 +222,54 @@ public class StoryViewerFragment extends Fragment { new AlertDialog.Builder(context) .setTitle(R.string.reply_story) .setView(input) - .setPositiveButton(R.string.confirm, (d, w) -> new CreateThreadAction(cookie, currentStory.getUserId(), thread -> { - if (thread == null) { - Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); - return; - } - try { - final Call request = directMessagesService - .broadcastStoryReply(BroadcastOptions.ThreadIdOrUserIds.of(thread.getThreadId()), - input.getText().toString(), - currentStory.getStoryMediaId(), - String.valueOf(currentStory.getUserId())); - request.enqueue(new Callback() { - @Override - public void onResponse(@NonNull final Call call, - @NonNull final Response response) { - if (!response.isSuccessful()) { - Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); - return; - } - Toast.makeText(context, R.string.answered_story, Toast.LENGTH_SHORT).show(); + .setPositiveButton(R.string.confirm, (d, w) -> { + final Call createThreadRequest = + directMessagesService.createThread(Collections.singletonList(currentStory.getUserId()), null); + createThreadRequest.enqueue(new Callback() { + @Override + public void onResponse(@NonNull final Call call, @NonNull final Response response) { + if (!response.isSuccessful() || response.body() == null) { + Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); + return; } + final DirectThread thread = response.body(); + try { + final Call request = directMessagesService + .broadcastStoryReply(BroadcastOptions.ThreadIdOrUserIds.of(thread.getThreadId()), + input.getText().toString(), + currentStory.getStoryMediaId(), + String.valueOf(currentStory.getUserId())); + request.enqueue(new Callback() { + @Override + public void onResponse(@NonNull final Call call, + @NonNull final Response response) { + if (!response.isSuccessful()) { + Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); + return; + } + Toast.makeText(context, R.string.answered_story, Toast.LENGTH_SHORT).show(); + } - @Override - public void onFailure(@NonNull final Call call, @NonNull final Throwable t) { - try { - Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); - Log.e(TAG, "onFailure: ", t); - } catch (Throwable ignored) {} + @Override + public void onFailure(@NonNull final Call call, @NonNull final Throwable t) { + try { + Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); + Log.e(TAG, "onFailure: ", t); + } catch (Throwable ignored) { + } + } + }); + } catch (UnsupportedEncodingException e) { + Log.e(TAG, "Error", e); } - }); - } catch (UnsupportedEncodingException e) { - Log.e(TAG, "Error", e); - } - }).execute()) + } + + @Override + public void onFailure(@NonNull final Call call, @NonNull final Throwable t) { + Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); + } + }); + }) .setNegativeButton(R.string.cancel, null) .show(); return true; diff --git a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java index 8fc98002..ad59cf06 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -55,7 +55,6 @@ import awais.instagrabber.R; import awais.instagrabber.activities.MainActivity; import awais.instagrabber.adapters.FeedAdapterV2; import awais.instagrabber.adapters.HighlightsAdapter; -import awais.instagrabber.asyncs.CreateThreadAction; import awais.instagrabber.asyncs.ProfilePostFetchService; import awais.instagrabber.customviews.PrimaryActionModeCallback; import awais.instagrabber.customviews.PrimaryActionModeCallback.CallbacksHelper; @@ -85,6 +84,7 @@ import awais.instagrabber.repositories.responses.FriendshipStatus; import awais.instagrabber.repositories.responses.Media; import awais.instagrabber.repositories.responses.User; import awais.instagrabber.repositories.responses.UserProfileContextLink; +import awais.instagrabber.repositories.responses.directmessages.DirectThread; import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.CookieUtils; import awais.instagrabber.utils.DownloadUtils; @@ -92,12 +92,16 @@ import awais.instagrabber.utils.TextUtils; import awais.instagrabber.utils.Utils; import awais.instagrabber.viewmodels.AppStateViewModel; import awais.instagrabber.viewmodels.HighlightsViewModel; +import awais.instagrabber.webservices.DirectMessagesService; import awais.instagrabber.webservices.FriendshipService; import awais.instagrabber.webservices.GraphQLService; import awais.instagrabber.webservices.MediaService; import awais.instagrabber.webservices.ServiceCallback; import awais.instagrabber.webservices.StoriesService; import awais.instagrabber.webservices.UserService; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; import static androidx.core.content.PermissionChecker.checkSelfPermission; import static awais.instagrabber.fragments.HashTagFragment.ARG_HASHTAG; @@ -122,6 +126,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe private MediaService mediaService; private UserService userService; private GraphQLService graphQLService; + private DirectMessagesService directMessagesService; private boolean shouldRefresh = true; private boolean hasStories = false; private HighlightsAdapter highlightsAdapter; @@ -313,6 +318,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie); fragmentActivity = (MainActivity) requireActivity(); friendshipService = isLoggedIn ? FriendshipService.getInstance(deviceUuid, csrfToken, myId) : null; + directMessagesService = isLoggedIn ? DirectMessagesService.getInstance(csrfToken, myId, deviceUuid) : null; storiesService = isLoggedIn ? StoriesService.getInstance(null, 0L, null) : null; mediaService = isLoggedIn ? MediaService.getInstance(null, null, 0) : null; userService = isLoggedIn ? UserService.getInstance() : null; @@ -1130,20 +1136,30 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe if (!disableDm) { profileDetailsBinding.btnDM.setOnClickListener(v -> { profileDetailsBinding.btnDM.setEnabled(false); - new CreateThreadAction(cookie, profileModel.getPk(), thread -> { - if (thread == null) { - Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); + final Call createThreadRequest = + directMessagesService.createThread(Collections.singletonList(profileModel.getPk()), null); + createThreadRequest.enqueue(new Callback() { + @Override + public void onResponse(@NonNull final Call call, @NonNull final Response response) { profileDetailsBinding.btnDM.setEnabled(true); - return; + if (!response.isSuccessful() || response.body() == null) { + Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); + return; + } + final InboxManager inboxManager = DirectMessagesManager.getInstance().getInboxManager(); + final DirectThread thread = response.body(); + if (!inboxManager.containsThread(thread.getThreadId())) { + thread.setTemp(true); + inboxManager.addThread(thread, 0); + } + fragmentActivity.navigateToThread(thread.getThreadId(), profileModel.getUsername()); } - final InboxManager inboxManager = DirectMessagesManager.getInstance().getInboxManager(); - if (!inboxManager.containsThread(thread.getThreadId())) { - thread.setTemp(true); - inboxManager.addThread(thread, 0); + + @Override + public void onFailure(@NonNull final Call call, @NonNull final Throwable t) { + Toast.makeText(context, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show(); } - fragmentActivity.navigateToThread(thread.getThreadId(), profileModel.getUsername()); - profileDetailsBinding.btnDM.setEnabled(true); - }).execute(); + }); }); } profileDetailsBinding.mainProfileImage.setOnClickListener(v -> {