From 3032a388f074829bb14f21483c0c0c1b5147ed19 Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Thu, 24 Jun 2021 13:01:23 -0400 Subject: [PATCH] share profile (unfinished) --- .../fragments/main/ProfileFragment.java | 63 +++++++++++-------- .../viewmodels/ProfileFragmentViewModel.kt | 7 ++- 2 files changed, 42 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java index e2a1d2a5..1f2d7638 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -35,6 +35,7 @@ import androidx.fragment.app.FragmentTransaction; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; +import androidx.navigation.NavBackStackEntry; import androidx.navigation.NavController; import androidx.navigation.NavDirections; import androidx.navigation.fragment.NavHostFragment; @@ -53,6 +54,7 @@ import java.util.Objects; import java.util.Set; import awais.instagrabber.R; +import awais.instagrabber.UserSearchNavGraphDirections; import awais.instagrabber.activities.MainActivity; import awais.instagrabber.adapters.FeedAdapterV2; import awais.instagrabber.adapters.HighlightsAdapter; @@ -67,6 +69,8 @@ import awais.instagrabber.db.repositories.FavoriteRepository; import awais.instagrabber.dialogs.PostsLayoutPreferencesDialogFragment; import awais.instagrabber.dialogs.ProfilePicDialogFragment; import awais.instagrabber.fragments.PostViewV2Fragment; +import awais.instagrabber.fragments.UserSearchFragment; +import awais.instagrabber.fragments.UserSearchFragmentDirections; import awais.instagrabber.managers.DirectMessagesManager; import awais.instagrabber.managers.InboxManager; import awais.instagrabber.models.HighlightModel; @@ -126,8 +130,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe private HighlightsViewModel highlightsViewModel; private MenuItem blockMenuItem, restrictMenuItem, chainingMenuItem; private MenuItem muteStoriesMenuItem, mutePostsMenuItem, removeFollowerMenuItem; - private MenuItem shareLinkMenuItem; - // private MenuItem shareDmMenuItem; + private MenuItem shareLinkMenuItem, shareDmMenuItem; private boolean accountIsUpdated = false; private boolean postsSetupDone = false; private Set selectedFeedModels; @@ -141,7 +144,6 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe private ProfileFragmentViewModel viewModel; private String csrfToken; private String deviceUuid; - private MutableLiveData backStackSavedStateCollectionLiveData; private MutableLiveData backStackSavedStateResultLiveData; private final ServiceCallback changeCb = new ServiceCallback() { @@ -334,7 +336,6 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe } } // clear result - backStackSavedStateCollectionLiveData.postValue(null); backStackSavedStateResultLiveData.postValue(null); }; @@ -466,10 +467,10 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe if (shareLinkMenuItem != null) { shareLinkMenuItem.setVisible(profileModel != null && !TextUtils.isEmpty(profileModel.getUsername())); } - // shareDmMenuItem = menu.findItem(R.id.share_dm); - // if (shareDmMenuItem != null) { - // shareDmMenuItem.setVisible(profileModel != null && profileModel.getPk() != 0L); - // } + shareDmMenuItem = menu.findItem(R.id.share_dm); + if (shareDmMenuItem != null) { + shareDmMenuItem.setVisible(profileModel != null && profileModel.getPk() != 0L); + } } @Override @@ -582,21 +583,20 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe startActivity(Intent.createChooser(sharingIntent, null)); return true; } else if (itemId == R.id.share_dm) { - // final UserSearchNavGraphDirections.ActionGlobalUserSearch actionGlobalUserSearch = UserSearchFragmentDirections - // .actionGlobalUserSearch() - // .setTitle(getString(R.string.share)) - // .setActionLabel(getString(R.string.send)) - // .setShowGroups(true) - // .setMultiple(true) - // .setSearchMode(UserSearchFragment.SearchMode.RAVEN); - // final NavController navController = NavHostFragment.findNavController(ProfileFragment.this); - // try { - // navController.navigate(actionGlobalUserSearch); - // } catch (Exception e) { - // Log.e(TAG, "setupShare: ", e); - // } - // return true; - return false; + final UserSearchNavGraphDirections.ActionGlobalUserSearch actionGlobalUserSearch = UserSearchFragmentDirections + .actionGlobalUserSearch() + .setTitle(getString(R.string.share)) + .setActionLabel(getString(R.string.send)) + .setShowGroups(true) + .setMultiple(true) + .setSearchMode(UserSearchFragment.SearchMode.RAVEN); + final NavController navController = NavHostFragment.findNavController(ProfileFragment.this); + try { + navController.navigate(actionGlobalUserSearch); + } catch (Exception e) { + Log.e(TAG, "setupShare: ", e); + } + return true; } return super.onOptionsItemSelected(item); } @@ -608,6 +608,17 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe fetchProfileDetails(); } + @Override + public void onResume() { + super.onResume(); + final NavController navController = NavHostFragment.findNavController(this); + final NavBackStackEntry backStackEntry = navController.getCurrentBackStackEntry(); + if (backStackEntry != null) { + backStackSavedStateResultLiveData = backStackEntry.getSavedStateHandle().getLiveData("result"); + backStackSavedStateResultLiveData.observe(getViewLifecycleOwner(), backStackSavedStateObserver); + } + } + @Override public void onDestroy() { super.onDestroy(); @@ -1061,9 +1072,9 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe if (shareLinkMenuItem != null) { shareLinkMenuItem.setVisible(!TextUtils.isEmpty(profileModel.getUsername())); } - // if (shareDmMenuItem != null) { - // shareDmMenuItem.setVisible(profileModel.getPk() != 0L); - // } + if (shareDmMenuItem != null) { + shareDmMenuItem.setVisible(profileModel.getPk() != 0L); + } } } diff --git a/app/src/main/java/awais/instagrabber/viewmodels/ProfileFragmentViewModel.kt b/app/src/main/java/awais/instagrabber/viewmodels/ProfileFragmentViewModel.kt index f9543c94..8594212d 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/ProfileFragmentViewModel.kt +++ b/app/src/main/java/awais/instagrabber/viewmodels/ProfileFragmentViewModel.kt @@ -11,6 +11,7 @@ import awais.instagrabber.managers.DirectMessagesManager import awais.instagrabber.models.HighlightModel import awais.instagrabber.models.Resource import awais.instagrabber.models.StoryModel +import awais.instagrabber.models.enums.BroadcastItemType import awais.instagrabber.models.enums.FavoriteType import awais.instagrabber.repositories.requests.StoryViewerOptions import awais.instagrabber.repositories.responses.User @@ -212,14 +213,16 @@ class ProfileFragmentViewModel( if (messageManager == null) { messageManager = DirectMessagesManager } -// messageManager?.sendMedia(result, mediaId, BroadcastItemType.PROFILE, viewModelScope) + val mediaId = profile.value?.data?.pk ?: return + messageManager?.sendMedia(result, mediaId.toString(10), BroadcastItemType.PROFILE, viewModelScope) } fun shareDm(recipients: Set) { if (messageManager == null) { messageManager = DirectMessagesManager } -// messageManager?.sendMedia(recipients, mediaId, BroadcastItemType.PROFILE, viewModelScope) + val mediaId = profile.value?.data?.pk ?: return + messageManager?.sendMedia(recipients, mediaId.toString(10), BroadcastItemType.PROFILE, viewModelScope) } }