Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zfdang committed Apr 29, 2022
1 parent f1a884a commit d33281f
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 195 deletions.
6 changes: 6 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ dependencies {
implementation "io.reactivex.rxjava2:rxjava:$rootProject.RXJAVA2_VERSION"
implementation "io.reactivex.rxjava2:rxandroid:$rootProject.RXANDROID2_VERSION"

def work_version = "2.7.1"
// (Java only)
implementation "androidx.work:work-runtime:$work_version"
// optional - RxJava2 support
implementation "androidx.work:work-rxjava2:$work_version"

// OkHttp
implementation "com.squareup.okhttp3:okhttp:$OKHTTP_VERSION"
implementation "com.squareup.okhttp3:logging-interceptor:$OKHTTP_VERSION"
Expand Down
6 changes: 0 additions & 6 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,6 @@
android:configChanges="orientation|screenSize" />

<!-- service -->
<service
android:name=".services.MaintainUserStatusService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false" />
<receiver
android:name=".services.AlarmBroadcastReceiver" />
<!-- aadapt to S8 and Mix2 -->
<meta-data
android:name="android.max_aspect"
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/com/zfdang/SMTHApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.zfdang.zsmth_android.helpers.GEODatabase;
import com.zfdang.zsmth_android.newsmth.SMTHHelper;
import com.zfdang.zsmth_android.newsmth.UserStatus;
import com.zfdang.zsmth_android.services.UserStatusReceiver;

import okhttp3.OkHttpClient;
import androidx.multidex.MultiDex;

Expand Down Expand Up @@ -48,6 +50,9 @@ public class SMTHApplication extends Application {
public static final String NOTIFICATION_NEW_LIKE = "你有新Like!";
public static final String NOTIFICATION_LOGIN_LOST = "登录已过期!请重新登录...";

public static final int INTERVAL_TO_CHECK_MESSAGE = 2; // 2 minutes for interval to check messages
public static UserStatusReceiver mUserStatusReceiver = null;

// IP database
public static GEODatabase geoDB;

Expand All @@ -59,7 +64,7 @@ protected void attachBaseContext(Context base) {

// current logined user
public static UserStatus activeUser;
public static String displayedUserId;
public static String displayedUserId = "guest";
public static boolean isValidUser() {
return activeUser != null && !activeUser.getId().equals("guest");
}
Expand Down
31 changes: 23 additions & 8 deletions app/src/main/java/com/zfdang/zsmth_android/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.work.Data;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkManager;
import androidx.work.WorkRequest;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.navigation.NavigationView;
Expand All @@ -56,10 +60,11 @@
import com.zfdang.zsmth_android.models.Topic;
import com.zfdang.zsmth_android.newsmth.AjaxResponse;
import com.zfdang.zsmth_android.newsmth.SMTHHelper;
import com.zfdang.zsmth_android.services.AlarmBroadcastReceiver;
import com.zfdang.zsmth_android.services.MaintainUserStatusWorker;
import com.zfdang.zsmth_android.services.UserStatusReceiver;

import java.lang.reflect.Field;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
Expand Down Expand Up @@ -176,10 +181,18 @@ public void onBackStackChanged() {
setupUserStatusReceiver();

// schedule the periodical background service
AlarmBroadcastReceiver.schedule(getApplicationContext(), mReceiver);
Data.Builder inputData = new Data.Builder();
inputData.putBoolean(MaintainUserStatusWorker.REPEAT, true);
WorkRequest userStatusWorkRequest =
new OneTimeWorkRequest.Builder(MaintainUserStatusWorker.class)
.setInitialDelay(SMTHApplication.INTERVAL_TO_CHECK_MESSAGE, TimeUnit.MINUTES)
.setInputData(inputData.build())
.build();
WorkManager.getInstance(getApplicationContext()).enqueue(userStatusWorkRequest);

// run the background service now
updateUserStatusNow();
UpdateNavigationViewHeader();
// UpdateNavigationViewHeader();

if (Settings.getInstance().isFirstRun()) {
// show info dialog after 5 seconds for the first run
Expand Down Expand Up @@ -259,7 +272,10 @@ private void initCircularActionMenu(FloatingActionButton fab) {

// triger the background service right now
private void updateUserStatusNow() {
AlarmBroadcastReceiver.runJobNow(getApplicationContext(), mReceiver);
// run worker immediately for once
WorkRequest userStatusWorkRequest =
new OneTimeWorkRequest.Builder(MaintainUserStatusWorker.class).build();
WorkManager.getInstance(getApplicationContext()).enqueue(userStatusWorkRequest);
}

private void setupUserStatusReceiver() {
Expand All @@ -279,6 +295,7 @@ private void setupUserStatusReceiver() {
}
}
});
SMTHApplication.mUserStatusReceiver = mReceiver;
}

private void showNotification(String text) {
Expand Down Expand Up @@ -435,12 +452,13 @@ public void UpdateNavigationViewHeader() {
if (faceURL != null) {
mAvatar.setImageFromStringURL(faceURL);
}
SMTHApplication.displayedUserId = SMTHApplication.activeUser.getId();
} else {
// when user is invalid, set notice to login
mUsername.setText(getString(R.string.nav_header_click_to_login));
mAvatar.setImageResource(R.drawable.ic_person_black_48dp);
SMTHApplication.displayedUserId = "guest";
}
SMTHApplication.displayedUserId = mUsername.getText().toString();
}

@Override public void onBackPressed() {
Expand Down Expand Up @@ -490,9 +508,6 @@ private void DoubleBackToExit() {
}

private void quitNow() {
// stop background service
AlarmBroadcastReceiver.unschedule();

// quit
finish();
android.os.Process.killProcess(android.os.Process.myPid());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public void setFace_url(String face_url) {
}

public String getId() {
if (id == null) return "guest";
return id;
}

Expand Down

This file was deleted.

Loading

0 comments on commit d33281f

Please sign in to comment.