Skip to content

Commit

Permalink
feat: add more infomation for mini tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
Z233 committed Oct 4, 2023
1 parent 9d3323d commit 82c89fe
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 29 deletions.
2 changes: 2 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { getActivityDataKey } from './util/helper'
import { isArray, isNumber } from 'lodash-es'
import type { Column } from '@tanstack/react-table'
import { planRecordSchema } from './schemas'

export class MdTableParser {
static parse(markupOrLines: string | string[]) {
Expand Down Expand Up @@ -136,6 +137,7 @@ export class Parser {
const activitiesRows = table
.getRows()
.slice(2)
.filter(row => row.getWidth() === (Object.keys(planRecordSchema.shape).length))
.map((row) => row.getCells().map((cell) => cell.content))

const activitiesData: Activity[] = activitiesRows.map((row) =>
Expand Down
4 changes: 2 additions & 2 deletions src/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class Scheduler {
actLen: 0,
}
})

this.activities = activities
this.schedule()

Expand Down Expand Up @@ -70,7 +70,7 @@ export class Scheduler {
duration: p.duration,
activities: this.schedulePart(p.duration, p.activities),
}))

const scheduledActivities: ScheduledActivity[] = this.mergeParts(parts)
this.activities = scheduledActivities
}
Expand Down
45 changes: 24 additions & 21 deletions src/tracker/plan-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ export type TrackerState = {
upcoming: Upcoming | null
}

type Ongoing = { activity: ScheduledActivity; leftSecs: number; progress: number }
type Ongoing = {
activity: ScheduledActivity
leftMins: number
timeoutMins: number
progress: number
}
type Upcoming = { activity: ScheduledActivity }

export type Observer = {
Expand Down Expand Up @@ -138,7 +143,7 @@ export class PlanTracker {
progress: 0,
leftSecs: 0,
leftMins: 0,
isAllDone: false,
timeoutMins: 0,
}

if (!this.scheduler) return initialState
Expand All @@ -149,38 +154,35 @@ export class PlanTracker {
(a) => nowMins >= a.start && a.isFixed
)
const now = this.scheduler.activities[nowIndex]

if (nowIndex === this.scheduler.activities.length - 1 && nowMins >= now.stop) {
return {
...initialState,
now,
isAllDone: true,
}
}
const nowIsLast = nowIndex === this.scheduler.activities.length - 1

const durationMins = nowMins - now.start
const durationSecs = durationMins * 60 + new Date().getSeconds()
const totalMins = now.actLen
const totalSecs = totalMins * 60
const progress = (durationSecs / totalSecs) * 100

const next = find(this.scheduler.activities, (a) => a.actLen >= 0, nowIndex + 1)

const progress = totalMins > 0 ? (durationSecs / totalSecs) * 100 : 100

console.log({ durationMins, durationSecs, totalMins, progress, totalSecs })

const next = nowIsLast
? null
: find(this.scheduler.activities, (a) => a.actLen >= 0, nowIndex + 1)

return {
...initialState,
now,
next,
progress: progress <= 100 ? progress : 100,
progress,
leftSecs: totalSecs - durationSecs,
leftMins: totalMins - durationMins,
isAllDone: false,
timeoutMins: durationMins - totalMins,
}
}

private async onTick() {
if (!this.scheduler) return

const { leftSecs, ...props } = this.computeProgress()
const { leftMins, timeoutMins, ...props } = this.computeProgress()

this.updateStatusBar(props)
const { now, next, progress } = props
Expand All @@ -190,20 +192,21 @@ export class PlanTracker {
this.now = now
}

const ongoing: Ongoing | null = now
const ongoing: Maybe<Ongoing> = now
? {
activity: now,
leftSecs,
leftMins,
timeoutMins,
progress,
}
: null

const upcoming: Upcoming | null = next
const upcoming: Maybe<Upcoming> = next
? {
activity: next,
}
: null

this.notifyObservers({ ongoing, upcoming })

// ================== Notification ==================
Expand Down
2 changes: 1 addition & 1 deletion src/window/mini-tracker/MiniClock.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export let progress = 0
export let size: number
$: angle = (360 * progress) / 100
$: angle = progress >= 0 && progress <= 100 ? (360 * progress) / 100 : 360
</script>

<div
Expand Down
18 changes: 16 additions & 2 deletions src/window/mini-tracker/MiniTrackerWindow.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@
let now: Maybe<ScheduledActivity> = null
let next: Maybe<ScheduledActivity> = null
let progress = 0
let leftMins = 0
let timeoutMins = 0
let estimatedLeftMins = 0
$: {
if (now) {
estimatedLeftMins = leftMins - (leftMins % 5)
console.log({ leftMins, estimatedLeftMins, timeoutMins, next })
estimatedLeftMins = estimatedLeftMins >= 0 ? estimatedLeftMins : 5
}
}
const ipcRenderer = require('electron').ipcRenderer
ipcRenderer.on('update', (e, payload: TrackerState) => {
if (!payload) return
Expand All @@ -16,6 +27,8 @@
if (ongoing) {
now = ongoing.activity
progress = ongoing.progress
leftMins = ongoing.leftMins
timeoutMins = -leftMins
}
if (upcoming) {
Expand Down Expand Up @@ -78,7 +91,8 @@
<div class="col-span-3 space-y-1">
<div class="text-gray-900 truncate">{now?.activity ?? 'No activity'}</div>
<div class="text-xs text-gray-400 truncate">
{next ? `Next: ${next.activity}` : 'All done'}
{ timeoutMins < 0 ? `~${estimatedLeftMins}` : `+${timeoutMins}` } mins
{ next ? `> ${next.activity}` : ''}
</div>
</div>
</div>
Expand Down
4 changes: 1 addition & 3 deletions src/window/mini-tracker/mini-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const SNAPPING_DISTANCE = 24
const SNAPPING_VISIBLE_EDGE = 16

const Size = {
WIDTH: 180,
WIDTH: 200,
HEIGHT: 60,
}

Expand All @@ -23,8 +23,6 @@ export class MiniTracker {

private onCloseCallbacks: (() => void)[] = []

private isSnapping = false

private constructor(private store: DataStore, private tracker: PlanTracker) {}

static new(store: DataStore, tracker: PlanTracker) {
Expand Down

0 comments on commit 82c89fe

Please sign in to comment.