refactor: ES6+ modernization + 2 bugfixes
- var→const/let, function→arrow/class across all 13 JS files - Timer: prototype→class with ES6 getters - plan days: IIFE→Array.from declarative generation - storage/plan: unified formatDate via util.js - Bugfix: getPlanDay now filters by planId (plan switch accuracy) - Bugfix: getTodayRecord searches all months (cross-month boundary) - WXML/WXSS unchanged; public API unchanged
This commit is contained in:
+29
-36
@@ -1,7 +1,7 @@
|
||||
var Timer = require('../../utils/timer')
|
||||
var storage = require('../../utils/storage')
|
||||
var planMod = require('../../utils/plan')
|
||||
var themeMod = require('../../utils/theme')
|
||||
const Timer = require('../../utils/timer')
|
||||
const storage = require('../../utils/storage')
|
||||
const planMod = require('../../utils/plan')
|
||||
const themeMod = require('../../utils/theme')
|
||||
|
||||
Page({
|
||||
data: {
|
||||
@@ -19,26 +19,21 @@ Page({
|
||||
isFreeMode: false
|
||||
},
|
||||
|
||||
onLoad: function (options) {
|
||||
onLoad(options) {
|
||||
themeMod.applyThemeToPage(this)
|
||||
|
||||
var target
|
||||
var planDay = 1
|
||||
var isFreeMode = false
|
||||
let target
|
||||
let planDay = 1
|
||||
let isFreeMode = false
|
||||
|
||||
if (options && options.free) {
|
||||
target = parseInt(options.free) || 60
|
||||
isFreeMode = true
|
||||
} else {
|
||||
var settings = storage.getSettings()
|
||||
var records = []
|
||||
var allRecords = storage.getRecords()
|
||||
Object.keys(allRecords).forEach(function (key) {
|
||||
records = records.concat(allRecords[key])
|
||||
})
|
||||
|
||||
var plan = planMod.getPlan(settings.planId)
|
||||
planDay = planMod.getPlanDay(settings.planId, records)
|
||||
const settings = storage.getSettings()
|
||||
const allRecords = Object.values(storage.getRecords()).flat()
|
||||
const plan = planMod.getPlan(settings.planId)
|
||||
planDay = planMod.getPlanDay(settings.planId, allRecords)
|
||||
target = planMod.getTodayTarget(settings.planId, Math.min(planDay, plan.totalDays))
|
||||
}
|
||||
|
||||
@@ -47,19 +42,18 @@ Page({
|
||||
remaining: target,
|
||||
todayTarget: target,
|
||||
planDay: isFreeMode ? 0 : Math.min(planDay, 99),
|
||||
isFreeMode: isFreeMode
|
||||
isFreeMode
|
||||
})
|
||||
|
||||
var self = this
|
||||
this._timer = new Timer({
|
||||
onTick: function (tick) {
|
||||
self.setData({
|
||||
onTick: (tick) => {
|
||||
this.setData({
|
||||
remaining: tick.remaining > 0 ? tick.remaining : 0,
|
||||
overtime: tick.remaining <= 0 ? tick.elapsed - self.data.duration : 0
|
||||
overtime: tick.remaining <= 0 ? tick.elapsed - this.data.duration : 0
|
||||
})
|
||||
},
|
||||
onComplete: function () {
|
||||
self.setData({ status: 'completed', isCompleted: true })
|
||||
onComplete: () => {
|
||||
this.setData({ status: 'completed', isCompleted: true })
|
||||
if (storage.getSettings().vibrate) {
|
||||
try { wx.vibrateLong() } catch (e) {}
|
||||
}
|
||||
@@ -68,11 +62,11 @@ Page({
|
||||
})
|
||||
},
|
||||
|
||||
onUnload: function () {
|
||||
onUnload() {
|
||||
if (this._timer) this._timer.stop()
|
||||
},
|
||||
|
||||
onStart: function () {
|
||||
onStart() {
|
||||
if (this.data.isPaused) {
|
||||
this._timer.resume()
|
||||
this.setData({ isRunning: true, isPaused: false, status: 'running' })
|
||||
@@ -83,26 +77,25 @@ Page({
|
||||
this.setData({ isRunning: true, status: 'running' })
|
||||
},
|
||||
|
||||
onPause: function () {
|
||||
onPause() {
|
||||
if (!this.data.isRunning || this.data.isPaused) return
|
||||
this._timer.pause()
|
||||
this.setData({ isPaused: true, status: 'paused' })
|
||||
},
|
||||
|
||||
onStop: function () {
|
||||
var self = this
|
||||
onStop() {
|
||||
wx.showModal({
|
||||
title: '结束训练',
|
||||
content: '确定要结束本次训练吗?',
|
||||
success: function (res) {
|
||||
if (res.confirm) self.finishTraining()
|
||||
success: (res) => {
|
||||
if (res.confirm) this.finishTraining()
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
finishTraining: function () {
|
||||
var elapsed = this._timer.stop()
|
||||
var today = storage.getToday()
|
||||
finishTraining() {
|
||||
const elapsed = this._timer.stop()
|
||||
const today = storage.getToday()
|
||||
storage.saveRecord({
|
||||
date: today,
|
||||
duration: elapsed,
|
||||
@@ -111,7 +104,7 @@ Page({
|
||||
})
|
||||
storage.updateStreak(today)
|
||||
|
||||
wx.showToast({ title: '已记录 ' + elapsed + '秒', icon: 'none', duration: 1500 })
|
||||
setTimeout(function () { wx.navigateBack() }, 1500)
|
||||
wx.showToast({ title: `已记录 ${elapsed}秒`, icon: 'none', duration: 1500 })
|
||||
setTimeout(() => { wx.navigateBack() }, 1500)
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user