From 383ffce89f6d62acf22ec740f7c0659c72de8237 Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Thu, 18 Mar 2021 16:34:26 -0400 Subject: [PATCH] better handling for own profile --- .../instagrabber/asyncs/ProfileFetcher.java | 19 ++- .../instagrabber/asyncs/UsernameFetcher.java | 55 -------- .../fragments/main/ProfileFragment.java | 119 +++++++----------- .../viewmodels/AppStateViewModel.java | 45 +------ 4 files changed, 69 insertions(+), 169 deletions(-) delete mode 100755 app/src/main/java/awais/instagrabber/asyncs/UsernameFetcher.java diff --git a/app/src/main/java/awais/instagrabber/asyncs/ProfileFetcher.java b/app/src/main/java/awais/instagrabber/asyncs/ProfileFetcher.java index 612259bd..1f1ba2cc 100755 --- a/app/src/main/java/awais/instagrabber/asyncs/ProfileFetcher.java +++ b/app/src/main/java/awais/instagrabber/asyncs/ProfileFetcher.java @@ -18,13 +18,16 @@ public final class ProfileFetcher extends AsyncTask { private final GraphQLService graphQLService; private final FetchListener fetchListener; + private final long myId; private final boolean isLoggedIn; private final String userName; public ProfileFetcher(final String userName, + final long myId, final boolean isLoggedIn, final FetchListener fetchListener) { this.userName = userName; + this.myId = myId; this.isLoggedIn = isLoggedIn; this.fetchListener = fetchListener; userService = isLoggedIn ? UserService.getInstance() : null; @@ -34,7 +37,7 @@ public final class ProfileFetcher extends AsyncTask { @Nullable @Override protected Void doInBackground(final Void... voids) { - if (isLoggedIn) { + if (isLoggedIn && userName != null) { userService.getUsernameInfo(userName, new ServiceCallback() { @Override public void onSuccess(final User user) { @@ -60,6 +63,20 @@ public final class ProfileFetcher extends AsyncTask { } }); } + else if (isLoggedIn) { + userService.getUserInfo(myId, new ServiceCallback() { + @Override + public void onSuccess(final User user) { + fetchListener.onResult(user); + } + + @Override + public void onFailure(final Throwable t) { + Log.e(TAG, "Error", t); + fetchListener.onFailure(t); + } + }); + } else { graphQLService.fetchUser(userName, new ServiceCallback() { @Override diff --git a/app/src/main/java/awais/instagrabber/asyncs/UsernameFetcher.java b/app/src/main/java/awais/instagrabber/asyncs/UsernameFetcher.java deleted file mode 100755 index d0feb459..00000000 --- a/app/src/main/java/awais/instagrabber/asyncs/UsernameFetcher.java +++ /dev/null @@ -1,55 +0,0 @@ -package awais.instagrabber.asyncs; - -import android.os.AsyncTask; -import android.util.Log; - -import androidx.annotation.Nullable; - -import org.json.JSONObject; - -import java.net.HttpURLConnection; -import java.net.URL; - -import awais.instagrabber.BuildConfig; -import awais.instagrabber.interfaces.FetchListener; -import awais.instagrabber.utils.Constants; -import awais.instagrabber.utils.NetworkUtils; -import awais.instagrabber.utils.Utils; - -public final class UsernameFetcher extends AsyncTask { - private final FetchListener fetchListener; - private final long uid; - - public UsernameFetcher(final long uid, final FetchListener fetchListener) { - this.uid = uid; - this.fetchListener = fetchListener; - } - - @Nullable - @Override - protected String doInBackground(final Void... voids) { - String result = null; - - try { - final HttpURLConnection conn = (HttpURLConnection) new URL("https://i.instagram.com/api/v1/users/" + uid + "/info/").openConnection(); - conn.setRequestProperty("User-Agent", Utils.settingsHelper.getString(Constants.BROWSER_UA)); - conn.setUseCaches(true); - - final JSONObject user; - if (conn.getResponseCode() == HttpURLConnection.HTTP_OK && - (user = new JSONObject(NetworkUtils.readFromConnection(conn)).optJSONObject(Constants.EXTRAS_USER)) != null) - result = user.getString(Constants.EXTRAS_USERNAME); - - conn.disconnect(); - } catch (final Exception e) { - if (BuildConfig.DEBUG) Log.e("AWAISKING_APP", "", e); - } - - return result; - } - - @Override - protected void onPostExecute(final String result) { - if (fetchListener != null) fetchListener.onResult(result); - } -} 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 df3f134e..4cd53a0b 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -59,7 +59,6 @@ import awais.instagrabber.adapters.HighlightsAdapter; import awais.instagrabber.asyncs.CreateThreadAction; import awais.instagrabber.asyncs.ProfileFetcher; import awais.instagrabber.asyncs.ProfilePostFetchService; -import awais.instagrabber.asyncs.UsernameFetcher; import awais.instagrabber.customviews.PrimaryActionModeCallback; import awais.instagrabber.customviews.PrimaryActionModeCallback.CallbacksHelper; import awais.instagrabber.databinding.FragmentProfileBinding; @@ -125,11 +124,12 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe private HighlightsViewModel highlightsViewModel; private MenuItem blockMenuItem, restrictMenuItem, chainingMenuItem; private MenuItem muteStoriesMenuItem, mutePostsMenuItem; - private boolean highlightsFetching; + private boolean accountIsUpdated = false; private boolean postsSetupDone = false; private Set selectedFeedModels; private Media downloadFeedModel; private int downloadChildPosition = -1; + private long myId; private PostsLayoutPreferences layoutPreferences = Utils.getPostsLayoutPreferences(Constants.PREF_PROFILE_POSTS_LAYOUT); private final Runnable usernameSettingRunnable = () -> { @@ -303,11 +303,11 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe super.onCreate(savedInstanceState); cookie = Utils.settingsHelper.getString(Constants.COOKIE); isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) > 0; - final long userId = CookieUtils.getUserIdFromCookie(cookie); + myId = CookieUtils.getUserIdFromCookie(cookie); final String deviceUuid = Utils.settingsHelper.getString(Constants.DEVICE_UUID); final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie); fragmentActivity = (MainActivity) requireActivity(); - friendshipService = isLoggedIn ? FriendshipService.getInstance(deviceUuid, csrfToken, userId) : null; + friendshipService = isLoggedIn ? FriendshipService.getInstance(deviceUuid, csrfToken, myId) : null; storiesService = isLoggedIn ? StoriesService.getInstance(null, 0L, null) : null; mediaService = isLoggedIn ? MediaService.getInstance(null, null, 0) : null; accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(getContext())); @@ -363,9 +363,10 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe public void onCreateOptionsMenu(@NonNull final Menu menu, @NonNull final MenuInflater inflater) { inflater.inflate(R.menu.profile_menu, menu); blockMenuItem = menu.findItem(R.id.block); + final boolean isNotMe = profileModel != null && !Objects.equals(profileModel.getPk(), CookieUtils.getUserIdFromCookie(cookie)); if (blockMenuItem != null) { - if (profileModel != null) { - blockMenuItem.setVisible(!Objects.equals(profileModel.getPk(), CookieUtils.getUserIdFromCookie(cookie))); + if (isNotMe) { + blockMenuItem.setVisible(true); blockMenuItem.setTitle(profileModel.getFriendshipStatus().isBlocking() ? R.string.unblock : R.string.block); } else { blockMenuItem.setVisible(false); @@ -373,8 +374,8 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe } restrictMenuItem = menu.findItem(R.id.restrict); if (restrictMenuItem != null) { - if (profileModel != null) { - restrictMenuItem.setVisible(!Objects.equals(profileModel.getPk(), CookieUtils.getUserIdFromCookie(cookie))); + if (isNotMe) { + restrictMenuItem.setVisible(true); restrictMenuItem.setTitle(profileModel.getFriendshipStatus().isRestricted() ? R.string.unrestrict : R.string.restrict); } else { restrictMenuItem.setVisible(false); @@ -382,8 +383,8 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe } muteStoriesMenuItem = menu.findItem(R.id.mute_stories); if (muteStoriesMenuItem != null) { - if (profileModel != null) { - muteStoriesMenuItem.setVisible(!Objects.equals(profileModel.getPk(), CookieUtils.getUserIdFromCookie(cookie))); + if (isNotMe) { + muteStoriesMenuItem.setVisible(true); muteStoriesMenuItem.setTitle(profileModel.getFriendshipStatus().isMutingReel() ? R.string.mute_stories : R.string.unmute_stories); } else { muteStoriesMenuItem.setVisible(false); @@ -391,8 +392,8 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe } mutePostsMenuItem = menu.findItem(R.id.mute_posts); if (mutePostsMenuItem != null) { - if (profileModel != null) { - mutePostsMenuItem.setVisible(!Objects.equals(profileModel.getPk(), CookieUtils.getUserIdFromCookie(cookie))); + if (isNotMe) { + mutePostsMenuItem.setVisible(true); mutePostsMenuItem.setTitle(profileModel.getFriendshipStatus().isMuting() ? R.string.mute_posts : R.string.unmute_posts); } else { mutePostsMenuItem.setVisible(false); @@ -400,11 +401,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe } chainingMenuItem = menu.findItem(R.id.chaining); if (chainingMenuItem != null) { - if (profileModel != null) { - chainingMenuItem.setVisible(!Objects.equals(profileModel.getPk(), CookieUtils.getUserIdFromCookie(cookie))); - } else { - chainingMenuItem.setVisible(false); - } + chainingMenuItem.setVisible(isNotMe); } } @@ -588,49 +585,20 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe binding.swipeRefreshLayout.setEnabled(true); setupHighlights(); setupCommonListeners(); - fetchUsername(); - } - - private void fetchUsername() { - final long uid = CookieUtils.getUserIdFromCookie(cookie); - if (TextUtils.isEmpty(username) && uid > 0) { - final FetchListener fetchListener = username -> { - if (TextUtils.isEmpty(username)) return; - this.username = username; - setUsernameDelayed(); - fetchProfileDetails(); - }; - accountRepository.getAccount(uid, new RepositoryCallback() { - @Override - public void onSuccess(final Account account) { - boolean found = false; - if (account != null) { - final String username = account.getUsername(); - if (!TextUtils.isEmpty(username)) { - found = true; - fetchListener.onResult("@" + username); - } - } - if (!found) { - // if not in database, fetch info from instagram - new UsernameFetcher(uid, fetchListener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - } - } - - @Override - public void onDataNotAvailable() {} - }); - return; - } fetchProfileDetails(); } private void fetchProfileDetails() { - if (TextUtils.isEmpty(username)) return; - new ProfileFetcher(username.trim().substring(1), isLoggedIn, new FetchListener() { + accountIsUpdated = false; + new ProfileFetcher(TextUtils.isEmpty(username) ? null : username.trim().substring(1), + myId, isLoggedIn, new FetchListener() { @Override public void onResult(final User user) { if (getContext() == null) return; + if (TextUtils.isEmpty(username)) { + username = user.getUsername(); + setUsernameDelayed(); + } profileModel = user; setProfileDetails(); } @@ -665,11 +633,10 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe } profileDetailsBinding.isVerified.setVisibility(profileModel.isVerified() ? View.VISIBLE : View.GONE); final long profileId = profileModel.getPk(); - final long myId = CookieUtils.getUserIdFromCookie(cookie); if (isLoggedIn) { fetchStoryAndHighlights(profileId); } - setupButtons(profileId, myId); + setupButtons(profileId); profileDetailsBinding.favChip.setVisibility(View.VISIBLE); final FavoriteRepository favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(getContext())); favoriteRepository.getFavorite(profileModel.getUsername(), FavoriteType.USER, new RepositoryCallback() { @@ -895,13 +862,10 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe profileDetailsBinding.mainFollowers.setOnClickListener(followersCount > 0 ? followClickListener : null); profileDetailsBinding.mainFollowing.setOnClickListener(followingCount > 0 ? followClickListener : null); } - binding.swipeRefreshLayout.setRefreshing(true); binding.postsRecyclerView.setVisibility(View.VISIBLE); - fetchPosts(); } else { profileDetailsBinding.mainFollowers.setClickable(false); profileDetailsBinding.mainFollowing.setClickable(false); - binding.swipeRefreshLayout.setRefreshing(false); // error binding.privatePage1.setImageResource(R.drawable.lock); binding.privatePage2.setText(R.string.priv_acc); @@ -910,7 +874,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe } } - private void setupButtons(final long profileId, final long myId) { + private void setupButtons(final long profileId) { profileDetailsBinding.btnTagged.setVisibility(isReallyPrivate() ? View.GONE : View.VISIBLE); if (isLoggedIn) { if (Objects.equals(profileId, myId)) { @@ -919,6 +883,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe profileDetailsBinding.btnLiked.setVisibility(View.VISIBLE); profileDetailsBinding.btnDM.setVisibility(View.GONE); profileDetailsBinding.btnSaved.setText(R.string.saved); + if (!accountIsUpdated) updateAccountInfo(); return; } profileDetailsBinding.btnSaved.setVisibility(View.GONE); @@ -973,6 +938,26 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe } } + private void updateAccountInfo() { + accountRepository.insertOrUpdateAccount( + profileModel.getPk(), + profileModel.getUsername(), + cookie, + profileModel.getFullName(), + profileModel.getProfilePicUrl(), + new RepositoryCallback() { + @Override + public void onSuccess(final Account result) { + accountIsUpdated = true; + } + + @Override + public void onDataNotAvailable() { + Log.e(TAG, "onDataNotAvailable: insert failed"); + } + }); + } + private void fetchStoryAndHighlights(final long profileId) { storiesService.getUserStory( StoryViewerOptions.forUser(profileId, profileModel.getFullName()), @@ -994,7 +979,6 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe new ServiceCallback>() { @Override public void onSuccess(final List result) { - highlightsFetching = false; if (result != null) { profileDetailsBinding.highlightsList.setVisibility(View.VISIBLE); highlightsViewModel.getList().postValue(result); @@ -1166,7 +1150,8 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe } private void updateSwipeRefreshState() { - binding.swipeRefreshLayout.setRefreshing(binding.postsRecyclerView.isFetching() || highlightsFetching); + Log.d("austin_debug", "usrs: "+binding.postsRecyclerView.isFetching()); + binding.swipeRefreshLayout.setRefreshing(binding.postsRecyclerView.isFetching()); } private void setupHighlights() { @@ -1185,14 +1170,6 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe highlightsViewModel.getList().observe(getViewLifecycleOwner(), highlightModels -> highlightsAdapter.submitList(highlightModels)); } - private void fetchPosts() { - // stopCurrentExecutor(); - binding.swipeRefreshLayout.setRefreshing(true); - // currentlyExecuting = new PostsFetcher(profileModel.getId(), PostItemType.MAIN, endCursor, postsFetchListener) - // .setUsername(profileModel.getUsername()) - // .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - } - private void navigateToProfile(final String username) { final NavController navController = NavHostFragment.findNavController(this); final Bundle bundle = new Bundle(); @@ -1211,8 +1188,8 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe } private boolean isReallyPrivate() { - final long myId = CookieUtils.getUserIdFromCookie(cookie); + if (profileModel.getPk() == myId) return false; final FriendshipStatus friendshipStatus = profileModel.getFriendshipStatus(); - return !friendshipStatus.isFollowing() && (profileModel.getPk() != myId) && profileModel.isPrivate(); + return !friendshipStatus.isFollowing() && profileModel.isPrivate(); } } diff --git a/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java b/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java index 2e1f01f2..c09e8e74 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java +++ b/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java @@ -7,7 +7,6 @@ import androidx.annotation.NonNull; import androidx.lifecycle.AndroidViewModel; import awais.instagrabber.asyncs.ProfileFetcher; -import awais.instagrabber.asyncs.UsernameFetcher; import awais.instagrabber.db.datasources.AccountDataSource; import awais.instagrabber.db.entities.Account; import awais.instagrabber.db.repositories.AccountRepository; @@ -28,7 +27,6 @@ public class AppStateViewModel extends AndroidViewModel { private User currentUser; private AccountRepository accountRepository; - private String username; public AppStateViewModel(@NonNull final Application application) { super(application); @@ -37,52 +35,15 @@ public class AppStateViewModel extends AndroidViewModel { isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) > 0; if (!isLoggedIn) return; accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(application)); - setCurrentUser(); - } - - private void setCurrentUser() { - if (!isLoggedIn) return; - final FetchListener usernameListener = username -> { - if (TextUtils.isEmpty(username)) return; - this.username = username; - fetchProfileDetails(); - }; - fetchUsername(usernameListener); + fetchProfileDetails(); } public User getCurrentUser() { return currentUser; } - private void fetchUsername(final FetchListener usernameListener) { - if (!TextUtils.isEmpty(username)) { - usernameListener.onResult(username); - return; - } - final long uid = CookieUtils.getUserIdFromCookie(cookie); - if (uid <= 0) return; - accountRepository.getAccount(uid, new RepositoryCallback() { - @Override - public void onSuccess(@NonNull final Account account) { - final String username = account.getUsername(); - if (TextUtils.isEmpty(username)) return; - usernameListener.onResult("@" + username); - } - - @Override - public void onDataNotAvailable() { - // if not in database, fetch info - new UsernameFetcher(uid, usernameListener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - } - }); - } - private void fetchProfileDetails() { - if (TextUtils.isEmpty(username)) return; - new ProfileFetcher( - username.trim().substring(1), - true, - user -> this.currentUser = user - ).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + final long uid = CookieUtils.getUserIdFromCookie(cookie); + new ProfileFetcher(null, uid, true, user -> this.currentUser = user).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } }