Skip to content

Commit

Permalink
Use the Handler in BaseActivity by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
Docile-Alligator committed Nov 23, 2024
1 parent 948b786 commit 6418bb1
Show file tree
Hide file tree
Showing 65 changed files with 269 additions and 322 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public abstract class BaseActivity extends AppCompatActivity implements CustomFo
public String accessToken;
@NonNull
public String accountName = Account.ANONYMOUS_ACCOUNT;
protected Handler mHandler;
public Handler mHandler;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,17 +329,16 @@ public void onSelectGiphyGif() {
});

binding.commentCommentEditText.requestFocus();
Utils.showKeyboard(this, new Handler(), binding.commentCommentEditText);
Utils.showKeyboard(this, mHandler, binding.commentCommentEditText);

Giphy.INSTANCE.configure(this, APIUtils.GIPHY_GIF_API_KEY);
}

private void loadCurrentAccount() {
Handler handler = new Handler();
mExecutor.execute(() -> {
Account account = mRedditDataRoomDatabase.accountDao().getCurrentAccount();
selectedAccount = account;
handler.post(() -> {
mHandler.post(() -> {
if (!isFinishing() && !isDestroyed() && account != null) {
mGlide.load(account.getProfileImageUrl())
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
Expand Down Expand Up @@ -457,7 +456,7 @@ public void sendComment(@Nullable MenuItem item) {
.connectionPool(new ConnectionPool(0, 1, TimeUnit.NANOSECONDS))
.build())
.build();
SendComment.sendComment(this, mExecutor, new Handler(), binding.commentCommentEditText.getText().toString(),
SendComment.sendComment(this, mExecutor, mHandler, binding.commentCommentEditText.getText().toString(),
parentFullname, parentDepth, uploadedImages, giphyGif, newAuthenticatorOauthRetrofit, selectedAccount,
new SendComment.SendCommentListener() {
@Override
Expand Down Expand Up @@ -516,10 +515,10 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
Toast.makeText(CommentActivity.this, R.string.error_getting_image, Toast.LENGTH_LONG).show();
return;
}
Utils.uploadImageToReddit(this, mExecutor, mOauthRetrofit, mUploadMediaRetrofit,
Utils.uploadImageToReddit(this, mExecutor, mHandler, mOauthRetrofit, mUploadMediaRetrofit,
accessToken, binding.commentCommentEditText, binding.commentCoordinatorLayout, data.getData(), uploadedImages);
} else if (requestCode == CAPTURE_IMAGE_REQUEST_CODE) {
Utils.uploadImageToReddit(this, mExecutor, mOauthRetrofit, mUploadMediaRetrofit,
Utils.uploadImageToReddit(this, mExecutor, mHandler, mOauthRetrofit, mUploadMediaRetrofit,
accessToken, binding.commentCommentEditText, binding.commentCoordinatorLayout, capturedImageUri, uploadedImages);
} else if (requestCode == MARKDOWN_PREVIEW_REQUEST_CODE) {
sendComment(mMenu == null ? null : mMenu.findItem(R.id.action_send_comment_activity));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.os.Handler;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
Expand Down Expand Up @@ -119,7 +118,7 @@ private void editAndCommentFilterUsageNameOfUsage(int type, String nameOfUsage)
textInputEditText.setHint(R.string.settings_tab_subreddit_name);
}

Utils.showKeyboard(this, new Handler(), textInputEditText);
Utils.showKeyboard(this, mHandler, textInputEditText);
new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme)
.setTitle(titleStringId)
.setView(dialogView)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.content.res.ColorStateList;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand Down Expand Up @@ -151,7 +150,7 @@ public void failed(int errorCode) {
}
});
} else {
CreateMultiReddit.anonymousCreateMultiReddit(mExecutor, new Handler(), mRedditDataRoomDatabase,
CreateMultiReddit.anonymousCreateMultiReddit(mExecutor, mHandler, mRedditDataRoomDatabase,
"/user/-/m/" + binding.multiRedditNameEditTextCreateMultiRedditActivity.getText().toString(),
binding.multiRedditNameEditTextCreateMultiRedditActivity.getText().toString(), binding.descriptionEditTextCreateMultiRedditActivity.getText().toString(),
mSubreddits, new CreateMultiReddit.CreateMultiRedditListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
Expand Down Expand Up @@ -208,7 +207,7 @@ public void changeName(String oldThemeName) {
EditText themeNameEditText = dialogView.findViewById(R.id.name_edit_text_edit_name_dialog);
themeNameEditText.setText(oldThemeName);
themeNameEditText.requestFocus();
Utils.showKeyboard(this, new Handler(), themeNameEditText);
Utils.showKeyboard(this, mHandler, themeNameEditText);
new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme)
.setTitle(R.string.edit_theme_name)
.setView(dialogView)
Expand All @@ -227,7 +226,7 @@ public void changeName(String oldThemeName) {

@Override
public void shareTheme(String themeName) {
GetCustomTheme.getCustomTheme(executor, new Handler(), redditDataRoomDatabase, themeName,
GetCustomTheme.getCustomTheme(executor, mHandler, redditDataRoomDatabase, themeName,
customTheme -> {
if (customTheme != null) {
String jsonModel = customTheme.getJSONModel();
Expand All @@ -251,7 +250,7 @@ public void delete(String themeName) {
.setTitle(R.string.delete_theme)
.setMessage(getString(R.string.delete_theme_dialog_message, themeName))
.setPositiveButton(R.string.yes, (dialogInterface, i)
-> DeleteTheme.deleteTheme(executor, new Handler(), redditDataRoomDatabase, themeName, (isLightTheme, isDarkTheme, isAmoledTheme) -> {
-> DeleteTheme.deleteTheme(executor, mHandler, redditDataRoomDatabase, themeName, (isLightTheme, isDarkTheme, isAmoledTheme) -> {
if (isLightTheme) {
CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(
CustomThemeWrapper.getIndigo(CustomThemeListingActivity.this), lightThemeSharedPreferences);
Expand Down Expand Up @@ -364,7 +363,7 @@ public void contentScrollDown() {
}

private void checkDuplicateAndImportTheme(CustomTheme customTheme, boolean checkDuplicate) {
InsertCustomTheme.insertCustomTheme(executor, new Handler(), redditDataRoomDatabase, lightThemeSharedPreferences,
InsertCustomTheme.insertCustomTheme(executor, mHandler, redditDataRoomDatabase, lightThemeSharedPreferences,
darkThemeSharedPreferences, amoledThemeSharedPreferences, customTheme, checkDuplicate,
new InsertCustomTheme.InsertCustomThemeListener() {
@Override
Expand All @@ -384,7 +383,7 @@ public void duplicate() {
EditText themeNameEditText = dialogView.findViewById(R.id.name_edit_text_edit_name_dialog);
themeNameEditText.setText(customTheme.name);
themeNameEditText.requestFocus();
Utils.showKeyboard(CustomThemeListingActivity.this, new Handler(), themeNameEditText);
Utils.showKeyboard(CustomThemeListingActivity.this, mHandler, themeNameEditText);
new MaterialAlertDialogBuilder(CustomThemeListingActivity.this, R.style.MaterialAlertDialogTheme)
.setTitle(R.string.edit_theme_name)
.setView(dialogView)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
}

private void saveCommentFilter(String originalName) {
SaveCommentFilter.saveCommentFilter(mExecutor, new Handler(), mRedditDataRoomDatabase, commentFilter, originalName,
SaveCommentFilter.saveCommentFilter(mExecutor, mHandler, mRedditDataRoomDatabase, commentFilter, originalName,
new SaveCommentFilter.SaveCommentFilterListener() {
@Override
public void success() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
}

private void savePostFilter(String originalName) {
SavePostFilter.savePostFilter(mExecutor, new Handler(), mRedditDataRoomDatabase, postFilter, originalName,
SavePostFilter.savePostFilter(mExecutor, mHandler, mRedditDataRoomDatabase, postFilter, originalName,
new SavePostFilter.SavePostFilterListener() {
@Override
public void success() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.content.res.ColorStateList;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand Down Expand Up @@ -122,7 +121,7 @@ protected void onCreate(Bundle savedInstanceState) {
if (customThemeSettingsItems == null) {
if (getIntent().hasExtra(EXTRA_THEME_TYPE)) {
int themeType = getIntent().getIntExtra(EXTRA_THEME_TYPE, EXTRA_LIGHT_THEME);
GetCustomTheme.getCustomTheme(mExecutor, new Handler(), redditDataRoomDatabase, themeType, customTheme -> {
GetCustomTheme.getCustomTheme(mExecutor, mHandler, redditDataRoomDatabase, themeType, customTheme -> {
if (customTheme == null) {
isPredefinedTheme = true;
switch (themeType) {
Expand Down Expand Up @@ -203,7 +202,7 @@ public void onFailure(@NonNull Call<String> call, @NonNull Throwable throwable)
}
});
} else {
GetCustomTheme.getCustomTheme(mExecutor, new Handler(), redditDataRoomDatabase,
GetCustomTheme.getCustomTheme(mExecutor, mHandler, redditDataRoomDatabase,
themeName, customTheme -> {
customThemeSettingsItems = CustomThemeSettingsItem.convertCustomThemeToSettingsItem(
CustomizeThemeActivity.this, customTheme, androidVersion);
Expand Down Expand Up @@ -311,7 +310,7 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
}

private void saveThemeLocally(CustomTheme customTheme) {
InsertCustomTheme.insertCustomTheme(mExecutor, new Handler(), redditDataRoomDatabase, lightThemeSharedPreferences,
InsertCustomTheme.insertCustomTheme(mExecutor, mHandler, redditDataRoomDatabase, lightThemeSharedPreferences,
darkThemeSharedPreferences, amoledThemeSharedPreferences, customTheme,
false, () -> {
Toast.makeText(CustomizeThemeActivity.this, R.string.theme_saved_locally, Toast.LENGTH_SHORT).show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public void onSelectGiphyGif() {
binding.markdownBottomBarRecyclerViewEditCommentActivity.setAdapter(adapter);

binding.commentEditTextEditCommentActivity.requestFocus();
Utils.showKeyboard(this, new Handler(), binding.commentEditTextEditCommentActivity);
Utils.showKeyboard(this, mHandler, binding.commentEditTextEditCommentActivity);

Giphy.INSTANCE.configure(this, APIUtils.GIPHY_GIF_API_KEY);
}
Expand Down Expand Up @@ -268,14 +268,13 @@ private void editComment() {
params.put(APIUtils.TEXT_KEY, content);
}

Handler handler = new Handler(getMainLooper());
mExecutor.execute(() -> {
try {
Response<String> response = mOauthRetrofit.create(RedditAPI.class)
.editPostOrComment(APIUtils.getOAuthHeader(mAccessToken), params).execute();
if (response.isSuccessful()) {
Comment comment = ParseComment.parseSingleComment(new JSONObject(response.body()), 0);
handler.post(() -> {
mHandler.post(() -> {
isSubmitting = false;
Toast.makeText(EditCommentActivity.this, R.string.edit_success, Toast.LENGTH_SHORT).show();

Expand All @@ -287,20 +286,20 @@ private void editComment() {
finish();
});
} else {
handler.post(() -> {
mHandler.post(() -> {
isSubmitting = false;
Snackbar.make(binding.coordinatorLayoutEditCommentActivity, R.string.post_failed, Snackbar.LENGTH_SHORT).show();
});
}
} catch (IOException e) {
e.printStackTrace();
handler.post(() -> {
mHandler.post(() -> {
isSubmitting = false;
Snackbar.make(binding.coordinatorLayoutEditCommentActivity, R.string.post_failed, Snackbar.LENGTH_SHORT).show();
});
} catch (JSONException e) {
e.printStackTrace();
handler.post(() -> {
mHandler.post(() -> {
isSubmitting = false;
Toast.makeText(EditCommentActivity.this, R.string.edit_success, Toast.LENGTH_SHORT).show();

Expand Down Expand Up @@ -335,10 +334,10 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
Toast.makeText(EditCommentActivity.this, R.string.error_getting_image, Toast.LENGTH_LONG).show();
return;
}
Utils.uploadImageToReddit(this, mExecutor, mOauthRetrofit, mUploadMediaRetrofit,
Utils.uploadImageToReddit(this, mExecutor, mHandler, mOauthRetrofit, mUploadMediaRetrofit,
mAccessToken, binding.commentEditTextEditCommentActivity, binding.coordinatorLayoutEditCommentActivity, data.getData(), uploadedImages);
} else if (requestCode == CAPTURE_IMAGE_REQUEST_CODE) {
Utils.uploadImageToReddit(this, mExecutor, mOauthRetrofit, mUploadMediaRetrofit,
Utils.uploadImageToReddit(this, mExecutor, mHandler, mOauthRetrofit, mUploadMediaRetrofit,
mAccessToken, binding.commentEditTextEditCommentActivity, binding.coordinatorLayoutEditCommentActivity, capturedImageUri, uploadedImages);
} else if (requestCode == MARKDOWN_PREVIEW_REQUEST_CODE) {
editComment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.content.res.ColorStateList;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand Down Expand Up @@ -98,7 +97,7 @@ protected void onCreate(Bundle savedInstanceState) {
private void bindView() {
if (multiReddit == null) {
if (accountName.equals(Account.ANONYMOUS_ACCOUNT)) {
FetchMultiRedditInfo.anonymousFetchMultiRedditInfo(mExecutor, new Handler(),
FetchMultiRedditInfo.anonymousFetchMultiRedditInfo(mExecutor, mHandler,
mRedditDataRoomDatabase, multipath, new FetchMultiRedditInfo.FetchMultiRedditInfoListener() {
@Override
public void success(MultiReddit multiReddit) {
Expand Down Expand Up @@ -172,7 +171,7 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
multiReddit.setDisplayName(name);
multiReddit.setName(name);
multiReddit.setDescription(binding.descriptionEditTextEditMultiRedditActivity.getText().toString());
EditMultiReddit.anonymousEditMultiReddit(mExecutor, new Handler(), mRedditDataRoomDatabase,
EditMultiReddit.anonymousEditMultiReddit(mExecutor, mHandler, mRedditDataRoomDatabase,
multiReddit, new EditMultiReddit.EditMultiRedditListener() {
@Override
public void success() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public void onUploadImage() {
binding.markdownBottomBarRecyclerViewEditPostActivity.setAdapter(adapter);

binding.postContentEditTextEditPostActivity.requestFocus();
Utils.showKeyboard(this, new Handler(), binding.postContentEditTextEditPostActivity);
Utils.showKeyboard(this, mHandler, binding.postContentEditTextEditPostActivity);
}

@Override
Expand Down Expand Up @@ -250,10 +250,10 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
Toast.makeText(EditPostActivity.this, R.string.error_getting_image, Toast.LENGTH_LONG).show();
return;
}
Utils.uploadImageToReddit(this, mExecutor, mOauthRetrofit, mUploadMediaRetrofit,
Utils.uploadImageToReddit(this, mExecutor, mHandler, mOauthRetrofit, mUploadMediaRetrofit,
mAccessToken, binding.postContentEditTextEditPostActivity, binding.coordinatorLayoutEditPostActivity, data.getData(), uploadedImages);
} else if (requestCode == CAPTURE_IMAGE_REQUEST_CODE) {
Utils.uploadImageToReddit(this, mExecutor, mOauthRetrofit, mUploadMediaRetrofit,
Utils.uploadImageToReddit(this, mExecutor, mHandler, mOauthRetrofit, mUploadMediaRetrofit,
mAccessToken, binding.postContentEditTextEditPostActivity, binding.coordinatorLayoutEditPostActivity, capturedImageUri, uploadedImages);
} else if (requestCode == MARKDOWN_PREVIEW_REQUEST_CODE) {
editPost();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

import java.util.concurrent.Executor;
Expand Down Expand Up @@ -49,7 +48,7 @@ protected void onCreate(Bundle savedInstanceState) {

int option = getIntent().getIntExtra(EXTRA_RANDOM_OPTION, RandomBottomSheetFragment.RANDOM_SUBREDDIT);

FetchPost.fetchRandomPost(mExecutor, new Handler(), mRetrofit,
FetchPost.fetchRandomPost(mExecutor, mHandler, mRetrofit,
option == RandomBottomSheetFragment.RANDOM_NSFW_SUBREDDIT
|| option == RandomBottomSheetFragment.RANDOM_NSFW_POST, new FetchPost.FetchRandomPostListener() {
@Override
Expand Down
Loading

0 comments on commit 6418bb1

Please sign in to comment.