2021-01-02 02:54:32 +00:00
|
|
|
package awais.instagrabber.viewmodels;
|
|
|
|
|
|
|
|
import android.app.Application;
|
2021-03-31 12:43:20 +00:00
|
|
|
import android.util.Log;
|
2021-01-02 02:54:32 +00:00
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
2021-05-02 09:16:25 +00:00
|
|
|
import androidx.annotation.Nullable;
|
2021-01-02 02:54:32 +00:00
|
|
|
import androidx.lifecycle.AndroidViewModel;
|
2021-03-31 12:43:20 +00:00
|
|
|
import androidx.lifecycle.LiveData;
|
|
|
|
import androidx.lifecycle.MutableLiveData;
|
2021-01-02 02:54:32 +00:00
|
|
|
|
2021-06-29 15:21:10 +00:00
|
|
|
import awais.instagrabber.db.repositories.AccountRepository;
|
2021-06-13 11:01:09 +00:00
|
|
|
import awais.instagrabber.models.Resource;
|
2021-01-07 12:36:33 +00:00
|
|
|
import awais.instagrabber.repositories.responses.User;
|
2021-06-29 15:21:10 +00:00
|
|
|
import awais.instagrabber.utils.AppExecutors;
|
2021-01-02 02:54:32 +00:00
|
|
|
import awais.instagrabber.utils.Constants;
|
|
|
|
import awais.instagrabber.utils.CookieUtils;
|
2021-06-06 01:11:30 +00:00
|
|
|
import awais.instagrabber.utils.CoroutineUtilsKt;
|
2021-01-02 02:54:32 +00:00
|
|
|
import awais.instagrabber.utils.TextUtils;
|
2021-06-08 23:40:13 +00:00
|
|
|
import awais.instagrabber.webservices.UserRepository;
|
2021-06-06 01:11:30 +00:00
|
|
|
import kotlinx.coroutines.Dispatchers;
|
2021-01-02 02:54:32 +00:00
|
|
|
|
|
|
|
import static awais.instagrabber.utils.Utils.settingsHelper;
|
|
|
|
|
|
|
|
public class AppStateViewModel extends AndroidViewModel {
|
|
|
|
private static final String TAG = AppStateViewModel.class.getSimpleName();
|
|
|
|
|
|
|
|
private final String cookie;
|
2021-06-13 11:01:09 +00:00
|
|
|
private final MutableLiveData<Resource<User>> currentUser = new MutableLiveData<>(Resource.loading(null));
|
2021-01-02 02:54:32 +00:00
|
|
|
|
2021-06-29 15:21:10 +00:00
|
|
|
private AccountRepository accountRepository;
|
|
|
|
|
2021-06-08 23:40:13 +00:00
|
|
|
private UserRepository userRepository;
|
2021-01-02 02:54:32 +00:00
|
|
|
|
|
|
|
public AppStateViewModel(@NonNull final Application application) {
|
|
|
|
super(application);
|
2021-01-19 17:24:49 +00:00
|
|
|
// Log.d(TAG, "AppStateViewModel: constructor");
|
2021-01-02 02:54:32 +00:00
|
|
|
cookie = settingsHelper.getString(Constants.COOKIE);
|
2021-04-13 15:17:23 +00:00
|
|
|
final boolean isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) != 0;
|
2021-06-13 11:01:09 +00:00
|
|
|
if (!isLoggedIn) {
|
|
|
|
currentUser.postValue(Resource.success(null));
|
|
|
|
return;
|
|
|
|
}
|
2021-06-09 00:09:40 +00:00
|
|
|
userRepository = UserRepository.Companion.getInstance();
|
2021-06-29 15:21:10 +00:00
|
|
|
accountRepository = AccountRepository.Companion.getInstance(application);
|
2021-03-18 20:34:26 +00:00
|
|
|
fetchProfileDetails();
|
2021-01-02 02:54:32 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 09:16:25 +00:00
|
|
|
@Nullable
|
2021-06-13 11:01:09 +00:00
|
|
|
public Resource<User> getCurrentUser() {
|
2021-03-31 12:43:20 +00:00
|
|
|
return currentUser.getValue();
|
|
|
|
}
|
|
|
|
|
2021-06-13 11:01:09 +00:00
|
|
|
public LiveData<Resource<User>> getCurrentUserLiveData() {
|
2021-01-02 02:54:32 +00:00
|
|
|
return currentUser;
|
|
|
|
}
|
|
|
|
|
2021-06-19 00:00:42 +00:00
|
|
|
public void fetchProfileDetails() {
|
2021-06-13 11:01:09 +00:00
|
|
|
currentUser.postValue(Resource.loading(null));
|
2021-03-18 20:34:26 +00:00
|
|
|
final long uid = CookieUtils.getUserIdFromCookie(cookie);
|
2021-06-13 11:01:09 +00:00
|
|
|
if (userRepository == null) {
|
|
|
|
currentUser.postValue(Resource.success(null));
|
|
|
|
return;
|
|
|
|
}
|
2021-06-08 23:40:13 +00:00
|
|
|
userRepository.getUserInfo(uid, CoroutineUtilsKt.getContinuation((user, throwable) -> {
|
2021-06-06 01:11:30 +00:00
|
|
|
if (throwable != null) {
|
|
|
|
Log.e(TAG, "onFailure: ", throwable);
|
2021-06-29 15:21:10 +00:00
|
|
|
final Resource<User> userResource = currentUser.getValue();
|
|
|
|
final User backup = userResource != null && userResource.data != null ? userResource.data : new User(uid);
|
2021-06-19 00:00:42 +00:00
|
|
|
currentUser.postValue(Resource.error(throwable.getMessage(), backup));
|
2021-06-06 01:11:30 +00:00
|
|
|
return;
|
2021-03-22 01:18:07 +00:00
|
|
|
}
|
2021-06-13 11:01:09 +00:00
|
|
|
currentUser.postValue(Resource.success(user));
|
2021-06-29 15:21:10 +00:00
|
|
|
if (accountRepository != null && user != null) {
|
|
|
|
accountRepository.insertOrUpdateAccount(
|
|
|
|
user.getPk(),
|
|
|
|
user.getUsername(),
|
|
|
|
cookie,
|
|
|
|
user.getFullName() != null ? user.getFullName() : "",
|
|
|
|
user.getProfilePicUrl(),
|
|
|
|
CoroutineUtilsKt.getContinuation((account, throwable1) -> AppExecutors.INSTANCE.getMainThread().execute(() -> {
|
|
|
|
if (throwable1 != null) {
|
|
|
|
Log.e(TAG, "updateAccountInfo: ", throwable1);
|
|
|
|
}
|
|
|
|
}), Dispatchers.getIO())
|
|
|
|
);
|
|
|
|
}
|
2021-06-06 01:11:30 +00:00
|
|
|
}, Dispatchers.getIO()));
|
2021-01-02 02:54:32 +00:00
|
|
|
}
|
|
|
|
}
|