Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#600) Notification system for settings (for crash logs and apk updates) #656

Merged
merged 15 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
(#600) Coder review fixes
  • Loading branch information
K1rakishou committed Mar 5, 2020
commit c024206c82a60670686b65306bf49e1a1d4e2c9a
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public void onCreate() {

if (ChanSettings.collectCrashLogs.get()) {
if (reportManager.hasCrashLogs()) {
settingsNotificationManager.notify(SettingNotificationType.CrashLogs);
settingsNotificationManager.notify(SettingNotificationType.CrashLog);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class ReportManager(
.doOnNext {
// If no more crash logs left, remove the notification
if (!hasCrashLogs()) {
settingsNotificationManager.cancel(SettingNotificationType.CrashLogs)
settingsNotificationManager.cancel(SettingNotificationType.CrashLog)
}
}
.subscribe({
Expand Down Expand Up @@ -208,47 +208,46 @@ class ReportManager(

fun deleteCrashLogs(crashLogs: List<CrashLog>) {
if (!createCrashLogsDirIfNotExists()) {
settingsNotificationManager.cancel(SettingNotificationType.CrashLogs)
settingsNotificationManager.cancel(SettingNotificationType.CrashLog)
return
}

crashLogs.forEach { crashLog -> crashLog.file.delete() }

val remainingCrashLogs = crashLogsDirPath.listFiles()?.size ?: 0
if (remainingCrashLogs == 0) {
settingsNotificationManager.cancel(SettingNotificationType.CrashLogs)
settingsNotificationManager.cancel(SettingNotificationType.CrashLog)
return
}

// There are still crash logs left, so show the notifications if they are not shown yet
settingsNotificationManager.notify(SettingNotificationType.CrashLogs)
settingsNotificationManager.notify(SettingNotificationType.CrashLog)
}

fun deleteAllCrashLogs() {
if (!createCrashLogsDirIfNotExists()) {
settingsNotificationManager.cancel(SettingNotificationType.CrashLogs)
settingsNotificationManager.cancel(SettingNotificationType.CrashLog)
return
}

val potentialCrashLogs = crashLogsDirPath.listFiles()
if (potentialCrashLogs.isNullOrEmpty()) {
Logger.d(TAG, "No new crash logs")
settingsNotificationManager.cancel(SettingNotificationType.CrashLogs)
settingsNotificationManager.cancel(SettingNotificationType.CrashLog)
return
}

potentialCrashLogs.asSequence()
.filter { file -> file.name.startsWith(CRASH_LOG_FILE_NAME_PREFIX) }
.forEach { crashLogFile -> crashLogFile.delete() }

val remainingCrashLogs = crashLogsDirPath.listFiles()?.size ?: 0
if (remainingCrashLogs == 0) {
settingsNotificationManager.cancel(SettingNotificationType.CrashLogs)
settingsNotificationManager.cancel(SettingNotificationType.CrashLog)
return
}

// There are still crash logs left, so show the notifications if they are not shown yet
settingsNotificationManager.notify(SettingNotificationType.CrashLogs)
settingsNotificationManager.notify(SettingNotificationType.CrashLog)
}

fun sendCrashLogs(crashLogs: List<CrashLog>): Completable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SettingsNotificationManager {
private val notifications: MutableSet<SettingNotificationType> = mutableSetOf()
K1rakishou marked this conversation as resolved.
Show resolved Hide resolved

/**
* A reactive stream that is being used to notify observer about [notifications] changes
* A reactive stream that is being used to notify observers about [notifications] changes
* */
private val activeNotificationsSubject = BehaviorProcessor.createDefault(Unit)

Expand All @@ -34,8 +34,8 @@ class SettingsNotificationManager {
return SettingNotificationType.ApkUpdate
}

if (contains(SettingNotificationType.CrashLogs)) {
return SettingNotificationType.CrashLogs
if (contains(SettingNotificationType.CrashLog)) {
return SettingNotificationType.CrashLog
}

// Add new notifications here. Don't forget that order matters! The order affects priority.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import com.github.adamantcheese.chan.core.net.UpdateApiRequest;
import com.github.adamantcheese.chan.core.net.UpdateApiRequest.UpdateApiResponse;
import com.github.adamantcheese.chan.core.settings.ChanSettings;
import com.github.adamantcheese.chan.core.settings.ChanState;
import com.github.adamantcheese.chan.core.settings.state.PersistableChanState;
import com.github.adamantcheese.chan.ui.helper.RuntimePermissionsHelper;
import com.github.adamantcheese.chan.ui.settings.SettingNotificationType;
import com.github.adamantcheese.chan.utils.BackgroundUtils;
Expand Down Expand Up @@ -158,7 +158,7 @@ public void manualUpdateCheck() {
}

private void runUpdateApi(final boolean manual) {
if (ChanState.hasNewApkUpdate().get()) {
if (PersistableChanState.getHasNewApkUpdate()) {
// If we noticed that there was an apk update on the previous check - show the
// notification
notifyNewApkUpdate();
Expand Down Expand Up @@ -275,12 +275,12 @@ private boolean processUpdateApiResponse(UpdateApiResponse response, boolean man
}

private void notifyNewApkUpdate() {
ChanState.hasNewApkUpdate().set(true);
PersistableChanState.setHasNewApkUpdate(true);
settingsNotificationManager.notify(SettingNotificationType.ApkUpdate);
}

private void cancelApkUpdateNotification() {
ChanState.hasNewApkUpdate().set(false);
PersistableChanState.setHasNewApkUpdate(false);
settingsNotificationManager.cancel(SettingNotificationType.ApkUpdate);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.github.adamantcheese.chan.core.settings
package com.github.adamantcheese.chan.core.settings.state

import com.github.adamantcheese.chan.core.settings.BooleanSetting
import com.github.adamantcheese.chan.core.settings.SharedPreferencesSettingProvider
import com.github.adamantcheese.chan.utils.AndroidUtils
import com.github.adamantcheese.chan.utils.Logger


object ChanState {
object PersistableChanState {
private const val TAG = "ChanState"
private val hasNewApkUpdate: BooleanSetting

Expand All @@ -20,5 +22,11 @@ object ChanState {

// Why? So it can be mocked in tests.
@JvmStatic
open fun hasNewApkUpdate() = hasNewApkUpdate
fun getHasNewApkUpdate(): Boolean = hasNewApkUpdate.get()

@JvmStatic
fun setHasNewApkUpdate(value: Boolean) = hasNewApkUpdate.set(value)

@JvmStatic
fun setHasNewApkUpdateSync(value: Boolean) = hasNewApkUpdate.setSync(value)
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import com.github.adamantcheese.chan.core.manager.FilterWatchManager;
import com.github.adamantcheese.chan.core.manager.WakeManager;
import com.github.adamantcheese.chan.core.settings.ChanSettings;
import com.github.adamantcheese.chan.core.settings.ChanState;
import com.github.adamantcheese.chan.core.settings.state.PersistableChanState;
import com.github.adamantcheese.chan.ui.controller.LogsController;
import com.github.adamantcheese.chan.utils.Logger;

Expand Down Expand Up @@ -202,7 +202,7 @@ public void onCreate() {
resetPrevApkHash.setOnClickListener(v -> {
ChanSettings.previousDevHash.setSync(NO_HASH_SET);
ChanSettings.updateCheckTime.setSync(0L);
ChanState.hasNewApkUpdate().setSync(false);
PersistableChanState.setHasNewApkUpdateSync(false);
((StartActivity) context).restartApp();
});
resetPrevApkHash.setText("Make app updated");
Expand All @@ -213,7 +213,7 @@ public void onCreate() {
setCurrentApkHashAsPrevApkHash.setOnClickListener(v -> {
ChanSettings.previousDevHash.setSync(BuildConfig.COMMIT_HASH);
ChanSettings.updateCheckTime.setSync(0L);
ChanState.hasNewApkUpdate().setSync(true);
PersistableChanState.setHasNewApkUpdateSync(true);
((StartActivity) context).restartApp();
});
setCurrentApkHashAsPrevApkHash.setText("Make app not updated");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private void onNotificationsChanged() {
getViewGroupOrThrow(updateSettingView)
);
updateSettingNotificationIcon(
settingsNotificationManager.getOrDefault(SettingNotificationType.CrashLogs),
settingsNotificationManager.getOrDefault(SettingNotificationType.CrashLog),
getViewGroupOrThrow(reportSettingView)
);
}
Expand Down Expand Up @@ -270,7 +270,7 @@ private LinkSettingView createReportSettingView() {
onReportSettingClick();
});

reportSettingView.setSettingNotificationType(SettingNotificationType.CrashLogs);
reportSettingView.setSettingNotificationType(SettingNotificationType.CrashLog);
return reportSettingView;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ enum class SettingNotificationType(@ColorInt val notificationIconTintColor: Int)
/**
* There is at least one crash log available notification
* */
CrashLogs(R.color.new_crash_log_icon_color)
CrashLog(R.color.new_crash_log_icon_color)
}