feat: 添加原生通知监听功能,自动同步通知和交易列表
This commit is contained in:
@@ -1,15 +1,39 @@
|
||||
<script setup>
|
||||
import { computed } from 'vue'
|
||||
import { computed, onMounted, onBeforeUnmount } from 'vue'
|
||||
import { storeToRefs } from 'pinia'
|
||||
import { App } from '@capacitor/app'
|
||||
import { useTransactionStore } from '../stores/transactions'
|
||||
import { useTransactionEntry } from '../composables/useTransactionEntry'
|
||||
|
||||
const transactionStore = useTransactionStore()
|
||||
const { totalIncome, totalExpense, todaysIncome, todaysExpense, latestTransactions } =
|
||||
storeToRefs(transactionStore)
|
||||
const { notifications, confirmNotification, dismissNotification, processingId } =
|
||||
const { notifications, confirmNotification, dismissNotification, processingId, syncNotifications } =
|
||||
useTransactionEntry()
|
||||
|
||||
let appStateListener = null
|
||||
|
||||
onMounted(async () => {
|
||||
try {
|
||||
// App 从后台回到前台时,自动同步一次通知和交易列表
|
||||
appStateListener = await App.addListener('appStateChange', async ({ isActive }) => {
|
||||
if (isActive) {
|
||||
await syncNotifications()
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
// Web 环境或插件不可用时忽略
|
||||
console.warn('[notifications] appStateChange listener failed', error)
|
||||
}
|
||||
})
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
if (appStateListener && typeof appStateListener.remove === 'function') {
|
||||
appStateListener.remove()
|
||||
}
|
||||
appStateListener = null
|
||||
})
|
||||
|
||||
const emit = defineEmits(['changeTab'])
|
||||
const monthlyBudget = 12000
|
||||
|
||||
|
||||
Reference in New Issue
Block a user