111 lines
2.9 KiB
TypeScript
111 lines
2.9 KiB
TypeScript
import { makeAutoObservable } from 'mobx'
|
|
|
|
import { type BleDevice } from '@/ble/core/types'
|
|
import { type DeviceInfo } from '@/ble/protocol/types'
|
|
import { storage } from '@/utils'
|
|
|
|
interface BleState {
|
|
isScanning: boolean
|
|
isConnected: boolean
|
|
connectedDevice: BleDevice | null
|
|
deviceInfo: DeviceInfo | null
|
|
version: string
|
|
isActivated: boolean
|
|
transferProgress: number
|
|
discoveredDevices: BleDevice[]
|
|
loading: {
|
|
connecting: boolean
|
|
querying: boolean
|
|
converting: boolean
|
|
transferring: boolean
|
|
}
|
|
error: string | null
|
|
}
|
|
|
|
interface bindDeviceItem {
|
|
id: string
|
|
name: string
|
|
userId: string
|
|
createAt: number
|
|
}
|
|
|
|
class BleStore {
|
|
// State
|
|
state: BleState = {
|
|
isScanning: false,
|
|
isConnected: false,
|
|
connectedDevice: null,
|
|
deviceInfo: null,
|
|
version: '',
|
|
isActivated: false,
|
|
transferProgress: 0,
|
|
discoveredDevices: [],
|
|
loading: {
|
|
connecting: false,
|
|
querying: false,
|
|
converting: false,
|
|
transferring: false,
|
|
},
|
|
error: null,
|
|
}
|
|
|
|
galleryList: string[] = []
|
|
bindDeviceList: bindDeviceItem[] = []
|
|
bleSessionId = `ble-session-${Date.now().toString()}`
|
|
|
|
constructor() {
|
|
makeAutoObservable(this)
|
|
this.loadData()
|
|
}
|
|
|
|
private async loadData() {
|
|
const data = await storage.get('ble_contents')
|
|
this.setState((prev) => ({
|
|
...prev,
|
|
contents: (data as string[]) ?? [],
|
|
}))
|
|
|
|
const bindData = await storage.get('ble_bindDeviceList')
|
|
this.bindDeviceList = (bindData as typeof this.bindDeviceList) ?? []
|
|
}
|
|
|
|
setGalleryList(contents: string[]) {
|
|
this.galleryList = contents
|
|
storage.set('ble_contents', JSON.stringify(contents))
|
|
}
|
|
addGalleryItem(content: string) {
|
|
if (this.galleryList.includes(content)) return
|
|
this.galleryList.push(content)
|
|
storage.set('ble_contents', JSON.stringify(this.galleryList))
|
|
}
|
|
removeGalleryItem(key: string) {
|
|
this.galleryList = this.galleryList.filter((c) => c !== key)
|
|
storage.set('ble_contents', JSON.stringify(this.galleryList))
|
|
}
|
|
|
|
addBindDeviceItem(item: bindDeviceItem) {
|
|
if (this.bindDeviceList.find((i) => i.id === item.id)) return
|
|
this.bindDeviceList.push({ ...item, createAt: Date.now() })
|
|
storage.set('ble_bindDeviceList', JSON.stringify(this.bindDeviceList))
|
|
}
|
|
updateBindDeviceItem(item: bindDeviceItem) {
|
|
const index = this.bindDeviceList.findIndex((i) => i.id === item.id)
|
|
if (index !== -1) {
|
|
this.bindDeviceList[index] = item
|
|
storage.set('ble_bindDeviceList', JSON.stringify(this.bindDeviceList))
|
|
}
|
|
}
|
|
removeBindDeviceItem(id: string) {
|
|
this.bindDeviceList = this.bindDeviceList.filter((i) => i.id !== id)
|
|
storage.set('ble_bindDeviceList', JSON.stringify(this.bindDeviceList))
|
|
}
|
|
|
|
// Actions
|
|
setState(updater: (prev: BleState) => BleState) {
|
|
this.state = updater(this.state)
|
|
}
|
|
}
|
|
|
|
// Global instance
|
|
export const bleStore = new BleStore()
|