mirror of
https://github.com/KokaKiwi/BarInsta
synced 2026-03-05 03:51:36 +00:00
Add ViewModel to post view to maintain state. Update some ui
This commit is contained in:
parent
92d8163c7b
commit
9ba1893746
13 changed files with 989 additions and 510 deletions
|
|
@ -41,11 +41,11 @@ public class FeedPostFetchService implements PostFetcher.PostFetchService {
|
|||
hasNextPage = result.hasNextPage();
|
||||
feedModels.addAll(result.getFeedModels());
|
||||
if (fetchListener != null) {
|
||||
if (feedModels.size() < 15 && hasNextPage) {
|
||||
feedService.fetch(csrfToken, nextCursor, this);
|
||||
} else {
|
||||
fetchListener.onResult(feedModels);
|
||||
}
|
||||
// if (feedModels.size() < 15 && hasNextPage) {
|
||||
// feedService.fetch(csrfToken, nextCursor, this);
|
||||
// } else {
|
||||
fetchListener.onResult(feedModels);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -116,7 +116,7 @@ public final class ProfilePicView extends CircularImageView {
|
|||
LARGE(3);
|
||||
|
||||
private final int value;
|
||||
private static Map<Integer, Size> map = new HashMap<>();
|
||||
private static final Map<Integer, Size> map = new HashMap<>();
|
||||
|
||||
static {
|
||||
for (Size size : Size.values()) {
|
||||
|
|
|
|||
|
|
@ -47,7 +47,10 @@ public abstract class SharedElementTransitionDialogFragment extends DialogFragme
|
|||
final int key = destView.hashCode();
|
||||
startViews.put(key, startView);
|
||||
destViews.put(key, destView);
|
||||
initialBoundsHandler.post(() -> setupInitialBounds(startView, destView));
|
||||
setupInitialBounds(startView, destView);
|
||||
// final View view = getView();
|
||||
// if (view == null) return;
|
||||
// view.post(() -> {});
|
||||
}
|
||||
|
||||
public void startPostponedEnterTransition() {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,92 @@
|
|||
package awais.instagrabber.dialogs;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.StringRes;
|
||||
import androidx.appcompat.widget.AppCompatEditText;
|
||||
import androidx.fragment.app.DialogFragment;
|
||||
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
|
||||
|
||||
import awais.instagrabber.R;
|
||||
import awais.instagrabber.utils.TextUtils;
|
||||
|
||||
public class EditTextDialogFragment extends DialogFragment {
|
||||
|
||||
private Context context;
|
||||
private EditTextDialogFragmentCallback callback;
|
||||
|
||||
public static EditTextDialogFragment newInstance(@StringRes final int title,
|
||||
@StringRes final int positiveText,
|
||||
@StringRes final int negativeText,
|
||||
@Nullable final String initialText) {
|
||||
Bundle args = new Bundle();
|
||||
args.putInt("title", title);
|
||||
args.putInt("positive", positiveText);
|
||||
args.putInt("negative", negativeText);
|
||||
args.putString("initial", initialText);
|
||||
EditTextDialogFragment fragment = new EditTextDialogFragment();
|
||||
fragment.setArguments(args);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
public EditTextDialogFragment() {}
|
||||
|
||||
@Override
|
||||
public void onAttach(@NonNull final Context context) {
|
||||
super.onAttach(context);
|
||||
try {
|
||||
callback = (EditTextDialogFragmentCallback) getParentFragment();
|
||||
} catch (ClassCastException e) {
|
||||
throw new ClassCastException("Calling fragment must implement EditTextDialogFragmentCallback interface");
|
||||
}
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Dialog onCreateDialog(@Nullable final Bundle savedInstanceState) {
|
||||
final Bundle arguments = getArguments();
|
||||
int title = -1;
|
||||
int positiveButtonText = R.string.ok;
|
||||
int negativeButtonText = R.string.cancel;
|
||||
String initialText = null;
|
||||
if (arguments != null) {
|
||||
title = arguments.getInt("title", -1);
|
||||
positiveButtonText = arguments.getInt("positive", R.string.ok);
|
||||
negativeButtonText = arguments.getInt("negative", R.string.cancel);
|
||||
initialText = arguments.getString("initial", null);
|
||||
}
|
||||
final AppCompatEditText input = new AppCompatEditText(context);
|
||||
if (!TextUtils.isEmpty(initialText)) {
|
||||
input.setText(initialText);
|
||||
}
|
||||
final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(context)
|
||||
.setView(input)
|
||||
.setPositiveButton(positiveButtonText, (d, w) -> {
|
||||
final String string = input.getText() != null ? input.getText().toString() : "";
|
||||
if (callback != null) {
|
||||
callback.onPositiveButtonClicked(string);
|
||||
}
|
||||
})
|
||||
.setNegativeButton(negativeButtonText, (dialog, which) -> {
|
||||
if (callback != null) {
|
||||
callback.onNegativeButtonClicked();
|
||||
}
|
||||
});
|
||||
if (title > 0) {
|
||||
builder.setTitle(title);
|
||||
}
|
||||
return builder.create();
|
||||
}
|
||||
|
||||
public interface EditTextDialogFragmentCallback {
|
||||
void onPositiveButtonClicked(String text);
|
||||
|
||||
void onNegativeButtonClicked();
|
||||
}
|
||||
}
|
||||
|
|
@ -11,6 +11,7 @@ import androidx.annotation.StringRes;
|
|||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.fragment.app.DialogFragment;
|
||||
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
|
||||
import com.google.common.primitives.Booleans;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
|
@ -72,7 +73,7 @@ public class MultiOptionDialogFragment<T extends Serializable> extends DialogFra
|
|||
title = arguments.getInt("title");
|
||||
type = (Type) arguments.getSerializable("type");
|
||||
}
|
||||
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
||||
final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(context);
|
||||
if (title > 0) {
|
||||
builder.setTitle(title);
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -0,0 +1,273 @@
|
|||
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;
|
||||
|
||||
import awais.instagrabber.R;
|
||||
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;
|
||||
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;
|
||||
|
||||
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);
|
||||
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 String csrfToken;
|
||||
private final boolean isLoggedIn;
|
||||
|
||||
private Media media;
|
||||
|
||||
public PostViewV2ViewModel() {
|
||||
mediaService = MediaService.getInstance();
|
||||
final String cookie = settingsHelper.getString(Constants.COOKIE);
|
||||
viewerId = CookieUtils.getUserIdFromCookie(cookie);
|
||||
csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie);
|
||||
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());
|
||||
type.postValue(media.getMediaType());
|
||||
liked.postValue(media.hasLiked());
|
||||
saved.postValue(media.hasViewerSaved());
|
||||
initOptions();
|
||||
}
|
||||
|
||||
private void initOptions() {
|
||||
final ImmutableList.Builder<Integer> builder = ImmutableList.builder();
|
||||
if (isLoggedIn && media.getUser().getPk() == viewerId) {
|
||||
builder.add(R.id.edit_caption);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
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() {
|
||||
if (media.hasLiked()) {
|
||||
return unlike();
|
||||
}
|
||||
return like();
|
||||
}
|
||||
|
||||
public LiveData<Resource<Object>> like() {
|
||||
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>();
|
||||
data.postValue(Resource.loading(null));
|
||||
mediaService.like(media.getPk(), viewerId, csrfToken, getLikeUnlikeCallback(data));
|
||||
return data;
|
||||
}
|
||||
|
||||
public LiveData<Resource<Object>> unlike() {
|
||||
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>();
|
||||
data.postValue(Resource.loading(null));
|
||||
mediaService.unlike(media.getPk(), viewerId, csrfToken, getLikeUnlikeCallback(data));
|
||||
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;
|
||||
if (!media.hasLiked()) {
|
||||
updatedCount = currentLikesCount + 1;
|
||||
media.setHasLiked(true);
|
||||
} else {
|
||||
updatedCount = currentLikesCount - 1;
|
||||
media.setHasLiked(false);
|
||||
}
|
||||
media.setLikeCount(updatedCount);
|
||||
likeCount.postValue(updatedCount);
|
||||
liked.postValue(media.hasLiked());
|
||||
}
|
||||
|
||||
@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() {
|
||||
if (!media.hasViewerSaved()) {
|
||||
return save();
|
||||
}
|
||||
return unsave();
|
||||
}
|
||||
|
||||
public LiveData<Resource<Object>> save() {
|
||||
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>();
|
||||
data.postValue(Resource.loading(null));
|
||||
mediaService.save(media.getPk(), viewerId, csrfToken, getSaveUnsaveCallback(data));
|
||||
return data;
|
||||
}
|
||||
|
||||
public LiveData<Resource<Object>> unsave() {
|
||||
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>();
|
||||
data.postValue(Resource.loading(null));
|
||||
mediaService.unsave(media.getPk(), viewerId, csrfToken, getSaveUnsaveCallback(data));
|
||||
return data;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private ServiceCallback<Boolean> getSaveUnsaveCallback(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));
|
||||
media.setHasViewerSaved(!media.hasViewerSaved());
|
||||
saved.postValue(media.hasViewerSaved());
|
||||
}
|
||||
|
||||
@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));
|
||||
mediaService.editCaption(media.getPk(), viewerId, caption, csrfToken, new ServiceCallback<Boolean>() {
|
||||
@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;
|
||||
mediaService.translate(String.valueOf(value.getPk()), "1", new ServiceCallback<String>() {
|
||||
@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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue