From e1532eb4b8400e40bfab8417b544ec0f0611a2e4 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Thu, 10 Jun 2021 16:50:25 +0900 Subject: [PATCH] Move FriendService object to FriendshipRepository constructor parameter. --- .../fragments/FollowViewerFragment.java | 2 +- .../NotificationsViewerFragment.java | 2 +- .../fragments/main/ProfileFragment.java | 2 +- .../instagrabber/managers/ThreadManager.kt | 9 ++++--- .../webservices/FriendshipRepository.kt | 25 +++++++++++++------ 5 files changed, 26 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/fragments/FollowViewerFragment.java b/app/src/main/java/awais/instagrabber/fragments/FollowViewerFragment.java index b84e4a8c..26a1381b 100644 --- a/app/src/main/java/awais/instagrabber/fragments/FollowViewerFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/FollowViewerFragment.java @@ -167,7 +167,7 @@ public final class FollowViewerFragment extends Fragment implements SwipeRefresh @Override public void onCreate(@Nullable final Bundle savedInstanceState) { super.onCreate(savedInstanceState); - friendshipRepository = FriendshipRepository.INSTANCE; + friendshipRepository = FriendshipRepository.Companion.getInstance(); fragmentActivity = (AppCompatActivity) getActivity(); setHasOptionsMenu(true); } diff --git a/app/src/main/java/awais/instagrabber/fragments/NotificationsViewerFragment.java b/app/src/main/java/awais/instagrabber/fragments/NotificationsViewerFragment.java index b79f8fa6..e3495fa2 100644 --- a/app/src/main/java/awais/instagrabber/fragments/NotificationsViewerFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/NotificationsViewerFragment.java @@ -228,7 +228,7 @@ public final class NotificationsViewerFragment extends Fragment implements Swipe userId = CookieUtils.getUserIdFromCookie(cookie); deviceUuid = Utils.settingsHelper.getString(Constants.DEVICE_UUID); csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie); - friendshipRepository = FriendshipRepository.INSTANCE; + friendshipRepository = FriendshipRepository.Companion.getInstance(); mediaService = MediaService.INSTANCE; newsService = NewsService.getInstance(); } 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 c16b1a71..1d19b883 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -333,7 +333,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe deviceUuid = Utils.settingsHelper.getString(Constants.DEVICE_UUID); csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie); fragmentActivity = (MainActivity) requireActivity(); - friendshipRepository = isLoggedIn ? FriendshipRepository.INSTANCE : null; + friendshipRepository = isLoggedIn ? FriendshipRepository.Companion.getInstance() : null; directMessagesService = isLoggedIn ? DirectMessagesService.INSTANCE : null; storiesService = isLoggedIn ? StoriesService.INSTANCE : null; mediaService = isLoggedIn ? MediaService.INSTANCE : null; diff --git a/app/src/main/java/awais/instagrabber/managers/ThreadManager.kt b/app/src/main/java/awais/instagrabber/managers/ThreadManager.kt index 6a2c7df7..df3841cc 100644 --- a/app/src/main/java/awais/instagrabber/managers/ThreadManager.kt +++ b/app/src/main/java/awais/instagrabber/managers/ThreadManager.kt @@ -62,6 +62,7 @@ class ThreadManager( val pendingRequests: LiveData = _pendingRequests private val inboxManager: InboxManager = if (pending) DirectMessagesManager.pendingInboxManager else DirectMessagesManager.inboxManager private val threadIdOrUserIds: ThreadIdOrUserIds = of(threadId) + private val friendshipRepository: FriendshipRepository by lazy { FriendshipRepository.getInstance() } val thread: LiveData by lazy { distinctUntilChanged(map(inboxManager.getInbox()) { inboxResource: Resource? -> @@ -1151,7 +1152,7 @@ class ThreadManager( val data = MutableLiveData>() scope.launch(Dispatchers.IO) { try { - FriendshipRepository.changeBlock(csrfToken, viewerId, deviceUuid, false, user.pk) + friendshipRepository.changeBlock(csrfToken, viewerId, deviceUuid, false, user.pk) refreshChats(scope) } catch (e: Exception) { Log.e(TAG, "onFailure: ", e) @@ -1165,7 +1166,7 @@ class ThreadManager( val data = MutableLiveData>() scope.launch(Dispatchers.IO) { try { - FriendshipRepository.changeBlock(csrfToken, viewerId, deviceUuid, true, user.pk) + friendshipRepository.changeBlock(csrfToken, viewerId, deviceUuid, true, user.pk) refreshChats(scope) } catch (e: Exception) { Log.e(TAG, "onFailure: ", e) @@ -1179,7 +1180,7 @@ class ThreadManager( val data = MutableLiveData>() scope.launch(Dispatchers.IO) { try { - FriendshipRepository.toggleRestrict(csrfToken, deviceUuid, user.pk, true) + friendshipRepository.toggleRestrict(csrfToken, deviceUuid, user.pk, true) refreshChats(scope) } catch (e: Exception) { Log.e(TAG, "onFailure: ", e) @@ -1193,7 +1194,7 @@ class ThreadManager( val data = MutableLiveData>() scope.launch(Dispatchers.IO) { try { - FriendshipRepository.toggleRestrict(csrfToken, deviceUuid, user.pk, false) + friendshipRepository.toggleRestrict(csrfToken, deviceUuid, user.pk, false) refreshChats(scope) } catch (e: Exception) { Log.e(TAG, "onFailure: ", e) diff --git a/app/src/main/java/awais/instagrabber/webservices/FriendshipRepository.kt b/app/src/main/java/awais/instagrabber/webservices/FriendshipRepository.kt index 1210d8dd..d286ed09 100644 --- a/app/src/main/java/awais/instagrabber/webservices/FriendshipRepository.kt +++ b/app/src/main/java/awais/instagrabber/webservices/FriendshipRepository.kt @@ -11,8 +11,7 @@ import org.json.JSONArray import org.json.JSONException import org.json.JSONObject -object FriendshipRepository { - private val service: FriendshipService = retrofit.create(FriendshipService::class.java) +class FriendshipRepository(private val service: FriendshipService) { suspend fun follow( csrfToken: String, @@ -34,9 +33,7 @@ object FriendshipRepository { deviceUuid: String, unblock: Boolean, targetUserId: Long, - ): FriendshipChangeResponse { - return change(csrfToken, userId, deviceUuid, if (unblock) "unblock" else "block", targetUserId) - } + ): FriendshipChangeResponse = change(csrfToken, userId, deviceUuid, if (unblock) "unblock" else "block", targetUserId) suspend fun toggleRestrict( csrfToken: String, @@ -144,12 +141,26 @@ object FriendshipRepository { val followModels = mutableListOf() for (i in 0 until items.length()) { val itemJson = items.optJSONObject(i) ?: continue - val followModel = FollowModel(itemJson.getString("pk"), + val followModel = FollowModel( + itemJson.getString("pk"), itemJson.getString("username"), itemJson.optString("full_name"), - itemJson.getString("profile_pic_url")) + itemJson.getString("profile_pic_url") + ) followModels.add(followModel) } return followModels } + + companion object { + @Volatile + private var INSTANCE: FriendshipRepository? = null + + fun getInstance(): FriendshipRepository { + return INSTANCE ?: synchronized(this) { + val service: FriendshipService = retrofit.create(FriendshipService::class.java) + FriendshipRepository(service).also { INSTANCE = it } + } + } + } } \ No newline at end of file