From 29d2b894d8455ea3f3da3de1152fdf1c1d180513 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Wed, 9 Jun 2021 08:40:13 +0900 Subject: [PATCH] Interchange UserService and UserRepository names. Check description. As per the sample provided at https://github.com/android/architecture-components-samples/tree/main/GithubBrowserSample, the Retrofit interfaces should be named Services and the classes using the services are Repositories. Once all are properly named, we can move the db repositories inside the repositories package. --- .../dialogs/ProfilePicDialogFragment.java | 6 +++--- .../fragments/main/ProfileFragment.java | 10 +++++----- .../settings/MorePreferencesFragment.java | 6 +++--- .../{UserRepository.kt => UserService.kt} | 2 +- .../instagrabber/viewmodels/AppStateViewModel.java | 10 +++++----- .../viewmodels/UserSearchViewModel.java | 8 ++++---- .../{UserService.kt => UserRepository.kt} | 14 +++++++------- 7 files changed, 28 insertions(+), 28 deletions(-) rename app/src/main/java/awais/instagrabber/repositories/{UserRepository.kt => UserService.kt} (96%) rename app/src/main/java/awais/instagrabber/webservices/{UserService.kt => UserRepository.kt} (64%) diff --git a/app/src/main/java/awais/instagrabber/dialogs/ProfilePicDialogFragment.java b/app/src/main/java/awais/instagrabber/dialogs/ProfilePicDialogFragment.java index 0ae0bbfb..9605fcf8 100644 --- a/app/src/main/java/awais/instagrabber/dialogs/ProfilePicDialogFragment.java +++ b/app/src/main/java/awais/instagrabber/dialogs/ProfilePicDialogFragment.java @@ -36,7 +36,7 @@ import awais.instagrabber.utils.CookieUtils; import awais.instagrabber.utils.CoroutineUtilsKt; import awais.instagrabber.utils.DownloadUtils; import awais.instagrabber.utils.TextUtils; -import awais.instagrabber.webservices.UserService; +import awais.instagrabber.webservices.UserRepository; import kotlinx.coroutines.Dispatchers; import static awais.instagrabber.utils.Utils.settingsHelper; @@ -132,8 +132,8 @@ public class ProfilePicDialogFragment extends DialogFragment { private void fetchAvatar() { if (isLoggedIn) { - final UserService userService = UserService.INSTANCE; - userService.getUserInfo(id, CoroutineUtilsKt.getContinuation((user, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> { + final UserRepository repository = UserRepository.INSTANCE; + repository.getUserInfo(id, CoroutineUtilsKt.getContinuation((user, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> { if (throwable != null) { final Context context = getContext(); if (context == null) { 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 4c762e64..9ed477af 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -96,7 +96,7 @@ 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 awais.instagrabber.webservices.UserRepository; import kotlinx.coroutines.Dispatchers; import static androidx.core.content.PermissionChecker.checkSelfPermission; @@ -120,7 +120,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe private FriendshipService friendshipService; private StoriesService storiesService; private MediaService mediaService; - private UserService userService; + private UserRepository userRepository; private GraphQLService graphQLService; private DirectMessagesService directMessagesService; private boolean shouldRefresh = true; @@ -336,7 +336,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe directMessagesService = isLoggedIn ? DirectMessagesService.INSTANCE : null; storiesService = isLoggedIn ? StoriesService.INSTANCE : null; mediaService = isLoggedIn ? MediaService.INSTANCE : null; - userService = isLoggedIn ? UserService.INSTANCE : null; + userRepository = isLoggedIn ? UserRepository.INSTANCE : null; graphQLService = isLoggedIn ? null : GraphQLService.INSTANCE; final Context context = getContext(); if (context == null) return; @@ -629,7 +629,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe return; } if (isLoggedIn) { - userService.getUsernameInfo( + userRepository.getUsernameInfo( usernameTemp, CoroutineUtilsKt.getContinuation((user, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> { if (throwable != null) { @@ -639,7 +639,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe Toast.makeText(context, throwable.getMessage(), Toast.LENGTH_SHORT).show(); return; } - userService.getUserFriendship( + userRepository.getUserFriendship( user.getPk(), CoroutineUtilsKt.getContinuation( (friendshipStatus, throwable1) -> AppExecutors.INSTANCE.getMainThread().execute(() -> { diff --git a/app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java b/app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java index 315b8bb3..a20e57d2 100644 --- a/app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java @@ -39,7 +39,7 @@ import awais.instagrabber.utils.FlavorTown; import awais.instagrabber.utils.ProcessPhoenix; import awais.instagrabber.utils.TextUtils; import awais.instagrabber.utils.Utils; -import awais.instagrabber.webservices.UserService; +import awais.instagrabber.webservices.UserRepository; import kotlinx.coroutines.Dispatchers; import static awais.instagrabber.utils.Utils.settingsHelper; @@ -285,8 +285,8 @@ public class MorePreferencesFragment extends BasePreferencesFragment { // adds cookies to database for quick access final long uid = CookieUtils.getUserIdFromCookie(cookie); - final UserService userService = UserService.INSTANCE; - userService.getUserInfo(uid, CoroutineUtilsKt.getContinuation((user, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> { + final UserRepository userRepository = UserRepository.INSTANCE; + userRepository.getUserInfo(uid, CoroutineUtilsKt.getContinuation((user, throwable) -> AppExecutors.INSTANCE.getMainThread().execute(() -> { if (throwable != null) { Log.e(TAG, "Error fetching user info", throwable); return; diff --git a/app/src/main/java/awais/instagrabber/repositories/UserRepository.kt b/app/src/main/java/awais/instagrabber/repositories/UserService.kt similarity index 96% rename from app/src/main/java/awais/instagrabber/repositories/UserRepository.kt rename to app/src/main/java/awais/instagrabber/repositories/UserService.kt index 7264505b..67d91d5a 100644 --- a/app/src/main/java/awais/instagrabber/repositories/UserRepository.kt +++ b/app/src/main/java/awais/instagrabber/repositories/UserService.kt @@ -7,7 +7,7 @@ import retrofit2.http.GET import retrofit2.http.Path import retrofit2.http.Query -interface UserRepository { +interface UserService { @GET("/api/v1/users/{uid}/info/") suspend fun getUserInfo(@Path("uid") uid: Long): WrappedUser diff --git a/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java b/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java index f7e4c1e9..7cebb0a0 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java +++ b/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java @@ -14,7 +14,7 @@ import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.CookieUtils; import awais.instagrabber.utils.CoroutineUtilsKt; import awais.instagrabber.utils.TextUtils; -import awais.instagrabber.webservices.UserService; +import awais.instagrabber.webservices.UserRepository; import kotlinx.coroutines.Dispatchers; import static awais.instagrabber.utils.Utils.settingsHelper; @@ -25,7 +25,7 @@ public class AppStateViewModel extends AndroidViewModel { private final String cookie; private final MutableLiveData currentUser = new MutableLiveData<>(); - private UserService userService; + private UserRepository userRepository; public AppStateViewModel(@NonNull final Application application) { super(application); @@ -33,7 +33,7 @@ public class AppStateViewModel extends AndroidViewModel { cookie = settingsHelper.getString(Constants.COOKIE); final boolean isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) != 0; if (!isLoggedIn) return; - userService = UserService.INSTANCE; + userRepository = UserRepository.INSTANCE; // final AccountRepository accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(application)); fetchProfileDetails(); } @@ -49,8 +49,8 @@ public class AppStateViewModel extends AndroidViewModel { private void fetchProfileDetails() { final long uid = CookieUtils.getUserIdFromCookie(cookie); - if (userService == null) return; - userService.getUserInfo(uid, CoroutineUtilsKt.getContinuation((user, throwable) -> { + if (userRepository == null) return; + userRepository.getUserInfo(uid, CoroutineUtilsKt.getContinuation((user, throwable) -> { if (throwable != null) { Log.e(TAG, "onFailure: ", throwable); return; diff --git a/app/src/main/java/awais/instagrabber/viewmodels/UserSearchViewModel.java b/app/src/main/java/awais/instagrabber/viewmodels/UserSearchViewModel.java index ea012d27..9e964865 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/UserSearchViewModel.java +++ b/app/src/main/java/awais/instagrabber/viewmodels/UserSearchViewModel.java @@ -32,7 +32,7 @@ import awais.instagrabber.utils.Debouncer; import awais.instagrabber.utils.RankedRecipientsCache; import awais.instagrabber.utils.TextUtils; import awais.instagrabber.webservices.DirectMessagesService; -import awais.instagrabber.webservices.UserService; +import awais.instagrabber.webservices.UserRepository; import kotlinx.coroutines.Dispatchers; import okhttp3.ResponseBody; import retrofit2.Call; @@ -58,7 +58,7 @@ public class UserSearchViewModel extends ViewModel { private final MutableLiveData showAction = new MutableLiveData<>(false); private final Debouncer searchDebouncer; private final Set selectedRecipients = new HashSet<>(); - private final UserService userService; + private final UserRepository userRepository; private final DirectMessagesService directMessagesService; private final RankedRecipientsCache rankedRecipientsCache; @@ -70,7 +70,7 @@ public class UserSearchViewModel extends ViewModel { if (TextUtils.isEmpty(csrfToken) || viewerId <= 0 || TextUtils.isEmpty(deviceUuid)) { throw new IllegalArgumentException("User is not logged in!"); } - userService = UserService.INSTANCE; + userRepository = UserRepository.INSTANCE; directMessagesService = DirectMessagesService.INSTANCE; rankedRecipientsCache = RankedRecipientsCache.INSTANCE; if ((rankedRecipientsCache.isFailed() || rankedRecipientsCache.isExpired()) && !rankedRecipientsCache.isUpdateInitiated()) { @@ -168,7 +168,7 @@ public class UserSearchViewModel extends ViewModel { } private void defaultUserSearch() { - userService.search(currentQuery, CoroutineUtilsKt.getContinuation((userSearchResponse, throwable) -> { + userRepository.search(currentQuery, CoroutineUtilsKt.getContinuation((userSearchResponse, throwable) -> { if (throwable != null) { Log.e(TAG, "onFailure: ", throwable); recipients.postValue(Resource.error(throwable.getMessage(), getCachedRecipients())); diff --git a/app/src/main/java/awais/instagrabber/webservices/UserService.kt b/app/src/main/java/awais/instagrabber/webservices/UserRepository.kt similarity index 64% rename from app/src/main/java/awais/instagrabber/webservices/UserService.kt rename to app/src/main/java/awais/instagrabber/webservices/UserRepository.kt index 708f1d8b..64f993a1 100644 --- a/app/src/main/java/awais/instagrabber/webservices/UserService.kt +++ b/app/src/main/java/awais/instagrabber/webservices/UserRepository.kt @@ -1,29 +1,29 @@ package awais.instagrabber.webservices -import awais.instagrabber.repositories.UserRepository +import awais.instagrabber.repositories.UserService import awais.instagrabber.repositories.responses.FriendshipStatus import awais.instagrabber.repositories.responses.User import awais.instagrabber.repositories.responses.UserSearchResponse import awais.instagrabber.webservices.RetrofitFactory.retrofit import java.util.* -object UserService : BaseService() { - private val repository: UserRepository = retrofit.create(UserRepository::class.java) +object UserRepository : BaseService() { + private val service: UserService = retrofit.create(UserService::class.java) suspend fun getUserInfo(uid: Long): User { - val response = repository.getUserInfo(uid) + val response = service.getUserInfo(uid) return response.user } suspend fun getUsernameInfo(username: String): User { - val response = repository.getUsernameInfo(username) + val response = service.getUsernameInfo(username) return response.user } - suspend fun getUserFriendship(uid: Long): FriendshipStatus = repository.getUserFriendship(uid) + suspend fun getUserFriendship(uid: Long): FriendshipStatus = service.getUserFriendship(uid) suspend fun search(query: String): UserSearchResponse { val timezoneOffset = TimeZone.getDefault().rawOffset.toFloat() / 1000 - return repository.search(timezoneOffset, query) + return service.search(timezoneOffset, query) } } \ No newline at end of file