mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-10-06 05:49:49 +02:00
Fixed getting same notifications for different accounts (for real this time). Add a settings acivity but with no use now. Minor bugs fixed. Minor UI tweaks.
This commit is contained in:
18
app/src/main/java/Settings/MainPreferenceFragment.java
Normal file
18
app/src/main/java/Settings/MainPreferenceFragment.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package Settings;
|
||||
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.preference.PreferenceFragmentCompat;
|
||||
|
||||
import ml.docilealligator.infinityforreddit.R;
|
||||
|
||||
/**
|
||||
* A simple {@link PreferenceFragmentCompat} subclass.
|
||||
*/
|
||||
public class MainPreferenceFragment extends PreferenceFragmentCompat {
|
||||
@Override
|
||||
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
||||
setPreferencesFromResource(R.xml.main_preferences, rootKey);
|
||||
}
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
package Settings;
|
||||
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.preference.PreferenceFragmentCompat;
|
||||
|
||||
import ml.docilealligator.infinityforreddit.R;
|
||||
|
||||
/**
|
||||
* A simple {@link Fragment} subclass.
|
||||
*/
|
||||
public class NotificationPreferenceFragment extends PreferenceFragmentCompat {
|
||||
|
||||
@Override
|
||||
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
||||
setPreferencesFromResource(R.xml.notification_preferences, rootKey);
|
||||
}
|
||||
}
|
@@ -31,10 +31,9 @@ class AppModule {
|
||||
|
||||
@Provides @Named("oauth_without_authenticator")
|
||||
@Singleton
|
||||
Retrofit provideOauthWithoutAuthenticatorRetrofit(OkHttpClient okHttpClient) {
|
||||
Retrofit provideOauthWithoutAuthenticatorRetrofit() {
|
||||
return new Retrofit.Builder()
|
||||
.baseUrl(RedditUtils.OAUTH_API_BASE_URI)
|
||||
.client(okHttpClient)
|
||||
.addConverterFactory(ScalarsConverterFactory.create())
|
||||
.build();
|
||||
}
|
||||
|
@@ -559,6 +559,8 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
|
||||
|
||||
settingsLinearLayout.setOnClickListener(view -> {
|
||||
drawer.closeDrawers();
|
||||
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
|
||||
startActivity(intent);
|
||||
});
|
||||
}
|
||||
|
||||
|
@@ -60,7 +60,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
|
||||
|
||||
private static final String IS_IN_LAZY_MODE_STATE = "IILMS";
|
||||
|
||||
@BindView(R.id.coordinator_layout_post_fragment) CoordinatorLayout mCoordinatorLayout;
|
||||
@BindView(R.id.recycler_view_post_fragment) RecyclerView mPostRecyclerView;
|
||||
@BindView(R.id.progress_bar_post_fragment) CircleProgressBar mProgressBar;
|
||||
@BindView(R.id.fetch_post_info_linear_layout_post_fragment) LinearLayout mFetchPostInfoLinearLayout;
|
||||
|
@@ -496,8 +496,8 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
|
||||
startActivity(intent);
|
||||
finish();
|
||||
} else {
|
||||
mMemu.getItem(R.id.action_send_post_image_activity).setEnabled(true);
|
||||
mMemu.getItem(R.id.action_send_post_image_activity).getIcon().setAlpha(255);
|
||||
mMemu.findItem(R.id.action_send_post_image_activity).setEnabled(true);
|
||||
mMemu.findItem(R.id.action_send_post_image_activity).getIcon().setAlpha(255);
|
||||
if (submitImagePostEvent.errorMessage == null || submitImagePostEvent.errorMessage.equals("")) {
|
||||
Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show();
|
||||
} else {
|
||||
|
@@ -489,6 +489,9 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
|
||||
public void onSubmitVideoPostEvent(SubmitVideoPostEvent submitVideoPostEvent) {
|
||||
isPosting = false;
|
||||
mPostingSnackbar.dismiss();
|
||||
mMemu.findItem(R.id.action_send_post_video_activity).setEnabled(true);
|
||||
mMemu.findItem(R.id.action_send_post_video_activity).getIcon().setAlpha(255);
|
||||
|
||||
if(submitVideoPostEvent.postSuccess) {
|
||||
Intent intent = new Intent(this, ViewUserDetailActivity.class);
|
||||
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY,
|
||||
@@ -498,8 +501,6 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
|
||||
} else if(submitVideoPostEvent.errorProcessingVideo) {
|
||||
Snackbar.make(coordinatorLayout, R.string.error_processing_video, Snackbar.LENGTH_SHORT).show();
|
||||
} else {
|
||||
mMemu.getItem(R.id.action_send_post_video_activity).setEnabled(true);
|
||||
mMemu.getItem(R.id.action_send_post_video_activity).getIcon().setAlpha(255);
|
||||
if (submitVideoPostEvent.errorMessage == null || submitVideoPostEvent.errorMessage.equals("")) {
|
||||
Snackbar.make(coordinatorLayout, R.string.post_failed, Snackbar.LENGTH_SHORT).show();
|
||||
} else {
|
||||
|
@@ -25,6 +25,7 @@ import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
|
||||
import Account.Account;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
@@ -37,6 +38,10 @@ public class PullNotificationWorker extends Worker {
|
||||
@Named("oauth_without_authenticator")
|
||||
Retrofit mOauthWithoutAuthenticatorRetrofit;
|
||||
|
||||
@Inject
|
||||
@Named("no_oauth")
|
||||
Retrofit mRetrofit;
|
||||
|
||||
@Inject
|
||||
RedditDataRoomDatabase redditDataRoomDatabase;
|
||||
|
||||
@@ -56,9 +61,10 @@ public class PullNotificationWorker extends Worker {
|
||||
List<Account> accounts = redditDataRoomDatabase.accountDao().getAllAccounts();
|
||||
for(int accountIndex = 0; accountIndex < accounts.size(); accountIndex++) {
|
||||
Account account = accounts.get(accountIndex);
|
||||
|
||||
String accountName = account.getUsername();
|
||||
|
||||
Response<String> response = fetchMessages(account);
|
||||
Response<String> response = fetchMessages(account, 1);
|
||||
|
||||
if(response != null && response.isSuccessful()) {
|
||||
Log.i("workmanager", "has response");
|
||||
@@ -162,56 +168,81 @@ public class PullNotificationWorker extends Worker {
|
||||
|
||||
Log.i("workmanager", "message size " + messages.size());
|
||||
} else {
|
||||
Log.i("workmanager", "retry1");
|
||||
return Result.retry();
|
||||
Log.i("workmanager", "no message");
|
||||
return Result.success();
|
||||
}
|
||||
} else {
|
||||
if(response != null) {
|
||||
Log.i("workmanager", "retry2 " + response.code());
|
||||
Log.i("workmanager", "retry1 " + response.code());
|
||||
}
|
||||
return Result.retry();
|
||||
}
|
||||
}
|
||||
} catch (IOException | JSONException e) {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Log.i("workmanager", "retry3");
|
||||
Log.i("workmanager", "retry2");
|
||||
return Result.retry();
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
Log.i("workmanager", "json failure");
|
||||
return Result.failure();
|
||||
}
|
||||
|
||||
Log.i("workmanager", "success");
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
private Response<String> fetchMessages(Account account) throws IOException, JSONException {
|
||||
Response<String> response = mOauthWithoutAuthenticatorRetrofit.create(RedditAPI.class)
|
||||
private Response<String> fetchMessages(Account account, int retryCount) throws IOException, JSONException {
|
||||
if(retryCount < 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Call<String> call = mOauthWithoutAuthenticatorRetrofit.create(RedditAPI.class)
|
||||
.getMessages(RedditUtils.getOAuthHeader(account.getAccessToken()),
|
||||
FetchMessages.WHERE_UNREAD, null).execute();
|
||||
FetchMessages.WHERE_INBOX, null);
|
||||
Response<String> response = call.execute();
|
||||
|
||||
if(response.isSuccessful()) {
|
||||
return response;
|
||||
} else {
|
||||
if(response.code() == 401) {
|
||||
String refreshToken = account.getRefreshToken();
|
||||
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put(RedditUtils.GRANT_TYPE_KEY, RedditUtils.GRANT_TYPE_REFRESH_TOKEN);
|
||||
params.put(RedditUtils.REFRESH_TOKEN_KEY, refreshToken);
|
||||
|
||||
Response accessTokenResponse = mOauthWithoutAuthenticatorRetrofit.create(RedditAPI.class)
|
||||
.getAccessToken(RedditUtils.getHttpBasicAuthHeader(), params).execute();
|
||||
if(accessTokenResponse.isSuccessful() && accessTokenResponse.body() != null) {
|
||||
JSONObject jsonObject = new JSONObject((String) accessTokenResponse.body());
|
||||
String newAccessToken = jsonObject.getString(RedditUtils.ACCESS_TOKEN_KEY);
|
||||
account.setAccessToken(newAccessToken);
|
||||
redditDataRoomDatabase.accountDao().changeAccessToken(account.getUsername(), newAccessToken);
|
||||
|
||||
return fetchMessages(account);
|
||||
} else {
|
||||
return null;
|
||||
String accessToken = refreshAccessToken(account);
|
||||
if(!accessToken.equals("")) {
|
||||
return fetchMessages(account, retryCount - 1);
|
||||
}
|
||||
|
||||
return null;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String refreshAccessToken(Account account) {
|
||||
String refreshToken = account.getRefreshToken();
|
||||
|
||||
RedditAPI api = mRetrofit.create(RedditAPI.class);
|
||||
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put(RedditUtils.GRANT_TYPE_KEY, RedditUtils.GRANT_TYPE_REFRESH_TOKEN);
|
||||
params.put(RedditUtils.REFRESH_TOKEN_KEY, refreshToken);
|
||||
|
||||
Call<String> accessTokenCall = api.getAccessToken(RedditUtils.getHttpBasicAuthHeader(), params);
|
||||
try {
|
||||
Response response = accessTokenCall.execute();
|
||||
if(response.isSuccessful() && response.body() != null) {
|
||||
JSONObject jsonObject = new JSONObject(response.body().toString());
|
||||
String newAccessToken = jsonObject.getString(RedditUtils.ACCESS_TOKEN_KEY);
|
||||
redditDataRoomDatabase.accountDao().changeAccessToken(account.getUsername(), newAccessToken);
|
||||
|
||||
Log.i("access token", newAccessToken);
|
||||
return newAccessToken;
|
||||
}
|
||||
return "";
|
||||
} catch (IOException | JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,103 @@
|
||||
package ml.docilealligator.infinityforreddit;
|
||||
|
||||
import android.content.res.Configuration;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.Window;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceFragmentCompat;
|
||||
|
||||
import Settings.MainPreferenceFragment;
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
public class SettingsActivity extends AppCompatActivity implements
|
||||
PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {
|
||||
|
||||
private static final String TITLE_STATE = "TS";
|
||||
|
||||
@BindView(R.id.toolbar_settings_activity) Toolbar toolbar;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.settings_activity);
|
||||
|
||||
ButterKnife.bind(this);
|
||||
|
||||
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
Window window = getWindow();
|
||||
if((getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) {
|
||||
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
|
||||
}
|
||||
window.setNavigationBarColor(ContextCompat.getColor(this, R.color.navBarColor));
|
||||
}
|
||||
|
||||
setSupportActionBar(toolbar);
|
||||
|
||||
if (savedInstanceState == null) {
|
||||
getSupportFragmentManager()
|
||||
.beginTransaction()
|
||||
.replace(R.id.frame_layout_settings_activity, new MainPreferenceFragment())
|
||||
.commit();
|
||||
} else {
|
||||
setTitle(savedInstanceState.getCharSequence(TITLE_STATE));
|
||||
}
|
||||
getSupportFragmentManager().addOnBackStackChangedListener(() -> {
|
||||
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
|
||||
setTitle(R.string.settings_activity_label);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
|
||||
if(item.getItemId() == android.R.id.home) {
|
||||
onBackPressed();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(@NonNull Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
outState.putCharSequence(TITLE_STATE, getTitle());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onSupportNavigateUp() {
|
||||
if (getSupportFragmentManager().popBackStackImmediate()) {
|
||||
return true;
|
||||
}
|
||||
return super.onSupportNavigateUp();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {
|
||||
// Instantiate the new Fragment
|
||||
final Bundle args = pref.getExtras();
|
||||
final Fragment fragment = getSupportFragmentManager().getFragmentFactory().instantiate(
|
||||
getClassLoader(),
|
||||
pref.getFragment());
|
||||
fragment.setArguments(args);
|
||||
fragment.setTargetFragment(caller, 0);
|
||||
|
||||
getSupportFragmentManager().beginTransaction()
|
||||
.replace(R.id.frame_layout_settings_activity, fragment)
|
||||
.addToBackStack(null)
|
||||
.commit();
|
||||
setTitle(pref.getTitle());
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user