Submitting crosspost using other accounts is available.

This commit is contained in:
Docile-Alligator 2022-04-28 15:46:25 +08:00
parent d9172110e6
commit 77ed06dac1
2 changed files with 99 additions and 5 deletions

View File

@ -55,7 +55,9 @@ import ml.docilealligator.infinityforreddit.Flair;
import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.account.Account;
import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIcon; import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIcon;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.AccountChooserBottomSheetFragment;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.FlairBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.FlairBottomSheetFragment;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.events.SubmitCrosspostEvent; import ml.docilealligator.infinityforreddit.events.SubmitCrosspostEvent;
@ -67,10 +69,12 @@ import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
import pl.droidsonroids.gif.GifImageView; import pl.droidsonroids.gif.GifImageView;
import retrofit2.Retrofit; import retrofit2.Retrofit;
public class SubmitCrosspostActivity extends BaseActivity implements FlairBottomSheetFragment.FlairSelectionCallback { public class SubmitCrosspostActivity extends BaseActivity implements FlairBottomSheetFragment.FlairSelectionCallback,
AccountChooserBottomSheetFragment.AccountChooserListener {
public static final String EXTRA_POST = "EP"; public static final String EXTRA_POST = "EP";
private static final String SELECTED_ACCOUNT_STATE = "SAS";
private static final String SUBREDDIT_NAME_STATE = "SNS"; private static final String SUBREDDIT_NAME_STATE = "SNS";
private static final String SUBREDDIT_ICON_STATE = "SIS"; private static final String SUBREDDIT_ICON_STATE = "SIS";
private static final String SUBREDDIT_SELECTED_STATE = "SSS"; private static final String SUBREDDIT_SELECTED_STATE = "SSS";
@ -89,6 +93,12 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom
AppBarLayout appBarLayout; AppBarLayout appBarLayout;
@BindView(R.id.toolbar_submit_crosspost_activity) @BindView(R.id.toolbar_submit_crosspost_activity)
Toolbar toolbar; Toolbar toolbar;
@BindView(R.id.account_linear_layout_submit_crosspost_activity)
LinearLayout accountLinearLayout;
@BindView(R.id.account_icon_gif_image_view_submit_crosspost_activity)
GifImageView accountIconImageView;
@BindView(R.id.account_name_text_view_submit_crosspost_activity)
TextView accountNameTextView;
@BindView(R.id.subreddit_icon_gif_image_view_submit_crosspost_activity) @BindView(R.id.subreddit_icon_gif_image_view_submit_crosspost_activity)
GifImageView iconGifImageView; GifImageView iconGifImageView;
@BindView(R.id.subreddit_name_text_view_submit_crosspost_activity) @BindView(R.id.subreddit_name_text_view_submit_crosspost_activity)
@ -143,6 +153,7 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom
CustomThemeWrapper mCustomThemeWrapper; CustomThemeWrapper mCustomThemeWrapper;
@Inject @Inject
Executor mExecutor; Executor mExecutor;
private Account selectedAccount;
private String mAccessToken; private String mAccessToken;
private Post post; private Post post;
private String iconUrl; private String iconUrl;
@ -200,6 +211,7 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom
mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null);
if (savedInstanceState != null) { if (savedInstanceState != null) {
selectedAccount = savedInstanceState.getParcelable(SELECTED_ACCOUNT_STATE);
subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE); subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE);
iconUrl = savedInstanceState.getString(SUBREDDIT_ICON_STATE); iconUrl = savedInstanceState.getString(SUBREDDIT_ICON_STATE);
subredditSelected = savedInstanceState.getBoolean(SUBREDDIT_SELECTED_STATE); subredditSelected = savedInstanceState.getBoolean(SUBREDDIT_SELECTED_STATE);
@ -210,6 +222,18 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom
isSpoiler = savedInstanceState.getBoolean(IS_SPOILER_STATE); isSpoiler = savedInstanceState.getBoolean(IS_SPOILER_STATE);
isNSFW = savedInstanceState.getBoolean(IS_NSFW_STATE); isNSFW = savedInstanceState.getBoolean(IS_NSFW_STATE);
if (selectedAccount != null) {
mGlide.load(selectedAccount.getProfileImageUrl())
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.error(mGlide.load(R.drawable.subreddit_default_icon)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))))
.into(accountIconImageView);
accountNameTextView.setText(selectedAccount.getAccountName());
} else {
loadCurrentAccount();
}
if (subredditName != null) { if (subredditName != null) {
subredditNameTextView.setTextColor(primaryTextColor); subredditNameTextView.setTextColor(primaryTextColor);
subredditNameTextView.setText(subredditName); subredditNameTextView.setText(subredditName);
@ -243,6 +267,8 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom
} else { } else {
isPosting = false; isPosting = false;
loadCurrentAccount();
mGlide.load(R.drawable.subreddit_default_icon) mGlide.load(R.drawable.subreddit_default_icon)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.into(iconGifImageView); .into(iconGifImageView);
@ -293,13 +319,18 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom
} }
} }
accountLinearLayout.setOnClickListener(view -> {
AccountChooserBottomSheetFragment fragment = new AccountChooserBottomSheetFragment();
fragment.show(getSupportFragmentManager(), fragment.getTag());
});
iconGifImageView.setOnClickListener(view -> { iconGifImageView.setOnClickListener(view -> {
Intent intent = new Intent(this, SubredditSelectionActivity.class); subredditNameTextView.performClick();
startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE);
}); });
subredditNameTextView.setOnClickListener(view -> { subredditNameTextView.setOnClickListener(view -> {
Intent intent = new Intent(this, SubredditSelectionActivity.class); Intent intent = new Intent(this, SubredditSelectionActivity.class);
intent.putExtra(SubredditSelectionActivity.EXTRA_SPECIFIED_ACCOUNT, selectedAccount);
startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE); startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE);
}); });
@ -368,6 +399,25 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom
}); });
} }
private void loadCurrentAccount() {
Handler handler = new Handler();
mExecutor.execute(() -> {
Account account = mRedditDataRoomDatabase.accountDao().getCurrentAccount();
selectedAccount = account;
handler.post(() -> {
if (!isFinishing() && !isDestroyed() && account != null) {
mGlide.load(account.getProfileImageUrl())
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.error(mGlide.load(R.drawable.subreddit_default_icon)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))))
.into(accountIconImageView);
accountNameTextView.setText(account.getAccountName());
}
});
});
}
@Nullable @Nullable
private Post.Preview getPreview(Post post) { private Post.Preview getPreview(Post post) {
ArrayList<Post.Preview> previews = post.getPreviews(); ArrayList<Post.Preview> previews = post.getPreviews();
@ -392,11 +442,12 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom
protected void applyCustomTheme() { protected void applyCustomTheme() {
coordinatorLayout.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor()); coordinatorLayout.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor());
applyAppBarLayoutAndCollapsingToolbarLayoutAndToolbarTheme(appBarLayout, null, toolbar); applyAppBarLayoutAndCollapsingToolbarLayoutAndToolbarTheme(appBarLayout, null, toolbar);
primaryTextColor = mCustomThemeWrapper.getPrimaryTextColor();
accountNameTextView.setTextColor(primaryTextColor);
int secondaryTextColor = mCustomThemeWrapper.getSecondaryTextColor(); int secondaryTextColor = mCustomThemeWrapper.getSecondaryTextColor();
subredditNameTextView.setTextColor(secondaryTextColor); subredditNameTextView.setTextColor(secondaryTextColor);
rulesButton.setTextColor(mCustomThemeWrapper.getButtonTextColor()); rulesButton.setTextColor(mCustomThemeWrapper.getButtonTextColor());
rulesButton.setBackgroundColor(mCustomThemeWrapper.getColorPrimaryLightTheme()); rulesButton.setBackgroundColor(mCustomThemeWrapper.getColorPrimaryLightTheme());
primaryTextColor = mCustomThemeWrapper.getPrimaryTextColor();
receivePostReplyNotificationsTextView.setTextColor(primaryTextColor); receivePostReplyNotificationsTextView.setTextColor(primaryTextColor);
int dividerColor = mCustomThemeWrapper.getDividerColor(); int dividerColor = mCustomThemeWrapper.getDividerColor();
divider1.setBackgroundColor(dividerColor); divider1.setBackgroundColor(dividerColor);
@ -518,7 +569,7 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom
} }
Intent intent = new Intent(this, SubmitPostService.class); Intent intent = new Intent(this, SubmitPostService.class);
intent.putExtra(SubmitPostService.EXTRA_ACCOUNT, mAccessToken); intent.putExtra(SubmitPostService.EXTRA_ACCOUNT, selectedAccount);
intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName);
intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString()); intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString());
if (post.isCrosspost()) { if (post.isCrosspost()) {
@ -556,6 +607,7 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom
@Override @Override
protected void onSaveInstanceState(@NonNull Bundle outState) { protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putParcelable(SELECTED_ACCOUNT_STATE, selectedAccount);
outState.putString(SUBREDDIT_NAME_STATE, subredditName); outState.putString(SUBREDDIT_NAME_STATE, subredditName);
outState.putString(SUBREDDIT_ICON_STATE, iconUrl); outState.putString(SUBREDDIT_ICON_STATE, iconUrl);
outState.putBoolean(SUBREDDIT_SELECTED_STATE, subredditSelected); outState.putBoolean(SUBREDDIT_SELECTED_STATE, subredditSelected);
@ -605,6 +657,21 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom
flairTextView.setTextColor(flairTextColor); flairTextView.setTextColor(flairTextColor);
} }
@Override
public void onAccountSelected(Account account) {
if (account != null) {
selectedAccount = account;
mGlide.load(selectedAccount.getProfileImageUrl())
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.error(mGlide.load(R.drawable.subreddit_default_icon)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))))
.into(accountIconImageView);
accountNameTextView.setText(selectedAccount.getAccountName());
}
}
@Subscribe @Subscribe
public void onAccountSwitchEvent(SwitchAccountEvent event) { public void onAccountSwitchEvent(SwitchAccountEvent event) {
finish(); finish();

View File

@ -33,6 +33,33 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical">
<LinearLayout
android:id="@+id/account_linear_layout_submit_crosspost_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground">
<pl.droidsonroids.gif.GifImageView
android:id="@+id/account_icon_gif_image_view_submit_crosspost_activity"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginStart="16dp" />
<TextView
android:id="@+id/account_name_text_view_submit_crosspost_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="32dp"
android:textSize="?attr/font_default"
android:fontFamily="?attr/font_family" />
</LinearLayout>
<RelativeLayout <RelativeLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"