105 lines
2.8 KiB
JavaScript
105 lines
2.8 KiB
JavaScript
var storage = require('../../utils/storage')
|
|
var planMod = require('../../utils/plan')
|
|
var util = require('../../utils/util')
|
|
var themeMod = require('../../utils/theme')
|
|
|
|
Page({
|
|
data: {
|
|
theme: { primary: '#FF6B35', primaryLight: '#FF8C5A', primaryBg: '#FFF3ED', primaryRgb: '255,107,53' },
|
|
themeStyle: '',
|
|
todayTarget: 0,
|
|
todayDone: false,
|
|
todayDuration: 0,
|
|
todayTargetText: '',
|
|
streakCount: 0,
|
|
planName: '',
|
|
planDay: 1,
|
|
planTotal: 7,
|
|
planProgress: 0,
|
|
showPicker: false,
|
|
presets: [30, 60, 90, 120, 180, 300],
|
|
customDuration: 60,
|
|
customInput: ''
|
|
},
|
|
|
|
onLoad: function () {
|
|
this.refresh()
|
|
},
|
|
|
|
onShow: function () {
|
|
themeMod.applyThemeToPage(this)
|
|
try {
|
|
var tb = this.getTabBar()
|
|
if (tb) tb.setData({ selected: 0 })
|
|
} catch (e) {}
|
|
this.refresh()
|
|
},
|
|
|
|
onPullDownRefresh: function () {
|
|
this.refresh()
|
|
wx.stopPullDownRefresh()
|
|
},
|
|
|
|
refresh: function () {
|
|
var settings = storage.getSettings()
|
|
var streak = storage.getStreak()
|
|
var todayRecord = storage.getTodayRecord()
|
|
|
|
var records = []
|
|
var allRecords = storage.getRecords()
|
|
Object.keys(allRecords).forEach(function (key) {
|
|
records = records.concat(allRecords[key])
|
|
})
|
|
|
|
var plan = planMod.getPlan(settings.planId)
|
|
var planDay = planMod.getPlanDay(settings.planId, records)
|
|
var target = planMod.getTodayTarget(settings.planId, Math.min(planDay, plan.totalDays))
|
|
|
|
this.setData({
|
|
todayTarget: target,
|
|
todayDone: todayRecord && todayRecord.duration >= target,
|
|
todayDuration: todayRecord ? todayRecord.duration : 0,
|
|
todayTargetText: util.formatTime(target),
|
|
streakCount: streak.count,
|
|
planName: plan.name,
|
|
planDay: Math.min(planDay, plan.totalDays),
|
|
planTotal: plan.totalDays,
|
|
planProgress: Math.round((Math.min(planDay, plan.totalDays) / plan.totalDays) * 100)
|
|
})
|
|
},
|
|
|
|
onStartTrain: function () {
|
|
wx.navigateTo({ url: '/pages/timer/timer' })
|
|
},
|
|
|
|
onFreeTrain: function () {
|
|
this.setData({ showPicker: true, customInput: '', customDuration: 60 })
|
|
},
|
|
|
|
onClosePicker: function () {
|
|
this.setData({ showPicker: false })
|
|
},
|
|
|
|
onSelectPreset: function (e) {
|
|
var val = e.currentTarget.dataset.value
|
|
this.setData({ customDuration: val, customInput: '' })
|
|
},
|
|
|
|
onCustomInput: function (e) {
|
|
var val = parseInt(e.detail.value) || 0
|
|
this.setData({ customInput: e.detail.value, customDuration: val })
|
|
},
|
|
|
|
onConfirmFree: function () {
|
|
var dur = this.data.customDuration
|
|
if (!dur || dur <= 0) {
|
|
wx.showToast({ title: '请输入有效时长', icon: 'none' })
|
|
return
|
|
}
|
|
this.setData({ showPicker: false })
|
|
wx.navigateTo({ url: '/pages/timer/timer?free=' + dur })
|
|
},
|
|
|
|
noop: function () {}
|
|
})
|