Browse Source
Convert to kotlin, suspend funs, and viewModelScope
renovate/org.robolectric-robolectric-4.x
Convert to kotlin, suspend funs, and viewModelScope
renovate/org.robolectric-robolectric-4.x
Ammar Githam
4 years ago
17 changed files with 668 additions and 812 deletions
-
5app/build.gradle
-
2app/src/main/java/awais/instagrabber/activities/MainActivity.kt
-
13app/src/main/java/awais/instagrabber/fragments/PostViewV2Fragment.java
-
16app/src/main/java/awais/instagrabber/managers/DirectMessagesManager.kt
-
71app/src/main/java/awais/instagrabber/managers/InboxManager.kt
-
158app/src/main/java/awais/instagrabber/managers/ThreadManager.kt
-
8app/src/main/java/awais/instagrabber/repositories/DirectMessagesRepository.kt
-
2app/src/main/java/awais/instagrabber/services/DMSyncService.java
-
56app/src/main/java/awais/instagrabber/viewmodels/DirectInboxViewModel.java
-
36app/src/main/java/awais/instagrabber/viewmodels/DirectInboxViewModel.kt
-
48app/src/main/java/awais/instagrabber/viewmodels/DirectPendingInboxViewModel.java
-
34app/src/main/java/awais/instagrabber/viewmodels/DirectPendingInboxViewModel.kt
-
299app/src/main/java/awais/instagrabber/viewmodels/DirectSettingsViewModel.java
-
201app/src/main/java/awais/instagrabber/viewmodels/DirectSettingsViewModel.kt
-
13app/src/main/java/awais/instagrabber/viewmodels/DirectThreadViewModel.kt
-
508app/src/main/java/awais/instagrabber/viewmodels/PostViewV2ViewModel.kt
-
10app/src/main/java/awais/instagrabber/webservices/DirectMessagesService.kt
@ -1,56 +0,0 @@ |
|||||
package awais.instagrabber.viewmodels; |
|
||||
|
|
||||
import androidx.lifecycle.LiveData; |
|
||||
import androidx.lifecycle.ViewModel; |
|
||||
|
|
||||
import java.util.List; |
|
||||
|
|
||||
import awais.instagrabber.managers.DirectMessagesManager; |
|
||||
import awais.instagrabber.managers.InboxManager; |
|
||||
import awais.instagrabber.models.Resource; |
|
||||
import awais.instagrabber.repositories.responses.User; |
|
||||
import awais.instagrabber.repositories.responses.directmessages.DirectInbox; |
|
||||
import awais.instagrabber.repositories.responses.directmessages.DirectThread; |
|
||||
|
|
||||
public class DirectInboxViewModel extends ViewModel { |
|
||||
private static final String TAG = DirectInboxViewModel.class.getSimpleName(); |
|
||||
|
|
||||
private final InboxManager inboxManager; |
|
||||
|
|
||||
public DirectInboxViewModel() { |
|
||||
final DirectMessagesManager messagesManager = DirectMessagesManager.INSTANCE; |
|
||||
inboxManager = messagesManager.getInboxManager(); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Resource<DirectInbox>> getInbox() { |
|
||||
return inboxManager.getInbox(); |
|
||||
} |
|
||||
|
|
||||
public LiveData<List<DirectThread>> getThreads() { |
|
||||
return inboxManager.getThreads(); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Resource<Integer>> getUnseenCount() { |
|
||||
return inboxManager.getUnseenCount(); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Integer> getPendingRequestsTotal() { |
|
||||
return inboxManager.getPendingRequestsTotal(); |
|
||||
} |
|
||||
|
|
||||
public User getViewer() { |
|
||||
return inboxManager.getViewer(); |
|
||||
} |
|
||||
|
|
||||
public void fetchInbox() { |
|
||||
inboxManager.fetchInbox(); |
|
||||
} |
|
||||
|
|
||||
public void refresh() { |
|
||||
inboxManager.refresh(); |
|
||||
} |
|
||||
|
|
||||
public void onDestroy() { |
|
||||
inboxManager.onDestroy(); |
|
||||
} |
|
||||
} |
|
@ -0,0 +1,36 @@ |
|||||
|
package awais.instagrabber.viewmodels |
||||
|
|
||||
|
import androidx.lifecycle.LiveData |
||||
|
import androidx.lifecycle.ViewModel |
||||
|
import androidx.lifecycle.viewModelScope |
||||
|
import awais.instagrabber.managers.DirectMessagesManager |
||||
|
import awais.instagrabber.managers.InboxManager |
||||
|
import awais.instagrabber.models.Resource |
||||
|
import awais.instagrabber.repositories.responses.User |
||||
|
import awais.instagrabber.repositories.responses.directmessages.DirectInbox |
||||
|
import awais.instagrabber.repositories.responses.directmessages.DirectThread |
||||
|
|
||||
|
class DirectInboxViewModel : ViewModel() { |
||||
|
private val inboxManager: InboxManager = DirectMessagesManager.inboxManager |
||||
|
val inbox: LiveData<Resource<DirectInbox?>> = inboxManager.getInbox() |
||||
|
val threads: LiveData<List<DirectThread>> = inboxManager.threads |
||||
|
val unseenCount: LiveData<Resource<Int?>> = inboxManager.getUnseenCount() |
||||
|
val pendingRequestsTotal: LiveData<Int> = inboxManager.getPendingRequestsTotal() |
||||
|
val viewer: User? = inboxManager.viewer |
||||
|
|
||||
|
fun fetchInbox() { |
||||
|
inboxManager.fetchInbox(viewModelScope) |
||||
|
} |
||||
|
|
||||
|
fun refresh() { |
||||
|
inboxManager.refresh(viewModelScope) |
||||
|
} |
||||
|
|
||||
|
fun onDestroy() { |
||||
|
inboxManager.onDestroy() |
||||
|
} |
||||
|
|
||||
|
init { |
||||
|
inboxManager.fetchInbox(viewModelScope) |
||||
|
} |
||||
|
} |
@ -1,48 +0,0 @@ |
|||||
package awais.instagrabber.viewmodels; |
|
||||
|
|
||||
import androidx.lifecycle.LiveData; |
|
||||
import androidx.lifecycle.ViewModel; |
|
||||
|
|
||||
import java.util.List; |
|
||||
|
|
||||
import awais.instagrabber.managers.DirectMessagesManager; |
|
||||
import awais.instagrabber.managers.InboxManager; |
|
||||
import awais.instagrabber.models.Resource; |
|
||||
import awais.instagrabber.repositories.responses.User; |
|
||||
import awais.instagrabber.repositories.responses.directmessages.DirectInbox; |
|
||||
import awais.instagrabber.repositories.responses.directmessages.DirectThread; |
|
||||
|
|
||||
public class DirectPendingInboxViewModel extends ViewModel { |
|
||||
private static final String TAG = DirectPendingInboxViewModel.class.getSimpleName(); |
|
||||
|
|
||||
private final InboxManager inboxManager; |
|
||||
|
|
||||
public DirectPendingInboxViewModel() { |
|
||||
inboxManager = DirectMessagesManager.INSTANCE.getPendingInboxManager(); |
|
||||
inboxManager.fetchInbox(); |
|
||||
} |
|
||||
|
|
||||
public LiveData<List<DirectThread>> getThreads() { |
|
||||
return inboxManager.getThreads(); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Resource<DirectInbox>> getInbox() { |
|
||||
return inboxManager.getInbox(); |
|
||||
} |
|
||||
|
|
||||
public User getViewer() { |
|
||||
return inboxManager.getViewer(); |
|
||||
} |
|
||||
|
|
||||
public void fetchInbox() { |
|
||||
inboxManager.fetchInbox(); |
|
||||
} |
|
||||
|
|
||||
public void refresh() { |
|
||||
inboxManager.refresh(); |
|
||||
} |
|
||||
|
|
||||
public void onDestroy() { |
|
||||
inboxManager.onDestroy(); |
|
||||
} |
|
||||
} |
|
@ -0,0 +1,34 @@ |
|||||
|
package awais.instagrabber.viewmodels |
||||
|
|
||||
|
import androidx.lifecycle.LiveData |
||||
|
import androidx.lifecycle.ViewModel |
||||
|
import androidx.lifecycle.viewModelScope |
||||
|
import awais.instagrabber.managers.DirectMessagesManager.pendingInboxManager |
||||
|
import awais.instagrabber.managers.InboxManager |
||||
|
import awais.instagrabber.models.Resource |
||||
|
import awais.instagrabber.repositories.responses.User |
||||
|
import awais.instagrabber.repositories.responses.directmessages.DirectInbox |
||||
|
import awais.instagrabber.repositories.responses.directmessages.DirectThread |
||||
|
|
||||
|
class DirectPendingInboxViewModel : ViewModel() { |
||||
|
private val inboxManager: InboxManager = pendingInboxManager |
||||
|
val threads: LiveData<List<DirectThread>> = inboxManager.threads |
||||
|
val inbox: LiveData<Resource<DirectInbox?>> = inboxManager.getInbox() |
||||
|
val viewer: User? = inboxManager.viewer |
||||
|
|
||||
|
fun fetchInbox() { |
||||
|
inboxManager.fetchInbox(viewModelScope) |
||||
|
} |
||||
|
|
||||
|
fun refresh() { |
||||
|
inboxManager.refresh(viewModelScope) |
||||
|
} |
||||
|
|
||||
|
fun onDestroy() { |
||||
|
inboxManager.onDestroy() |
||||
|
} |
||||
|
|
||||
|
init { |
||||
|
inboxManager.fetchInbox(viewModelScope) |
||||
|
} |
||||
|
} |
@ -1,299 +0,0 @@ |
|||||
package awais.instagrabber.viewmodels; |
|
||||
|
|
||||
import android.app.Application; |
|
||||
import android.content.ContentResolver; |
|
||||
import android.content.res.Resources; |
|
||||
|
|
||||
import androidx.annotation.NonNull; |
|
||||
import androidx.annotation.StringRes; |
|
||||
import androidx.core.util.Pair; |
|
||||
import androidx.lifecycle.AndroidViewModel; |
|
||||
import androidx.lifecycle.LiveData; |
|
||||
|
|
||||
import java.util.ArrayList; |
|
||||
import java.util.List; |
|
||||
import java.util.Set; |
|
||||
|
|
||||
import awais.instagrabber.R; |
|
||||
import awais.instagrabber.dialogs.MultiOptionDialogFragment.Option; |
|
||||
import awais.instagrabber.managers.DirectMessagesManager; |
|
||||
import awais.instagrabber.managers.ThreadManager; |
|
||||
import awais.instagrabber.models.Resource; |
|
||||
import awais.instagrabber.repositories.responses.User; |
|
||||
import awais.instagrabber.repositories.responses.directmessages.DirectThread; |
|
||||
import awais.instagrabber.repositories.responses.directmessages.DirectThreadParticipantRequestsResponse; |
|
||||
import awais.instagrabber.utils.Constants; |
|
||||
import awais.instagrabber.utils.CookieUtils; |
|
||||
import awais.instagrabber.utils.TextUtils; |
|
||||
|
|
||||
import static awais.instagrabber.utils.Utils.settingsHelper; |
|
||||
|
|
||||
public class DirectSettingsViewModel extends AndroidViewModel { |
|
||||
private static final String TAG = DirectSettingsViewModel.class.getSimpleName(); |
|
||||
private static final String ACTION_KICK = "kick"; |
|
||||
private static final String ACTION_MAKE_ADMIN = "make_admin"; |
|
||||
private static final String ACTION_REMOVE_ADMIN = "remove_admin"; |
|
||||
private static final String ACTION_BLOCK = "block"; |
|
||||
private static final String ACTION_UNBLOCK = "unblock"; |
|
||||
// private static final String ACTION_REPORT = "report"; |
|
||||
private static final String ACTION_RESTRICT = "restrict"; |
|
||||
private static final String ACTION_UNRESTRICT = "unrestrict"; |
|
||||
|
|
||||
private final long viewerId; |
|
||||
private final Resources resources; |
|
||||
private final ThreadManager threadManager; |
|
||||
|
|
||||
public DirectSettingsViewModel(final Application application, |
|
||||
@NonNull final String threadId, |
|
||||
final boolean pending, |
|
||||
@NonNull final User currentUser) { |
|
||||
super(application); |
|
||||
final String cookie = settingsHelper.getString(Constants.COOKIE); |
|
||||
viewerId = CookieUtils.getUserIdFromCookie(cookie); |
|
||||
final String deviceUuid = settingsHelper.getString(Constants.DEVICE_UUID); |
|
||||
final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie); |
|
||||
if (TextUtils.isEmpty(csrfToken) || viewerId <= 0 || TextUtils.isEmpty(deviceUuid)) { |
|
||||
throw new IllegalArgumentException("User is not logged in!"); |
|
||||
} |
|
||||
final ContentResolver contentResolver = application.getContentResolver(); |
|
||||
resources = getApplication().getResources(); |
|
||||
final DirectMessagesManager messagesManager = DirectMessagesManager.INSTANCE; |
|
||||
threadManager = messagesManager.getThreadManager(threadId, pending, currentUser, contentResolver); |
|
||||
} |
|
||||
|
|
||||
@NonNull |
|
||||
public LiveData<DirectThread> getThread() { |
|
||||
return threadManager.getThread(); |
|
||||
} |
|
||||
|
|
||||
// public void setThread(@NonNull final DirectThread thread) { |
|
||||
// this.thread = thread; |
|
||||
// inputMode.postValue(thread.getInputMode()); |
|
||||
// List<User> users = thread.getUsers(); |
|
||||
// final ImmutableList.Builder<User> builder = ImmutableList.<User>builder().add(currentUser); |
|
||||
// if (users != null) { |
|
||||
// builder.addAll(users); |
|
||||
// } |
|
||||
// users = builder.build(); |
|
||||
// this.users.postValue(new Pair<>(users, thread.getLeftUsers())); |
|
||||
// // setTitle(thread.getThreadTitle()); |
|
||||
// final List<Long> adminUserIds = thread.getAdminUserIds(); |
|
||||
// this.adminUserIds.postValue(adminUserIds); |
|
||||
// viewerIsAdmin = adminUserIds.contains(viewerId); |
|
||||
// muted.postValue(thread.getMuted()); |
|
||||
// mentionsMuted.postValue(thread.isMentionsMuted()); |
|
||||
// approvalRequiredToJoin.postValue(thread.isApprovalRequiredForNewMembers()); |
|
||||
// isPending.postValue(thread.isPending()); |
|
||||
// if (thread.getInputMode() != 1 && thread.isGroup() && viewerIsAdmin) { |
|
||||
// fetchPendingRequests(); |
|
||||
// } |
|
||||
// } |
|
||||
|
|
||||
public LiveData<Integer> getInputMode() { |
|
||||
return threadManager.getInputMode(); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Boolean> isGroup() { |
|
||||
return threadManager.isGroup(); |
|
||||
} |
|
||||
|
|
||||
public LiveData<List<User>> getUsers() { |
|
||||
return threadManager.getUsersWithCurrent(); |
|
||||
} |
|
||||
|
|
||||
public LiveData<List<User>> getLeftUsers() { |
|
||||
return threadManager.getLeftUsers(); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Pair<List<User>, List<User>>> getUsersAndLeftUsers() { |
|
||||
return threadManager.getUsersAndLeftUsers(); |
|
||||
} |
|
||||
|
|
||||
public LiveData<String> getTitle() { |
|
||||
return threadManager.getThreadTitle(); |
|
||||
} |
|
||||
|
|
||||
// public void setTitle(final String title) { |
|
||||
// if (title == null) { |
|
||||
// this.title.postValue(""); |
|
||||
// return; |
|
||||
// } |
|
||||
// this.title.postValue(title.trim()); |
|
||||
// } |
|
||||
|
|
||||
public LiveData<List<Long>> getAdminUserIds() { |
|
||||
return threadManager.getAdminUserIds(); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Boolean> isMuted() { |
|
||||
return threadManager.isMuted(); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Boolean> getApprovalRequiredToJoin() { |
|
||||
return threadManager.isApprovalRequiredToJoin(); |
|
||||
} |
|
||||
|
|
||||
public LiveData<DirectThreadParticipantRequestsResponse> getPendingRequests() { |
|
||||
return threadManager.getPendingRequests(); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Boolean> isPending() { |
|
||||
return threadManager.isPending(); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Boolean> isViewerAdmin() { |
|
||||
return threadManager.isViewerAdmin(); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Resource<Object>> updateTitle(final String newTitle) { |
|
||||
return threadManager.updateTitle(newTitle); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Resource<Object>> addMembers(final Set<User> users) { |
|
||||
return threadManager.addMembers(users); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Resource<Object>> removeMember(final User user) { |
|
||||
return threadManager.removeMember(user); |
|
||||
} |
|
||||
|
|
||||
private LiveData<Resource<Object>> makeAdmin(final User user) { |
|
||||
return threadManager.makeAdmin(user); |
|
||||
} |
|
||||
|
|
||||
private LiveData<Resource<Object>> removeAdmin(final User user) { |
|
||||
return threadManager.removeAdmin(user); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Resource<Object>> mute() { |
|
||||
return threadManager.mute(); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Resource<Object>> unmute() { |
|
||||
return threadManager.unmute(); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Resource<Object>> muteMentions() { |
|
||||
return threadManager.muteMentions(); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Resource<Object>> unmuteMentions() { |
|
||||
return threadManager.unmuteMentions(); |
|
||||
} |
|
||||
|
|
||||
private LiveData<Resource<Object>> blockUser(final User user) { |
|
||||
return threadManager.blockUser(user); |
|
||||
} |
|
||||
|
|
||||
private LiveData<Resource<Object>> unblockUser(final User user) { |
|
||||
return threadManager.unblockUser(user); |
|
||||
} |
|
||||
|
|
||||
private LiveData<Resource<Object>> restrictUser(final User user) { |
|
||||
return threadManager.restrictUser(user); |
|
||||
} |
|
||||
|
|
||||
private LiveData<Resource<Object>> unRestrictUser(final User user) { |
|
||||
return threadManager.unRestrictUser(user); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Resource<Object>> approveUsers(final List<User> users) { |
|
||||
return threadManager.approveUsers(users); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Resource<Object>> denyUsers(final List<User> users) { |
|
||||
return threadManager.denyUsers(users); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Resource<Object>> approvalRequired() { |
|
||||
return threadManager.approvalRequired(); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Resource<Object>> approvalNotRequired() { |
|
||||
return threadManager.approvalNotRequired(); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Resource<Object>> leave() { |
|
||||
return threadManager.leave(); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Resource<Object>> end() { |
|
||||
return threadManager.end(); |
|
||||
} |
|
||||
|
|
||||
public ArrayList<Option<String>> createUserOptions(final User user) { |
|
||||
final ArrayList<Option<String>> options = new ArrayList<>(); |
|
||||
if (user == null || isSelf(user) || hasLeft(user)) { |
|
||||
return options; |
|
||||
} |
|
||||
final Boolean viewerIsAdmin = threadManager.isViewerAdmin().getValue(); |
|
||||
if (viewerIsAdmin != null && viewerIsAdmin) { |
|
||||
options.add(new Option<>(getString(R.string.dms_action_kick), ACTION_KICK)); |
|
||||
|
|
||||
final boolean isAdmin = threadManager.isAdmin(user); |
|
||||
options.add(new Option<>( |
|
||||
isAdmin ? getString(R.string.dms_action_remove_admin) : getString(R.string.dms_action_make_admin), |
|
||||
isAdmin ? ACTION_REMOVE_ADMIN : ACTION_MAKE_ADMIN |
|
||||
)); |
|
||||
} |
|
||||
|
|
||||
final boolean blocking = user.getFriendshipStatus().getBlocking(); |
|
||||
options.add(new Option<>( |
|
||||
blocking ? getString(R.string.unblock) : getString(R.string.block), |
|
||||
blocking ? ACTION_UNBLOCK : ACTION_BLOCK |
|
||||
)); |
|
||||
|
|
||||
// options.add(new Option<>(getString(R.string.report), ACTION_REPORT)); |
|
||||
final Boolean isGroup = threadManager.isGroup().getValue(); |
|
||||
if (isGroup != null && isGroup) { |
|
||||
final boolean restricted = user.getFriendshipStatus().isRestricted(); |
|
||||
options.add(new Option<>( |
|
||||
restricted ? getString(R.string.unrestrict) : getString(R.string.restrict), |
|
||||
restricted ? ACTION_UNRESTRICT : ACTION_RESTRICT |
|
||||
)); |
|
||||
} |
|
||||
return options; |
|
||||
} |
|
||||
|
|
||||
private boolean hasLeft(final User user) { |
|
||||
final List<User> leftUsers = getLeftUsers().getValue(); |
|
||||
if (leftUsers == null) return false; |
|
||||
return leftUsers.contains(user); |
|
||||
} |
|
||||
|
|
||||
private boolean isSelf(final User user) { |
|
||||
return user.getPk() == viewerId; |
|
||||
} |
|
||||
|
|
||||
private String getString(@StringRes final int resId) { |
|
||||
return resources.getString(resId); |
|
||||
} |
|
||||
|
|
||||
public LiveData<Resource<Object>> doAction(final User user, final String action) { |
|
||||
if (user == null || action == null) return null; |
|
||||
switch (action) { |
|
||||
case ACTION_KICK: |
|
||||
return removeMember(user); |
|
||||
case ACTION_MAKE_ADMIN: |
|
||||
return makeAdmin(user); |
|
||||
case ACTION_REMOVE_ADMIN: |
|
||||
return removeAdmin(user); |
|
||||
case ACTION_BLOCK: |
|
||||
return blockUser(user); |
|
||||
case ACTION_UNBLOCK: |
|
||||
return unblockUser(user); |
|
||||
// case ACTION_REPORT: |
|
||||
// break; |
|
||||
case ACTION_RESTRICT: |
|
||||
return restrictUser(user); |
|
||||
case ACTION_UNRESTRICT: |
|
||||
return unRestrictUser(user); |
|
||||
default: |
|
||||
return null; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public LiveData<User> getInviter() { |
|
||||
return threadManager.getInviter(); |
|
||||
} |
|
||||
} |
|
@ -0,0 +1,201 @@ |
|||||
|
package awais.instagrabber.viewmodels |
||||
|
|
||||
|
import android.app.Application |
||||
|
import androidx.annotation.StringRes |
||||
|
import androidx.core.util.Pair |
||||
|
import androidx.lifecycle.AndroidViewModel |
||||
|
import androidx.lifecycle.LiveData |
||||
|
import androidx.lifecycle.viewModelScope |
||||
|
import awais.instagrabber.R |
||||
|
import awais.instagrabber.dialogs.MultiOptionDialogFragment.Option |
||||
|
import awais.instagrabber.managers.DirectMessagesManager |
||||
|
import awais.instagrabber.models.Resource |
||||
|
import awais.instagrabber.repositories.responses.User |
||||
|
import awais.instagrabber.repositories.responses.directmessages.DirectThread |
||||
|
import awais.instagrabber.repositories.responses.directmessages.DirectThreadParticipantRequestsResponse |
||||
|
import awais.instagrabber.utils.Constants |
||||
|
import awais.instagrabber.utils.Utils |
||||
|
import awais.instagrabber.utils.getCsrfTokenFromCookie |
||||
|
import awais.instagrabber.utils.getUserIdFromCookie |
||||
|
|
||||
|
class DirectSettingsViewModel( |
||||
|
application: Application, |
||||
|
threadId: String, |
||||
|
pending: Boolean, |
||||
|
currentUser: User, |
||||
|
) : AndroidViewModel(application) { |
||||
|
private val viewerId: Long |
||||
|
private val resources = application.resources |
||||
|
private val threadManager = DirectMessagesManager.getThreadManager(threadId, pending, currentUser, application.contentResolver) |
||||
|
|
||||
|
val thread: LiveData<DirectThread?> = threadManager.thread |
||||
|
|
||||
|
// public void setThread(@NonNull final DirectThread thread) { |
||||
|
// this.thread = thread; |
||||
|
// inputMode.postValue(thread.getInputMode()); |
||||
|
// List<User> users = thread.getUsers(); |
||||
|
// final ImmutableList.Builder<User> builder = ImmutableList.<User>builder().add(currentUser); |
||||
|
// if (users != null) { |
||||
|
// builder.addAll(users); |
||||
|
// } |
||||
|
// users = builder.build(); |
||||
|
// this.users.postValue(new Pair<>(users, thread.getLeftUsers())); |
||||
|
// // setTitle(thread.getThreadTitle()); |
||||
|
// final List<Long> adminUserIds = thread.getAdminUserIds(); |
||||
|
// this.adminUserIds.postValue(adminUserIds); |
||||
|
// viewerIsAdmin = adminUserIds.contains(viewerId); |
||||
|
// muted.postValue(thread.getMuted()); |
||||
|
// mentionsMuted.postValue(thread.isMentionsMuted()); |
||||
|
// approvalRequiredToJoin.postValue(thread.isApprovalRequiredForNewMembers()); |
||||
|
// isPending.postValue(thread.isPending()); |
||||
|
// if (thread.getInputMode() != 1 && thread.isGroup() && viewerIsAdmin) { |
||||
|
// fetchPendingRequests(); |
||||
|
// } |
||||
|
// } |
||||
|
val inputMode: LiveData<Int> = threadManager.inputMode |
||||
|
|
||||
|
fun isGroup(): LiveData<Boolean> = threadManager.isGroup |
||||
|
|
||||
|
fun getUsers(): LiveData<List<User>> = threadManager.usersWithCurrent |
||||
|
|
||||
|
fun getLeftUsers(): LiveData<List<User>> = threadManager.leftUsers |
||||
|
|
||||
|
fun getUsersAndLeftUsers(): LiveData<Pair<List<User>, List<User>>> = threadManager.usersAndLeftUsers |
||||
|
|
||||
|
fun getTitle(): LiveData<String?> = threadManager.threadTitle |
||||
|
|
||||
|
// public void setTitle(final String title) { |
||||
|
// if (title == null) { |
||||
|
// this.title.postValue(""); |
||||
|
// return; |
||||
|
// } |
||||
|
// this.title.postValue(title.trim()); |
||||
|
// } |
||||
|
fun getAdminUserIds(): LiveData<List<Long>> = threadManager.adminUserIds |
||||
|
|
||||
|
fun isMuted(): LiveData<Boolean> = threadManager.isMuted |
||||
|
|
||||
|
fun getApprovalRequiredToJoin(): LiveData<Boolean> = threadManager.isApprovalRequiredToJoin |
||||
|
|
||||
|
fun getPendingRequests(): LiveData<DirectThreadParticipantRequestsResponse?> = threadManager.pendingRequests |
||||
|
|
||||
|
fun isPending(): LiveData<Boolean> = threadManager.isPending |
||||
|
|
||||
|
fun isViewerAdmin(): LiveData<Boolean> = threadManager.isViewerAdmin |
||||
|
|
||||
|
fun updateTitle(newTitle: String): LiveData<Resource<Any?>> = threadManager.updateTitle(newTitle) |
||||
|
|
||||
|
fun addMembers(users: Set<User>): LiveData<Resource<Any?>> = threadManager.addMembers(users) |
||||
|
|
||||
|
fun removeMember(user: User): LiveData<Resource<Any?>> = threadManager.removeMember(user) |
||||
|
|
||||
|
private fun makeAdmin(user: User): LiveData<Resource<Any?>> = threadManager.makeAdmin(user) |
||||
|
|
||||
|
private fun removeAdmin(user: User): LiveData<Resource<Any?>> = threadManager.removeAdmin(user) |
||||
|
|
||||
|
fun mute(): LiveData<Resource<Any?>> = threadManager.mute() |
||||
|
|
||||
|
fun unmute(): LiveData<Resource<Any?>> = threadManager.unmute() |
||||
|
|
||||
|
fun muteMentions(): LiveData<Resource<Any?>> = threadManager.muteMentions() |
||||
|
|
||||
|
fun unmuteMentions(): LiveData<Resource<Any?>> = threadManager.unmuteMentions() |
||||
|
|
||||
|
private fun blockUser(user: User): LiveData<Resource<Any?>> = threadManager.blockUser(user, viewModelScope) |
||||
|
|
||||
|
private fun unblockUser(user: User): LiveData<Resource<Any?>> = threadManager.unblockUser(user, viewModelScope) |
||||
|
|
||||
|
private fun restrictUser(user: User): LiveData<Resource<Any?>> = threadManager.restrictUser(user, viewModelScope) |
||||
|
|
||||
|
private fun unRestrictUser(user: User): LiveData<Resource<Any?>> = threadManager.unRestrictUser(user, viewModelScope) |
||||
|
|
||||
|
fun approveUsers(users: List<User>): LiveData<Resource<Any?>> = threadManager.approveUsers(users) |
||||
|
|
||||
|
fun denyUsers(users: List<User>): LiveData<Resource<Any?>> = threadManager.denyUsers(users) |
||||
|
|
||||
|
fun approvalRequired(): LiveData<Resource<Any?>> = threadManager.approvalRequired() |
||||
|
|
||||
|
fun approvalNotRequired(): LiveData<Resource<Any?>> = threadManager.approvalNotRequired() |
||||
|
|
||||
|
fun leave(): LiveData<Resource<Any?>> = threadManager.leave() |
||||
|
|
||||
|
fun end(): LiveData<Resource<Any?>> = threadManager.end() |
||||
|
|
||||
|
fun createUserOptions(user: User?): ArrayList<Option<String>> { |
||||
|
val options: ArrayList<Option<String>> = ArrayList() |
||||
|
if (user == null || isSelf(user) || hasLeft(user)) { |
||||
|
return options |
||||
|
} |
||||
|
val viewerIsAdmin: Boolean? = threadManager.isViewerAdmin.value |
||||
|
if (viewerIsAdmin != null && viewerIsAdmin) { |
||||
|
options.add(Option(getString(R.string.dms_action_kick), ACTION_KICK)) |
||||
|
val isAdmin: Boolean = threadManager.isAdmin(user) |
||||
|
options.add(Option( |
||||
|
if (isAdmin) getString(R.string.dms_action_remove_admin) else getString(R.string.dms_action_make_admin), |
||||
|
if (isAdmin) ACTION_REMOVE_ADMIN else ACTION_MAKE_ADMIN |
||||
|
)) |
||||
|
} |
||||
|
val blocking: Boolean = user.friendshipStatus.blocking |
||||
|
options.add(Option( |
||||
|
if (blocking) getString(R.string.unblock) else getString(R.string.block), |
||||
|
if (blocking) ACTION_UNBLOCK else ACTION_BLOCK |
||||
|
)) |
||||
|
|
||||
|
// options.add(new Option<>(getString(R.string.report), ACTION_REPORT)); |
||||
|
val isGroup: Boolean? = threadManager.isGroup.value |
||||
|
if (isGroup != null && isGroup) { |
||||
|
val restricted: Boolean = user.friendshipStatus.isRestricted |
||||
|
options.add(Option( |
||||
|
if (restricted) getString(R.string.unrestrict) else getString(R.string.restrict), |
||||
|
if (restricted) ACTION_UNRESTRICT else ACTION_RESTRICT |
||||
|
)) |
||||
|
} |
||||
|
return options |
||||
|
} |
||||
|
|
||||
|
private fun hasLeft(user: User): Boolean { |
||||
|
val leftUsers: List<User> = getLeftUsers().value ?: return false |
||||
|
return leftUsers.contains(user) |
||||
|
} |
||||
|
|
||||
|
private fun isSelf(user: User): Boolean = user.pk == viewerId |
||||
|
|
||||
|
private fun getString(@StringRes resId: Int): String { |
||||
|
return resources.getString(resId) |
||||
|
} |
||||
|
|
||||
|
fun doAction(user: User?, action: String?): LiveData<Resource<Any?>>? { |
||||
|
return if (user == null || action == null) null else when (action) { |
||||
|
ACTION_KICK -> removeMember(user) |
||||
|
ACTION_MAKE_ADMIN -> makeAdmin(user) |
||||
|
ACTION_REMOVE_ADMIN -> removeAdmin(user) |
||||
|
ACTION_BLOCK -> blockUser(user) |
||||
|
ACTION_UNBLOCK -> unblockUser(user) |
||||
|
ACTION_RESTRICT -> restrictUser(user) |
||||
|
ACTION_UNRESTRICT -> unRestrictUser(user) |
||||
|
else -> null |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fun getInviter(): LiveData<User?> = threadManager.inviter |
||||
|
|
||||
|
companion object { |
||||
|
private const val ACTION_KICK = "kick" |
||||
|
private const val ACTION_MAKE_ADMIN = "make_admin" |
||||
|
private const val ACTION_REMOVE_ADMIN = "remove_admin" |
||||
|
private const val ACTION_BLOCK = "block" |
||||
|
private const val ACTION_UNBLOCK = "unblock" |
||||
|
|
||||
|
// private static final String ACTION_REPORT = "report"; |
||||
|
private const val ACTION_RESTRICT = "restrict" |
||||
|
private const val ACTION_UNRESTRICT = "unrestrict" |
||||
|
} |
||||
|
|
||||
|
init { |
||||
|
val cookie = Utils.settingsHelper.getString(Constants.COOKIE) |
||||
|
viewerId = getUserIdFromCookie(cookie) |
||||
|
val deviceUuid = Utils.settingsHelper.getString(Constants.DEVICE_UUID) |
||||
|
val csrfToken = getCsrfTokenFromCookie(cookie) |
||||
|
require(!csrfToken.isNullOrBlank() && viewerId != 0L && deviceUuid.isNotBlank()) { "User is not logged in!" } |
||||
|
} |
||||
|
} |
@ -1,347 +1,329 @@ |
|||||
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 java.util.Set; |
|
||||
|
|
||||
import awais.instagrabber.R; |
|
||||
import awais.instagrabber.managers.DirectMessagesManager; |
|
||||
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.repositories.responses.directmessages.RankedRecipient; |
|
||||
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 retrofit2.Call; |
|
||||
import retrofit2.Callback; |
|
||||
import retrofit2.Response; |
|
||||
|
|
||||
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<Long> viewCount = 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 boolean isLoggedIn; |
|
||||
|
|
||||
private Media media; |
|
||||
private DirectMessagesManager messageManager; |
|
||||
|
|
||||
public PostViewV2ViewModel() { |
|
||||
final String cookie = settingsHelper.getString(Constants.COOKIE); |
|
||||
final String deviceUuid = settingsHelper.getString(Constants.DEVICE_UUID); |
|
||||
final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie); |
|
||||
viewerId = CookieUtils.getUserIdFromCookie(cookie); |
|
||||
mediaService = MediaService.getInstance(deviceUuid, csrfToken, viewerId); |
|
||||
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()); |
|
||||
viewCount.postValue(media.getMediaType() == MediaItemType.MEDIA_TYPE_VIDEO ? media.getViewCount() : null); |
|
||||
type.postValue(media.getMediaType()); |
|
||||
liked.postValue(media.getHasLiked()); |
|
||||
saved.postValue(media.getHasViewerSaved()); |
|
||||
initOptions(); |
|
||||
} |
|
||||
|
|
||||
private void initOptions() { |
|
||||
final ImmutableList.Builder<Integer> builder = ImmutableList.builder(); |
|
||||
if (isLoggedIn && media.getUser() != null && media.getUser().getPk() == viewerId) { |
|
||||
builder.add(R.id.edit_caption); |
|
||||
builder.add(R.id.delete); |
|
||||
|
package awais.instagrabber.viewmodels |
||||
|
|
||||
|
import android.util.Log |
||||
|
import androidx.lifecycle.LiveData |
||||
|
import androidx.lifecycle.MutableLiveData |
||||
|
import androidx.lifecycle.ViewModel |
||||
|
import androidx.lifecycle.viewModelScope |
||||
|
import awais.instagrabber.R |
||||
|
import awais.instagrabber.managers.DirectMessagesManager |
||||
|
import awais.instagrabber.models.Resource |
||||
|
import awais.instagrabber.models.Resource.Companion.error |
||||
|
import awais.instagrabber.models.Resource.Companion.loading |
||||
|
import awais.instagrabber.models.Resource.Companion.success |
||||
|
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.repositories.responses.directmessages.RankedRecipient |
||||
|
import awais.instagrabber.utils.Constants |
||||
|
import awais.instagrabber.utils.Utils |
||||
|
import awais.instagrabber.utils.extensions.TAG |
||||
|
import awais.instagrabber.utils.getCsrfTokenFromCookie |
||||
|
import awais.instagrabber.utils.getUserIdFromCookie |
||||
|
import awais.instagrabber.webservices.MediaService |
||||
|
import awais.instagrabber.webservices.ServiceCallback |
||||
|
import com.google.common.collect.ImmutableList |
||||
|
import retrofit2.Call |
||||
|
import retrofit2.Callback |
||||
|
import retrofit2.Response |
||||
|
import java.util.* |
||||
|
|
||||
|
class PostViewV2ViewModel : ViewModel() { |
||||
|
private val user = MutableLiveData<User?>() |
||||
|
private val caption = MutableLiveData<Caption?>() |
||||
|
private val location = MutableLiveData<Location?>() |
||||
|
private val date = MutableLiveData<String>() |
||||
|
private val likeCount = MutableLiveData(0L) |
||||
|
private val commentCount = MutableLiveData(0L) |
||||
|
private val viewCount = MutableLiveData(0L) |
||||
|
private val type = MutableLiveData<MediaItemType?>() |
||||
|
private val liked = MutableLiveData(false) |
||||
|
private val saved = MutableLiveData(false) |
||||
|
private val options = MutableLiveData<List<Int>>(ArrayList()) |
||||
|
private val viewerId: Long |
||||
|
val isLoggedIn: Boolean |
||||
|
lateinit var media: Media |
||||
|
private set |
||||
|
private var mediaService: MediaService? = null |
||||
|
private var messageManager: DirectMessagesManager? = null |
||||
|
|
||||
|
fun setMedia(media: Media) { |
||||
|
this.media = media |
||||
|
user.postValue(media.user) |
||||
|
caption.postValue(media.caption) |
||||
|
location.postValue(media.location) |
||||
|
date.postValue(media.date) |
||||
|
likeCount.postValue(media.likeCount) |
||||
|
commentCount.postValue(media.commentCount) |
||||
|
viewCount.postValue(if (media.mediaType == MediaItemType.MEDIA_TYPE_VIDEO) media.viewCount else null) |
||||
|
type.postValue(media.mediaType) |
||||
|
liked.postValue(media.hasLiked) |
||||
|
saved.postValue(media.hasViewerSaved) |
||||
|
initOptions() |
||||
|
} |
||||
|
|
||||
|
private fun initOptions() { |
||||
|
val builder = ImmutableList.builder<Int>() |
||||
|
val user1 = media.user |
||||
|
if (isLoggedIn && user1 != null && user1.pk == viewerId) { |
||||
|
builder.add(R.id.edit_caption) |
||||
|
builder.add(R.id.delete) |
||||
} |
} |
||||
options.postValue(builder.build()); |
|
||||
} |
|
||||
|
|
||||
public Media getMedia() { |
|
||||
return media; |
|
||||
} |
|
||||
|
|
||||
public boolean isLoggedIn() { |
|
||||
return isLoggedIn; |
|
||||
|
options.postValue(builder.build()) |
||||
} |
} |
||||
|
|
||||
public LiveData<User> getUser() { |
|
||||
return user; |
|
||||
|
fun getUser(): LiveData<User?> { |
||||
|
return user |
||||
} |
} |
||||
|
|
||||
public LiveData<Caption> getCaption() { |
|
||||
return caption; |
|
||||
|
fun getCaption(): LiveData<Caption?> { |
||||
|
return caption |
||||
} |
} |
||||
|
|
||||
public LiveData<Location> getLocation() { |
|
||||
return location; |
|
||||
|
fun getLocation(): LiveData<Location?> { |
||||
|
return location |
||||
} |
} |
||||
|
|
||||
public LiveData<String> getDate() { |
|
||||
return date; |
|
||||
|
fun getDate(): LiveData<String> { |
||||
|
return date |
||||
} |
} |
||||
|
|
||||
public LiveData<Long> getLikeCount() { |
|
||||
return likeCount; |
|
||||
|
fun getLikeCount(): LiveData<Long> { |
||||
|
return likeCount |
||||
} |
} |
||||
|
|
||||
public LiveData<Long> getCommentCount() { |
|
||||
return commentCount; |
|
||||
|
fun getCommentCount(): LiveData<Long> { |
||||
|
return commentCount |
||||
} |
} |
||||
|
|
||||
public LiveData<Long> getViewCount() { |
|
||||
return viewCount; |
|
||||
|
fun getViewCount(): LiveData<Long?> { |
||||
|
return viewCount |
||||
} |
} |
||||
|
|
||||
public LiveData<MediaItemType> getType() { |
|
||||
return type; |
|
||||
|
fun getType(): LiveData<MediaItemType?> { |
||||
|
return type |
||||
} |
} |
||||
|
|
||||
public LiveData<Boolean> getLiked() { |
|
||||
return liked; |
|
||||
|
fun getLiked(): LiveData<Boolean> { |
||||
|
return liked |
||||
} |
} |
||||
|
|
||||
public LiveData<Boolean> getSaved() { |
|
||||
return saved; |
|
||||
|
fun getSaved(): LiveData<Boolean> { |
||||
|
return saved |
||||
} |
} |
||||
|
|
||||
public LiveData<List<Integer>> getOptions() { |
|
||||
return options; |
|
||||
|
fun getOptions(): LiveData<List<Int>> { |
||||
|
return options |
||||
} |
} |
||||
|
|
||||
@NonNull |
|
||||
public LiveData<Resource<Object>> toggleLike() { |
|
||||
if (media.getHasLiked()) { |
|
||||
return unlike(); |
|
||||
} |
|
||||
return like(); |
|
||||
|
fun toggleLike(): LiveData<Resource<Any?>> { |
||||
|
return if (media.hasLiked) { |
||||
|
unlike() |
||||
|
} else like() |
||||
} |
} |
||||
|
|
||||
public LiveData<Resource<Object>> like() { |
|
||||
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>(); |
|
||||
data.postValue(Resource.loading(null)); |
|
||||
mediaService.like(media.getPk(), getLikeUnlikeCallback(data)); |
|
||||
return data; |
|
||||
|
fun like(): LiveData<Resource<Any?>> { |
||||
|
val data = MutableLiveData<Resource<Any?>>() |
||||
|
data.postValue(loading(null)) |
||||
|
mediaService?.like(media.pk, 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(), getLikeUnlikeCallback(data)); |
|
||||
return data; |
|
||||
|
fun unlike(): LiveData<Resource<Any?>> { |
||||
|
val data = MutableLiveData<Resource<Any?>>() |
||||
|
data.postValue(loading(null)) |
||||
|
mediaService?.unlike(media.pk, 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; |
|
||||
|
private fun getLikeUnlikeCallback(data: MutableLiveData<Resource<Any?>>): ServiceCallback<Boolean?> { |
||||
|
return object : ServiceCallback<Boolean?> { |
||||
|
override fun onSuccess(result: Boolean?) { |
||||
|
if (result != null && !result) { |
||||
|
data.postValue(error("", null)) |
||||
|
return |
||||
} |
} |
||||
data.postValue(Resource.success(true)); |
|
||||
final long currentLikesCount = media.getLikeCount(); |
|
||||
final long updatedCount; |
|
||||
if (!media.getHasLiked()) { |
|
||||
updatedCount = currentLikesCount + 1; |
|
||||
media.setHasLiked(true); |
|
||||
|
data.postValue(success(true)) |
||||
|
val currentLikesCount = media.likeCount |
||||
|
val updatedCount: Long |
||||
|
if (!media.hasLiked) { |
||||
|
updatedCount = currentLikesCount + 1 |
||||
|
media.hasLiked = true |
||||
} else { |
} else { |
||||
updatedCount = currentLikesCount - 1; |
|
||||
media.setHasLiked(false); |
|
||||
|
updatedCount = currentLikesCount - 1 |
||||
|
media.hasLiked = false |
||||
} |
} |
||||
media.setLikeCount(updatedCount); |
|
||||
likeCount.postValue(updatedCount); |
|
||||
liked.postValue(media.getHasLiked()); |
|
||||
|
media.likeCount = 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); |
|
||||
|
override fun onFailure(t: Throwable) { |
||||
|
data.postValue(error(t.message, null)) |
||||
|
Log.e(TAG, "Error during like/unlike", t) |
||||
} |
} |
||||
}; |
|
||||
|
} |
||||
} |
} |
||||
|
|
||||
@NonNull |
|
||||
public LiveData<Resource<Object>> toggleSave() { |
|
||||
if (!media.getHasViewerSaved()) { |
|
||||
return save(null, false); |
|
||||
} |
|
||||
return unsave(); |
|
||||
|
fun toggleSave(): LiveData<Resource<Any?>> { |
||||
|
return if (!media.hasViewerSaved) { |
||||
|
save(null, false) |
||||
|
} else unsave() |
||||
} |
} |
||||
|
|
||||
@NonNull |
|
||||
public LiveData<Resource<Object>> toggleSave(final String collection, final boolean ignoreSaveState) { |
|
||||
return save(collection, ignoreSaveState); |
|
||||
|
fun toggleSave(collection: String?, ignoreSaveState: Boolean): LiveData<Resource<Any?>> { |
||||
|
return save(collection, ignoreSaveState) |
||||
} |
} |
||||
|
|
||||
public LiveData<Resource<Object>> save(final String collection, final boolean ignoreSaveState) { |
|
||||
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>(); |
|
||||
data.postValue(Resource.loading(null)); |
|
||||
mediaService.save(media.getPk(), collection, getSaveUnsaveCallback(data, ignoreSaveState)); |
|
||||
return data; |
|
||||
|
fun save(collection: String?, ignoreSaveState: Boolean): LiveData<Resource<Any?>> { |
||||
|
val data = MutableLiveData<Resource<Any?>>() |
||||
|
data.postValue(loading(null)) |
||||
|
mediaService?.save(media.pk, collection, getSaveUnsaveCallback(data, ignoreSaveState)) |
||||
|
return data |
||||
} |
} |
||||
|
|
||||
public LiveData<Resource<Object>> unsave() { |
|
||||
final MutableLiveData<Resource<Object>> data = new MutableLiveData<>(); |
|
||||
data.postValue(Resource.loading(null)); |
|
||||
mediaService.unsave(media.getPk(), getSaveUnsaveCallback(data, false)); |
|
||||
return data; |
|
||||
|
fun unsave(): LiveData<Resource<Any?>> { |
||||
|
val data = MutableLiveData<Resource<Any?>>() |
||||
|
data.postValue(loading(null)) |
||||
|
mediaService?.unsave(media.pk, getSaveUnsaveCallback(data, false)) |
||||
|
return data |
||||
} |
} |
||||
|
|
||||
@NonNull |
|
||||
private ServiceCallback<Boolean> getSaveUnsaveCallback(final MutableLiveData<Resource<Object>> data, |
|
||||
final boolean ignoreSaveState) { |
|
||||
return new ServiceCallback<Boolean>() { |
|
||||
@Override |
|
||||
public void onSuccess(final Boolean result) { |
|
||||
if (!result) { |
|
||||
data.postValue(Resource.error("", null)); |
|
||||
return; |
|
||||
|
private fun getSaveUnsaveCallback( |
||||
|
data: MutableLiveData<Resource<Any?>>, |
||||
|
ignoreSaveState: Boolean, |
||||
|
): ServiceCallback<Boolean?> { |
||||
|
return object : ServiceCallback<Boolean?> { |
||||
|
override fun onSuccess(result: Boolean?) { |
||||
|
if (result != null && !result) { |
||||
|
data.postValue(error("", null)) |
||||
|
return |
||||
} |
} |
||||
data.postValue(Resource.success(true)); |
|
||||
if (!ignoreSaveState) media.setHasViewerSaved(!media.getHasViewerSaved()); |
|
||||
saved.postValue(media.getHasViewerSaved()); |
|
||||
|
data.postValue(success(true)) |
||||
|
if (!ignoreSaveState) media.hasViewerSaved = !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); |
|
||||
|
override fun onFailure(t: Throwable) { |
||||
|
data.postValue(error(t.message, 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(), caption, 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; |
|
||||
|
fun updateCaption(caption: String): LiveData<Resource<Any?>> { |
||||
|
val data = MutableLiveData<Resource<Any?>>() |
||||
|
data.postValue(loading(null)) |
||||
|
mediaService?.editCaption(media.pk, caption, object : ServiceCallback<Boolean?> { |
||||
|
override fun onSuccess(result: Boolean?) { |
||||
|
if (result != null && result) { |
||||
|
data.postValue(success("")) |
||||
|
media.setPostCaption(caption) |
||||
|
this@PostViewV2ViewModel.caption.postValue(media.caption) |
||||
|
return |
||||
} |
} |
||||
data.postValue(Resource.error("", null)); |
|
||||
|
data.postValue(error("", null)) |
||||
} |
} |
||||
|
|
||||
@Override |
|
||||
public void onFailure(final Throwable t) { |
|
||||
Log.e(TAG, "Error editing caption", t); |
|
||||
data.postValue(Resource.error(t.getMessage(), null)); |
|
||||
|
override fun onFailure(t: Throwable) { |
||||
|
Log.e(TAG, "Error editing caption", t) |
||||
|
data.postValue(error(t.message, 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(value.getPk(), "1", new ServiceCallback<String>() { |
|
||||
@Override |
|
||||
public void onSuccess(final String result) { |
|
||||
if (TextUtils.isEmpty(result)) { |
|
||||
data.postValue(Resource.error("", null)); |
|
||||
return; |
|
||||
|
}) |
||||
|
return data |
||||
|
} |
||||
|
|
||||
|
fun translateCaption(): LiveData<Resource<String?>> { |
||||
|
val data = MutableLiveData<Resource<String?>>() |
||||
|
data.postValue(loading(null)) |
||||
|
val value = caption.value ?: return data |
||||
|
mediaService?.translate(value.pk, "1", object : ServiceCallback<String?> { |
||||
|
override fun onSuccess(result: String?) { |
||||
|
if (result.isNullOrBlank()) { |
||||
|
data.postValue(error("", null)) |
||||
|
return |
||||
} |
} |
||||
data.postValue(Resource.success(result)); |
|
||||
|
data.postValue(success(result)) |
||||
} |
} |
||||
|
|
||||
@Override |
|
||||
public void onFailure(final Throwable t) { |
|
||||
Log.e(TAG, "Error translating comment", t); |
|
||||
data.postValue(Resource.error(t.getMessage(), null)); |
|
||||
|
override fun onFailure(t: Throwable) { |
||||
|
Log.e(TAG, "Error translating comment", t) |
||||
|
data.postValue(error(t.message, null)) |
||||
} |
} |
||||
}); |
|
||||
return data; |
|
||||
|
}) |
||||
|
return data |
||||
} |
} |
||||
|
|
||||
public boolean hasPk() { |
|
||||
return media.getPk() != null; |
|
||||
|
fun hasPk(): Boolean { |
||||
|
return media.pk != null |
||||
} |
} |
||||
|
|
||||
public void setViewCount(final Long viewCount) { |
|
||||
this.viewCount.postValue(viewCount); |
|
||||
|
fun setViewCount(viewCount: Long?) { |
||||
|
this.viewCount.postValue(viewCount) |
||||
} |
} |
||||
|
|
||||
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()); |
|
||||
|
fun delete(): LiveData<Resource<Any?>> { |
||||
|
val data = MutableLiveData<Resource<Any?>>() |
||||
|
data.postValue(loading(null)) |
||||
|
val mediaId = media.id |
||||
|
val mediaType = media.mediaType |
||||
|
if (mediaId == null || mediaType == null) { |
||||
|
data.postValue(error("media id or type is null", null)) |
||||
|
return data |
||||
|
} |
||||
|
val request = mediaService?.delete(mediaId, mediaType) |
||||
if (request == null) { |
if (request == null) { |
||||
data.postValue(Resource.success(new Object())); |
|
||||
return data; |
|
||||
|
data.postValue(success(Any())) |
||||
|
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; |
|
||||
|
request.enqueue(object : Callback<String?> { |
||||
|
override fun onResponse(call: Call<String?>, response: Response<String?>) { |
||||
|
if (!response.isSuccessful) { |
||||
|
data.postValue(error(R.string.generic_null_response, null)) |
||||
|
return |
||||
} |
} |
||||
final String body = response.body(); |
|
||||
|
val body = response.body() |
||||
if (body == null) { |
if (body == null) { |
||||
data.postValue(Resource.error(R.string.generic_null_response, null)); |
|
||||
return; |
|
||||
|
data.postValue(error(R.string.generic_null_response, null)) |
||||
|
return |
||||
} |
} |
||||
data.postValue(Resource.success(new Object())); |
|
||||
|
data.postValue(success(Any())) |
||||
} |
} |
||||
|
|
||||
@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)); |
|
||||
|
override fun onFailure(call: Call<String?>, t: Throwable) { |
||||
|
Log.e(TAG, "onFailure: ", t) |
||||
|
data.postValue(error(t.message, null)) |
||||
} |
} |
||||
}); |
|
||||
return data; |
|
||||
|
}) |
||||
|
return data |
||||
} |
} |
||||
|
|
||||
public void shareDm(@NonNull final RankedRecipient result) { |
|
||||
|
fun shareDm(result: RankedRecipient) { |
||||
if (messageManager == null) { |
if (messageManager == null) { |
||||
messageManager = DirectMessagesManager.INSTANCE; |
|
||||
|
messageManager = DirectMessagesManager |
||||
} |
} |
||||
messageManager.sendMedia(result, media.getId()); |
|
||||
|
val mediaId = media.id ?: return |
||||
|
messageManager?.sendMedia(result, mediaId, viewModelScope) |
||||
} |
} |
||||
|
|
||||
public void shareDm(@NonNull final Set<RankedRecipient> recipients) { |
|
||||
|
fun shareDm(recipients: Set<RankedRecipient>) { |
||||
if (messageManager == null) { |
if (messageManager == null) { |
||||
messageManager = DirectMessagesManager.INSTANCE; |
|
||||
|
messageManager = DirectMessagesManager |
||||
|
} |
||||
|
val mediaId = media.id ?: return |
||||
|
messageManager?.sendMedia(recipients, mediaId, viewModelScope) |
||||
|
} |
||||
|
|
||||
|
init { |
||||
|
val cookie = Utils.settingsHelper.getString(Constants.COOKIE) |
||||
|
val deviceUuid = Utils.settingsHelper.getString(Constants.DEVICE_UUID) |
||||
|
val csrfToken: String? = getCsrfTokenFromCookie(cookie) |
||||
|
viewerId = getUserIdFromCookie(cookie) |
||||
|
isLoggedIn = cookie.isNotBlank() && viewerId != 0L |
||||
|
if (!csrfToken.isNullOrBlank()) { |
||||
|
mediaService = MediaService.getInstance(deviceUuid, csrfToken, viewerId) |
||||
} |
} |
||||
messageManager.sendMedia(recipients, media.getId()); |
|
||||
} |
} |
||||
} |
|
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue