2021-01-09 11:24:16 +00:00
|
|
|
package awais.instagrabber.viewmodels;
|
|
|
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.lifecycle.LiveData;
|
|
|
|
import androidx.lifecycle.MutableLiveData;
|
|
|
|
import androidx.lifecycle.ViewModel;
|
|
|
|
|
|
|
|
import com.google.common.collect.ImmutableList;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2021-05-23 06:57:01 +00:00
|
|
|
import java.util.Set;
|
2021-01-09 11:24:16 +00:00
|
|
|
|
|
|
|
import awais.instagrabber.R;
|
2021-05-23 06:57:01 +00:00
|
|
|
import awais.instagrabber.managers.DirectMessagesManager;
|
2021-01-09 11:24:16 +00:00
|
|
|
import awais.instagrabber.models.Resource;
|
|
|
|
import awais.instagrabber.models.enums.MediaItemType;
|
|
|
|
import awais.instagrabber.repositories.responses.Caption;
|
|
|
|
import awais.instagrabber.repositories.responses.Location;
|
|
|
|
import awais.instagrabber.repositories.responses.Media;
|
|
|
|
import awais.instagrabber.repositories.responses.User;
|
2021-05-23 06:57:01 +00:00
|
|
|
import awais.instagrabber.repositories.responses.directmessages.RankedRecipient;
|
2021-01-09 11:24:16 +00:00
|
|
|
import awais.instagrabber.utils.Constants;
|
|
|
|
import awais.instagrabber.utils.CookieUtils;
|
|
|
|
import awais.instagrabber.utils.TextUtils;
|
|
|
|
import awais.instagrabber.webservices.MediaService;
|
|
|
|
import awais.instagrabber.webservices.ServiceCallback;
|
2021-03-20 16:14:57 +00:00
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
|
|
|
import retrofit2.Response;
|
2021-01-09 11:24:16 +00:00
|
|
|
|
|
|
|
import static awais.instagrabber.utils.Utils.settingsHelper;
|
|
|
|
|
|
|
|
public class PostViewV2ViewModel extends ViewModel {
|
|
|
|
private static final String TAG = PostViewV2ViewModel.class.getSimpleName();
|
|
|
|
|
|
|
|
private final MutableLiveData<User> user = new MutableLiveData<>();
|
|
|
|
private final MutableLiveData<Caption> caption = new MutableLiveData<>();
|
|
|
|
private final MutableLiveData<Location> location = new MutableLiveData<>();
|
|
|
|
private final MutableLiveData<String> date = new MutableLiveData<>();
|
|
|
|
private final MutableLiveData<Long> likeCount = new MutableLiveData<>(0L);
|
|
|
|
private final MutableLiveData<Long> commentCount = new MutableLiveData<>(0L);
|
2021-01-10 09:19:27 +00:00
|
|
|
private final MutableLiveData<Long> viewCount = new MutableLiveData<>(0L);
|
2021-01-09 11:24:16 +00:00
|
|
|
private final MutableLiveData<MediaItemType> type = new MutableLiveData<>();
|
|
|
|
private final MutableLiveData<Boolean> liked = new MutableLiveData<>(false);
|
|
|
|
private final MutableLiveData<Boolean> saved = new MutableLiveData<>(false);
|
|
|
|
private final MutableLiveData<List<Integer>> options = new MutableLiveData<>(new ArrayList<>());
|
|
|
|
private final MediaService mediaService;
|
|
|
|
private final long viewerId;
|
|
|
|
private final boolean isLoggedIn;
|
|
|
|
|
|
|
|
private Media media;
|
2021-05-23 06:57:01 +00:00
|
|
|
private DirectMessagesManager messageManager;
|
2021-01-09 11:24:16 +00:00
|
|
|
|
|
|
|
public PostViewV2ViewModel() {
|
|
|
|
final String cookie = settingsHelper.getString(Constants.COOKIE);
|
2021-01-11 20:08:23 +00:00
|
|
|
final String deviceUuid = settingsHelper.getString(Constants.DEVICE_UUID);
|
|
|
|
final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie);
|
2021-01-09 11:24:16 +00:00
|
|
|
viewerId = CookieUtils.getUserIdFromCookie(cookie);
|
2021-01-11 20:08:23 +00:00
|
|
|
mediaService = MediaService.getInstance(deviceUuid, csrfToken, viewerId);
|
2021-01-09 11:24:16 +00:00
|
|
|
isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMedia(final Media media) {
|
|
|
|
this.media = media;
|
|
|
|
user.postValue(media.getUser());
|
|
|
|
caption.postValue(media.getCaption());
|
|
|
|
location.postValue(media.getLocation());
|
|
|
|
date.postValue(media.getDate());
|
|
|
|
likeCount.postValue(media.getLikeCount());
|
|
|
|
commentCount.postValue(media.getCommentCount());
|
2021-01-10 09:19:27 +00:00
|
|
|
viewCount.postValue(media.getMediaType() == MediaItemType.MEDIA_TYPE_VIDEO ? media.getViewCount() : null);
|
2021-01-09 11:24:16 +00:00
|
|
|
type.postValue(media.getMediaType());
|
2021-05-22 21:26:33 +00:00
|
|
|
liked.postValue(media.getHasLiked());
|
|
|
|
saved.postValue(media.getHasViewerSaved());
|
2021-01-09 11:24:16 +00:00
|
|
|
initOptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initOptions() {
|
|
|
|
final ImmutableList.Builder<Integer> builder = ImmutableList.builder();
|
2021-01-10 09:19:27 +00:00
|
|
|
if (isLoggedIn && media.getUser() != null && media.getUser().getPk() == viewerId) {
|
2021-01-09 11:24:16 +00:00
|
|
|
builder.add(R.id.edit_caption);
|
2021-03-20 16:14:57 +00:00
|
|
|
builder.add(R.id.delete);
|
2021-01-09 11:24:16 +00:00
|
|
|
}
|
|
|
|
options.postValue(builder.build());
|
|
|
|
}
|
|
|
|
|
|
|
|
public Media getMedia() {
|
|
|
|
return media;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isLoggedIn() {
|
|
|
|
return isLoggedIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
public LiveData<User> getUser() {
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
public LiveData<Caption> getCaption() {
|
|
|
|
return caption;
|
|
|
|
}
|
|
|
|
|
|
|
|
public LiveData<Location> getLocation() {
|
|
|
|
return location;
|
|
|
|
}
|
|
|
|
|
|
|
|
public LiveData<String> getDate() {
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
|
|
|
|
public LiveData<Long> getLikeCount() {
|
|
|
|
return likeCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
public LiveData<Long> getCommentCount() {
|
|
|
|
return commentCount;
|
|
|
|
}
|
|
|
|
|
2021-01-10 09:19:27 +00:00
|
|
|
public LiveData<Long> getViewCount() {
|
|
|
|
return viewCount;
|
|
|
|
}
|
|
|
|
|
2021-01-09 11:24:16 +00:00
|
|
|
public LiveData<MediaItemType> getType() {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
public LiveData<Boolean> getLiked() {
|
|
|
|
return liked;
|
|
|
|
}
|
|
|
|
|
|
|
|
public LiveData<Boolean> getSaved() {
|
|
|
|
return saved;
|
|
|
|
}
|
|
|
|
|
|
|
|
public LiveData<List<Integer>> getOptions() {
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
public LiveData<Resource<Object>> toggleLike() {
|
2021-05-22 21:26:33 +00:00
|
|
|
if (media.getHasLiked()) {
|
2021-01-09 11:24:16 +00:00
|
|
|
return unlike();
|
|
|
|
}
|
|
|
|
return like();
|
|
|
|
}
|
|
|
|
|
|
|
|
public LiveData<Resource<Object>> like() {
|
|
|
|
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>();
|
|
|
|
data.postValue(Resource.loading(null));
|
2021-01-11 20:08:23 +00:00
|
|
|
mediaService.like(media.getPk(), getLikeUnlikeCallback(data));
|
2021-01-09 11:24:16 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public LiveData<Resource<Object>> unlike() {
|
|
|
|
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>();
|
|
|
|
data.postValue(Resource.loading(null));
|
2021-01-11 20:08:23 +00:00
|
|
|
mediaService.unlike(media.getPk(), getLikeUnlikeCallback(data));
|
2021-01-09 11:24:16 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
private ServiceCallback<Boolean> getLikeUnlikeCallback(final MutableLiveData<Resource<Object>> data) {
|
|
|
|
return new ServiceCallback<Boolean>() {
|
|
|
|
@Override
|
|
|
|
public void onSuccess(final Boolean result) {
|
|
|
|
if (!result) {
|
|
|
|
data.postValue(Resource.error("", null));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
data.postValue(Resource.success(true));
|
|
|
|
final long currentLikesCount = media.getLikeCount();
|
|
|
|
final long updatedCount;
|
2021-05-22 21:26:33 +00:00
|
|
|
if (!media.getHasLiked()) {
|
2021-01-09 11:24:16 +00:00
|
|
|
updatedCount = currentLikesCount + 1;
|
|
|
|
media.setHasLiked(true);
|
|
|
|
} else {
|
|
|
|
updatedCount = currentLikesCount - 1;
|
|
|
|
media.setHasLiked(false);
|
|
|
|
}
|
|
|
|
media.setLikeCount(updatedCount);
|
|
|
|
likeCount.postValue(updatedCount);
|
2021-05-22 21:26:33 +00:00
|
|
|
liked.postValue(media.getHasLiked());
|
2021-01-09 11:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(final Throwable t) {
|
|
|
|
data.postValue(Resource.error(t.getMessage(), null));
|
|
|
|
Log.e(TAG, "Error during like/unlike", t);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
public LiveData<Resource<Object>> toggleSave() {
|
2021-05-22 21:26:33 +00:00
|
|
|
if (!media.getHasViewerSaved()) {
|
2021-01-23 04:33:36 +00:00
|
|
|
return save(null, false);
|
2021-01-09 11:24:16 +00:00
|
|
|
}
|
|
|
|
return unsave();
|
|
|
|
}
|
|
|
|
|
2021-01-23 04:33:36 +00:00
|
|
|
@NonNull
|
|
|
|
public LiveData<Resource<Object>> toggleSave(final String collection, final boolean ignoreSaveState) {
|
|
|
|
return save(collection, ignoreSaveState);
|
|
|
|
}
|
|
|
|
|
|
|
|
public LiveData<Resource<Object>> save(final String collection, final boolean ignoreSaveState) {
|
2021-01-09 11:24:16 +00:00
|
|
|
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>();
|
|
|
|
data.postValue(Resource.loading(null));
|
2021-01-23 04:33:36 +00:00
|
|
|
mediaService.save(media.getPk(), collection, getSaveUnsaveCallback(data, ignoreSaveState));
|
2021-01-09 11:24:16 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public LiveData<Resource<Object>> unsave() {
|
|
|
|
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>();
|
|
|
|
data.postValue(Resource.loading(null));
|
2021-01-23 04:33:36 +00:00
|
|
|
mediaService.unsave(media.getPk(), getSaveUnsaveCallback(data, false));
|
2021-01-09 11:24:16 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
2021-01-23 04:33:36 +00:00
|
|
|
private ServiceCallback<Boolean> getSaveUnsaveCallback(final MutableLiveData<Resource<Object>> data,
|
|
|
|
final boolean ignoreSaveState) {
|
2021-01-09 11:24:16 +00:00
|
|
|
return new ServiceCallback<Boolean>() {
|
|
|
|
@Override
|
|
|
|
public void onSuccess(final Boolean result) {
|
|
|
|
if (!result) {
|
|
|
|
data.postValue(Resource.error("", null));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
data.postValue(Resource.success(true));
|
2021-05-22 21:26:33 +00:00
|
|
|
if (!ignoreSaveState) media.setHasViewerSaved(!media.getHasViewerSaved());
|
|
|
|
saved.postValue(media.getHasViewerSaved());
|
2021-01-09 11:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(final Throwable t) {
|
|
|
|
data.postValue(Resource.error(t.getMessage(), null));
|
|
|
|
Log.e(TAG, "Error during save/unsave", t);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public LiveData<Resource<Object>> updateCaption(final String caption) {
|
|
|
|
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>();
|
|
|
|
data.postValue(Resource.loading(null));
|
2021-01-11 20:08:23 +00:00
|
|
|
mediaService.editCaption(media.getPk(), caption, new ServiceCallback<Boolean>() {
|
2021-01-09 11:24:16 +00:00
|
|
|
@Override
|
|
|
|
public void onSuccess(final Boolean result) {
|
|
|
|
if (result) {
|
|
|
|
data.postValue(Resource.success(""));
|
|
|
|
media.setPostCaption(caption);
|
|
|
|
PostViewV2ViewModel.this.caption.postValue(media.getCaption());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
data.postValue(Resource.error("", null));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(final Throwable t) {
|
|
|
|
Log.e(TAG, "Error editing caption", t);
|
|
|
|
data.postValue(Resource.error(t.getMessage(), null));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public LiveData<Resource<String>> translateCaption() {
|
|
|
|
final MutableLiveData<Resource<String>> data = new MutableLiveData<>();
|
|
|
|
data.postValue(Resource.loading(null));
|
|
|
|
final Caption value = caption.getValue();
|
|
|
|
if (value == null) return data;
|
2021-05-24 17:08:46 +00:00
|
|
|
mediaService.translate(value.getPk(), "1", new ServiceCallback<String>() {
|
2021-01-09 11:24:16 +00:00
|
|
|
@Override
|
|
|
|
public void onSuccess(final String result) {
|
|
|
|
if (TextUtils.isEmpty(result)) {
|
|
|
|
data.postValue(Resource.error("", null));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
data.postValue(Resource.success(result));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(final Throwable t) {
|
|
|
|
Log.e(TAG, "Error translating comment", t);
|
|
|
|
data.postValue(Resource.error(t.getMessage(), null));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return data;
|
|
|
|
}
|
2021-01-10 09:19:27 +00:00
|
|
|
|
|
|
|
public boolean hasPk() {
|
|
|
|
return media.getPk() != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setViewCount(final Long viewCount) {
|
|
|
|
this.viewCount.postValue(viewCount);
|
|
|
|
}
|
2021-03-20 16:14:57 +00:00
|
|
|
|
|
|
|
public LiveData<Resource<Object>> delete() {
|
|
|
|
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>();
|
|
|
|
data.postValue(Resource.loading(null));
|
|
|
|
final Call<String> request = mediaService.delete(media.getId(), media.getMediaType());
|
|
|
|
if (request == null) {
|
|
|
|
data.postValue(Resource.success(new Object()));
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
request.enqueue(new Callback<String>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull final Call<String> call, @NonNull final Response<String> response) {
|
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
data.postValue(Resource.error(R.string.generic_null_response, null));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final String body = response.body();
|
|
|
|
if (body == null) {
|
|
|
|
data.postValue(Resource.error(R.string.generic_null_response, null));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
data.postValue(Resource.success(new Object()));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull final Call<String> call, @NonNull final Throwable t) {
|
|
|
|
Log.e(TAG, "onFailure: ", t);
|
|
|
|
data.postValue(Resource.error(t.getMessage(), null));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return data;
|
|
|
|
}
|
2021-05-23 06:57:01 +00:00
|
|
|
|
|
|
|
public void shareDm(@NonNull final RankedRecipient result) {
|
|
|
|
if (messageManager == null) {
|
2021-05-29 13:22:28 +00:00
|
|
|
messageManager = DirectMessagesManager.INSTANCE;
|
2021-05-23 06:57:01 +00:00
|
|
|
}
|
|
|
|
messageManager.sendMedia(result, media.getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void shareDm(@NonNull final Set<RankedRecipient> recipients) {
|
|
|
|
if (messageManager == null) {
|
2021-05-29 13:22:28 +00:00
|
|
|
messageManager = DirectMessagesManager.INSTANCE;
|
2021-05-23 06:57:01 +00:00
|
|
|
}
|
|
|
|
messageManager.sendMedia(recipients, media.getId());
|
|
|
|
}
|
2021-01-09 11:24:16 +00:00
|
|
|
}
|