From 3f09c7d173c21b78ce4d753bad6536a45c79c856 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Sat, 5 Jun 2021 19:58:01 +0900 Subject: [PATCH] Convert User to kotlin --- .../DirectMessageSettingsFragment.kt | 4 +- .../fragments/main/ProfileFragment.java | 6 +- .../repositories/responses/User.kt | 328 ++---------------- .../viewmodels/DirectSettingsViewModel.kt | 4 +- .../webservices/GraphQLService.java | 8 +- 5 files changed, 46 insertions(+), 304 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageSettingsFragment.kt b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageSettingsFragment.kt index 069ebe09..74b56d7a 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageSettingsFragment.kt +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageSettingsFragment.kt @@ -296,8 +296,8 @@ class DirectMessageSettingsFragment : Fragment(), ConfirmDialogFragmentCallback usersAdapter = DirectUsersAdapter( inviter?.pk ?: -1, { _: Int, user: User, _: Boolean -> - if (isEmpty(user.username) && !isEmpty(user.fbId)) { - Utils.openURL(context, "https://facebook.com/" + user.fbId) + if (user.username.isBlank() && !user.interopMessagingUserFbid.isNullOrBlank()) { + Utils.openURL(context, "https://facebook.com/" + user.interopMessagingUserFbid) return@DirectUsersAdapter } if (isEmpty(user.username)) return@DirectUsersAdapter 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 6146a89c..1fd246cb 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -433,7 +433,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe } chainingMenuItem = menu.findItem(R.id.chaining); if (chainingMenuItem != null) { - chainingMenuItem.setVisible(isNotMe && profileModel.hasChaining()); + chainingMenuItem.setVisible(isNotMe && profileModel.getHasChaining()); } removeFollowerMenuItem = menu.findItem(R.id.remove_follower); if (removeFollowerMenuItem != null) { @@ -859,7 +859,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe profileDetailsBinding.profileContext.setVisibility(View.GONE); } else { profileDetailsBinding.profileContext.setVisibility(View.VISIBLE); - final List userProfileContextLinks = profileModel.getProfileContextLinks(); + final List userProfileContextLinks = profileModel.getProfileContextLinksWithUserIds(); for (int i = 0; i < userProfileContextLinks.size(); i++) { final UserProfileContextLink link = userProfileContextLinks.get(i); if (link.getUsername() != null) @@ -980,7 +980,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe mutePostsMenuItem.setTitle(profileModel.getFriendshipStatus().getMuting() ? R.string.unmute_posts : R.string.mute_posts); } if (chainingMenuItem != null) { - chainingMenuItem.setVisible(profileModel.hasChaining()); + chainingMenuItem.setVisible(profileModel.getHasChaining()); } if (removeFollowerMenuItem != null) { removeFollowerMenuItem.setVisible(profileModel.getFriendshipStatus().getFollowedBy()); diff --git a/app/src/main/java/awais/instagrabber/repositories/responses/User.kt b/app/src/main/java/awais/instagrabber/repositories/responses/User.kt index 0d707f72..6b82b1df 100644 --- a/app/src/main/java/awais/instagrabber/repositories/responses/User.kt +++ b/app/src/main/java/awais/instagrabber/repositories/responses/User.kt @@ -1,296 +1,38 @@ -package awais.instagrabber.repositories.responses; +package awais.instagrabber.repositories.responses -import java.io.Serializable; -import java.util.List; -import java.util.Objects; +import java.io.Serializable -public class User implements Serializable { - private final long pk; - private final String username; - private final String fullName; - private final boolean isPrivate; - private final String profilePicUrl; - private final String profilePicId; - private FriendshipStatus friendshipStatus; - private final boolean isVerified; - private final boolean hasAnonymousProfilePicture; - private final boolean isUnpublished; - private final boolean isFavorite; - private final boolean isDirectappInstalled; - private final boolean hasChaining; - private final String reelAutoArchive; - private final String allowedCommenterType; - private final long mediaCount; - private final long followerCount; - private final long followingCount; - private final long followingTagCount; - private final String biography; - private final String externalUrl; - private final long usertagsCount; - private final String publicEmail; - private final HdProfilePicUrlInfo hdProfilePicUrlInfo; - private final String profileContext; // "also followed by" your friends - private final List profileContextLinksWithUserIds; // ^ - private final String socialContext; // AYML - private final String interopMessagingUserFbid; // in DMs only: Facebook user ID - public User(final long pk, - final String username, - final String fullName, - final boolean isPrivate, - final String profilePicUrl, - final String profilePicId, - final FriendshipStatus friendshipStatus, - final boolean isVerified, - final boolean hasAnonymousProfilePicture, - final boolean isUnpublished, - final boolean isFavorite, - final boolean isDirectappInstalled, - final boolean hasChaining, - final String reelAutoArchive, - final String allowedCommenterType, - final long mediaCount, - final long followerCount, - final long followingCount, - final long followingTagCount, - final String biography, - final String externalUrl, - final long usertagsCount, - final String publicEmail, - final HdProfilePicUrlInfo hdProfilePicUrlInfo, - final String profileContext, - final List profileContextLinksWithUserIds, - final String socialContext, - final String interopMessagingUserFbid) { - this.pk = pk; - this.username = username; - this.fullName = fullName; - this.isPrivate = isPrivate; - this.profilePicUrl = profilePicUrl; - this.profilePicId = profilePicId; - this.friendshipStatus = friendshipStatus; - this.isVerified = isVerified; - this.hasAnonymousProfilePicture = hasAnonymousProfilePicture; - this.isUnpublished = isUnpublished; - this.isFavorite = isFavorite; - this.isDirectappInstalled = isDirectappInstalled; - this.hasChaining = hasChaining; - this.reelAutoArchive = reelAutoArchive; - this.allowedCommenterType = allowedCommenterType; - this.mediaCount = mediaCount; - this.followerCount = followerCount; - this.followingCount = followingCount; - this.followingTagCount = followingTagCount; - this.biography = biography; - this.externalUrl = externalUrl; - this.usertagsCount = usertagsCount; - this.publicEmail = publicEmail; - this.hdProfilePicUrlInfo = hdProfilePicUrlInfo; - this.profileContext = profileContext; - this.profileContextLinksWithUserIds = profileContextLinksWithUserIds; - this.socialContext = socialContext; - this.interopMessagingUserFbid = interopMessagingUserFbid; - } - - public User(final long pk, - final String username, - final String fullName, - final boolean isPrivate, - final String profilePicUrl, - final boolean isVerified) { - this.pk = pk; - this.username = username; - this.fullName = fullName; - this.isPrivate = isPrivate; - this.profilePicUrl = profilePicUrl; - this.profilePicId = null; - this.friendshipStatus = new FriendshipStatus( - false, - false, - false, - false, - false, - false, - false, - false, - false, - false - ); - this.isVerified = isVerified; - this.hasAnonymousProfilePicture = false; - this.isUnpublished = false; - this.isFavorite = false; - this.isDirectappInstalled = false; - this.hasChaining = false; - this.reelAutoArchive = null; - this.allowedCommenterType = null; - this.mediaCount = 0; - this.followerCount = 0; - this.followingCount = 0; - this.followingTagCount = 0; - this.biography = null; - this.externalUrl = null; - this.usertagsCount = 0; - this.publicEmail = null; - this.hdProfilePicUrlInfo = null; - this.profileContext = null; - this.profileContextLinksWithUserIds = null; - this.socialContext = null; - this.interopMessagingUserFbid = null; - } - - public long getPk() { - return pk; - } - - public String getUsername() { - return username; - } - - public String getFullName() { - return fullName; - } - - public boolean isPrivate() { - return isPrivate; - } - - public String getProfilePicUrl() { - return profilePicUrl; - } - - public String getHDProfilePicUrl() { - if (hdProfilePicUrlInfo == null) { - return getProfilePicUrl(); - } - return hdProfilePicUrlInfo.getUrl(); - } - - public String getProfilePicId() { - return profilePicId; - } - - public FriendshipStatus getFriendshipStatus() { - return friendshipStatus; - } - - public void setFriendshipStatus(final FriendshipStatus friendshipStatus) { - this.friendshipStatus = friendshipStatus; - } - - public boolean isVerified() { - return isVerified; - } - - public boolean hasAnonymousProfilePicture() { - return hasAnonymousProfilePicture; - } - - public boolean isUnpublished() { - return isUnpublished; - } - - public boolean isFavorite() { - return isFavorite; - } - - public boolean isDirectappInstalled() { - return isDirectappInstalled; - } - - public boolean hasChaining() { - return hasChaining; - } - - public String getReelAutoArchive() { - return reelAutoArchive; - } - - public String getAllowedCommenterType() { - return allowedCommenterType; - } - - public long getMediaCount() { - return mediaCount; - } - - public long getFollowerCount() { - return followerCount; - } - - public long getFollowingCount() { - return followingCount; - } - - public long getFollowingTagCount() { - return followingTagCount; - } - - public String getBiography() { - return biography; - } - - public String getExternalUrl() { - return externalUrl; - } - - public long getUsertagsCount() { - return usertagsCount; - } - - public String getPublicEmail() { - return publicEmail; - } - - public String getProfileContext() { - return profileContext; - } - - public String getSocialContext() { - return socialContext; - } - - public List getProfileContextLinks() { - return profileContextLinksWithUserIds; - } - - public String getFbId() { - return interopMessagingUserFbid; - } - - @Override - public boolean equals(final Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - final User user = (User) o; - return pk == user.pk && - isPrivate == user.isPrivate && - isVerified == user.isVerified && - hasAnonymousProfilePicture == user.hasAnonymousProfilePicture && - isUnpublished == user.isUnpublished && - isFavorite == user.isFavorite && - isDirectappInstalled == user.isDirectappInstalled && - mediaCount == user.mediaCount && - followerCount == user.followerCount && - followingCount == user.followingCount && - followingTagCount == user.followingTagCount && - usertagsCount == user.usertagsCount && - Objects.equals(username, user.username) && - Objects.equals(fullName, user.fullName) && - Objects.equals(profilePicUrl, user.profilePicUrl) && - Objects.equals(profilePicId, user.profilePicId) && - Objects.equals(friendshipStatus, user.friendshipStatus) && - Objects.equals(reelAutoArchive, user.reelAutoArchive) && - Objects.equals(allowedCommenterType, user.allowedCommenterType) && - Objects.equals(biography, user.biography) && - Objects.equals(externalUrl, user.externalUrl) && - Objects.equals(publicEmail, user.publicEmail); - } - - @Override - public int hashCode() { - return Objects.hash(pk, username, fullName, isPrivate, profilePicUrl, profilePicId, friendshipStatus, isVerified, hasAnonymousProfilePicture, - isUnpublished, isFavorite, isDirectappInstalled, hasChaining, reelAutoArchive, allowedCommenterType, mediaCount, - followerCount, followingCount, followingTagCount, biography, externalUrl, usertagsCount, publicEmail); - } -} +data class User @JvmOverloads constructor( + val pk: Long = 0, + val username: String = "", + val fullName: String = "", + val isPrivate: Boolean = false, + val profilePicUrl: String? = null, + val isVerified: Boolean = false, + val profilePicId: String? = null, + var friendshipStatus: FriendshipStatus? = null, + val hasAnonymousProfilePicture: Boolean = false, + val isUnpublished: Boolean = false, + val isFavorite: Boolean = false, + val isDirectappInstalled: Boolean = false, + val hasChaining: Boolean = false, + val reelAutoArchive: String? = null, + val allowedCommenterType: String? = null, + val mediaCount: Long = 0, + val followerCount: Long = 0, + val followingCount: Long = 0, + val followingTagCount: Long = 0, + val biography: String? = null, + val externalUrl: String? = null, + val usertagsCount: Long = 0, + val publicEmail: String? = null, + val hdProfilePicUrlInfo: HdProfilePicUrlInfo? = null, + val profileContext: String? = null, // "also followed by" your friends + val profileContextLinksWithUserIds: List? = null, // ^ + val socialContext: String? = null, // AYML + val interopMessagingUserFbid: String? = null, // in DMs only: Facebook user ID +) : Serializable { + val hDProfilePicUrl: String + get() = hdProfilePicUrlInfo?.url ?: profilePicUrl ?: "" +} \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/viewmodels/DirectSettingsViewModel.kt b/app/src/main/java/awais/instagrabber/viewmodels/DirectSettingsViewModel.kt index 70b3c361..eea45149 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/DirectSettingsViewModel.kt +++ b/app/src/main/java/awais/instagrabber/viewmodels/DirectSettingsViewModel.kt @@ -135,7 +135,7 @@ class DirectSettingsViewModel( if (isAdmin) ACTION_REMOVE_ADMIN else ACTION_MAKE_ADMIN )) } - val blocking: Boolean = user.friendshipStatus.blocking + val blocking: Boolean = user.friendshipStatus?.blocking ?: false options.add(Option( if (blocking) getString(R.string.unblock) else getString(R.string.block), if (blocking) ACTION_UNBLOCK else ACTION_BLOCK @@ -144,7 +144,7 @@ class DirectSettingsViewModel( // options.add(new Option<>(getString(R.string.report), ACTION_REPORT)); val isGroup: Boolean? = threadManager.isGroup.value if (isGroup != null && isGroup) { - val restricted: Boolean = user.friendshipStatus.isRestricted + val restricted: Boolean = user.friendshipStatus?.isRestricted ?: false options.add(Option( if (restricted) getString(R.string.unrestrict) else getString(R.string.restrict), if (restricted) ACTION_UNRESTRICT else ACTION_RESTRICT diff --git a/app/src/main/java/awais/instagrabber/webservices/GraphQLService.java b/app/src/main/java/awais/instagrabber/webservices/GraphQLService.java index 1c501e7b..5e43ca1f 100644 --- a/app/src/main/java/awais/instagrabber/webservices/GraphQLService.java +++ b/app/src/main/java/awais/instagrabber/webservices/GraphQLService.java @@ -41,8 +41,8 @@ public class GraphQLService extends BaseService { private GraphQLService() { repository = RetrofitFactory.INSTANCE - .getRetrofitWeb() - .create(GraphQLRepository.class); + .getRetrofitWeb() + .create(GraphQLRepository.class); } public static GraphQLService getInstance() { @@ -309,6 +309,7 @@ public class GraphQLService extends BaseService { userJson.getString("full_name"), isPrivate, userJson.getString("profile_pic_url_hd"), + userJson.getBoolean("is_verified"), null, new FriendshipStatus( userJson.optBoolean("followed_by_viewer"), @@ -322,7 +323,6 @@ public class GraphQLService extends BaseService { userJson.optBoolean("restricted_by_viewer"), false ), - userJson.getBoolean("is_verified"), false, false, false, @@ -376,7 +376,7 @@ public class GraphQLService extends BaseService { try { final JSONObject body = new JSONObject(rawBody); final JSONObject media = body.getJSONObject("graphql") - .getJSONObject("shortcode_media"); + .getJSONObject("shortcode_media"); callback.onSuccess(ResponseBodyUtils.parseGraphQLItem(media, null)); } catch (JSONException e) { Log.e(TAG, "onResponse", e);