Refactored some of the untouched classes last time to support multi user. Bugs fixed related to subreddit selection in PostXXXActivity.

This commit is contained in:
Alex Ning 2019-08-08 11:14:18 +08:00
parent b5c9e98ec9
commit 1c8ba320bc
33 changed files with 299 additions and 211 deletions

View File

@ -22,12 +22,16 @@ public interface AccountDao {
@Query("DELETE FROM accounts")
void deleteAllAccounts();
@Query("SELECT * FROM accounts WHERE username = :userName COLLATE NOCASE LIMIT 1")
LiveData<Account> getAccountLiveData(String userName);
@Query("SELECT * FROM accounts WHERE username = :username COLLATE NOCASE LIMIT 1")
LiveData<Account> getAccountLiveData(String username);
@Query("SELECT * FROM accounts WHERE username = :userName COLLATE NOCASE LIMIT 1")
Account getAccountData(String userName);
@Query("SELECT * FROM accounts WHERE username = :username COLLATE NOCASE LIMIT 1")
Account getAccountData(String username);
@Query("SELECT * FROM accounts WHERE is_current_user = 1 LIMIT 1")
Account getCurrentAccount();
@Query("UPDATE accounts SET profile_image_url = :profileImageUrl, banner_image_url = :bannerImageUrl, " +
"karma = :karma WHERE username = :username")
void updateAccountInfo(String username, String profileImageUrl, String bannerImageUrl, int karma);
}

View File

@ -30,4 +30,5 @@ interface AppComponent {
void inject(SearchResultActivity searchResultActivity);
void inject(SearchSubredditsResultActivity searchSubredditsResultActivity);
void inject(FollowedUsersListingFragment followedUsersListingFragment);
void inject(SubredditSelectionActivity subredditSelectionActivity);
}

View File

@ -1,8 +1,6 @@
package ml.docilealligator.infinityforreddit;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import javax.inject.Named;
import javax.inject.Singleton;
@ -66,17 +64,6 @@ class AppModule {
return okHttpClientBuilder.build();
}
@Provides @Named("auth_info")
@Singleton
SharedPreferences provideAuthInfoSharedPreferences() {
return mApplication.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE);
}
@Provides @Named("user_info")
SharedPreferences provideUserInfoSharedPreferences() {
return mApplication.getSharedPreferences(SharedPreferencesUtils.USER_INFO_FILE_KEY, Context.MODE_PRIVATE);
}
@Provides
@Singleton
RedditDataRoomDatabase provideRedditDataRoomDatabase() {

View File

@ -1,7 +1,6 @@
package ml.docilealligator.infinityforreddit;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
@ -32,11 +31,16 @@ public class CommentActivity extends AppCompatActivity {
static final String EXTRA_IS_REPLYING_KEY = "EIRK";
static final int WRITE_COMMENT_REQUEST_CODE = 1;
private static final String NULL_ACCESS_TOKEN_STATE = "NATS";
private static final String ACCESS_TOKEN_STATE = "ATS";
@BindView(R.id.coordinator_layout_comment_activity) CoordinatorLayout coordinatorLayout;
@BindView(R.id.toolbar_comment_activity) Toolbar toolbar;
@BindView(R.id.comment_parent_markwon_view_comment_activity) MarkwonView commentParentMarkwonView;
@BindView(R.id.comment_edit_text_comment_activity) EditText commentEditText;
private boolean mNullAccessToken = false;
private String mAccessToken;
private String parentFullname;
private int parentDepth;
private int parentPosition;
@ -47,8 +51,7 @@ public class CommentActivity extends AppCompatActivity {
Retrofit mOauthRetrofit;
@Inject
@Named("auth_info")
SharedPreferences sharedPreferences;
RedditDataRoomDatabase mRedditDataRoomDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -57,7 +60,17 @@ public class CommentActivity extends AppCompatActivity {
ButterKnife.bind(this);
((Infinity) getApplication()).getmAppComponent().inject(this);
((Infinity) getApplication()).getAppComponent().inject(this);
if(savedInstanceState == null) {
getCurrentAccount();
} else {
mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE);
mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE);
if(!mNullAccessToken && mAccessToken == null) {
getCurrentAccount();
}
}
Intent intent = getIntent();
commentParentMarkwonView.setMarkdown(intent.getExtras().getString(EXTRA_COMMENT_PARENT_TEXT_KEY));
@ -72,6 +85,16 @@ public class CommentActivity extends AppCompatActivity {
setSupportActionBar(toolbar);
}
private void getCurrentAccount() {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(account == null) {
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
}
}).execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.comment_activity, menu);
@ -92,7 +115,7 @@ public class CommentActivity extends AppCompatActivity {
SendComment.sendComment(commentEditText.getText().toString(), parentFullname, parentDepth,
getResources().getConfiguration().locale, mOauthRetrofit,
sharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""),
mAccessToken,
new SendComment.SendCommentListener() {
@Override
public void sendCommentSuccess(CommentData commentData) {
@ -126,4 +149,11 @@ public class CommentActivity extends AppCompatActivity {
return false;
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);
outState.putString(ACCESS_TOKEN_STATE, mAccessToken);
}
}

View File

@ -70,7 +70,7 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_comments_listing, container, false);
((Infinity) activity.getApplication()).getmAppComponent().inject(this);
((Infinity) activity.getApplication()).getAppComponent().inject(this);
ButterKnife.bind(this, rootView);

View File

@ -1,6 +1,5 @@
package ml.docilealligator.infinityforreddit;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import androidx.annotation.NonNull;
@ -21,10 +20,9 @@ class FetchFlairsInSubreddit {
void fetchFailed();
}
static void fetchFlairs(Retrofit oauthRetrofit, SharedPreferences authInfoSharedPreferences, String subredditName, FetchFlairsInSubredditListener fetchFlairsInSubredditListener) {
static void fetchFlairs(Retrofit oauthRetrofit, String accessToken, String subredditName, FetchFlairsInSubredditListener fetchFlairsInSubredditListener) {
RedditAPI api = oauthRetrofit.create(RedditAPI.class);
String accessToken = authInfoSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
Call<String> flairsCall = api.getFlairs(RedditUtils.getOAuthHeader(accessToken), subredditName);
flairsCall.enqueue(new Callback<String>() {
@Override

View File

@ -51,7 +51,7 @@ public class FilteredPostsActivity extends AppCompatActivity implements SortType
ButterKnife.bind(this);
((Infinity) getApplication()).getmAppComponent().inject(this);
((Infinity) getApplication()).getAppComponent().inject(this);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

View File

@ -2,7 +2,6 @@ package ml.docilealligator.infinityforreddit;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
@ -35,19 +34,19 @@ public class FlairBottomSheetFragment extends BottomSheetDialogFragment {
void flairSelected(String flair);
}
static final String EXTRA_ACCESS_TOKEN = "EAT";
static final String EXTRA_SUBREDDIT_NAME = "ESN";
@BindView(R.id.progress_bar_flair_bottom_sheet_fragment) ProgressBar progressBar;
@BindView(R.id.error_text_view_flair_bottom_sheet_fragment) TextView errorTextView;
@BindView(R.id.recycler_view_bottom_sheet_fragment) RecyclerView recyclerView;
private String mAccessToken;
private String mSubredditName;
private Activity mAcitivity;
private FlairBottomSheetRecyclerViewAdapter mAdapter;
@Inject
@Named("auth_info")
SharedPreferences mAuthInfoSharedPreferences;
@Inject
@Named("oauth")
Retrofit mOauthRetrofit;
@ -66,21 +65,23 @@ public class FlairBottomSheetFragment extends BottomSheetDialogFragment {
mAcitivity = getActivity();
((Infinity) mAcitivity.getApplication()).getmAppComponent().inject(this);
((Infinity) mAcitivity.getApplication()).getAppComponent().inject(this);
mAdapter = new FlairBottomSheetRecyclerViewAdapter(flair -> ((FlairSelectionCallback) mAcitivity).flairSelected(flair));
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(mAdapter);
String subredditName = getArguments().getString(EXTRA_SUBREDDIT_NAME);
fetchFlairs(subredditName);
mAccessToken = getArguments().getString(EXTRA_ACCESS_TOKEN);
mSubredditName = getArguments().getString(EXTRA_SUBREDDIT_NAME);
fetchFlairs();
return rootView;
}
private void fetchFlairs(String subredditName) {
FetchFlairsInSubreddit.fetchFlairs(mOauthRetrofit, mAuthInfoSharedPreferences,
subredditName, new FetchFlairsInSubreddit.FetchFlairsInSubredditListener() {
private void fetchFlairs() {
FetchFlairsInSubreddit.fetchFlairs(mOauthRetrofit, mAccessToken,
mSubredditName, new FetchFlairsInSubreddit.FetchFlairsInSubredditListener() {
@Override
public void fetchSuccessful(ArrayList<String> flairs) {
progressBar.setVisibility(View.GONE);
@ -98,7 +99,7 @@ public class FlairBottomSheetFragment extends BottomSheetDialogFragment {
progressBar.setVisibility(View.GONE);
errorTextView.setVisibility(View.VISIBLE);
errorTextView.setText(R.string.error_loading_flairs);
errorTextView.setOnClickListener(view -> fetchFlairs(subredditName));
errorTextView.setOnClickListener(view -> fetchFlairs());
}
});
}

View File

@ -57,7 +57,7 @@ public class FollowedUsersListingFragment extends Fragment {
mActivity = getActivity();
((Infinity) mActivity.getApplication()).getmAppComponent().inject(this);
((Infinity) mActivity.getApplication()).getAppComponent().inject(this);
mGlide = Glide.with(this);

View File

@ -34,7 +34,7 @@ public class Infinity extends Application {
});
}
public AppComponent getmAppComponent() {
public AppComponent getAppComponent() {
return mAppComponent;
}
}

View File

@ -47,7 +47,7 @@ public class LoginActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
((Infinity) getApplication()).getmAppComponent().inject(this);
((Infinity) getApplication()).getAppComponent().inject(this);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
@ -100,7 +100,7 @@ public class LoginActivity extends AppCompatActivity {
FetchMyInfo.fetchMyInfo(mOauthRetrofit, accessToken, new FetchMyInfo.FetchUserMyListener() {
@Override
public void onFetchMyInfoSuccess(String response) {
ParseMyInfo.parseMyInfo(response, new ParseMyInfo.ParseMyInfoListener() {
ParseAndSaveAccountInfo.parseAndSaveAccountInfo(response, mRedditDataRoomDatabase, new ParseAndSaveAccountInfo.ParseAndSaveAccountInfoListener() {
@Override
public void onParseMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) {
new ParseAndInsertNewAccountAsyncTask(name, accessToken, refreshToken, profileImageUrl, bannerImageUrl,

View File

@ -2,7 +2,6 @@ package ml.docilealligator.infinityforreddit;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
@ -86,10 +85,6 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
private boolean isInLazyMode = false;
@Inject
@Named("user_info")
SharedPreferences mUserInfoSharedPreferences;
@Inject
@Named("oauth")
Retrofit mOauthRetrofit;
@ -104,7 +99,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
ButterKnife.bind(this);
((Infinity) getApplication()).getmAppComponent().inject(this);
((Infinity) getApplication()).getAppComponent().inject(this);
postTypeBottomSheetFragment = new PostTypeBottomSheetFragment();
@ -149,13 +144,15 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
private void getCurrentAccountAndBindView() {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(account == null) {
mNullAccessToken = true;
Intent loginIntent = new Intent(this, LoginActivity.class);
startActivityForResult(loginIntent, LOGIN_ACTIVITY_REQUEST_CODE);
} else {
mAccessToken = account.getAccessToken();
if(mAccessToken == null) {
mNullAccessToken = true;
}
mName = account.getUsername();
mProfileImageUrl = account.getProfileImageUrl();
mBannerImageUrl = account.getBannerImageUrl();
mKarma = Integer.toString(account.getKarma());
bindView();
}
}).execute();
@ -183,12 +180,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
mProfileImageView = header.findViewById(R.id.profile_image_view_nav_header_main);
mBannerImageView = header.findViewById(R.id.banner_image_view_nav_header_main);
loadUserData(mAccessToken);
mName = mUserInfoSharedPreferences.getString(SharedPreferencesUtils.USER_KEY, "");
mProfileImageUrl = mUserInfoSharedPreferences.getString(SharedPreferencesUtils.PROFILE_IMAGE_URL_KEY, "");
mBannerImageUrl = mUserInfoSharedPreferences.getString(SharedPreferencesUtils.BANNER_IMAGE_URL_KEY, "");
mKarma = mUserInfoSharedPreferences.getString(SharedPreferencesUtils.KARMA_KEY, "");
loadUserData();
mNameTextView.setText(mName);
mKarmaTextView.setText(mKarma);
@ -225,12 +217,12 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
});
}
private void loadUserData(String accessToken) {
private void loadUserData() {
if (!mFetchUserInfoSuccess) {
FetchMyInfo.fetchMyInfo(mOauthRetrofit, accessToken, new FetchMyInfo.FetchUserMyListener() {
FetchMyInfo.fetchMyInfo(mOauthRetrofit, mAccessToken, new FetchMyInfo.FetchUserMyListener() {
@Override
public void onFetchMyInfoSuccess(String response) {
ParseMyInfo.parseMyInfo(response, new ParseMyInfo.ParseMyInfoListener() {
ParseAndSaveAccountInfo.parseAndSaveAccountInfo(response, mRedditDataRoomDatabase, new ParseAndSaveAccountInfo.ParseAndSaveAccountInfoListener() {
@Override
public void onParseMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) {
mNameTextView.setText(name);
@ -256,12 +248,6 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
mKarmaTextView.setText(mKarma);
SharedPreferences.Editor editor = mUserInfoSharedPreferences.edit();
editor.putString(SharedPreferencesUtils.USER_KEY, name);
editor.putString(SharedPreferencesUtils.PROFILE_IMAGE_URL_KEY, profileImageUrl);
editor.putString(SharedPreferencesUtils.BANNER_IMAGE_URL_KEY, bannerImageUrl);
editor.putString(SharedPreferencesUtils.KARMA_KEY, mKarma);
editor.apply();
mFetchUserInfoSuccess = true;
}
@ -328,7 +314,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
case R.id.action_refresh_main_activity:
sectionsPagerAdapter.refresh(viewPager.getCurrentItem());
mFetchUserInfoSuccess = false;
loadUserData(mAccessToken);
loadUserData();
return true;
case R.id.action_lazy_mode_main_activity:
/*MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_main_activity);

View File

@ -7,19 +7,21 @@ import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
class ParseMyInfo {
interface ParseMyInfoListener {
class ParseAndSaveAccountInfo {
interface ParseAndSaveAccountInfoListener {
void onParseMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma);
void onParseMyInfoFail();
}
static void parseMyInfo(String response, ParseMyInfoListener parseMyInfoListener) {
new ParseMyInfoAsyncTask(response, parseMyInfoListener).execute();
static void parseAndSaveAccountInfo(String response, RedditDataRoomDatabase redditDataRoomDatabase,
ParseAndSaveAccountInfoListener parseAndSaveAccountInfoListener) {
new ParseAndSaveAccountInfoAsyncTask(response, redditDataRoomDatabase, parseAndSaveAccountInfoListener).execute();
}
private static class ParseMyInfoAsyncTask extends AsyncTask<Void, Void, Void> {
private static class ParseAndSaveAccountInfoAsyncTask extends AsyncTask<Void, Void, Void> {
private JSONObject jsonResponse;
private ParseMyInfoListener parseMyInfoListener;
private RedditDataRoomDatabase redditDataRoomDatabase;
private ParseAndSaveAccountInfoListener parseAndSaveAccountInfoListener;
private boolean parseFailed;
private String name;
@ -27,14 +29,16 @@ class ParseMyInfo {
private String bannerImageUrl;
private int karma;
ParseMyInfoAsyncTask(String response, ParseMyInfoListener parseMyInfoListener){
ParseAndSaveAccountInfoAsyncTask(String response, RedditDataRoomDatabase redditDataRoomDatabase,
ParseAndSaveAccountInfoListener parseAndSaveAccountInfoListener){
try {
jsonResponse = new JSONObject(response);
this.parseMyInfoListener = parseMyInfoListener;
this.redditDataRoomDatabase = redditDataRoomDatabase;
this.parseAndSaveAccountInfoListener = parseAndSaveAccountInfoListener;
parseFailed = false;
} catch (JSONException e) {
Log.i("user info json error", e.getMessage());
parseMyInfoListener.onParseMyInfoFail();
Log.i("user info json error", "message: " + e.getMessage());
parseAndSaveAccountInfoListener.onParseMyInfoFail();
}
}
@ -49,9 +53,11 @@ class ParseMyInfo {
int linkKarma = jsonResponse.getInt(JSONUtils.LINK_KARMA_KEY);
int commentKarma = jsonResponse.getInt(JSONUtils.COMMENT_KARMA_KEY);
karma = linkKarma + commentKarma;
redditDataRoomDatabase.accountDao().updateAccountInfo(name, profileImageUrl, bannerImageUrl, karma);
} catch (JSONException e) {
parseFailed = true;
Log.i("parse comment error", e.getMessage());
Log.i("parse comment error", "message: " + e.getMessage());
}
return null;
}
@ -59,9 +65,9 @@ class ParseMyInfo {
@Override
protected void onPostExecute(Void aVoid) {
if(!parseFailed) {
parseMyInfoListener.onParseMyInfoSuccess(name, profileImageUrl, bannerImageUrl, karma);
parseAndSaveAccountInfoListener.onParseMyInfoSuccess(name, profileImageUrl, bannerImageUrl, karma);
} else {
parseMyInfoListener.onParseMyInfoFail();
parseAndSaveAccountInfoListener.onParseMyInfoFail();
}
}
}

View File

@ -108,7 +108,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_post, container, false);
((Infinity) activity.getApplication()).getmAppComponent().inject(this);
((Infinity) activity.getApplication()).getAppComponent().inject(this);
ButterKnife.bind(this, rootView);

View File

@ -58,6 +58,9 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
private static final String FLAIR_STATE = "FS";
private static final String IS_SPOILER_STATE = "ISS";
private static final String IS_NSFW_STATE = "INS";
private static final String NULL_ACCESS_TOKEN_STATE = "NATS";
private static final String ACCESS_TOKEN_STATE = "ATS";
private static final String ACCOUNT_NAME_STATE = "ANS";
private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0;
private static final int PICK_IMAGE_REQUEST_CODE = 1;
@ -78,6 +81,9 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
@BindView(R.id.select_again_text_view_post_image_activity) TextView selectAgainTextView;
@BindView(R.id.image_view_post_image_activity) ImageView imageView;
private boolean mNullAccessToken = false;
private String mAccessToken;
private String mAccountName;
private String iconUrl;
private String subredditName;
private boolean subredditSelected = false;
@ -108,7 +114,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
Retrofit mUploadMediaRetrofit;
@Inject
RedditDataRoomDatabase redditDataRoomDatabase;
RedditDataRoomDatabase mRedditDataRoomDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -119,7 +125,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
EventBus.getDefault().register(this);
((Infinity) getApplication()).getmAppComponent().inject(this);
((Infinity) getApplication()).getAppComponent().inject(this);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
@ -127,6 +133,14 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
mGlide = Glide.with(this);
if(savedInstanceState != null) {
mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE);
mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE);
mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE);
if(!mNullAccessToken && mAccessToken == null) {
getCurrentAccount();
}
subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE);
iconUrl = savedInstanceState.getString(SUBREDDIT_ICON_STATE);
subredditSelected = savedInstanceState.getBoolean(SUBREDDIT_SELECTED_STATE);
@ -167,6 +181,8 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
nsfwTextView.setBackgroundColor(getResources().getColor(R.color.colorAccent));
}
} else {
getCurrentAccount();
isPosting = false;
if(getIntent().hasExtra(EXTRA_SUBREDDIT_NAME)) {
@ -208,6 +224,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
if(flair == null) {
flairSelectionBottomSheetFragment = new FlairBottomSheetFragment();
Bundle bundle = new Bundle();
bundle.putString(FlairBottomSheetFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(FlairBottomSheetFragment.EXTRA_SUBREDDIT_NAME, subredditName);
flairSelectionBottomSheetFragment.setArguments(bundle);
flairSelectionBottomSheetFragment.show(getSupportFragmentManager(), flairSelectionBottomSheetFragment.getTag());
@ -270,6 +287,17 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
});
}
private void getCurrentAccount() {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(account == null) {
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
}
}).execute();
}
private void loadImage() {
constraintLayout.setVisibility(View.GONE);
imageView.setVisibility(View.VISIBLE);
@ -292,7 +320,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
}
private void loadSubredditIcon() {
new LoadSubredditIconAsyncTask(redditDataRoomDatabase.subredditDao(),
new LoadSubredditIconAsyncTask(mRedditDataRoomDatabase.subredditDao(),
subredditName, mRetrofit, iconImageUrl -> {
iconUrl = iconImageUrl;
displaySubredditIcon();
@ -344,6 +372,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
Intent intent = new Intent(this, SubmitPostService.class);
intent.setData(imageUri);
intent.putExtra(SubmitPostService.EXTRA_ACCESS_TOKEN, mAccessToken);
intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName);
intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString());
intent.putExtra(SubmitPostService.EXTRA_FLAIR, flair);
@ -378,6 +407,9 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
outState.putString(FLAIR_STATE, flair);
outState.putBoolean(IS_SPOILER_STATE, isSpoiler);
outState.putBoolean(IS_NSFW_STATE, isNSFW);
outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);
outState.putString(ACCESS_TOKEN_STATE, mAccessToken);
outState.putString(ACCOUNT_NAME_STATE, mAccountName);
}
@Override
@ -434,13 +466,11 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
public void onSubmitImagePostEvent(SubmitImagePostEvent submitImagePostEvent) {
isPosting = false;
if(submitImagePostEvent.postSuccess) {
new GetCurrentAccountAsyncTask(redditDataRoomDatabase.accountDao(), account -> {
Intent intent = new Intent(PostImageActivity.this, ViewUserDetailActivity.class);
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY,
account.getUsername());
startActivity(intent);
finish();
}).execute();
Intent intent = new Intent(PostImageActivity.this, ViewUserDetailActivity.class);
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY,
mAccountName);
startActivity(intent);
finish();
} else {
mPostingSnackbar.dismiss();
mMemu.getItem(R.id.action_send_post_image_activity).setEnabled(true);

View File

@ -46,8 +46,8 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh
private static final String FLAIR_STATE = "FS";
private static final String IS_SPOILER_STATE = "ISS";
private static final String IS_NSFW_STATE = "INS";
private static final String NULL_ACCOUNT_NAME_STATE = "NATS";
private static final String ACCOUNT_NAME_STATE = "ANS";
private static final String NULL_ACCESS_TOKEN_STATE = "NATS";
private static final String ACCESS_TOKEN_STATE = "ATS";
private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0;
@ -62,8 +62,8 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh
@BindView(R.id.post_title_edit_text_post_link_activity) EditText titleEditText;
@BindView(R.id.post_link_edit_text_post_link_activity) EditText contentEditText;
private boolean mNullAccountName = false;
private String mAccountName;
private boolean mNullAccessToken = false;
private String mAccessToken;
private String iconUrl;
private String subredditName;
private boolean subredditSelected = false;
@ -100,7 +100,7 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh
EventBus.getDefault().register(this);
((Infinity) getApplication()).getmAppComponent().inject(this);
((Infinity) getApplication()).getAppComponent().inject(this);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
@ -108,6 +108,13 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh
mGlide = Glide.with(this);
if(savedInstanceState != null) {
mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE);
mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE);
if(!mNullAccessToken && mAccessToken == null) {
getCurrentAccount();
}
subredditName = savedInstanceState.getString(SUBREDDIT_NAME_STATE);
iconUrl = savedInstanceState.getString(SUBREDDIT_ICON_STATE);
subredditSelected = savedInstanceState.getBoolean(SUBREDDIT_SELECTED_STATE);
@ -143,6 +150,8 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh
nsfwTextView.setBackgroundColor(getResources().getColor(R.color.colorAccent));
}
} else {
getCurrentAccount();
isPosting = false;
if(getIntent().hasExtra(EXTRA_SUBREDDIT_NAME)) {
@ -185,6 +194,7 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh
if(flair == null) {
flairSelectionBottomSheetFragment = new FlairBottomSheetFragment();
Bundle bundle = new Bundle();
bundle.putString(FlairBottomSheetFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(FlairBottomSheetFragment.EXTRA_SUBREDDIT_NAME, subredditName);
flairSelectionBottomSheetFragment.setArguments(bundle);
flairSelectionBottomSheetFragment.show(getSupportFragmentManager(), flairSelectionBottomSheetFragment.getTag());
@ -216,12 +226,12 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh
});
}
private void getCurrentAccountName() {
private void getCurrentAccount() {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(account == null) {
mNullAccountName = true;
mNullAccessToken = true;
} else {
mAccountName = account.getUsername();
mAccessToken = account.getAccessToken();
}
}).execute();
}
@ -287,6 +297,7 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh
}
Intent intent = new Intent(this, SubmitPostService.class);
intent.putExtra(SubmitPostService.EXTRA_ACCESS_TOKEN, mAccessToken);
intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName);
intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString());
intent.putExtra(SubmitPostService.EXTRA_CONTENT, contentEditText.getText().toString());
@ -315,8 +326,8 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh
outState.putString(FLAIR_STATE, flair);
outState.putBoolean(IS_SPOILER_STATE, isSpoiler);
outState.putBoolean(IS_NSFW_STATE, isNSFW);
outState.putBoolean(NULL_ACCOUNT_NAME_STATE, mNullAccountName);
outState.putString(ACCOUNT_NAME_STATE, mAccountName);
outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);
outState.putString(ACCESS_TOKEN_STATE, mAccessToken);
}
@Override

View File

@ -1,7 +1,6 @@
package ml.docilealligator.infinityforreddit;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
@ -47,8 +46,8 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh
private static final String FLAIR_STATE = "FS";
private static final String IS_SPOILER_STATE = "ISS";
private static final String IS_NSFW_STATE = "INS";
private static final String NULL_ACCOUNT_NAME_STATE = "NATS";
private static final String ACCOUNT_NAME_STATE = "ANS";
private static final String NULL_ACCESS_TOKEN_STATE = "NATS";
private static final String ACCESS_TOKEN_STATE = "ATS";
private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0;
@ -63,8 +62,8 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh
@BindView(R.id.post_title_edit_text_post_text_activity) EditText titleEditText;
@BindView(R.id.post_text_content_edit_text_post_text_activity) EditText contentEditText;
private boolean mNullAccountName = false;
private String mAccountName;
private boolean mNullAccessToken = false;
private String mAccessToken;
private String iconUrl;
private String subredditName;
private boolean subredditSelected = false;
@ -89,10 +88,6 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh
@Named("oauth")
Retrofit mOauthRetrofit;
@Inject
@Named("auth_info")
SharedPreferences sharedPreferences;
@Inject
RedditDataRoomDatabase mRedditDataRoomDatabase;
@ -105,7 +100,7 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh
EventBus.getDefault().register(this);
((Infinity) getApplication()).getmAppComponent().inject(this);
((Infinity) getApplication()).getAppComponent().inject(this);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
@ -191,6 +186,7 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh
if(flair == null) {
flairSelectionBottomSheetFragment = new FlairBottomSheetFragment();
Bundle bundle = new Bundle();
bundle.putString(FlairBottomSheetFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
if(subredditIsUser) {
bundle.putString(FlairBottomSheetFragment.EXTRA_SUBREDDIT_NAME, "u_" + subredditName);
} else {
@ -229,9 +225,9 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh
private void getCurrentAccountName() {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(account == null) {
mNullAccountName = true;
mNullAccessToken = true;
} else {
mAccountName = account.getUsername();
mAccessToken = account.getAccessToken();
}
}).execute();
}
@ -297,6 +293,7 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh
}
Intent intent = new Intent(this, SubmitPostService.class);
intent.putExtra(SubmitPostService.EXTRA_ACCESS_TOKEN, mAccessToken);
intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName);
intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString());
intent.putExtra(SubmitPostService.EXTRA_CONTENT, contentEditText.getText().toString());
@ -325,8 +322,8 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh
outState.putString(FLAIR_STATE, flair);
outState.putBoolean(IS_SPOILER_STATE, isSpoiler);
outState.putBoolean(IS_NSFW_STATE, isNSFW);
outState.putBoolean(NULL_ACCOUNT_NAME_STATE, mNullAccountName);
outState.putString(ACCOUNT_NAME_STATE, mAccountName);
outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);
outState.putString(ACCESS_TOKEN_STATE, mAccessToken);
}
@Override

View File

@ -53,7 +53,8 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
private static final String FLAIR_STATE = "FS";
private static final String IS_SPOILER_STATE = "ISS";
private static final String IS_NSFW_STATE = "INS";
private static final String NULL_ACCOUNT_NAME_STATE = "NATS";
private static final String NULL_ACCESS_TOKEN_STATE = "NATS";
private static final String ACCESS_TOKEN_STATE = "ATS";
private static final String ACCOUNT_NAME_STATE = "ANS";
private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0;
@ -75,7 +76,8 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
@BindView(R.id.select_again_text_view_post_video_activity) TextView selectAgainTextView;
@BindView(R.id.video_view_post_video_activity) VideoView videoView;
private boolean mNullAccountName = false;
private boolean mNullAccessToken = false;
private String mAccessToken;
private String mAccountName;
private String iconUrl;
private String subredditName;
@ -122,7 +124,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
EventBus.getDefault().register(this);
((Infinity) getApplication()).getmAppComponent().inject(this);
((Infinity) getApplication()).getAppComponent().inject(this);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
@ -139,11 +141,12 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
flair = savedInstanceState.getString(FLAIR_STATE);
isSpoiler = savedInstanceState.getBoolean(IS_SPOILER_STATE);
isNSFW = savedInstanceState.getBoolean(IS_NSFW_STATE);
mNullAccountName = savedInstanceState.getBoolean(NULL_ACCOUNT_NAME_STATE);
mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE);
mAccessToken = savedInstanceState.getString(ACCESS_TOKEN_STATE);
mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE);
if(!mNullAccountName && mAccountName == null) {
getCurrentAccountName();
if(!mNullAccessToken && mAccessToken == null) {
getCurrentAccount();
}
if(savedInstanceState.getString(VIDEO_URI_STATE) != null) {
@ -176,7 +179,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
nsfwTextView.setBackgroundColor(getResources().getColor(R.color.colorAccent));
}
} else {
getCurrentAccountName();
getCurrentAccount();
isPosting = false;
@ -219,6 +222,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
if(flair == null) {
mFlairSelectionBottomSheetFragment = new FlairBottomSheetFragment();
Bundle bundle = new Bundle();
bundle.putString(FlairBottomSheetFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(FlairBottomSheetFragment.EXTRA_SUBREDDIT_NAME, subredditName);
mFlairSelectionBottomSheetFragment.setArguments(bundle);
mFlairSelectionBottomSheetFragment.show(getSupportFragmentManager(), mFlairSelectionBottomSheetFragment.getTag());
@ -277,11 +281,12 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
});
}
private void getCurrentAccountName() {
private void getCurrentAccount() {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(account == null) {
mNullAccountName = true;
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
}
}).execute();
@ -362,6 +367,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
Intent intent = new Intent(this, SubmitPostService.class);
intent.setData(videoUri);
intent.putExtra(SubmitPostService.EXTRA_ACCESS_TOKEN, mAccessToken);
intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName);
intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString());
intent.putExtra(SubmitPostService.EXTRA_FLAIR, flair);
@ -402,7 +408,8 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
outState.putString(FLAIR_STATE, flair);
outState.putBoolean(IS_SPOILER_STATE, isSpoiler);
outState.putBoolean(IS_NSFW_STATE, isNSFW);
outState.putBoolean(NULL_ACCOUNT_NAME_STATE, mNullAccountName);
outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);
outState.putString(ACCESS_TOKEN_STATE, mAccessToken);
outState.putString(ACCOUNT_NAME_STATE, mAccountName);
}

View File

@ -53,7 +53,7 @@ public class RulesActivity extends AppCompatActivity {
ButterKnife.bind(this);
((Infinity) getApplication()).getmAppComponent().inject(this);
((Infinity) getApplication()).getAppComponent().inject(this);
ActionBar actionBar = getSupportActionBar();
Drawable upArrow = getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp);

View File

@ -55,7 +55,7 @@ public class SearchResultActivity extends AppCompatActivity implements SearchPos
ButterKnife.bind(this);
((Infinity) getApplication()).getmAppComponent().inject(this);
((Infinity) getApplication()).getAppComponent().inject(this);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

View File

@ -44,7 +44,7 @@ public class SearchSubredditsResultActivity extends AppCompatActivity {
ButterKnife.bind(this);
((Infinity) getApplication()).getmAppComponent().inject(this);
((Infinity) getApplication()).getAppComponent().inject(this);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

View File

@ -1,6 +1,5 @@
package ml.docilealligator.infinityforreddit;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;
@ -41,23 +40,23 @@ class SubmitPost {
void uploadFailed(@Nullable String errorMessage);
}
static void submitTextOrLinkPost(Retrofit oauthRetrofit, SharedPreferences authInfoSharedPreferences,
static void submitTextOrLinkPost(Retrofit oauthRetrofit, String accessToken,
Locale locale, String subredditName, String title, String content,
String flair, boolean isSpoiler, boolean isNSFW, String kind,
SubmitPostListener submitPostListener) {
submitPost(oauthRetrofit, authInfoSharedPreferences, locale, subredditName, title, content,
submitPost(oauthRetrofit, accessToken, locale, subredditName, title, content,
flair, isSpoiler, isNSFW, kind, null, submitPostListener);
}
static void submitImagePost(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
SharedPreferences authInfoSharedPreferences, Locale locale,
String accessToken, Locale locale,
String subredditName, String title, Bitmap image, String flair,
boolean isSpoiler, boolean isNSFW, SubmitPostListener submitPostListener) {
uploadImage(oauthRetrofit, uploadMediaRetrofit, authInfoSharedPreferences, image,
uploadImage(oauthRetrofit, uploadMediaRetrofit, accessToken, image,
new UploadImageListener() {
@Override
public void uploaded(String imageUrl) {
submitPost(oauthRetrofit, authInfoSharedPreferences, locale,
submitPost(oauthRetrofit, accessToken, locale,
subredditName, title, imageUrl, flair, isSpoiler, isNSFW,
RedditUtils.KIND_IMAGE, null, submitPostListener);
}
@ -70,12 +69,11 @@ class SubmitPost {
}
static void submitVideoPost(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
Retrofit uploadVideoRetrofit, SharedPreferences authInfoSharedPreferences,
Retrofit uploadVideoRetrofit, String accessToken,
Locale locale, String subredditName, String title, byte[] buffer, String mimeType,
Bitmap posterBitmap, String flair, boolean isSpoiler, boolean isNSFW,
SubmitPostListener submitPostListener) {
RedditAPI api = oauthRetrofit.create(RedditAPI.class);
String accessToken = authInfoSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
String fileType = mimeType.substring(mimeType.indexOf("/") + 1);
@ -110,16 +108,16 @@ class SubmitPost {
new ParseXMLReponseFromAWSAsyncTask(response.body(), new ParseXMLReponseFromAWSAsyncTask.ParseXMLResponseFromAWSListener() {
@Override
public void parseSuccessful(String url) {
uploadImage(oauthRetrofit, uploadMediaRetrofit, authInfoSharedPreferences,
uploadImage(oauthRetrofit, uploadMediaRetrofit, accessToken,
posterBitmap, new UploadImageListener() {
@Override
public void uploaded(String imageUrl) {
if(fileType.equals("gif")) {
submitPost(oauthRetrofit, authInfoSharedPreferences, locale,
submitPost(oauthRetrofit, accessToken, locale,
subredditName, title, url, flair, isSpoiler, isNSFW,
RedditUtils.KIND_VIDEOGIF, imageUrl, submitPostListener);
} else {
submitPost(oauthRetrofit, authInfoSharedPreferences, locale,
submitPost(oauthRetrofit, accessToken, locale,
subredditName, title, url, flair, isSpoiler, isNSFW,
RedditUtils.KIND_VIDEO, imageUrl, submitPostListener);
}
@ -167,12 +165,11 @@ class SubmitPost {
});
}
private static void submitPost(Retrofit oauthRetrofit, SharedPreferences authInfoSharedPreferences,
private static void submitPost(Retrofit oauthRetrofit, String accessToken,
Locale locale, String subredditName, String title, String content,
String flair, boolean isSpoiler, boolean isNSFW, String kind,
@Nullable String posterUrl, SubmitPostListener submitPostListener) {
RedditAPI api = oauthRetrofit.create(RedditAPI.class);
String accessToken = authInfoSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
Map<String, String> params = new HashMap<>();
params.put(RedditUtils.API_TYPE_KEY, RedditUtils.API_TYPE_JSON);
@ -211,7 +208,7 @@ class SubmitPost {
Log.i("code", "asfd" + response.body());
if(response.isSuccessful()) {
try {
getSubmittedPost(response.body(), kind, oauthRetrofit, authInfoSharedPreferences,
getSubmittedPost(response.body(), kind, oauthRetrofit, accessToken,
locale, submitPostListener);
} catch (JSONException e) {
e.printStackTrace();
@ -232,10 +229,9 @@ class SubmitPost {
}
private static void uploadImage(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
SharedPreferences authInfoSharedPreferences, Bitmap image,
String accessToken, Bitmap image,
UploadImageListener uploadImageListener) {
RedditAPI api = oauthRetrofit.create(RedditAPI.class);
String accessToken = authInfoSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
Map<String, String> uploadImageParams = new HashMap<>();
uploadImageParams.put(RedditUtils.FILEPATH_KEY, "post_image.jpg");
@ -414,7 +410,7 @@ class SubmitPost {
}
private static void getSubmittedPost(String response, String kind, Retrofit oauthRetrofit,
SharedPreferences authInfoSharedPreferences, Locale locale,
String accessToken, Locale locale,
SubmitPostListener submitPostListener) throws JSONException {
JSONObject responseObject = new JSONObject(response).getJSONObject(JSONUtils.JSON_KEY);
if(responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() != 0) {
@ -442,7 +438,6 @@ class SubmitPost {
String postId = responseObject.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.ID_KEY);
RedditAPI api = oauthRetrofit.create(RedditAPI.class);
String accessToken = authInfoSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
Call<String> getPostCall = api.getPost(postId, RedditUtils.getOAuthHeader(accessToken));
getPostCall.enqueue(new Callback<String>() {

View File

@ -5,7 +5,6 @@ import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
@ -33,6 +32,7 @@ import javax.inject.Named;
import retrofit2.Retrofit;
public class SubmitPostService extends Service {
static final String EXTRA_ACCESS_TOKEN = "EAT";
static final String EXTRA_SUBREDDIT_NAME = "ESN";
static final String EXTRA_TITLE = "ET";
static final String EXTRA_CONTENT = "EC";
@ -45,6 +45,16 @@ public class SubmitPostService extends Service {
static final int EXTRA_POST_TYPE_IMAGE = 1;
static final int EXTRA_POST_TYPE_VIDEO = 2;
private String mAccessToken;
private String subredditName;
private String title;
private String flair;
private boolean isSpoiler;
private boolean isNSFW;
private String content;
private String kind;
private Uri mediaUri;
@Inject
@Named("oauth")
Retrofit mOauthRetrofit;
@ -57,14 +67,6 @@ public class SubmitPostService extends Service {
@Named("upload_video")
Retrofit mUploadVideoRetrofit;
@Inject
@Named("user_info")
SharedPreferences mUserInfoSharedPreferences;
@Inject
@Named("auth_info")
SharedPreferences sharedPreferences;
public SubmitPostService() {
}
@ -75,13 +77,14 @@ public class SubmitPostService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
((Infinity) getApplication()).getmAppComponent().inject(this);
((Infinity) getApplication()).getAppComponent().inject(this);
String subredditName = intent.getExtras().getString(EXTRA_SUBREDDIT_NAME);
String title = intent.getExtras().getString(EXTRA_TITLE);
String flair = intent.getExtras().getString(EXTRA_FLAIR);
boolean isSpoiler = intent.getExtras().getBoolean(EXTRA_IS_SPOILER);
boolean isNSFW = intent.getExtras().getBoolean(EXTRA_IS_NSFW);
mAccessToken = intent.getExtras().getString(EXTRA_ACCESS_TOKEN);
subredditName = intent.getExtras().getString(EXTRA_SUBREDDIT_NAME);
title = intent.getExtras().getString(EXTRA_TITLE);
flair = intent.getExtras().getString(EXTRA_FLAIR);
isSpoiler = intent.getExtras().getBoolean(EXTRA_IS_SPOILER);
isNSFW = intent.getExtras().getBoolean(EXTRA_IS_NSFW);
int postType = intent.getExtras().getInt(EXTRA_POST_TYPE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@ -96,18 +99,18 @@ public class SubmitPostService extends Service {
}
if(postType == EXTRA_POST_TEXT_OR_LINK) {
String content = intent.getExtras().getString(EXTRA_CONTENT);
String kind = intent.getExtras().getString(EXTRA_KIND);
content = intent.getExtras().getString(EXTRA_CONTENT);
kind = intent.getExtras().getString(EXTRA_KIND);
startForeground(1, createNotification(R.string.posting));
submitTextOrLinkPost(subredditName, title, content, flair, isSpoiler, isNSFW, kind);
submitTextOrLinkPost();
} else if(postType == EXTRA_POST_TYPE_IMAGE) {
Uri imageUri = intent.getData();
mediaUri = intent.getData();
startForeground(1, createNotification(R.string.posting_image));
submitImagePost(imageUri, subredditName, title, flair, isSpoiler, isNSFW);
submitImagePost();
} else {
Uri videoUri = intent.getData();
mediaUri = intent.getData();
startForeground(1, createNotification(R.string.posting_video));
submitVideoPost(videoUri, subredditName, title, flair, isSpoiler, isNSFW);
submitVideoPost();
}
return START_NOT_STICKY;
@ -121,9 +124,8 @@ public class SubmitPostService extends Service {
.build();
}
private void submitTextOrLinkPost(String subredditName, String title, String content, String flair,
boolean isSpoiler, boolean isNSFW, String kind) {
SubmitPost.submitTextOrLinkPost(mOauthRetrofit, sharedPreferences, getResources().getConfiguration().locale,
private void submitTextOrLinkPost() {
SubmitPost.submitTextOrLinkPost(mOauthRetrofit, mAccessToken, getResources().getConfiguration().locale,
subredditName, title, content, flair, isSpoiler, isNSFW, kind, new SubmitPost.SubmitPostListener() {
@Override
public void submitSuccessful(Post post) {
@ -143,16 +145,15 @@ public class SubmitPostService extends Service {
});
}
private void submitImagePost(Uri imageUri, String subredditName, String title, String flair,
boolean isSpoiler, boolean isNSFW) {
private void submitImagePost() {
Glide.with(this)
.asBitmap()
.load(imageUri)
.load(mediaUri)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
SubmitPost.submitImagePost(mOauthRetrofit, mUploadMediaRetrofit, sharedPreferences,
SubmitPost.submitImagePost(mOauthRetrofit, mUploadMediaRetrofit, mAccessToken,
getResources().getConfiguration().locale, subredditName, title, resource,
flair, isSpoiler, isNSFW, new SubmitPost.SubmitPostListener() {
@Override
@ -181,9 +182,8 @@ public class SubmitPostService extends Service {
});
}
private void submitVideoPost(Uri videoUri, String subredditName, String title, String flair,
boolean isSpoiler, boolean isNSFW) {
try (ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(videoUri, "r")) {
private void submitVideoPost() {
try (ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(mediaUri, "r")) {
FileInputStream in = new FileInputStream(pfd.getFileDescriptor());
byte[] buffer;
buffer = new byte[in.available()];
@ -191,13 +191,13 @@ public class SubmitPostService extends Service {
Glide.with(this)
.asBitmap()
.load(videoUri)
.load(mediaUri)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
SubmitPost.submitVideoPost(mOauthRetrofit, mUploadMediaRetrofit, mUploadVideoRetrofit,
sharedPreferences, getResources().getConfiguration().locale, subredditName, title,
buffer, getContentResolver().getType(videoUri), resource, flair, isSpoiler, isNSFW,
mAccessToken, getResources().getConfiguration().locale, subredditName, title,
buffer, getContentResolver().getType(mediaUri), resource, flair, isSpoiler, isNSFW,
new SubmitPost.SubmitPostListener() {
@Override
public void submitSuccessful(Post post) {

View File

@ -3,7 +3,6 @@ package ml.docilealligator.infinityforreddit;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
@ -53,9 +52,6 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
SubredditListingViewModel mSubredditListingViewModel;
@Inject @Named("auth_info")
SharedPreferences mAuthInfoSharedPreferences;
@Inject @Named("no_oauth")
Retrofit mRetrofit;
@ -78,7 +74,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
Activity activity = getActivity();
((Infinity) activity.getApplication()).getmAppComponent().inject(this);
((Infinity) activity.getApplication()).getAppComponent().inject(this);
ButterKnife.bind(this, rootView);

View File

@ -12,6 +12,8 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import javax.inject.Inject;
import butterknife.BindView;
import butterknife.ButterKnife;
@ -23,12 +25,22 @@ public class SubredditSelectionActivity extends AppCompatActivity {
static final String EXTRA_RETURN_SUBREDDIT_IS_USER = "ERSIU";
private static final int SUBREDDIT_SEARCH_REQUEST_CODE = 0;
private static final String NULL_ACCOUNT_NAME_STATE = "NATS";
private static final String ACCOUNT_NAME_STATE = "ATS";
private static final String ACCOUNT_PROFILE_IMAGE_URL = "APIU";
private static final String FRAGMENT_OUT_STATE = "FOS";
@BindView(R.id.toolbar_subreddit_selection_activity) Toolbar toolbar;
private boolean mNullAccountName = false;
private String mAccountName;
private String mAccountProfileImageUrl;
private Fragment mFragment;
@Inject
RedditDataRoomDatabase mRedditDataRoomDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -36,25 +48,53 @@ public class SubredditSelectionActivity extends AppCompatActivity {
ButterKnife.bind(this);
((Infinity) getApplication()).getAppComponent().inject(this);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if(savedInstanceState == null) {
mFragment = new SubscribedSubredditsListingFragment();
Bundle bundle = new Bundle();
bundle.putBoolean(SubscribedSubredditsListingFragment.EXTRA_IS_SUBREDDIT_SELECTION, true);
if(getIntent().hasExtra(EXTRA_EXTRA_CLEAR_SELECTION)) {
bundle.putBoolean(SubscribedSubredditsListingFragment.EXTRA_EXTRA_CLEAR_SELECTION,
getIntent().getExtras().getBoolean(EXTRA_EXTRA_CLEAR_SELECTION));
}
mFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_subreddit_selection_activity, mFragment).commit();
getCurrentAccountAndInitializeFragment();
} else {
mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_subreddit_selection_activity, mFragment).commit();
mNullAccountName = savedInstanceState.getBoolean(NULL_ACCOUNT_NAME_STATE);
mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE);
mAccountProfileImageUrl = savedInstanceState.getString(ACCOUNT_PROFILE_IMAGE_URL);
if(!mNullAccountName && mAccountName == null) {
getCurrentAccountAndInitializeFragment();
} else {
mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_subreddit_selection_activity, mFragment).commit();
}
}
}
private void getCurrentAccountAndInitializeFragment() {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(account == null) {
mNullAccountName = true;
} else {
mAccountName = account.getUsername();
mAccountProfileImageUrl = account.getProfileImageUrl();
}
initializeFragment();
}).execute();
}
private void initializeFragment() {
mFragment = new SubscribedSubredditsListingFragment();
Bundle bundle = new Bundle();
bundle.putString(SubscribedSubredditsListingFragment.EXTRA_ACCOUNT_NAME, mAccountName);
bundle.putString(SubscribedSubredditsListingFragment.EXTRA_ACCOUNT_PROFILE_IMAGE_URL, mAccountProfileImageUrl);
bundle.putBoolean(SubscribedSubredditsListingFragment.EXTRA_IS_SUBREDDIT_SELECTION, true);
if(getIntent().hasExtra(EXTRA_EXTRA_CLEAR_SELECTION)) {
bundle.putBoolean(SubscribedSubredditsListingFragment.EXTRA_EXTRA_CLEAR_SELECTION,
getIntent().getExtras().getBoolean(EXTRA_EXTRA_CLEAR_SELECTION));
}
mFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_subreddit_selection_activity, mFragment).commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.subreddit_selection_activity, menu);
@ -109,5 +149,8 @@ public class SubredditSelectionActivity extends AppCompatActivity {
if (mFragment != null) {
getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment);
}
outState.putBoolean(NULL_ACCOUNT_NAME_STATE, mNullAccountName);
outState.putString(ACCOUNT_NAME_STATE, mAccountName);
outState.putString(ACCOUNT_PROFILE_IMAGE_URL, mAccountProfileImageUrl);
}
}

View File

@ -61,7 +61,7 @@ public class SubscribedSubredditsListingFragment extends Fragment {
mActivity = getActivity();
((Infinity) mActivity.getApplication()).getmAppComponent().inject(this);
((Infinity) mActivity.getApplication()).getAppComponent().inject(this);
String accountName = getArguments().getString(EXTRA_ACCOUNT_NAME);

View File

@ -94,7 +94,7 @@ class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapter<Recyc
});
}
if(!iconUrl.equals("")) {
if(iconUrl != null && !iconUrl.equals("")) {
glide.load(iconUrl)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.error(glide.load(R.drawable.subreddit_default_icon)

View File

@ -59,7 +59,7 @@ public class SubscribedThingListingActivity extends AppCompatActivity {
ButterKnife.bind(this);
((Infinity) getApplication()).getmAppComponent().inject(this);
((Infinity) getApplication()).getAppComponent().inject(this);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

View File

@ -1,7 +1,6 @@
package ml.docilealligator.infinityforreddit;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
@ -51,10 +50,6 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato
UserListingViewModel mUserListingViewModel;
@Inject
@Named("auth_info")
SharedPreferences mAuthInfoSharedPreferences;
@Inject @Named("no_oauth")
Retrofit mRetrofit;
@ -75,7 +70,7 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_user_listing, container, false);
((Infinity) getActivity().getApplication()).getmAppComponent().inject(this);
((Infinity) getActivity().getApplication()).getAppComponent().inject(this);
ButterKnife.bind(this, rootView);

View File

@ -111,7 +111,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
EventBus.getDefault().register(this);
((Infinity) getApplication()).getmAppComponent().inject(this);
((Infinity) getApplication()).getAppComponent().inject(this);
toolbar.setTitle("");
setSupportActionBar(toolbar);

View File

@ -97,7 +97,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So
ButterKnife.bind(this);
((Infinity) getApplication()).getmAppComponent().inject(this);
((Infinity) getApplication()).getAppComponent().inject(this);
if(savedInstanceState == null) {
getCurrentAccountAndBindView();

View File

@ -100,7 +100,7 @@ public class ViewUserDetailActivity extends AppCompatActivity {
ButterKnife.bind(this);
((Infinity) getApplication()).getmAppComponent().inject(this);
((Infinity) getApplication()).getAppComponent().inject(this);
if(savedInstanceState == null) {
getCurrentAccountAndInitializeViewPager();

View File

@ -27,7 +27,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center" />
android:gravity="center"
android:text="@string/no_subreddits"/>
</LinearLayout>