Skip to content

Commit

Permalink
Background android app pending payments
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Oct 29, 2023
1 parent 6f628cf commit 36e54bc
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 10 deletions.
3 changes: 2 additions & 1 deletion android/app/capacitor.build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ android {
apply from: "../capacitor-cordova-android-plugins/cordova.variables.gradle"
dependencies {
implementation project(':capacitor-app')
implementation project(':capacitor-background-runner')
implementation project(':capacitor-browser')
implementation project(':capacitor-clipboard')
implementation project(':capacitor-filesystem')
implementation project(':capacitor-share')
implementation project(':capacitor-haptics')
implementation project(':capacitor-share')
implementation project(':capacitor-toast')
implementation project(':mutinywallet-barcode-scanner')

Expand Down
2 changes: 2 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@
<uses-permission android:name="android.permission.WRITE_CLIPBOARD" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
</manifest>
9 changes: 6 additions & 3 deletions android/capacitor.settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ project(':capacitor-android').projectDir = new File('../node_modules/.pnpm/@capa
include ':capacitor-app'
project(':capacitor-app').projectDir = new File('../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@capacitor/app/android')

include ':capacitor-background-runner'
project(':capacitor-background-runner').projectDir = new File('../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@capacitor/background-runner/android')

include ':capacitor-browser'
project(':capacitor-browser').projectDir = new File('../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@capacitor/browser/android')

Expand All @@ -14,12 +17,12 @@ project(':capacitor-clipboard').projectDir = new File('../node_modules/.pnpm/@ca
include ':capacitor-filesystem'
project(':capacitor-filesystem').projectDir = new File('../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@capacitor/filesystem/android')

include ':capacitor-share'
project(':capacitor-share').projectDir = new File('../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@capacitor/share/android')

include ':capacitor-haptics'
project(':capacitor-haptics').projectDir = new File('../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@capacitor/haptics/android')

include ':capacitor-share'
project(':capacitor-share').projectDir = new File('../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@capacitor/share/android')

include ':capacitor-toast'
project(':capacitor-toast').projectDir = new File('../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@capacitor/toast/android')

Expand Down
12 changes: 11 additions & 1 deletion capacitor.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ const config: CapacitorConfig = {
webDir: 'dist/public',
server: {
androidScheme: 'https'
}
},
plugins: {
BackgroundRunner: {
label: 'com.mutinywallet.mutinywallet.background',
src: 'runners/background.ts',
event: 'checkPaymentsInFlight',
repeat: true,
interval: 60,
autoStart: true,
},
},
};

export default config;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@
"dependencies": {
"@capacitor/android": "^5.2.2",
"@capacitor/app": "^5.0.6",
"@capacitor/background-runner": "^1.0.5",
"@capacitor/browser": "^5.0.6",
"@capacitor/clipboard": "^5.0.6",
"@capacitor/core": "^5.2.2",
"@capacitor/filesystem": "^5.1.4",
"@capacitor/share": "^5.0.6",
"@capacitor/haptics": "^5.0.6",
"@capacitor/share": "^5.0.6",
"@capacitor/toast": "^5.0.6",
"@kobalte/core": "^0.9.8",
"@kobalte/tailwindcss": "^0.5.0",
Expand Down
20 changes: 16 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 98 additions & 0 deletions src/runners/background.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { CapacitorNotifications } from "@capacitor/background-runner";

addEventListener("checkPaymentsInFlight", async (resolve, reject, _args) => {
try {
await checkPaymentsInFlight();
resolve();
} catch (e) {
reject(e);
}
});

interface OutboundPayment {
status: string;
}

async function checkPaymentsInFlight() {
console.log("checkPaymentsInFlight");
const db = await openDatabase();

const transaction = db.transaction("wallet_store", "readonly");
const store = transaction.objectStore("wallet_store");

// Get keys prefixed with "payment_outbound"
const keys = await getAllKeysWithPrefix(store, "payment_outbound");

for (const key of keys) {
const payment = await get(store, key);
console.log(payment.status);
if (payment && payment.status === "InFlight") {
showNotification();
break;
}
}
transaction.commit();
}

function openDatabase(): Promise<IDBDatabase> {
return new Promise((resolve, reject) => {
const request = indexedDB.open("wallet");
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
}

function getAllKeysWithPrefix(
store: IDBObjectStore,
prefix: string
): Promise<string[]> {
return new Promise((resolve, reject) => {
const keys: string[] = [];
const cursorRequest = store.openKeyCursor();

cursorRequest.onsuccess = function (event) {
const cursor = (event.target as IDBRequest).result as IDBCursor;
if (cursor) {
if (cursor.key.toString().startsWith(prefix)) {
keys.push(cursor.key.toString());
}
cursor.continue();
} else {
resolve(keys);
}
};

cursorRequest.onerror = function () {
reject(cursorRequest.error);
};
});
}

function get(store: IDBObjectStore, key: string): Promise<OutboundPayment> {
return new Promise((resolve, reject) => {
const request = store.get(key);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
}

function showNotification() {
// generate random id
const id = Math.random() * 100_000_000;

// send notification in 5 seconds
const scheduleDate = new Date();
scheduleDate.setSeconds(scheduleDate.getSeconds() + 5);

// todo make pretty
CapacitorNotifications.schedule([
{
id,
title: "You have a payment in flight",
body: "Open Mutiny to make sure it completes.",
scheduleAt: scheduleDate
}
]);
}

export default checkPaymentsInFlight;

0 comments on commit 36e54bc

Please sign in to comment.