1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-16 19:57:31 +00:00

Fix crash when fetching self profile info is slow

This commit is contained in:
Ammar Githam 2021-03-31 21:43:20 +09:00
parent cbb7b8654a
commit 666a36ff20
2 changed files with 39 additions and 27 deletions

View File

@ -12,7 +12,6 @@ import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan; import android.text.style.StyleSpan;
import android.util.Log; import android.util.Log;
import android.view.ActionMode; import android.view.ActionMode;
import android.view.Gravity;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
@ -604,12 +603,16 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
usernameTemp = usernameTemp.substring(1); usernameTemp = usernameTemp.substring(1);
} }
if (TextUtils.isEmpty(usernameTemp)) { if (TextUtils.isEmpty(usernameTemp)) {
profileModel = appStateViewModel.getCurrentUser(); appStateViewModel.getCurrentUserLiveData().observe(getViewLifecycleOwner(), user -> {
if (user == null) return;
profileModel = user;
username = profileModel.getUsername(); username = profileModel.getUsername();
setUsernameDelayed(); setUsernameDelayed();
setProfileDetails(); setProfileDetails();
});
return;
} }
else if (isLoggedIn) { if (isLoggedIn) {
userService.getUsernameInfo(usernameTemp, new ServiceCallback<User>() { userService.getUsernameInfo(usernameTemp, new ServiceCallback<User>() {
@Override @Override
public void onSuccess(final User user) { public void onSuccess(final User user) {
@ -643,8 +646,8 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
} catch (final Throwable ignored) {} } catch (final Throwable ignored) {}
} }
}); });
return;
} }
else {
graphQLService.fetchUser(usernameTemp, new ServiceCallback<User>() { graphQLService.fetchUser(usernameTemp, new ServiceCallback<User>() {
@Override @Override
public void onSuccess(final User user) { public void onSuccess(final User user) {
@ -663,7 +666,6 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
} }
}); });
} }
}
private void setProfileDetails() { private void setProfileDetails() {
final Context context = getContext(); final Context context = getContext();
@ -987,6 +989,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
} }
private void updateAccountInfo() { private void updateAccountInfo() {
if (profileModel == null) return;
accountRepository.insertOrUpdateAccount( accountRepository.insertOrUpdateAccount(
profileModel.getPk(), profileModel.getPk(),
profileModel.getUsername(), profileModel.getUsername(),

View File

@ -1,9 +1,12 @@
package awais.instagrabber.viewmodels; package awais.instagrabber.viewmodels;
import android.app.Application; import android.app.Application;
import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import awais.instagrabber.db.datasources.AccountDataSource; import awais.instagrabber.db.datasources.AccountDataSource;
import awais.instagrabber.db.repositories.AccountRepository; import awais.instagrabber.db.repositories.AccountRepository;
@ -21,8 +24,8 @@ public class AppStateViewModel extends AndroidViewModel {
private final String cookie; private final String cookie;
private final boolean isLoggedIn; private final boolean isLoggedIn;
private final MutableLiveData<User> currentUser = new MutableLiveData<>();
private User currentUser;
private AccountRepository accountRepository; private AccountRepository accountRepository;
private UserService userService; private UserService userService;
@ -38,6 +41,10 @@ public class AppStateViewModel extends AndroidViewModel {
} }
public User getCurrentUser() { public User getCurrentUser() {
return currentUser.getValue();
}
public LiveData<User> getCurrentUserLiveData() {
return currentUser; return currentUser;
} }
@ -46,11 +53,13 @@ public class AppStateViewModel extends AndroidViewModel {
userService.getUserInfo(uid, new ServiceCallback<User>() { userService.getUserInfo(uid, new ServiceCallback<User>() {
@Override @Override
public void onSuccess(final User user) { public void onSuccess(final User user) {
currentUser = user; currentUser.postValue(user);
} }
@Override @Override
public void onFailure(final Throwable t) {} public void onFailure(final Throwable t) {
Log.e(TAG, "onFailure: ", t);
}
}); });
} }
} }