From fe9aaf123fe93cf57ce2cc3ceaacb6f901fef20e Mon Sep 17 00:00:00 2001 From: Robin Genz Date: Tue, 6 Jul 2021 22:15:27 +0200 Subject: [PATCH 1/4] feat: add `increase` and `decrease` methods --- src/definitions.ts | 8 ++++++++ src/web.ts | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/definitions.ts b/src/definitions.ts index 23bdb9a..d612e8c 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -12,6 +12,14 @@ export interface BadgePlugin { * Set the badge count. */ set(options: SetBadgeOptions): Promise; + /** + * Increase the badge count. + */ + increase(): Promise; + /** + * Decrease the badge count. + */ + decrease(): Promise; /** * Clear the badge count. */ diff --git a/src/web.ts b/src/web.ts index 66a8b00..ab47fff 100644 --- a/src/web.ts +++ b/src/web.ts @@ -3,8 +3,8 @@ import { WebPlugin } from '@capacitor/core'; import type { BadgePlugin, GetBadgeResult, - SetBadgeOptions, PermissionStatus, + SetBadgeOptions, } from './definitions'; export class BadgeWeb extends WebPlugin implements BadgePlugin { @@ -40,6 +40,19 @@ export class BadgeWeb extends WebPlugin implements BadgePlugin { localStorage.setItem(BadgeWeb.STORAGE_KEY, value); } + public async increase(): Promise { + const { count } = await this.get(); + await this.set({ count: count + 1 }); + } + + public async decrease(): Promise { + const { count } = await this.get(); + if (count < 1) { + return; + } + await this.set({ count: count - 1 }); + } + public async clear(): Promise { await this.set({ count: 0 }); } From 72b6926e1fada57bdbd38ffe443bdf1123839a74 Mon Sep 17 00:00:00 2001 From: Robin Genz Date: Tue, 27 Jul 2021 14:37:28 +0200 Subject: [PATCH 2/4] wip: android --- .../dev/robingenz/capacitor/badge/Badge.java | 13 ++++++++++++ .../capacitor/badge/BadgePlugin.java | 20 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/android/src/main/java/dev/robingenz/capacitor/badge/Badge.java b/android/src/main/java/dev/robingenz/capacitor/badge/Badge.java index 841a2d9..45adc0a 100644 --- a/android/src/main/java/dev/robingenz/capacitor/badge/Badge.java +++ b/android/src/main/java/dev/robingenz/capacitor/badge/Badge.java @@ -31,6 +31,19 @@ public void set(int count) { ShortcutBadger.applyCount(context, count); } + public void increase() { + int count = get(); + set(count + 1); + } + + public void decrease() { + int count = get(); + if (count < 1) { + return; + } + set(count - 1); + } + public void clear() { set(0); } diff --git a/android/src/main/java/dev/robingenz/capacitor/badge/BadgePlugin.java b/android/src/main/java/dev/robingenz/capacitor/badge/BadgePlugin.java index 6d3c05c..c26dad8 100644 --- a/android/src/main/java/dev/robingenz/capacitor/badge/BadgePlugin.java +++ b/android/src/main/java/dev/robingenz/capacitor/badge/BadgePlugin.java @@ -40,6 +40,26 @@ public void set(PluginCall call) { } } + @PluginMethod + public void increase(PluginCall call) { + try { + implementation.increase(); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } + } + + @PluginMethod + public void decrease(PluginCall call) { + try { + implementation.decrease(); + call.resolve(); + } catch (Exception ex) { + call.reject(ex.getLocalizedMessage()); + } + } + @PluginMethod public void clear(PluginCall call) { try { From c8471ffb0f5714ddb08689fefd01a61813ab91f7 Mon Sep 17 00:00:00 2001 From: Robin Genz Date: Tue, 27 Jul 2021 14:45:01 +0200 Subject: [PATCH 3/4] wip: ios --- ios/Plugin/Badge.swift | 14 ++++++++++++++ ios/Plugin/BadgePlugin.m | 2 ++ ios/Plugin/BadgePlugin.swift | 30 ++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/ios/Plugin/Badge.swift b/ios/Plugin/Badge.swift index 45ecb38..70db4bc 100644 --- a/ios/Plugin/Badge.swift +++ b/ios/Plugin/Badge.swift @@ -52,6 +52,20 @@ import Capacitor } } + @objc public func increase(completion: @escaping () -> Void) { + let count = get() + set(count: count + 1, completion: completion) + } + + @objc public func decrease(completion: @escaping () -> Void) { + let count = get() + if (count < 1) { + completion() + return + } + set(count: count - 1, completion: completion) + } + @objc public func clear(completion: @escaping () -> Void) { set(count: 0, completion: completion) } diff --git a/ios/Plugin/BadgePlugin.m b/ios/Plugin/BadgePlugin.m index 6f77a18..294f33a 100644 --- a/ios/Plugin/BadgePlugin.m +++ b/ios/Plugin/BadgePlugin.m @@ -8,5 +8,7 @@ CAP_PLUGIN_METHOD(checkPermissions, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(get, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(set, CAPPluginReturnPromise); + CAP_PLUGIN_METHOD(increase, CAPPluginReturnPromise); + CAP_PLUGIN_METHOD(decrease, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(clear, CAPPluginReturnPromise); ) diff --git a/ios/Plugin/BadgePlugin.swift b/ios/Plugin/BadgePlugin.swift index 176adc3..b3baf3a 100644 --- a/ios/Plugin/BadgePlugin.swift +++ b/ios/Plugin/BadgePlugin.swift @@ -50,6 +50,36 @@ public class BadgePlugin: CAPPlugin { }) } + @objc func increase(_ call: CAPPluginCall) { + implementation.requestPermissions(completion: { [weak self] granted, error in + guard let strongSelf = self else { + return + } + guard error == nil else { + call.reject(error!.localizedDescription) + return + } + strongSelf.implementation.increase(completion: { + call.resolve() + }) + }) + } + + @objc func decrease(_ call: CAPPluginCall) { + implementation.requestPermissions(completion: { [weak self] granted, error in + guard let strongSelf = self else { + return + } + guard error == nil else { + call.reject(error!.localizedDescription) + return + } + strongSelf.implementation.decrease(completion: { + call.resolve() + }) + }) + } + @objc func clear(_ call: CAPPluginCall) { implementation.requestPermissions(completion: { [weak self] granted, error in guard let strongSelf = self else { From e005a004e1fa7db4cb78e2484610317f41bb1958 Mon Sep 17 00:00:00 2001 From: Robin Genz Date: Tue, 27 Jul 2021 14:50:27 +0200 Subject: [PATCH 4/4] docs --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index 753a627..fe40537 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,8 @@ const requestPermissions = async () => { * [`get()`](#get) * [`set(...)`](#set) +* [`increase()`](#increase) +* [`decrease()`](#decrease) * [`clear()`](#clear) * [`checkPermissions()`](#checkpermissions) * [`requestPermissions()`](#requestpermissions) @@ -112,6 +114,28 @@ Set the badge count. -------------------- +### increase() + +```typescript +increase() => Promise +``` + +Increase the badge count. + +-------------------- + + +### decrease() + +```typescript +decrease() => Promise +``` + +Decrease the badge count. + +-------------------- + + ### clear() ```typescript