xyyd-fatfox/src/widgets/work/useTomatoStore.ts

148 lines
4.1 KiB
TypeScript

import { sendParent } from "@/utils/parent";
import { generateRandomString } from "@/utils/tool";
import useTimeStore from "@/utils/useTimeStore";
import dayjs from "dayjs";
import { defineStore } from "pinia";
import { computed, reactive, ref, watch } from "vue";
const TOTAL_TIME = 60 * 60 * 15
export type TomatoTarget = {
id: string;
finishTime: number;
remindTime: number | null;
title: string;
isCompleted: boolean;
}
export type TomatoTime = {
date: number;
finishTime: number;
}
export const musicList = [
{
name: 'A Part of Us',
music: 'https://newfatfox.oss-cn-beijing.aliyuncs.com/admin/music/aPartOfUs.mp3'
},
{
name: 'a signal rose',
music: 'https://newfatfox.oss-cn-beijing.aliyuncs.com/admin/music/AsignalRose.mp3'
},
{
name: 'a thousand lifetimes',
music: 'https://newfatfox.oss-cn-beijing.aliyuncs.com/admin/music/aThousandLifetimes.mp3'
},
{
name: 'A Very Brady Special',
music: 'https://newfatfox.oss-cn-beijing.aliyuncs.com/admin/music/AVeryBradySpecial.mp3'
},
{
name: 'A Wonderful Story',
music: 'https://newfatfox.oss-cn-beijing.aliyuncs.com/admin/music/AWonderfulStore.mp3'
}
]
const initData = {
list: [] as TomatoTarget[],
timeList: [] as number[],
isPlaying: false as boolean,
selectMusic: 0,
isStart: false as boolean,
beginTime: -1 as number,
}
export default defineStore("work", () => {
const state = reactive({ ...initData })
const audio = new Audio()
const time = useTimeStore()
const remainingTime = computed(() => {
return dayjs(state.beginTime).add(15, 'minute').diff(dayjs(time.date), 'second')
})
const stopTomatoTime = () => {
state.isStart = false
state.beginTime = -1
stopMusic()
}
const togglePlay = () => {
if (state.isPlaying) {
audio.pause()
} else {
audio.play()
}
state.isPlaying = !state.isPlaying
}
const setTrack = (trackIndex: number) => {
state.selectMusic = trackIndex
audio.src = musicList[trackIndex].music
audio.loop = true
audio.load()
if (state.isPlaying) {
audio.play()
}
}
const stopMusic = () => {
audio.pause()
audio.currentTime = 0
state.isPlaying = false
}
const beginTomatoTime = () => {
state.beginTime = dayjs().valueOf()
state.isStart = true
setTrack(state.selectMusic)
togglePlay()
}
watch(remainingTime, (val) => {
if (val < 0) {
stopTomatoTime()
}
// sendParent(['information', '恭喜你完成了一个番茄钟' + generateRandomString(4)])
if (remainingTime.value === 0) {
state.timeList.push(
dayjs().valueOf()
)
sendParent(['information', '恭喜你完成了一个番茄钟'])
}
})
const openShowModel = ref<undefined | null | TomatoTarget>()
const openFullscreen = ref(false)
const todayHour = computed(() => {
return state.timeList.filter(val => dayjs(val).isSame(dayjs(), 'day')).length
})
const yestodayHour = computed(() => {
return state.timeList.filter(val => dayjs(val).isSame(dayjs().subtract(1, 'day'), 'day')).length
})
const todayFinishTarget = computed(() => {
return state.list.filter(val => dayjs(val.finishTime).isSame(dayjs(), 'day') && val.isCompleted).length
})
const yesFinishTarget = computed(() => {
return state.list.filter(val => dayjs(val.finishTime).isSame(dayjs().subtract(1, 'day'), 'day') && val.isCompleted).length
})
const reset = () => {
Object.assign(state, { ...initData })
}
return {
state,
openShowModel,
openFullscreen,
beginTomatoTime,
remainingTime,
togglePlay,
stopMusic,
stopTomatoTime,
todayHour,
yestodayHour,
todayFinishTarget,
yesFinishTarget,
setTrack,
reset
}
}, {
persist: true
})