Skip to content

Commit

Permalink
feat: capacitor notifications
Browse files Browse the repository at this point in the history
fix: capacitor version in settings
  • Loading branch information
ThaUnknown committed Jun 25, 2024
1 parent 14f3302 commit 41d85a1
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 5 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# Build output
**/dist/
**/build/
**/android/

# Dependencies
node_modules
Expand Down
2 changes: 2 additions & 0 deletions capacitor/.gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/node_modules/
android/*
!android/variables.gradle
!android/app
android/app/*
!android/app/build.gradle
!android/app/src
android/app/src/*
!android/app/src/main
Expand Down
65 changes: 65 additions & 0 deletions capacitor/android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
apply plugin: 'com.android.application'

def verCode = 0

def jsonFile = file('../../../electron/package.json')
def parsedJson = new groovy.json.JsonSlurper().parseText(jsonFile.text)
def verName = parsedJson.version
def versions = verName.tokenize('.')

versions.each (code) -> {
verCode = (verCode*100) + Integer.parseInt(code)
}

android {
namespace "watch.miru"
compileSdkVersion rootProject.ext.compileSdkVersion
defaultConfig {
applicationId "watch.miru"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode verCode
versionName verName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
aaptOptions {
// Files and dirs to omit from the packaged assets dir, modified to accommodate modern web apps.
// Default: https://android.googlesource.com/platform/frameworks/base/+/282e181b58cf72b6ca770dc7ca5f91f135444502/tools/aapt/AaptAssets.cpp#61
ignoreAssetsPattern '!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~'
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

repositories {
flatDir{
dirs '../capacitor-cordova-android-plugins/src/main/libs', 'libs'
}
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
implementation "androidx.coordinatorlayout:coordinatorlayout:$androidxCoordinatorLayoutVersion"
implementation "androidx.core:core-splashscreen:$coreSplashScreenVersion"
implementation project(':capacitor-android')
testImplementation "junit:junit:$junitVersion"
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
implementation project(':capacitor-cordova-android-plugins')
}

apply from: 'capacitor.build.gradle'

try {
def servicesJSON = file('google-services.json')
if (servicesJSON.text) {
apply plugin: 'com.google.gms.google-services'
}
} catch(Exception e) {
logger.info("google-services.json not found, google-services plugin not applied. Push Notifications won't work")
}
16 changes: 16 additions & 0 deletions capacitor/android/variables.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
ext {
minSdkVersion = 22
compileSdkVersion = 33
targetSdkVersion = 33
androidxActivityVersion = '1.7.0'
androidxAppCompatVersion = '1.6.1'
androidxCoordinatorLayoutVersion = '1.2.0'
androidxCoreVersion = '1.10.0'
androidxFragmentVersion = '1.5.6'
coreSplashScreenVersion = '1.0.0'
androidxWebkitVersion = '1.6.1'
junitVersion = '4.13.2'
androidxJunitVersion = '1.1.5'
androidxEspressoCoreVersion = '3.5.1'
cordovaAndroidVersion = '10.1.1'
}
1 change: 1 addition & 0 deletions capacitor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@capacitor/cli": "^5.5.1",
"@capacitor/core": "^5.5.1",
"@capacitor/ios": "^5.5.1",
"@capacitor/local-notifications": "5.0.8",
"@capacitor/status-bar": "^5.0.6",
"capacitor-nodejs": "https://github.com/funniray/Capacitor-NodeJS/releases/download/nodejs-18/capacitor-nodejs-1.0.0-beta.6.tgz",
"capacitor-plugin-safe-area": "^2.0.5",
Expand Down
31 changes: 31 additions & 0 deletions capacitor/src/capacitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,43 @@ import { StatusBar, Style } from '@capacitor/status-bar'
import { SafeArea } from 'capacitor-plugin-safe-area'
import { App } from '@capacitor/app'
import { Browser } from '@capacitor/browser'
import { LocalNotifications } from '@capacitor/local-notifications'
import IPC from './ipc.js'

IPC.on('open', url => Browser.open({ url }))

App.addListener('appUrlOpen', ({ url }) => handleProtocol(url))

let canShowNotifications = false

LocalNotifications.checkPermissions().then(async value => {
if (value) {
try {
await LocalNotifications.requestPermissions()
canShowNotifications = true
} catch (e) {
console.error(e)
}
}
})

let id = 0
IPC.on('notification', noti => {
/** @type {import('@capacitor/local-notifications').LocalNotificationSchema} */
const notification = {
title: noti.title,
body: noti.body,
id: id++,
attachments: [
{
id: '' + id++,
url: noti.icon
}
]
}
if (canShowNotifications) LocalNotifications.schedule({ notifications: [notification] })
})

// schema: miru://key/value
const protocolMap = {
auth: token => sendToken(token),
Expand Down
9 changes: 7 additions & 2 deletions capacitor/src/ipc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { App } from '@capacitor/app'
import { NodeJS } from 'capacitor-nodejs'
import EventEmitter from 'events'

Expand Down Expand Up @@ -25,6 +26,10 @@ const [_platform, arch] = navigator.platform.split(' ')

globalThis.version = {
platform: globalThis.cordova?.platformId,
arch,
version: globalThis.cordova?.version
arch
}

main.once('version', async () => {
const { version } = await App.getInfo()
main.emit('version', version)
})
4 changes: 2 additions & 2 deletions common/components/Menubar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

<div class='w-full z-101 navbar bg-transparent border-0 p-0 d-flex'>
<div class='d-flex h-full draggable align-items-center text-center'>
{#if window.version.platform !== 'darwin'}
{#if window.version?.platform !== 'darwin'}
<img src='./logo_filled.png' class='position-absolute w-50 h-50 m-10 pointer d-md-block d-none p-5' alt='ico' use:click={close} />
{/if}
</div>
<div class='h-full bg-dark flex-grow-1'>
{#if window.version.platform === 'linux'}
{#if window.version?.platform === 'linux'}
<div class='d-flex align-items-center close h-full' use:click={() => IPC.emit('close')}>
<svg viewBox='0 0 24 24'>
<path d='M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41z' />
Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 41d85a1

Please sign in to comment.