From abe6ab60654c8a177a6b4425745e89b48bd94ff6 Mon Sep 17 00:00:00 2001 From: Kosh Date: Sun, 19 Mar 2017 10:49:18 +0800 Subject: [PATCH] added background job indicator for star, fork, watch and mark single/all as read to at least let users know what is going on #173 --- .../tasks/git/GithubActionService.java | 62 ++++++++++++++++--- .../notification/ReadNotificationService.java | 26 +++++++- .../ui/modules/feeds/FeedsPresenter.java | 5 +- app/src/main/res/drawable/ic_sync.xml | 9 +++ 4 files changed, 89 insertions(+), 13 deletions(-) create mode 100644 app/src/main/res/drawable/ic_sync.xml diff --git a/app/src/main/java/com/fastaccess/provider/tasks/git/GithubActionService.java b/app/src/main/java/com/fastaccess/provider/tasks/git/GithubActionService.java index f77c99fe..164ec5b3 100644 --- a/app/src/main/java/com/fastaccess/provider/tasks/git/GithubActionService.java +++ b/app/src/main/java/com/fastaccess/provider/tasks/git/GithubActionService.java @@ -1,13 +1,16 @@ package com.fastaccess.provider.tasks.git; import android.app.IntentService; +import android.app.NotificationManager; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.annotation.IntDef; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.v4.app.NotificationCompat; +import com.fastaccess.R; import com.fastaccess.helper.BundleConstant; import com.fastaccess.helper.Bundler; import com.fastaccess.provider.rest.RestProvider; @@ -31,6 +34,8 @@ public class GithubActionService extends IntentService { public static final int STAR_GIST = 6; public static final int UNSTAR_GIST = 7; public static final int FORK_GIST = 8; + private NotificationCompat.Builder notification; + private NotificationManager notificationManager; @IntDef({ STAR_REPO, @@ -105,81 +110,122 @@ public class GithubActionService extends IntentService { private void forkGist(@Nullable String id) { if (id != null) { + String msg = getString(R.string.forking, getString(R.string.gist)); RestProvider.getGistService() .forkGist(id) + .doOnSubscribe(() -> showNotification(msg)) .subscribeOn(Schedulers.io()) .subscribe(response -> { - }, Throwable::printStackTrace); + }, throwable -> hideNotification(msg), () -> hideNotification(msg)); } } private void forkRepo(@Nullable String id, @Nullable String login) { if (id != null && login != null) { + String msg = getString(R.string.forking, id); RestProvider.getRepoService() .forkRepo(login, id) + .doOnSubscribe(() -> showNotification(msg)) .subscribeOn(Schedulers.io()) .subscribe(response -> { - }, Throwable::printStackTrace); + }, throwable -> hideNotification(msg), () -> hideNotification(msg)); } } private void starGist(@Nullable String id) { if (id != null) { + String msg = getString(R.string.staring, getString(R.string.gist)); RestProvider.getGistService() .starGist(id) + .doOnSubscribe(() -> showNotification(msg)) .subscribeOn(Schedulers.io()) .subscribe(response -> { - }, Throwable::printStackTrace); + }, throwable -> hideNotification(msg), () -> hideNotification(msg)); } } private void starRepo(@Nullable String id, @Nullable String login) { if (id != null && login != null) { + String msg = getString(R.string.staring, id); RestProvider.getRepoService() .starRepo(login, id) + .doOnSubscribe(() -> showNotification(msg)) .subscribeOn(Schedulers.io()) .subscribe(response -> { - }, Throwable::printStackTrace); + }, throwable -> hideNotification(msg), () -> hideNotification(msg)); } } private void unStarGist(@Nullable String id) { if (id != null) { + String msg = getString(R.string.un_staring, getString(R.string.gist)); RestProvider.getGistService() .unStarGist(id) + .doOnSubscribe(() -> showNotification(msg)) .subscribeOn(Schedulers.io()) .subscribe(response -> { - }, Throwable::printStackTrace); + }, throwable -> hideNotification(msg), () -> hideNotification(msg)); } } private void unStarRepo(@Nullable String id, @Nullable String login) { if (id != null && login != null) { + String msg = getString(R.string.un_staring, id); RestProvider.getRepoService() .unstarRepo(login, id) + .doOnSubscribe(() -> showNotification(msg)) .subscribeOn(Schedulers.io()) .subscribe(response -> { - }, Throwable::printStackTrace); + }, throwable -> hideNotification(msg), () -> hideNotification(msg)); } } private void unWatchRepo(@Nullable String id, @Nullable String login) { if (id != null && login != null) { + String msg = getString(R.string.un_watching, id); RestProvider.getRepoService() .unwatchRepo(login, id) + .doOnSubscribe(() -> showNotification(msg)) .subscribeOn(Schedulers.io()) .subscribe(response -> { - }, Throwable::printStackTrace); + }, throwable -> hideNotification(msg), () -> hideNotification(msg)); } } private void watchRepo(@Nullable String id, @Nullable String login) { if (id != null && login != null) { + String msg = getString(R.string.watching, id); RestProvider.getRepoService() .watchRepo(login, id) + .doOnSubscribe(() -> showNotification(msg)) .subscribeOn(Schedulers.io()) .subscribe(response -> { - }, Throwable::printStackTrace); + }, throwable -> hideNotification(msg), () -> hideNotification(msg)); } } + + private NotificationCompat.Builder getNotification(@NonNull String title) { + if (notification == null) { + notification = new NotificationCompat.Builder(this) + .setSmallIcon(R.drawable.ic_sync) + .setProgress(0, 100, true); + } + notification.setContentTitle(title); + return notification; + } + + private NotificationManager getNotificationManager() { + if (notificationManager == null) { + notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + } + return notificationManager; + } + + private void showNotification(@NonNull String msg) { + getNotificationManager().notify(msg.hashCode(), getNotification(msg).build()); + } + + private void hideNotification(@NonNull String msg) { + getNotificationManager().cancel(msg.hashCode()); + } } diff --git a/app/src/main/java/com/fastaccess/provider/tasks/notification/ReadNotificationService.java b/app/src/main/java/com/fastaccess/provider/tasks/notification/ReadNotificationService.java index aadc2e66..bb83cfdc 100644 --- a/app/src/main/java/com/fastaccess/provider/tasks/notification/ReadNotificationService.java +++ b/app/src/main/java/com/fastaccess/provider/tasks/notification/ReadNotificationService.java @@ -1,13 +1,16 @@ package com.fastaccess.provider.tasks.notification; import android.app.IntentService; +import android.app.NotificationManager; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.v4.app.NotificationCompat; import com.annimon.stream.LongStream; +import com.fastaccess.R; import com.fastaccess.helper.BundleConstant; import com.fastaccess.helper.Bundler; import com.fastaccess.provider.rest.RestProvider; @@ -22,6 +25,8 @@ public class ReadNotificationService extends IntentService { public static final int READ_SINGLE = 1; public static final int READ_ALL = 2; + private NotificationCompat.Builder notification; + private NotificationManager notificationManager; public static void start(@NonNull Context context, long id) { Intent intent = new Intent(context.getApplicationContext(), ReadNotificationService.class); @@ -66,7 +71,26 @@ public class ReadNotificationService extends IntentService { private void markSingleAsRead(long id) { RestProvider.getNotificationService() .markAsRead(String.valueOf(id)) + .doOnSubscribe(() -> getNotificationManager().notify((int) id, getNotification().build())) .subscribeOn(Schedulers.io()) - .subscribe(booleanResponse -> {}, Throwable::printStackTrace); + .subscribe(booleanResponse -> { + }, Throwable::printStackTrace, () -> getNotificationManager().cancel((int) id)); + } + + public NotificationCompat.Builder getNotification() { + if (notification == null) { + notification = new NotificationCompat.Builder(this) + .setContentTitle(getString(R.string.marking_as_read)) + .setSmallIcon(R.drawable.ic_sync) + .setProgress(0, 100, true); + } + return notification; + } + + public NotificationManager getNotificationManager() { + if (notificationManager == null) { + notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + } + return notificationManager; } } diff --git a/app/src/main/java/com/fastaccess/ui/modules/feeds/FeedsPresenter.java b/app/src/main/java/com/fastaccess/ui/modules/feeds/FeedsPresenter.java index fd765da7..3c3d0998 100644 --- a/app/src/main/java/com/fastaccess/ui/modules/feeds/FeedsPresenter.java +++ b/app/src/main/java/com/fastaccess/ui/modules/feeds/FeedsPresenter.java @@ -13,7 +13,6 @@ import com.fastaccess.data.dao.model.Event; import com.fastaccess.data.dao.model.Login; import com.fastaccess.data.dao.model.Repo; import com.fastaccess.data.dao.types.EventsType; -import com.fastaccess.helper.InputHelper; import com.fastaccess.helper.RxHelper; import com.fastaccess.provider.rest.RestProvider; import com.fastaccess.provider.scheme.SchemeParser; @@ -112,9 +111,7 @@ class FeedsPresenter extends BasePresenter implements FeedsMvp.Pr SchemeParser.launchUri(v.getContext(), Uri.parse(item.getPayload().getPullRequest().getHtmlUrl()), true); } else { Repo repoModel = item.getRepo(); - String name = InputHelper.isEmpty(repoModel.getName()) ? repoModel.getFullName() : repoModel.getName(); - if (name == null) return; - if (item.getRepo() != null) SchemeParser.launchUri(v.getContext(), Uri.parse(name), true); + if (item.getRepo() != null) SchemeParser.launchUri(v.getContext(), Uri.parse(repoModel.getName()), true); } } } diff --git a/app/src/main/res/drawable/ic_sync.xml b/app/src/main/res/drawable/ic_sync.xml new file mode 100644 index 00000000..260f061a --- /dev/null +++ b/app/src/main/res/drawable/ic_sync.xml @@ -0,0 +1,9 @@ + + +