Add a FloatingActionButton in ViewMultiRedditDetailActivity.

This commit is contained in:
Docile-Alligator 2022-03-25 17:34:57 +08:00
parent 8ac8ea3d8b
commit 7422098734
2 changed files with 388 additions and 1 deletions

View File

@ -1,29 +1,38 @@
package ml.docilealligator.infinityforreddit.activities;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.textfield.TextInputEditText;
import com.r0adkll.slidr.Slidr;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.concurrent.Executor;
import javax.inject.Inject;
@ -39,8 +48,13 @@ import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.SortType;
import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback;
import ml.docilealligator.infinityforreddit.adapters.SubredditAutocompleteRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.asynctasks.DeleteMultiredditInDatabase;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.FABMoreOptionsBottomSheetFragment;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostLayoutBottomSheetFragment;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostTypeBottomSheetFragment;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.RandomBottomSheetFragment;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.SortTimeBottomSheetFragment;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.SortTypeBottomSheetFragment;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
@ -51,12 +65,20 @@ import ml.docilealligator.infinityforreddit.multireddit.MultiReddit;
import ml.docilealligator.infinityforreddit.post.Post;
import ml.docilealligator.infinityforreddit.post.PostPagingSource;
import ml.docilealligator.infinityforreddit.readpost.InsertReadPost;
import ml.docilealligator.infinityforreddit.subreddit.ParseSubredditData;
import ml.docilealligator.infinityforreddit.subreddit.SubredditData;
import ml.docilealligator.infinityforreddit.utils.APIUtils;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
import ml.docilealligator.infinityforreddit.utils.Utils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
public class ViewMultiRedditDetailActivity extends BaseActivity implements SortTypeSelectionCallback,
PostLayoutBottomSheetFragment.PostLayoutSelectionCallback, ActivityToolbarInterface, MarkPostAsReadInterface {
PostLayoutBottomSheetFragment.PostLayoutSelectionCallback, ActivityToolbarInterface, MarkPostAsReadInterface,
PostTypeBottomSheetFragment.PostTypeSelectionCallback, FABMoreOptionsBottomSheetFragment.FABOptionSelectionCallback,
RandomBottomSheetFragment.RandomOptionSelectionCallback {
public static final String EXTRA_MULTIREDDIT_DATA = "EMD";
public static final String EXTRA_MULTIREDDIT_PATH = "EMP";
@ -71,6 +93,8 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT
CollapsingToolbarLayout collapsingToolbarLayout;
@BindView(R.id.toolbar_view_multi_reddit_detail_activity)
Toolbar toolbar;
@BindView(R.id.fab_view_multi_reddit_detail_activity)
FloatingActionButton fab;
@Inject
@Named("no_oauth")
Retrofit mRetrofit;
@ -92,6 +116,12 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT
@Named("current_account")
SharedPreferences mCurrentAccountSharedPreferences;
@Inject
@Named("bottom_app_bar")
SharedPreferences bottomAppBarSharedPreference;
@Inject
@Named("nsfw_and_spoiler")
SharedPreferences mNsfwAndSpoilerSharedPreferences;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
@Inject
Executor mExecutor;
@ -102,6 +132,8 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT
private SortTypeBottomSheetFragment sortTypeBottomSheetFragment;
private SortTimeBottomSheetFragment sortTimeBottomSheetFragment;
private PostLayoutBottomSheetFragment postLayoutBottomSheetFragment;
private int fabOption;
private Call<String> subredditAutocompleteCall;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -133,6 +165,13 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT
window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
adjustToolbar(toolbar);
int navBarHeight = getNavBarHeight();
if (navBarHeight > 0) {
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
params.bottomMargin += navBarHeight;
fab.setLayoutParams(params);
}
}
}
@ -173,6 +212,105 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT
sortTimeBottomSheetFragment = new SortTimeBottomSheetFragment();
postLayoutBottomSheetFragment = new PostLayoutBottomSheetFragment();
fabOption = bottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB, SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_SUBMIT_POSTS);
switch (fabOption) {
case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_REFRESH:
fab.setImageResource(R.drawable.ic_refresh_24dp);
break;
case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_CHANGE_SORT_TYPE:
fab.setImageResource(R.drawable.ic_sort_toolbar_24dp);
break;
case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_CHANGE_POST_LAYOUT:
fab.setImageResource(R.drawable.ic_post_layout_24dp);
break;
case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_SEARCH:
fab.setImageResource(R.drawable.ic_search_black_24dp);
break;
case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_GO_TO_SUBREDDIT:
fab.setImageResource(R.drawable.ic_subreddit_24dp);
break;
case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_GO_TO_USER:
fab.setImageResource(R.drawable.ic_user_24dp);
break;
case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_RANDOM:
fab.setImageResource(R.drawable.ic_random_24dp);
break;
case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_HIDE_READ_POSTS:
if (mAccessToken == null) {
fab.setImageResource(R.drawable.ic_filter_24dp);
fabOption = SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_FAB_FILTER_POSTS;
} else {
fab.setImageResource(R.drawable.ic_hide_read_posts_24dp);
}
break;
case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_FILTER_POSTS:
fab.setImageResource(R.drawable.ic_filter_24dp);
break;
default:
if (mAccessToken == null) {
fab.setImageResource(R.drawable.ic_filter_24dp);
fabOption = SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_FAB_FILTER_POSTS;
} else {
fab.setImageResource(R.drawable.ic_add_day_night_24dp);
}
break;
}
fab.setOnClickListener(view -> {
switch (fabOption) {
case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_REFRESH: {
if (mFragment instanceof PostFragment) {
((PostFragment) mFragment).refresh();
}
break;
}
case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_CHANGE_SORT_TYPE: {
sortTypeBottomSheetFragment.show(getSupportFragmentManager(), sortTypeBottomSheetFragment.getTag());
break;
}
case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_CHANGE_POST_LAYOUT: {
PostLayoutBottomSheetFragment postLayoutBottomSheetFragment = new PostLayoutBottomSheetFragment();
postLayoutBottomSheetFragment.show(getSupportFragmentManager(), postLayoutBottomSheetFragment.getTag());
break;
}
case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_SEARCH: {
Intent intent = new Intent(this, SearchActivity.class);
startActivity(intent);
break;
}
case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_GO_TO_SUBREDDIT:
goToSubreddit();
break;
case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_GO_TO_USER:
goToUser();
break;
case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_RANDOM:
random();
break;
case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_HIDE_READ_POSTS:
if (mFragment instanceof PostFragment) {
((PostFragment) mFragment).hideReadPosts();
}
break;
case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_FILTER_POSTS:
if (mFragment instanceof PostFragment) {
((PostFragment) mFragment).filterPosts();
}
break;
default:
PostTypeBottomSheetFragment postTypeBottomSheetFragment = new PostTypeBottomSheetFragment();
postTypeBottomSheetFragment.show(getSupportFragmentManager(), postTypeBottomSheetFragment.getTag());
break;
}
});
fab.setOnLongClickListener(view -> {
FABMoreOptionsBottomSheetFragment fabMoreOptionsBottomSheetFragment = new FABMoreOptionsBottomSheetFragment();
Bundle bundle = new Bundle();
bundle.putBoolean(FABMoreOptionsBottomSheetFragment.EXTRA_ANONYMOUS_MODE, mAccessToken == null);
fabMoreOptionsBottomSheetFragment.setArguments(bundle);
fabMoreOptionsBottomSheetFragment.show(getSupportFragmentManager(), fabMoreOptionsBottomSheetFragment.getTag());
return true;
});
}
private void initializeFragment() {
@ -186,6 +324,160 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_view_multi_reddit_detail_activity, mFragment).commit();
}
private void goToSubreddit() {
View rootView = getLayoutInflater().inflate(R.layout.dialog_go_to_thing_edit_text, coordinatorLayout, false);
TextInputEditText thingEditText = rootView.findViewById(R.id.text_input_edit_text_go_to_thing_edit_text);
RecyclerView recyclerView = rootView.findViewById(R.id.recycler_view_go_to_thing_edit_text);
thingEditText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
SubredditAutocompleteRecyclerViewAdapter adapter = new SubredditAutocompleteRecyclerViewAdapter(
this, mCustomThemeWrapper, subredditData -> {
if (imm != null) {
imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0);
}
Intent intent = new Intent(this, ViewSubredditDetailActivity.class);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, subredditData.getName());
startActivity(intent);
});
recyclerView.setAdapter(adapter);
if (imm != null) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
thingEditText.setOnEditorActionListener((textView, i, keyEvent) -> {
if (i == EditorInfo.IME_ACTION_DONE) {
if (imm != null) {
imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0);
}
Intent subredditIntent = new Intent(this, ViewSubredditDetailActivity.class);
subredditIntent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, thingEditText.getText().toString());
startActivity(subredditIntent);
return true;
}
return false;
});
boolean nsfw = mNsfwAndSpoilerSharedPreferences.getBoolean((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.NSFW_BASE, false);
thingEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (subredditAutocompleteCall != null) {
subredditAutocompleteCall.cancel();
}
subredditAutocompleteCall = mOauthRetrofit.create(RedditAPI.class).subredditAutocomplete(APIUtils.getOAuthHeader(mAccessToken),
editable.toString(), nsfw);
subredditAutocompleteCall.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
ParseSubredditData.parseSubredditListingData(response.body(), nsfw, new ParseSubredditData.ParseSubredditListingDataListener() {
@Override
public void onParseSubredditListingDataSuccess(ArrayList<SubredditData> subredditData, String after) {
adapter.setSubreddits(subredditData);
}
@Override
public void onParseSubredditListingDataFail() {
}
});
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
}
});
}
});
new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme)
.setTitle(R.string.go_to_subreddit)
.setView(rootView)
.setPositiveButton(R.string.ok, (dialogInterface, i)
-> {
if (imm != null) {
imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0);
}
Intent subredditIntent = new Intent(this, ViewSubredditDetailActivity.class);
subredditIntent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, thingEditText.getText().toString());
startActivity(subredditIntent);
})
.setNegativeButton(R.string.cancel, (dialogInterface, i) -> {
if (imm != null) {
imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0);
}
})
.setOnDismissListener(dialogInterface -> {
if (imm != null) {
imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0);
}
})
.show();
}
private void goToUser() {
View rootView = getLayoutInflater().inflate(R.layout.dialog_go_to_thing_edit_text, coordinatorLayout, false);
TextInputEditText thingEditText = rootView.findViewById(R.id.text_input_edit_text_go_to_thing_edit_text);
thingEditText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
thingEditText.setOnEditorActionListener((textView, i, keyEvent) -> {
if (i == EditorInfo.IME_ACTION_DONE) {
if (imm != null) {
imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0);
}
Intent userIntent = new Intent(this, ViewUserDetailActivity.class);
userIntent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, thingEditText.getText().toString());
startActivity(userIntent);
return true;
}
return false;
});
new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme)
.setTitle(R.string.go_to_user)
.setView(rootView)
.setPositiveButton(R.string.ok, (dialogInterface, i)
-> {
if (imm != null) {
imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0);
}
Intent userIntent = new Intent(this, ViewUserDetailActivity.class);
userIntent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, thingEditText.getText().toString());
startActivity(userIntent);
})
.setNegativeButton(R.string.cancel, (dialogInterface, i) -> {
if (imm != null) {
imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0);
}
})
.setOnDismissListener(dialogInterface -> {
if (imm != null) {
imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0);
}
})
.show();
}
private void random() {
RandomBottomSheetFragment randomBottomSheetFragment = new RandomBottomSheetFragment();
Bundle bundle = new Bundle();
bundle.putBoolean(RandomBottomSheetFragment.EXTRA_IS_NSFW, !mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_NSFW_FOREVER, false) && mNsfwAndSpoilerSharedPreferences.getBoolean((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.NSFW_BASE, false));
randomBottomSheetFragment.setArguments(bundle);
randomBottomSheetFragment.show(getSupportFragmentManager(), randomBottomSheetFragment.getTag());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.view_multi_reddit_detail_activity, menu);
@ -299,6 +591,7 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT
protected void applyCustomTheme() {
coordinatorLayout.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor());
applyAppBarLayoutAndCollapsingToolbarLayoutAndToolbarTheme(appBarLayout, collapsingToolbarLayout, toolbar);
applyFABTheme(fab);
}
@Override
@ -320,4 +613,91 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT
public void markPostAsRead(Post post) {
InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mExecutor, mAccountName, post.getId());
}
@Override
public void postTypeSelected(int postType) {
Intent intent;
switch (postType) {
case PostTypeBottomSheetFragment.TYPE_TEXT:
intent = new Intent(this, PostTextActivity.class);
startActivity(intent);
break;
case PostTypeBottomSheetFragment.TYPE_LINK:
intent = new Intent(this, PostLinkActivity.class);
startActivity(intent);
break;
case PostTypeBottomSheetFragment.TYPE_IMAGE:
intent = new Intent(this, PostImageActivity.class);
startActivity(intent);
break;
case PostTypeBottomSheetFragment.TYPE_VIDEO:
intent = new Intent(this, PostVideoActivity.class);
startActivity(intent);
break;
case PostTypeBottomSheetFragment.TYPE_GALLERY:
intent = new Intent(this, PostGalleryActivity.class);
startActivity(intent);
break;
case PostTypeBottomSheetFragment.TYPE_POLL:
intent = new Intent(this, PostPollActivity.class);
startActivity(intent);
}
}
@Override
public void fabOptionSelected(int option) {
switch (option) {
case FABMoreOptionsBottomSheetFragment.FAB_OPTION_SUBMIT_POST:
PostTypeBottomSheetFragment postTypeBottomSheetFragment = new PostTypeBottomSheetFragment();
postTypeBottomSheetFragment.show(getSupportFragmentManager(), postTypeBottomSheetFragment.getTag());
break;
case FABMoreOptionsBottomSheetFragment.FAB_OPTION_REFRESH:
if (mFragment instanceof PostFragment) {
((PostFragment) mFragment).refresh();
}
break;
case FABMoreOptionsBottomSheetFragment.FAB_OPTION_CHANGE_SORT_TYPE:
sortTypeBottomSheetFragment.show(getSupportFragmentManager(), sortTypeBottomSheetFragment.getTag());
break;
case FABMoreOptionsBottomSheetFragment.FAB_OPTION_CHANGE_POST_LAYOUT:
PostLayoutBottomSheetFragment postLayoutBottomSheetFragment = new PostLayoutBottomSheetFragment();
postLayoutBottomSheetFragment.show(getSupportFragmentManager(), postLayoutBottomSheetFragment.getTag());
break;
case FABMoreOptionsBottomSheetFragment.FAB_OPTION_SEARCH:
Intent intent = new Intent(this, SearchActivity.class);
startActivity(intent);
break;
case FABMoreOptionsBottomSheetFragment.FAB_OPTION_GO_TO_SUBREDDIT: {
goToSubreddit();
break;
}
case FABMoreOptionsBottomSheetFragment.FAB_OPTION_GO_TO_USER: {
goToUser();
break;
}
case FABMoreOptionsBottomSheetFragment.FAB_RANDOM: {
random();
break;
}
case FABMoreOptionsBottomSheetFragment.FAB_HIDE_READ_POSTS: {
if (mFragment instanceof PostFragment) {
((PostFragment) mFragment).hideReadPosts();
}
break;
}
case FABMoreOptionsBottomSheetFragment.FAB_FILTER_POSTS: {
if (mFragment instanceof PostFragment) {
((PostFragment) mFragment).filterPosts();
}
break;
}
}
}
@Override
public void randomOptionSelected(int option) {
Intent intent = new Intent(this, FetchRandomSubredditOrPostActivity.class);
intent.putExtra(FetchRandomSubredditOrPostActivity.EXTRA_RANDOM_OPTION, option);
startActivity(intent);
}
}

View File

@ -39,4 +39,11 @@
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_view_multi_reddit_detail_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:layout_gravity="bottom|end" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>