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") @Query("DELETE FROM accounts")
void deleteAllAccounts(); void deleteAllAccounts();
@Query("SELECT * FROM accounts WHERE username = :userName COLLATE NOCASE LIMIT 1") @Query("SELECT * FROM accounts WHERE username = :username COLLATE NOCASE LIMIT 1")
LiveData<Account> getAccountLiveData(String userName); LiveData<Account> getAccountLiveData(String username);
@Query("SELECT * FROM accounts WHERE username = :userName COLLATE NOCASE LIMIT 1") @Query("SELECT * FROM accounts WHERE username = :username COLLATE NOCASE LIMIT 1")
Account getAccountData(String userName); Account getAccountData(String username);
@Query("SELECT * FROM accounts WHERE is_current_user = 1 LIMIT 1") @Query("SELECT * FROM accounts WHERE is_current_user = 1 LIMIT 1")
Account getCurrentAccount(); 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(SearchResultActivity searchResultActivity);
void inject(SearchSubredditsResultActivity searchSubredditsResultActivity); void inject(SearchSubredditsResultActivity searchSubredditsResultActivity);
void inject(FollowedUsersListingFragment followedUsersListingFragment); void inject(FollowedUsersListingFragment followedUsersListingFragment);
void inject(SubredditSelectionActivity subredditSelectionActivity);
} }

View File

@ -1,8 +1,6 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
import android.app.Application; import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
@ -66,17 +64,6 @@ class AppModule {
return okHttpClientBuilder.build(); 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 @Provides
@Singleton @Singleton
RedditDataRoomDatabase provideRedditDataRoomDatabase() { RedditDataRoomDatabase provideRedditDataRoomDatabase() {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2,7 +2,6 @@ package ml.docilealligator.infinityforreddit;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
@ -86,10 +85,6 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
private boolean isInLazyMode = false; private boolean isInLazyMode = false;
@Inject
@Named("user_info")
SharedPreferences mUserInfoSharedPreferences;
@Inject @Inject
@Named("oauth") @Named("oauth")
Retrofit mOauthRetrofit; Retrofit mOauthRetrofit;
@ -104,7 +99,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
ButterKnife.bind(this); ButterKnife.bind(this);
((Infinity) getApplication()).getmAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
postTypeBottomSheetFragment = new PostTypeBottomSheetFragment(); postTypeBottomSheetFragment = new PostTypeBottomSheetFragment();
@ -149,13 +144,15 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
private void getCurrentAccountAndBindView() { private void getCurrentAccountAndBindView() {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(account == null) { if(account == null) {
mNullAccessToken = true;
Intent loginIntent = new Intent(this, LoginActivity.class); Intent loginIntent = new Intent(this, LoginActivity.class);
startActivityForResult(loginIntent, LOGIN_ACTIVITY_REQUEST_CODE); startActivityForResult(loginIntent, LOGIN_ACTIVITY_REQUEST_CODE);
} else { } else {
mAccessToken = account.getAccessToken(); mAccessToken = account.getAccessToken();
if(mAccessToken == null) { mName = account.getUsername();
mNullAccessToken = true; mProfileImageUrl = account.getProfileImageUrl();
} mBannerImageUrl = account.getBannerImageUrl();
mKarma = Integer.toString(account.getKarma());
bindView(); bindView();
} }
}).execute(); }).execute();
@ -183,12 +180,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
mProfileImageView = header.findViewById(R.id.profile_image_view_nav_header_main); mProfileImageView = header.findViewById(R.id.profile_image_view_nav_header_main);
mBannerImageView = header.findViewById(R.id.banner_image_view_nav_header_main); mBannerImageView = header.findViewById(R.id.banner_image_view_nav_header_main);
loadUserData(mAccessToken); loadUserData();
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, "");
mNameTextView.setText(mName); mNameTextView.setText(mName);
mKarmaTextView.setText(mKarma); mKarmaTextView.setText(mKarma);
@ -225,12 +217,12 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
}); });
} }
private void loadUserData(String accessToken) { private void loadUserData() {
if (!mFetchUserInfoSuccess) { if (!mFetchUserInfoSuccess) {
FetchMyInfo.fetchMyInfo(mOauthRetrofit, accessToken, new FetchMyInfo.FetchUserMyListener() { FetchMyInfo.fetchMyInfo(mOauthRetrofit, mAccessToken, new FetchMyInfo.FetchUserMyListener() {
@Override @Override
public void onFetchMyInfoSuccess(String response) { public void onFetchMyInfoSuccess(String response) {
ParseMyInfo.parseMyInfo(response, new ParseMyInfo.ParseMyInfoListener() { ParseAndSaveAccountInfo.parseAndSaveAccountInfo(response, mRedditDataRoomDatabase, new ParseAndSaveAccountInfo.ParseAndSaveAccountInfoListener() {
@Override @Override
public void onParseMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) { public void onParseMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) {
mNameTextView.setText(name); mNameTextView.setText(name);
@ -256,12 +248,6 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
mKarmaTextView.setText(mKarma); 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; mFetchUserInfoSuccess = true;
} }
@ -328,7 +314,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
case R.id.action_refresh_main_activity: case R.id.action_refresh_main_activity:
sectionsPagerAdapter.refresh(viewPager.getCurrentItem()); sectionsPagerAdapter.refresh(viewPager.getCurrentItem());
mFetchUserInfoSuccess = false; mFetchUserInfoSuccess = false;
loadUserData(mAccessToken); loadUserData();
return true; return true;
case R.id.action_lazy_mode_main_activity: case R.id.action_lazy_mode_main_activity:
/*MenuItem lazyModeItem = mMenu.findItem(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.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
class ParseMyInfo { class ParseAndSaveAccountInfo {
interface ParseMyInfoListener { interface ParseAndSaveAccountInfoListener {
void onParseMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma); void onParseMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma);
void onParseMyInfoFail(); void onParseMyInfoFail();
} }
static void parseMyInfo(String response, ParseMyInfoListener parseMyInfoListener) { static void parseAndSaveAccountInfo(String response, RedditDataRoomDatabase redditDataRoomDatabase,
new ParseMyInfoAsyncTask(response, parseMyInfoListener).execute(); 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 JSONObject jsonResponse;
private ParseMyInfoListener parseMyInfoListener; private RedditDataRoomDatabase redditDataRoomDatabase;
private ParseAndSaveAccountInfoListener parseAndSaveAccountInfoListener;
private boolean parseFailed; private boolean parseFailed;
private String name; private String name;
@ -27,14 +29,16 @@ class ParseMyInfo {
private String bannerImageUrl; private String bannerImageUrl;
private int karma; private int karma;
ParseMyInfoAsyncTask(String response, ParseMyInfoListener parseMyInfoListener){ ParseAndSaveAccountInfoAsyncTask(String response, RedditDataRoomDatabase redditDataRoomDatabase,
ParseAndSaveAccountInfoListener parseAndSaveAccountInfoListener){
try { try {
jsonResponse = new JSONObject(response); jsonResponse = new JSONObject(response);
this.parseMyInfoListener = parseMyInfoListener; this.redditDataRoomDatabase = redditDataRoomDatabase;
this.parseAndSaveAccountInfoListener = parseAndSaveAccountInfoListener;
parseFailed = false; parseFailed = false;
} catch (JSONException e) { } catch (JSONException e) {
Log.i("user info json error", e.getMessage()); Log.i("user info json error", "message: " + e.getMessage());
parseMyInfoListener.onParseMyInfoFail(); parseAndSaveAccountInfoListener.onParseMyInfoFail();
} }
} }
@ -49,9 +53,11 @@ class ParseMyInfo {
int linkKarma = jsonResponse.getInt(JSONUtils.LINK_KARMA_KEY); int linkKarma = jsonResponse.getInt(JSONUtils.LINK_KARMA_KEY);
int commentKarma = jsonResponse.getInt(JSONUtils.COMMENT_KARMA_KEY); int commentKarma = jsonResponse.getInt(JSONUtils.COMMENT_KARMA_KEY);
karma = linkKarma + commentKarma; karma = linkKarma + commentKarma;
redditDataRoomDatabase.accountDao().updateAccountInfo(name, profileImageUrl, bannerImageUrl, karma);
} catch (JSONException e) { } catch (JSONException e) {
parseFailed = true; parseFailed = true;
Log.i("parse comment error", e.getMessage()); Log.i("parse comment error", "message: " + e.getMessage());
} }
return null; return null;
} }
@ -59,9 +65,9 @@ class ParseMyInfo {
@Override @Override
protected void onPostExecute(Void aVoid) { protected void onPostExecute(Void aVoid) {
if(!parseFailed) { if(!parseFailed) {
parseMyInfoListener.onParseMyInfoSuccess(name, profileImageUrl, bannerImageUrl, karma); parseAndSaveAccountInfoListener.onParseMyInfoSuccess(name, profileImageUrl, bannerImageUrl, karma);
} else { } else {
parseMyInfoListener.onParseMyInfoFail(); parseAndSaveAccountInfoListener.onParseMyInfoFail();
} }
} }
} }

View File

@ -108,7 +108,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
// Inflate the layout for this fragment // Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_post, container, false); 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); 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 FLAIR_STATE = "FS";
private static final String IS_SPOILER_STATE = "ISS"; private static final String IS_SPOILER_STATE = "ISS";
private static final String IS_NSFW_STATE = "INS"; 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 SUBREDDIT_SELECTION_REQUEST_CODE = 0;
private static final int PICK_IMAGE_REQUEST_CODE = 1; 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.select_again_text_view_post_image_activity) TextView selectAgainTextView;
@BindView(R.id.image_view_post_image_activity) ImageView imageView; @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 iconUrl;
private String subredditName; private String subredditName;
private boolean subredditSelected = false; private boolean subredditSelected = false;
@ -108,7 +114,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
Retrofit mUploadMediaRetrofit; Retrofit mUploadMediaRetrofit;
@Inject @Inject
RedditDataRoomDatabase redditDataRoomDatabase; RedditDataRoomDatabase mRedditDataRoomDatabase;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -119,7 +125,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
EventBus.getDefault().register(this); EventBus.getDefault().register(this);
((Infinity) getApplication()).getmAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
@ -127,6 +133,14 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
mGlide = Glide.with(this); mGlide = Glide.with(this);
if(savedInstanceState != null) { 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); 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);
@ -167,6 +181,8 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
nsfwTextView.setBackgroundColor(getResources().getColor(R.color.colorAccent)); nsfwTextView.setBackgroundColor(getResources().getColor(R.color.colorAccent));
} }
} else { } else {
getCurrentAccount();
isPosting = false; isPosting = false;
if(getIntent().hasExtra(EXTRA_SUBREDDIT_NAME)) { if(getIntent().hasExtra(EXTRA_SUBREDDIT_NAME)) {
@ -208,6 +224,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
if(flair == null) { if(flair == null) {
flairSelectionBottomSheetFragment = new FlairBottomSheetFragment(); flairSelectionBottomSheetFragment = new FlairBottomSheetFragment();
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putString(FlairBottomSheetFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(FlairBottomSheetFragment.EXTRA_SUBREDDIT_NAME, subredditName); bundle.putString(FlairBottomSheetFragment.EXTRA_SUBREDDIT_NAME, subredditName);
flairSelectionBottomSheetFragment.setArguments(bundle); flairSelectionBottomSheetFragment.setArguments(bundle);
flairSelectionBottomSheetFragment.show(getSupportFragmentManager(), flairSelectionBottomSheetFragment.getTag()); 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() { private void loadImage() {
constraintLayout.setVisibility(View.GONE); constraintLayout.setVisibility(View.GONE);
imageView.setVisibility(View.VISIBLE); imageView.setVisibility(View.VISIBLE);
@ -292,7 +320,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
} }
private void loadSubredditIcon() { private void loadSubredditIcon() {
new LoadSubredditIconAsyncTask(redditDataRoomDatabase.subredditDao(), new LoadSubredditIconAsyncTask(mRedditDataRoomDatabase.subredditDao(),
subredditName, mRetrofit, iconImageUrl -> { subredditName, mRetrofit, iconImageUrl -> {
iconUrl = iconImageUrl; iconUrl = iconImageUrl;
displaySubredditIcon(); displaySubredditIcon();
@ -344,6 +372,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
Intent intent = new Intent(this, SubmitPostService.class); Intent intent = new Intent(this, SubmitPostService.class);
intent.setData(imageUri); intent.setData(imageUri);
intent.putExtra(SubmitPostService.EXTRA_ACCESS_TOKEN, mAccessToken);
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());
intent.putExtra(SubmitPostService.EXTRA_FLAIR, flair); intent.putExtra(SubmitPostService.EXTRA_FLAIR, flair);
@ -378,6 +407,9 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
outState.putString(FLAIR_STATE, flair); outState.putString(FLAIR_STATE, flair);
outState.putBoolean(IS_SPOILER_STATE, isSpoiler); outState.putBoolean(IS_SPOILER_STATE, isSpoiler);
outState.putBoolean(IS_NSFW_STATE, isNSFW); 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 @Override
@ -434,13 +466,11 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
public void onSubmitImagePostEvent(SubmitImagePostEvent submitImagePostEvent) { public void onSubmitImagePostEvent(SubmitImagePostEvent submitImagePostEvent) {
isPosting = false; isPosting = false;
if(submitImagePostEvent.postSuccess) { if(submitImagePostEvent.postSuccess) {
new GetCurrentAccountAsyncTask(redditDataRoomDatabase.accountDao(), account -> { Intent intent = new Intent(PostImageActivity.this, ViewUserDetailActivity.class);
Intent intent = new Intent(PostImageActivity.this, ViewUserDetailActivity.class); intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY,
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, mAccountName);
account.getUsername()); startActivity(intent);
startActivity(intent); finish();
finish();
}).execute();
} else { } else {
mPostingSnackbar.dismiss(); mPostingSnackbar.dismiss();
mMemu.getItem(R.id.action_send_post_image_activity).setEnabled(true); 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 FLAIR_STATE = "FS";
private static final String IS_SPOILER_STATE = "ISS"; private static final String IS_SPOILER_STATE = "ISS";
private static final String IS_NSFW_STATE = "INS"; 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 ACCOUNT_NAME_STATE = "ANS"; private static final String ACCESS_TOKEN_STATE = "ATS";
private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0; 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_title_edit_text_post_link_activity) EditText titleEditText;
@BindView(R.id.post_link_edit_text_post_link_activity) EditText contentEditText; @BindView(R.id.post_link_edit_text_post_link_activity) EditText contentEditText;
private boolean mNullAccountName = false; private boolean mNullAccessToken = false;
private String mAccountName; private String mAccessToken;
private String iconUrl; private String iconUrl;
private String subredditName; private String subredditName;
private boolean subredditSelected = false; private boolean subredditSelected = false;
@ -100,7 +100,7 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh
EventBus.getDefault().register(this); EventBus.getDefault().register(this);
((Infinity) getApplication()).getmAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
@ -108,6 +108,13 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh
mGlide = Glide.with(this); mGlide = Glide.with(this);
if(savedInstanceState != null) { 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); 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);
@ -143,6 +150,8 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh
nsfwTextView.setBackgroundColor(getResources().getColor(R.color.colorAccent)); nsfwTextView.setBackgroundColor(getResources().getColor(R.color.colorAccent));
} }
} else { } else {
getCurrentAccount();
isPosting = false; isPosting = false;
if(getIntent().hasExtra(EXTRA_SUBREDDIT_NAME)) { if(getIntent().hasExtra(EXTRA_SUBREDDIT_NAME)) {
@ -185,6 +194,7 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh
if(flair == null) { if(flair == null) {
flairSelectionBottomSheetFragment = new FlairBottomSheetFragment(); flairSelectionBottomSheetFragment = new FlairBottomSheetFragment();
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putString(FlairBottomSheetFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(FlairBottomSheetFragment.EXTRA_SUBREDDIT_NAME, subredditName); bundle.putString(FlairBottomSheetFragment.EXTRA_SUBREDDIT_NAME, subredditName);
flairSelectionBottomSheetFragment.setArguments(bundle); flairSelectionBottomSheetFragment.setArguments(bundle);
flairSelectionBottomSheetFragment.show(getSupportFragmentManager(), flairSelectionBottomSheetFragment.getTag()); 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 -> { new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(account == null) { if(account == null) {
mNullAccountName = true; mNullAccessToken = true;
} else { } else {
mAccountName = account.getUsername(); mAccessToken = account.getAccessToken();
} }
}).execute(); }).execute();
} }
@ -287,6 +297,7 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh
} }
Intent intent = new Intent(this, SubmitPostService.class); 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_SUBREDDIT_NAME, subredditName);
intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString()); intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString());
intent.putExtra(SubmitPostService.EXTRA_CONTENT, contentEditText.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.putString(FLAIR_STATE, flair);
outState.putBoolean(IS_SPOILER_STATE, isSpoiler); outState.putBoolean(IS_SPOILER_STATE, isSpoiler);
outState.putBoolean(IS_NSFW_STATE, isNSFW); outState.putBoolean(IS_NSFW_STATE, isNSFW);
outState.putBoolean(NULL_ACCOUNT_NAME_STATE, mNullAccountName); outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);
outState.putString(ACCOUNT_NAME_STATE, mAccountName); outState.putString(ACCESS_TOKEN_STATE, mAccessToken);
} }
@Override @Override

View File

@ -1,7 +1,6 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; 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 FLAIR_STATE = "FS";
private static final String IS_SPOILER_STATE = "ISS"; private static final String IS_SPOILER_STATE = "ISS";
private static final String IS_NSFW_STATE = "INS"; 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 ACCOUNT_NAME_STATE = "ANS"; private static final String ACCESS_TOKEN_STATE = "ATS";
private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0; 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_title_edit_text_post_text_activity) EditText titleEditText;
@BindView(R.id.post_text_content_edit_text_post_text_activity) EditText contentEditText; @BindView(R.id.post_text_content_edit_text_post_text_activity) EditText contentEditText;
private boolean mNullAccountName = false; private boolean mNullAccessToken = false;
private String mAccountName; private String mAccessToken;
private String iconUrl; private String iconUrl;
private String subredditName; private String subredditName;
private boolean subredditSelected = false; private boolean subredditSelected = false;
@ -89,10 +88,6 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh
@Named("oauth") @Named("oauth")
Retrofit mOauthRetrofit; Retrofit mOauthRetrofit;
@Inject
@Named("auth_info")
SharedPreferences sharedPreferences;
@Inject @Inject
RedditDataRoomDatabase mRedditDataRoomDatabase; RedditDataRoomDatabase mRedditDataRoomDatabase;
@ -105,7 +100,7 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh
EventBus.getDefault().register(this); EventBus.getDefault().register(this);
((Infinity) getApplication()).getmAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
@ -191,6 +186,7 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh
if(flair == null) { if(flair == null) {
flairSelectionBottomSheetFragment = new FlairBottomSheetFragment(); flairSelectionBottomSheetFragment = new FlairBottomSheetFragment();
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putString(FlairBottomSheetFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
if(subredditIsUser) { if(subredditIsUser) {
bundle.putString(FlairBottomSheetFragment.EXTRA_SUBREDDIT_NAME, "u_" + subredditName); bundle.putString(FlairBottomSheetFragment.EXTRA_SUBREDDIT_NAME, "u_" + subredditName);
} else { } else {
@ -229,9 +225,9 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh
private void getCurrentAccountName() { private void getCurrentAccountName() {
new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> { new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(account == null) { if(account == null) {
mNullAccountName = true; mNullAccessToken = true;
} else { } else {
mAccountName = account.getUsername(); mAccessToken = account.getAccessToken();
} }
}).execute(); }).execute();
} }
@ -297,6 +293,7 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh
} }
Intent intent = new Intent(this, SubmitPostService.class); 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_SUBREDDIT_NAME, subredditName);
intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString()); intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString());
intent.putExtra(SubmitPostService.EXTRA_CONTENT, contentEditText.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.putString(FLAIR_STATE, flair);
outState.putBoolean(IS_SPOILER_STATE, isSpoiler); outState.putBoolean(IS_SPOILER_STATE, isSpoiler);
outState.putBoolean(IS_NSFW_STATE, isNSFW); outState.putBoolean(IS_NSFW_STATE, isNSFW);
outState.putBoolean(NULL_ACCOUNT_NAME_STATE, mNullAccountName); outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken);
outState.putString(ACCOUNT_NAME_STATE, mAccountName); outState.putString(ACCESS_TOKEN_STATE, mAccessToken);
} }
@Override @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 FLAIR_STATE = "FS";
private static final String IS_SPOILER_STATE = "ISS"; private static final String IS_SPOILER_STATE = "ISS";
private static final String IS_NSFW_STATE = "INS"; 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 String ACCOUNT_NAME_STATE = "ANS";
private static final int SUBREDDIT_SELECTION_REQUEST_CODE = 0; 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.select_again_text_view_post_video_activity) TextView selectAgainTextView;
@BindView(R.id.video_view_post_video_activity) VideoView videoView; @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 mAccountName;
private String iconUrl; private String iconUrl;
private String subredditName; private String subredditName;
@ -122,7 +124,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
EventBus.getDefault().register(this); EventBus.getDefault().register(this);
((Infinity) getApplication()).getmAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
@ -139,11 +141,12 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
flair = savedInstanceState.getString(FLAIR_STATE); flair = savedInstanceState.getString(FLAIR_STATE);
isSpoiler = savedInstanceState.getBoolean(IS_SPOILER_STATE); isSpoiler = savedInstanceState.getBoolean(IS_SPOILER_STATE);
isNSFW = savedInstanceState.getBoolean(IS_NSFW_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); mAccountName = savedInstanceState.getString(ACCOUNT_NAME_STATE);
if(!mNullAccountName && mAccountName == null) { if(!mNullAccessToken && mAccessToken == null) {
getCurrentAccountName(); getCurrentAccount();
} }
if(savedInstanceState.getString(VIDEO_URI_STATE) != null) { 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)); nsfwTextView.setBackgroundColor(getResources().getColor(R.color.colorAccent));
} }
} else { } else {
getCurrentAccountName(); getCurrentAccount();
isPosting = false; isPosting = false;
@ -219,6 +222,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
if(flair == null) { if(flair == null) {
mFlairSelectionBottomSheetFragment = new FlairBottomSheetFragment(); mFlairSelectionBottomSheetFragment = new FlairBottomSheetFragment();
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putString(FlairBottomSheetFragment.EXTRA_ACCESS_TOKEN, mAccessToken);
bundle.putString(FlairBottomSheetFragment.EXTRA_SUBREDDIT_NAME, subredditName); bundle.putString(FlairBottomSheetFragment.EXTRA_SUBREDDIT_NAME, subredditName);
mFlairSelectionBottomSheetFragment.setArguments(bundle); mFlairSelectionBottomSheetFragment.setArguments(bundle);
mFlairSelectionBottomSheetFragment.show(getSupportFragmentManager(), mFlairSelectionBottomSheetFragment.getTag()); 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 -> { new GetCurrentAccountAsyncTask(mRedditDataRoomDatabase.accountDao(), account -> {
if(account == null) { if(account == null) {
mNullAccountName = true; mNullAccessToken = true;
} else { } else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername(); mAccountName = account.getUsername();
} }
}).execute(); }).execute();
@ -362,6 +367,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
Intent intent = new Intent(this, SubmitPostService.class); Intent intent = new Intent(this, SubmitPostService.class);
intent.setData(videoUri); intent.setData(videoUri);
intent.putExtra(SubmitPostService.EXTRA_ACCESS_TOKEN, mAccessToken);
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());
intent.putExtra(SubmitPostService.EXTRA_FLAIR, flair); intent.putExtra(SubmitPostService.EXTRA_FLAIR, flair);
@ -402,7 +408,8 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
outState.putString(FLAIR_STATE, flair); outState.putString(FLAIR_STATE, flair);
outState.putBoolean(IS_SPOILER_STATE, isSpoiler); outState.putBoolean(IS_SPOILER_STATE, isSpoiler);
outState.putBoolean(IS_NSFW_STATE, isNSFW); 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); outState.putString(ACCOUNT_NAME_STATE, mAccountName);
} }

View File

@ -53,7 +53,7 @@ public class RulesActivity extends AppCompatActivity {
ButterKnife.bind(this); ButterKnife.bind(this);
((Infinity) getApplication()).getmAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
ActionBar actionBar = getSupportActionBar(); ActionBar actionBar = getSupportActionBar();
Drawable upArrow = getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp); 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); ButterKnife.bind(this);
((Infinity) getApplication()).getmAppComponent().inject(this); ((Infinity) getApplication()).getAppComponent().inject(this);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);

View File

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

View File

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

View File

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

View File

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

View File

@ -12,6 +12,8 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import javax.inject.Inject;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
@ -23,12 +25,22 @@ public class SubredditSelectionActivity extends AppCompatActivity {
static final String EXTRA_RETURN_SUBREDDIT_IS_USER = "ERSIU"; static final String EXTRA_RETURN_SUBREDDIT_IS_USER = "ERSIU";
private static final int SUBREDDIT_SEARCH_REQUEST_CODE = 0; 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"; private static final String FRAGMENT_OUT_STATE = "FOS";
@BindView(R.id.toolbar_subreddit_selection_activity) Toolbar toolbar; @BindView(R.id.toolbar_subreddit_selection_activity) Toolbar toolbar;
private boolean mNullAccountName = false;
private String mAccountName;
private String mAccountProfileImageUrl;
private Fragment mFragment; private Fragment mFragment;
@Inject
RedditDataRoomDatabase mRedditDataRoomDatabase;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -36,25 +48,53 @@ public class SubredditSelectionActivity extends AppCompatActivity {
ButterKnife.bind(this); ButterKnife.bind(this);
((Infinity) getApplication()).getAppComponent().inject(this);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if(savedInstanceState == null) { if(savedInstanceState == null) {
mFragment = new SubscribedSubredditsListingFragment(); getCurrentAccountAndInitializeFragment();
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();
} else { } else {
mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); mNullAccountName = savedInstanceState.getBoolean(NULL_ACCOUNT_NAME_STATE);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_subreddit_selection_activity, mFragment).commit(); 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 @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.subreddit_selection_activity, menu); getMenuInflater().inflate(R.menu.subreddit_selection_activity, menu);
@ -109,5 +149,8 @@ public class SubredditSelectionActivity extends AppCompatActivity {
if (mFragment != null) { if (mFragment != null) {
getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE, mFragment); 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(); mActivity = getActivity();
((Infinity) mActivity.getApplication()).getmAppComponent().inject(this); ((Infinity) mActivity.getApplication()).getAppComponent().inject(this);
String accountName = getArguments().getString(EXTRA_ACCOUNT_NAME); 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) glide.load(iconUrl)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.error(glide.load(R.drawable.subreddit_default_icon) .error(glide.load(R.drawable.subreddit_default_icon)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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