From d48c72924a5bcc81ddf107ebcb82b2667da9927a Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Sun, 15 Nov 2020 04:04:14 +0900 Subject: [PATCH] Check for null PostsLayoutPreferences before setting variable in fragments --- .../fragments/HashTagFragment.java | 2 +- .../fragments/TopicPostsFragment.java | 3 +- .../fragments/main/DiscoverFragment.java | 194 ------------------ .../fragments/main/FeedFragment.java | 3 +- .../fragments/main/ProfileFragment.java | 2 +- 5 files changed, 4 insertions(+), 200 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java b/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java index eb39180f..00da14bd 100644 --- a/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java @@ -95,7 +95,7 @@ public class HashTagFragment extends Fragment implements SwipeRefreshLayout.OnRe private Set selectedFeedModels; private FeedModel downloadFeedModel; private int downloadChildPosition = -1; - private PostsLayoutPreferences layoutPreferences = PostsLayoutPreferences.fromJson(settingsHelper.getString(Constants.PREF_HASHTAG_POSTS_LAYOUT)); + private PostsLayoutPreferences layoutPreferences = Utils.getPostsLayoutPreferences(Constants.PREF_HASHTAG_POSTS_LAYOUT); private LayoutHashtagDetailsBinding hashtagDetailsBinding; private final OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(false) { diff --git a/app/src/main/java/awais/instagrabber/fragments/TopicPostsFragment.java b/app/src/main/java/awais/instagrabber/fragments/TopicPostsFragment.java index 720ed3e3..81830779 100644 --- a/app/src/main/java/awais/instagrabber/fragments/TopicPostsFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/TopicPostsFragment.java @@ -60,7 +60,6 @@ import awais.instagrabber.webservices.DiscoverService; import static androidx.core.content.PermissionChecker.checkSelfPermission; import static awais.instagrabber.utils.DownloadUtils.WRITE_PERMISSION; -import static awais.instagrabber.utils.Utils.settingsHelper; public class TopicPostsFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener { private static final int STORAGE_PERM_REQUEST_CODE = 8020; @@ -75,7 +74,7 @@ public class TopicPostsFragment extends Fragment implements SwipeRefreshLayout.O private Set selectedFeedModels; private FeedModel downloadFeedModel; private int downloadChildPosition = -1; - private PostsLayoutPreferences layoutPreferences = PostsLayoutPreferences.fromJson(settingsHelper.getString(Constants.PREF_TOPIC_POSTS_LAYOUT)); + private PostsLayoutPreferences layoutPreferences = Utils.getPostsLayoutPreferences(Constants.PREF_TOPIC_POSTS_LAYOUT); private final OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(false) { @Override diff --git a/app/src/main/java/awais/instagrabber/fragments/main/DiscoverFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/DiscoverFragment.java index db91702c..24ccf8ee 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/DiscoverFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/DiscoverFragment.java @@ -2,13 +2,10 @@ package awais.instagrabber.fragments.main; import android.os.Bundle; import android.util.Log; -import android.view.ActionMode; import android.view.LayoutInflater; -import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import androidx.activity.OnBackPressedCallback; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.coordinatorlayout.widget.CoordinatorLayout; @@ -18,10 +15,8 @@ import androidx.navigation.fragment.FragmentNavigator; import androidx.navigation.fragment.NavHostFragment; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; -import awais.instagrabber.R; import awais.instagrabber.activities.MainActivity; import awais.instagrabber.adapters.DiscoverTopicsAdapter; -import awais.instagrabber.customviews.PrimaryActionModeCallback; import awais.instagrabber.customviews.helpers.GridSpacingItemDecoration; import awais.instagrabber.databinding.FragmentDiscoverBinding; import awais.instagrabber.utils.Utils; @@ -35,77 +30,10 @@ public class DiscoverFragment extends Fragment implements SwipeRefreshLayout.OnR private MainActivity fragmentActivity; private CoordinatorLayout root; private FragmentDiscoverBinding binding; - private ActionMode actionMode; private TopicClusterViewModel topicClusterViewModel; private boolean shouldRefresh = true; private DiscoverService discoverService; - // private final FetchListener postsFetchListener = new FetchListener() { - // @Override - // public void doBefore() {} - // - // @Override - // public void onResult(final DiscoverItemModel[] result) { - // if (result == null || result.length <= 0) { - // binding.swipeRefreshLayout.setRefreshing(false); - // final Context context = getContext(); - // if (context == null) return; - // Toast.makeText(context, R.string.discover_empty, Toast.LENGTH_SHORT).show(); - // return; - // } - // List current = discoverItemViewModel.getList().getValue(); - // final List resultList = Arrays.asList(result); - // current = current == null ? new ArrayList<>() : new ArrayList<>(current); // copy to modifiable list - // if (isPullToRefresh) { - // current = resultList; - // isPullToRefresh = false; - // } else { - // current.addAll(resultList); - // } - // discoverItemViewModel.getList().postValue(current); - // binding.swipeRefreshLayout.setRefreshing(false); - // final DiscoverItemModel discoverItemModel = result[result.length - 1]; - // if (discoverItemModel != null) { - // discoverEndMaxId = discoverItemModel.getNextMaxId(); - // discoverHasMore = discoverItemModel.hasMore(); - // discoverItemModel.setMore(false, null); - // } - // } - // }; - private final OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(false) { - @Override - public void handleOnBackPressed() { - setEnabled(false); - remove(); - // if (discoverAdapter == null) return; - // discoverAdapter.clearSelection(); - } - }; - private final PrimaryActionModeCallback multiSelectAction = new PrimaryActionModeCallback( - R.menu.multi_select_download_menu, - new PrimaryActionModeCallback.CallbacksHelper() { - @Override - public void onDestroy(final ActionMode mode) { - onBackPressedCallback.handleOnBackPressed(); - } - - @Override - public boolean onActionItemClicked(final ActionMode mode, final MenuItem item) { - if (item.getItemId() == R.id.action_download) { - // if (discoverAdapter == null) return false; - // final Context context = getContext(); - // if (context == null) return false; - // DownloadUtils.batchDownload(context, - // null, - // DownloadMethod.DOWNLOAD_DISCOVER, - // discoverAdapter.getSelectedModels()); - // checkAndResetAction(); - return true; - } - return false; - } - }); - @Override public void onCreate(@Nullable final Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -135,31 +63,6 @@ public class DiscoverFragment extends Fragment implements SwipeRefreshLayout.OnR } private void init() { - // setExitSharedElementCallback(new SharedElementCallback() { - // @Override - // public void onSharedElementsArrived(final List sharedElementNames, - // final List sharedElements, - // final OnSharedElementsReadyListener listener) { - // super.onSharedElementsArrived(sharedElementNames, sharedElements, listener); - // Log.d(TAG, "onSharedElementsArrived: sharedElementNames: " + sharedElementNames); - // } - // - // @Override - // public void onSharedElementEnd(final List sharedElementNames, - // final List sharedElements, - // final List sharedElementSnapshots) { - // super.onSharedElementEnd(sharedElementNames, sharedElements, sharedElementSnapshots); - // Log.d(TAG, "onSharedElementEnd: sharedElementNames: " + sharedElementNames); - // } - // - // @Override - // public void onSharedElementStart(final List sharedElementNames, - // final List sharedElements, - // final List sharedElementSnapshots) { - // super.onSharedElementStart(sharedElementNames, sharedElements, sharedElementSnapshots); - // Log.d(TAG, "onSharedElementStart: sharedElementNames: " + sharedElementNames); - // } - // }); setupTopics(); fetchTopics(); } @@ -175,7 +78,6 @@ public class DiscoverFragment extends Fragment implements SwipeRefreshLayout.OnR final DiscoverTopicsAdapter adapter = new DiscoverTopicsAdapter((topicCluster, root, cover, title, titleColor, backgroundColor) -> { final FragmentNavigator.Extras.Builder builder = new FragmentNavigator.Extras.Builder() .addSharedElement(cover, "cover-" + topicCluster.getId()); - // .addSharedElement(title, "title-" + topicCluster.getId()); final DiscoverFragmentDirections.ActionDiscoverFragmentToTopicPostsFragment action = DiscoverFragmentDirections .actionDiscoverFragmentToTopicPostsFragment(topicCluster, titleColor, backgroundColor); NavHostFragment.findNavController(this).navigate(action, builder.build()); @@ -184,82 +86,7 @@ public class DiscoverFragment extends Fragment implements SwipeRefreshLayout.OnR topicClusterViewModel.getList().observe(getViewLifecycleOwner(), adapter::submitList); } - private void setupExplore() { - // discoverItemViewModel = new ViewModelProvider(fragmentActivity).get(DiscoverItemViewModel.class); - // final Context context = getContext(); - // if (context == null) return; - // final GridAutofitLayoutManager layoutManager = new GridAutofitLayoutManager(context, Utils.convertDpToPx(110)); - // binding.postsRecyclerView.setLayoutManager(layoutManager); - // binding.postsRecyclerView.addItemDecoration(new GridSpacingItemDecoration(Utils.convertDpToPx(4))); - // binding.discoverType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { - // @Override - // public void onItemSelected(AdapterView parent, View view, int pos, long id) { - // if (topicIds == null || topicIds.length <= 0) return; - // currentTopic = topicIds[pos]; - // onRefresh(); - // } - // - // @Override - // public void onNothingSelected(AdapterView parent) {} - // }); - // discoverAdapter = new DiscoverAdapter((model, position) -> { - // if (discoverAdapter.isSelecting()) { - // if (actionMode == null) return; - // final String title = getString(R.string.number_selected, discoverAdapter.getSelectedModels().size()); - // actionMode.setTitle(title); - // return; - // } - // if (checkAndResetAction()) return; - // final List discoverItemModels = discoverItemViewModel.getList().getValue(); - // if (discoverItemModels == null || discoverItemModels.size() == 0) return; - // if (discoverItemModels.get(0) == null) return; - // final String postId = discoverItemModels.get(0).getPostId(); - // final boolean isId = postId != null; - // final String[] idsOrShortCodes = new String[discoverItemModels.size()]; - // for (int i = 0; i < discoverItemModels.size(); i++) { - // idsOrShortCodes[i] = isId ? discoverItemModels.get(i).getPostId() - // : discoverItemModels.get(i).getShortCode(); - // } - // final NavDirections action = DiscoverFragmentDirections.actionGlobalPostViewFragment( - // position, - // idsOrShortCodes, - // isId); - // NavHostFragment.findNavController(this).navigate(action); - // }, (model, position) -> { - // if (!discoverAdapter.isSelecting()) { - // checkAndResetAction(); - // return true; - // } - // final OnBackPressedDispatcher onBackPressedDispatcher = fragmentActivity.getOnBackPressedDispatcher(); - // if (onBackPressedCallback.isEnabled()) { - // return true; - // } - // actionMode = fragmentActivity.startActionMode(multiSelectAction); - // final String title = getString(R.string.number_selected, 1); - // actionMode.setTitle(title); - // onBackPressedDispatcher.addCallback(getViewLifecycleOwner(), onBackPressedCallback); - // return true; - // }); - // binding.postsRecyclerView.setAdapter(discoverAdapter); - // discoverItemViewModel.getList().observe(fragmentActivity, discoverAdapter::submitList); - // lazyLoader = new RecyclerLazyLoader(layoutManager, (page, totalItemsCount) -> { - // if (discoverHasMore) { - // fetchPosts(); - // } - // }, 3); - // binding.postsRecyclerView.addOnScrollListener(lazyLoader); - // binding.postsRecyclerView.setViewModelStoreOwner(this) - // .setLifeCycleOwner(this) - // .setPostFetchService(new DiscoverPostFetchService()) - // .setLayoutPreferences(PostsLayoutPreferences.fromJson(settingsHelper.getString(Constants.PREF_PROFILE_POSTS_LAYOUT))) - // .addFetchStatusChangeListener(fetching -> updateSwipeRefreshState()) - // .setFeedItemCallback(feedItemCallback) - // .init(); - // binding.swipeRefreshLayout.setRefreshing(true); - } - private void fetchTopics() { - // new iTopicFetcher(topicFetchListener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); binding.swipeRefreshLayout.setRefreshing(true); discoverService.topicalExplore(new DiscoverService.TopicalExploreRequest(), new ServiceCallback() { @Override @@ -275,25 +102,4 @@ public class DiscoverFragment extends Fragment implements SwipeRefreshLayout.OnR } }); } - - // private void fetchPosts() { - // binding.swipeRefreshLayout.setRefreshing(true); - // new DiscoverFetcher(currentTopic, discoverEndMaxId, rankToken, postsFetchListener, false) - // .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - // } - - private boolean checkAndResetAction() { - if (!onBackPressedCallback.isEnabled() && actionMode == null) { - return false; - } - if (onBackPressedCallback.isEnabled()) { - onBackPressedCallback.setEnabled(false); - onBackPressedCallback.remove(); - } - if (actionMode != null) { - actionMode.finish(); - actionMode = null; - } - return true; - } } diff --git a/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java index 678cf0d5..afc14848 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java @@ -55,7 +55,6 @@ import awais.instagrabber.webservices.StoriesService; import static androidx.core.content.PermissionChecker.checkSelfPermission; import static awais.instagrabber.utils.DownloadUtils.WRITE_PERMISSION; -import static awais.instagrabber.utils.Utils.settingsHelper; public class FeedFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener { private static final String TAG = "FeedFragment"; @@ -73,7 +72,7 @@ public class FeedFragment extends Fragment implements SwipeRefreshLayout.OnRefre private Set selectedFeedModels; private FeedModel downloadFeedModel; private int downloadChildPosition = -1; - private PostsLayoutPreferences layoutPreferences = PostsLayoutPreferences.fromJson(settingsHelper.getString(Constants.PREF_POSTS_LAYOUT)); + private PostsLayoutPreferences layoutPreferences = Utils.getPostsLayoutPreferences(Constants.PREF_POSTS_LAYOUT); private RecyclerView storiesRecyclerView; private final FeedAdapterV2.FeedItemCallback feedItemCallback = new FeedAdapterV2.FeedItemCallback() { 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 9f8c44ea..20b7abfb 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -117,7 +117,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe private Set selectedFeedModels; private FeedModel downloadFeedModel; private int downloadChildPosition = -1; - private PostsLayoutPreferences layoutPreferences = PostsLayoutPreferences.fromJson(settingsHelper.getString(Constants.PREF_PROFILE_POSTS_LAYOUT)); + private PostsLayoutPreferences layoutPreferences = Utils.getPostsLayoutPreferences(Constants.PREF_PROFILE_POSTS_LAYOUT); private final Runnable usernameSettingRunnable = () -> { final ActionBar actionBar = fragmentActivity.getSupportActionBar();