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' } ] export default defineStore("work", () => { const state = reactive({ list: [] as TomatoTarget[], timeList: [] as number[], isPlaying: false as boolean, selectMusic: 0, isStart: false as boolean, beginTime: -1 as number, }) const audio = new Audio() const time = useTimeStore() const remainingTime = computed(() => { if (!state.isStart) { return 0 } return dayjs(state.beginTime).add(1, 'minute').diff(dayjs(time.date), 'second') }) const stopTomatoTime = () => { state.isStart = false state.beginTime = -1 stopMusic() if (remainingTime.value <= 0) { state.timeList.push( dayjs().valueOf() ) } } 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() } }) const openShowModel = ref() 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 }) return { state, openShowModel, openFullscreen, beginTomatoTime, remainingTime, togglePlay, stopMusic, stopTomatoTime, todayHour, yestodayHour, todayFinishTarget, yesFinishTarget, setTrack } }, { persist: true })