mirror of
https://github.com/KokaKiwi/BarInsta
synced 2024-11-22 14:47:29 +00:00
migrate ProfileFetcher
This commit is contained in:
parent
6a6a59a334
commit
e9239c31b0
@ -1,101 +0,0 @@
|
|||||||
package awais.instagrabber.asyncs;
|
|
||||||
|
|
||||||
import android.os.AsyncTask;
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
|
|
||||||
import awais.instagrabber.interfaces.FetchListener;
|
|
||||||
import awais.instagrabber.repositories.responses.FriendshipStatus;
|
|
||||||
import awais.instagrabber.repositories.responses.User;
|
|
||||||
import awais.instagrabber.webservices.GraphQLService;
|
|
||||||
import awais.instagrabber.webservices.ServiceCallback;
|
|
||||||
import awais.instagrabber.webservices.UserService;
|
|
||||||
|
|
||||||
public final class ProfileFetcher extends AsyncTask<Void, Void, Void> {
|
|
||||||
private static final String TAG = ProfileFetcher.class.getSimpleName();
|
|
||||||
private final UserService userService;
|
|
||||||
private final GraphQLService graphQLService;
|
|
||||||
|
|
||||||
private final FetchListener<User> 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<User> fetchListener) {
|
|
||||||
this.userName = userName;
|
|
||||||
this.myId = myId;
|
|
||||||
this.isLoggedIn = isLoggedIn;
|
|
||||||
this.fetchListener = fetchListener;
|
|
||||||
userService = isLoggedIn ? UserService.getInstance() : null;
|
|
||||||
graphQLService = isLoggedIn ? null : GraphQLService.getInstance();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
protected Void doInBackground(final Void... voids) {
|
|
||||||
if (isLoggedIn && userName != null) {
|
|
||||||
userService.getUsernameInfo(userName, new ServiceCallback<User>() {
|
|
||||||
@Override
|
|
||||||
public void onSuccess(final User user) {
|
|
||||||
userService.getUserFriendship(user.getPk(), new ServiceCallback<FriendshipStatus>() {
|
|
||||||
@Override
|
|
||||||
public void onSuccess(final FriendshipStatus status) {
|
|
||||||
user.setFriendshipStatus(status);
|
|
||||||
fetchListener.onResult(user);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onFailure(final Throwable t) {
|
|
||||||
Log.e(TAG, "Error", t);
|
|
||||||
fetchListener.onFailure(t);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onFailure(final Throwable t) {
|
|
||||||
Log.e(TAG, "Error", t);
|
|
||||||
fetchListener.onFailure(t);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else if (isLoggedIn) {
|
|
||||||
userService.getUserInfo(myId, new ServiceCallback<User>() {
|
|
||||||
@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<User>() {
|
|
||||||
@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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPreExecute() {
|
|
||||||
if (fetchListener != null) fetchListener.doBefore();
|
|
||||||
}
|
|
||||||
}
|
|
@ -4,7 +4,6 @@ import android.content.Context;
|
|||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.graphics.Typeface;
|
import android.graphics.Typeface;
|
||||||
import android.os.AsyncTask;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
@ -57,7 +56,6 @@ import awais.instagrabber.activities.MainActivity;
|
|||||||
import awais.instagrabber.adapters.FeedAdapterV2;
|
import awais.instagrabber.adapters.FeedAdapterV2;
|
||||||
import awais.instagrabber.adapters.HighlightsAdapter;
|
import awais.instagrabber.adapters.HighlightsAdapter;
|
||||||
import awais.instagrabber.asyncs.CreateThreadAction;
|
import awais.instagrabber.asyncs.CreateThreadAction;
|
||||||
import awais.instagrabber.asyncs.ProfileFetcher;
|
|
||||||
import awais.instagrabber.asyncs.ProfilePostFetchService;
|
import awais.instagrabber.asyncs.ProfilePostFetchService;
|
||||||
import awais.instagrabber.customviews.PrimaryActionModeCallback;
|
import awais.instagrabber.customviews.PrimaryActionModeCallback;
|
||||||
import awais.instagrabber.customviews.PrimaryActionModeCallback.CallbacksHelper;
|
import awais.instagrabber.customviews.PrimaryActionModeCallback.CallbacksHelper;
|
||||||
@ -73,7 +71,6 @@ import awais.instagrabber.db.repositories.RepositoryCallback;
|
|||||||
import awais.instagrabber.dialogs.PostsLayoutPreferencesDialogFragment;
|
import awais.instagrabber.dialogs.PostsLayoutPreferencesDialogFragment;
|
||||||
import awais.instagrabber.dialogs.ProfilePicDialogFragment;
|
import awais.instagrabber.dialogs.ProfilePicDialogFragment;
|
||||||
import awais.instagrabber.fragments.PostViewV2Fragment;
|
import awais.instagrabber.fragments.PostViewV2Fragment;
|
||||||
import awais.instagrabber.interfaces.FetchListener;
|
|
||||||
import awais.instagrabber.managers.DirectMessagesManager;
|
import awais.instagrabber.managers.DirectMessagesManager;
|
||||||
import awais.instagrabber.managers.InboxManager;
|
import awais.instagrabber.managers.InboxManager;
|
||||||
import awais.instagrabber.models.HighlightModel;
|
import awais.instagrabber.models.HighlightModel;
|
||||||
@ -93,11 +90,14 @@ import awais.instagrabber.utils.CookieUtils;
|
|||||||
import awais.instagrabber.utils.DownloadUtils;
|
import awais.instagrabber.utils.DownloadUtils;
|
||||||
import awais.instagrabber.utils.TextUtils;
|
import awais.instagrabber.utils.TextUtils;
|
||||||
import awais.instagrabber.utils.Utils;
|
import awais.instagrabber.utils.Utils;
|
||||||
|
import awais.instagrabber.viewmodels.AppStateViewModel;
|
||||||
import awais.instagrabber.viewmodels.HighlightsViewModel;
|
import awais.instagrabber.viewmodels.HighlightsViewModel;
|
||||||
import awais.instagrabber.webservices.FriendshipService;
|
import awais.instagrabber.webservices.FriendshipService;
|
||||||
|
import awais.instagrabber.webservices.GraphQLService;
|
||||||
import awais.instagrabber.webservices.MediaService;
|
import awais.instagrabber.webservices.MediaService;
|
||||||
import awais.instagrabber.webservices.ServiceCallback;
|
import awais.instagrabber.webservices.ServiceCallback;
|
||||||
import awais.instagrabber.webservices.StoriesService;
|
import awais.instagrabber.webservices.StoriesService;
|
||||||
|
import awais.instagrabber.webservices.UserService;
|
||||||
|
|
||||||
import static androidx.core.content.PermissionChecker.checkSelfPermission;
|
import static androidx.core.content.PermissionChecker.checkSelfPermission;
|
||||||
import static awais.instagrabber.fragments.HashTagFragment.ARG_HASHTAG;
|
import static awais.instagrabber.fragments.HashTagFragment.ARG_HASHTAG;
|
||||||
@ -120,6 +120,8 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
|
|||||||
private FriendshipService friendshipService;
|
private FriendshipService friendshipService;
|
||||||
private StoriesService storiesService;
|
private StoriesService storiesService;
|
||||||
private MediaService mediaService;
|
private MediaService mediaService;
|
||||||
|
private UserService userService;
|
||||||
|
private GraphQLService graphQLService;
|
||||||
private boolean shouldRefresh = true;
|
private boolean shouldRefresh = true;
|
||||||
private boolean hasStories = false;
|
private boolean hasStories = false;
|
||||||
private HighlightsAdapter highlightsAdapter;
|
private HighlightsAdapter highlightsAdapter;
|
||||||
@ -304,6 +306,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
|
|||||||
private LayoutProfileDetailsBinding profileDetailsBinding;
|
private LayoutProfileDetailsBinding profileDetailsBinding;
|
||||||
private AccountRepository accountRepository;
|
private AccountRepository accountRepository;
|
||||||
private FavoriteRepository favoriteRepository;
|
private FavoriteRepository favoriteRepository;
|
||||||
|
private AppStateViewModel appStateViewModel;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(@Nullable final Bundle savedInstanceState) {
|
public void onCreate(@Nullable final Bundle savedInstanceState) {
|
||||||
@ -317,8 +320,11 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
|
|||||||
friendshipService = isLoggedIn ? FriendshipService.getInstance(deviceUuid, csrfToken, myId) : null;
|
friendshipService = isLoggedIn ? FriendshipService.getInstance(deviceUuid, csrfToken, myId) : null;
|
||||||
storiesService = isLoggedIn ? StoriesService.getInstance(null, 0L, null) : null;
|
storiesService = isLoggedIn ? StoriesService.getInstance(null, 0L, null) : null;
|
||||||
mediaService = isLoggedIn ? MediaService.getInstance(null, null, 0) : null;
|
mediaService = isLoggedIn ? MediaService.getInstance(null, null, 0) : null;
|
||||||
|
userService = isLoggedIn ? UserService.getInstance() : null;
|
||||||
|
graphQLService = isLoggedIn ? null : GraphQLService.getInstance();
|
||||||
accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(getContext()));
|
accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(getContext()));
|
||||||
favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(getContext()));
|
favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(getContext()));
|
||||||
|
appStateViewModel = new ViewModelProvider(fragmentActivity).get(AppStateViewModel.class);
|
||||||
setHasOptionsMenu(true);
|
setHasOptionsMenu(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -601,14 +607,51 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
|
|||||||
if (usernameTemp.startsWith("@")) {
|
if (usernameTemp.startsWith("@")) {
|
||||||
usernameTemp = usernameTemp.substring(1);
|
usernameTemp = usernameTemp.substring(1);
|
||||||
}
|
}
|
||||||
new ProfileFetcher(TextUtils.isEmpty(username) ? null : usernameTemp, myId, isLoggedIn, new FetchListener<User>() {
|
if (TextUtils.isEmpty(usernameTemp)) {
|
||||||
@Override
|
profileModel = appStateViewModel.getCurrentUser();
|
||||||
public void onResult(final User user) {
|
username = profileModel.getUsername();
|
||||||
if (getContext() == null) return;
|
|
||||||
if (TextUtils.isEmpty(username)) {
|
|
||||||
username = user.getUsername();
|
|
||||||
setUsernameDelayed();
|
setUsernameDelayed();
|
||||||
|
setProfileDetails();
|
||||||
}
|
}
|
||||||
|
else if (isLoggedIn) {
|
||||||
|
userService.getUsernameInfo(usernameTemp, new ServiceCallback<User>() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(final User user) {
|
||||||
|
userService.getUserFriendship(user.getPk(), new ServiceCallback<FriendshipStatus>() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(final FriendshipStatus status) {
|
||||||
|
user.setFriendshipStatus(status);
|
||||||
|
profileModel = user;
|
||||||
|
setProfileDetails();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(final Throwable t) {
|
||||||
|
Log.e(TAG, "Error fetching profile relationship", t);
|
||||||
|
final Context context = getContext();
|
||||||
|
try {
|
||||||
|
if (t == null) Toast.makeText(context, R.string.error_loading_profile_loggedin, Toast.LENGTH_LONG).show();
|
||||||
|
else Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show();
|
||||||
|
} catch (final Throwable ignored) {}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(final Throwable t) {
|
||||||
|
Log.e(TAG, "Error fetching profile", t);
|
||||||
|
final Context context = getContext();
|
||||||
|
try {
|
||||||
|
if (t == null) Toast.makeText(context, R.string.error_loading_profile_loggedin, Toast.LENGTH_LONG).show();
|
||||||
|
else Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show();
|
||||||
|
} catch (final Throwable ignored) {}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
graphQLService.fetchUser(usernameTemp, new ServiceCallback<User>() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(final User user) {
|
||||||
profileModel = user;
|
profileModel = user;
|
||||||
setProfileDetails();
|
setProfileDetails();
|
||||||
}
|
}
|
||||||
@ -618,14 +661,12 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
|
|||||||
Log.e(TAG, "Error fetching profile", t);
|
Log.e(TAG, "Error fetching profile", t);
|
||||||
final Context context = getContext();
|
final Context context = getContext();
|
||||||
try {
|
try {
|
||||||
if (t == null) Toast.makeText(context,
|
if (t == null) Toast.makeText(context, R.string.error_loading_profile, Toast.LENGTH_LONG).show();
|
||||||
isLoggedIn ? R.string.error_loading_profile_loggedin : R.string.error_loading_profile,
|
|
||||||
Toast.LENGTH_LONG).show();
|
|
||||||
else Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show();
|
else Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show();
|
||||||
} catch (final Throwable ignored) {}
|
} catch (final Throwable ignored) {}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setProfileDetails() {
|
private void setProfileDetails() {
|
||||||
|
@ -1,21 +1,18 @@
|
|||||||
package awais.instagrabber.viewmodels;
|
package awais.instagrabber.viewmodels;
|
||||||
|
|
||||||
import android.app.Application;
|
import android.app.Application;
|
||||||
import android.os.AsyncTask;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.lifecycle.AndroidViewModel;
|
import androidx.lifecycle.AndroidViewModel;
|
||||||
|
|
||||||
import awais.instagrabber.asyncs.ProfileFetcher;
|
|
||||||
import awais.instagrabber.db.datasources.AccountDataSource;
|
import awais.instagrabber.db.datasources.AccountDataSource;
|
||||||
import awais.instagrabber.db.entities.Account;
|
|
||||||
import awais.instagrabber.db.repositories.AccountRepository;
|
import awais.instagrabber.db.repositories.AccountRepository;
|
||||||
import awais.instagrabber.db.repositories.RepositoryCallback;
|
|
||||||
import awais.instagrabber.interfaces.FetchListener;
|
|
||||||
import awais.instagrabber.repositories.responses.User;
|
import awais.instagrabber.repositories.responses.User;
|
||||||
import awais.instagrabber.utils.Constants;
|
import awais.instagrabber.utils.Constants;
|
||||||
import awais.instagrabber.utils.CookieUtils;
|
import awais.instagrabber.utils.CookieUtils;
|
||||||
import awais.instagrabber.utils.TextUtils;
|
import awais.instagrabber.utils.TextUtils;
|
||||||
|
import awais.instagrabber.webservices.ServiceCallback;
|
||||||
|
import awais.instagrabber.webservices.UserService;
|
||||||
|
|
||||||
import static awais.instagrabber.utils.Utils.settingsHelper;
|
import static awais.instagrabber.utils.Utils.settingsHelper;
|
||||||
|
|
||||||
@ -27,6 +24,7 @@ public class AppStateViewModel extends AndroidViewModel {
|
|||||||
|
|
||||||
private User currentUser;
|
private User currentUser;
|
||||||
private AccountRepository accountRepository;
|
private AccountRepository accountRepository;
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
public AppStateViewModel(@NonNull final Application application) {
|
public AppStateViewModel(@NonNull final Application application) {
|
||||||
super(application);
|
super(application);
|
||||||
@ -34,6 +32,7 @@ public class AppStateViewModel extends AndroidViewModel {
|
|||||||
cookie = settingsHelper.getString(Constants.COOKIE);
|
cookie = settingsHelper.getString(Constants.COOKIE);
|
||||||
isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) > 0;
|
isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) > 0;
|
||||||
if (!isLoggedIn) return;
|
if (!isLoggedIn) return;
|
||||||
|
userService = UserService.getInstance();
|
||||||
accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(application));
|
accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(application));
|
||||||
fetchProfileDetails();
|
fetchProfileDetails();
|
||||||
}
|
}
|
||||||
@ -44,6 +43,14 @@ public class AppStateViewModel extends AndroidViewModel {
|
|||||||
|
|
||||||
private void fetchProfileDetails() {
|
private void fetchProfileDetails() {
|
||||||
final long uid = CookieUtils.getUserIdFromCookie(cookie);
|
final long uid = CookieUtils.getUserIdFromCookie(cookie);
|
||||||
new ProfileFetcher(null, uid, true, user -> this.currentUser = user).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
userService.getUserInfo(uid, new ServiceCallback<User>() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(final User user) {
|
||||||
|
currentUser = user;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(final Throwable t) {}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user