feat(usage): 使用记录按天分组
This commit is contained in:
1 parent
02d98fb8b3
commit
f28bf0ae0a
3 files changed
+43
-10
No files matched your search
@@ -12,6 +12,8 @@ interface SessionRow {
|
||||
appName: string
|
||||
iconUrl: string
|
||||
timeText: string
|
||||
/** 组头日期,仅每天第一条有值;为空表示组内后续项 */
|
||||
dayHeader: string
|
||||
}
|
||||
|
||||
interface AppRow {
|
||||
@@ -111,7 +113,15 @@ Page({
|
||||
const result = await getUsageSessionsPage(sn, reset ? undefined : this.beforeTs)
|
||||
const items = (result.items || []).filter((i) => (Number(i.durationMs) || 0) >= 1000)
|
||||
const base = reset ? 0 : this.data.sessions.length
|
||||
const rows = items.map((item, index) => toSessionRow(item, base + index))
|
||||
// 按天分组:记录与上一条不同日期时生成组头
|
||||
let lastDay = reset ? '' : this.lastRenderedDay()
|
||||
const rows: SessionRow[] = items.map((item, index) => {
|
||||
const start = new Date(Number(item.startTs) || 0)
|
||||
const day = dayText(start)
|
||||
const header = day !== lastDay ? day : ''
|
||||
lastDay = day
|
||||
return toSessionRow(item, base + index, header)
|
||||
})
|
||||
this.beforeTs = Number(result.nextBeforeTs) || 0
|
||||
this.setData({
|
||||
sessions: reset ? rows : this.data.sessions.concat(rows),
|
||||
@@ -120,6 +130,17 @@ Page({
|
||||
void this.syncMissing(items)
|
||||
},
|
||||
|
||||
/** 已渲染的最后一条记录的日期(分页追加时用于判断是否需要新组头) */
|
||||
lastRenderedDay(): string {
|
||||
const list = this.data.sessions
|
||||
if (!list.length) return ''
|
||||
// 回溯找到最近一个带组头的行,反推日期
|
||||
for (let i = list.length - 1; i >= 0; i--) {
|
||||
if (list[i].dayHeader) return list[i].dayHeader
|
||||
}
|
||||
return ''
|
||||
},
|
||||
|
||||
async loadMore() {
|
||||
if (this.data.loadingMore || !this.data.hasMore) return
|
||||
const sn = getDeviceSn()
|
||||
@@ -183,7 +204,7 @@ Page({
|
||||
}
|
||||
})
|
||||
|
||||
function toSessionRow(item: UsageSession, index: number): SessionRow {
|
||||
function toSessionRow(item: UsageSession, index: number, dayHeader: string): SessionRow {
|
||||
const start = new Date(Number(item.startTs) || 0)
|
||||
const end = new Date(Number(item.endTs) || 0)
|
||||
const range = item.open
|
||||
@@ -193,7 +214,8 @@ function toSessionRow(item: UsageSession, index: number): SessionRow {
|
||||
key: `${index}_${item.startTs}`,
|
||||
appName: item.appName || item.packageName || '未知应用',
|
||||
iconUrl: item.iconUrl || '',
|
||||
timeText: `${dayText(start)} ${range}`
|
||||
timeText: range,
|
||||
dayHeader
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,14 +22,17 @@
|
||||
<!-- 按时间 -->
|
||||
<block wx:if="{{tab === 'timeline'}}">
|
||||
<view wx:if="{{!sessions.length && !loading}}" class="empty">暂无使用记录</view>
|
||||
<view wx:for="{{sessions}}" wx:key="key" class="card record-row">
|
||||
<image wx:if="{{item.iconUrl}}" class="app-icon" src="{{item.iconUrl}}" mode="aspectFill" />
|
||||
<view wx:else class="app-icon app-icon-fallback">📱</view>
|
||||
<view class="record-body">
|
||||
<text class="record-title">{{item.appName}}</text>
|
||||
<text class="record-meta">{{item.timeText}}</text>
|
||||
<block wx:for="{{sessions}}" wx:key="key">
|
||||
<view wx:if="{{item.dayHeader}}" class="day-header">{{item.dayHeader}}</view>
|
||||
<view class="card record-row">
|
||||
<image wx:if="{{item.iconUrl}}" class="app-icon" src="{{item.iconUrl}}" mode="aspectFill" />
|
||||
<view wx:else class="app-icon app-icon-fallback">📱</view>
|
||||
<view class="record-body">
|
||||
<text class="record-title">{{item.appName}}</text>
|
||||
<text class="record-meta">{{item.timeText}}</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</block>
|
||||
<view wx:if="{{loadingMore}}" class="empty small">加载中…</view>
|
||||
</block>
|
||||
|
||||
|
||||
@@ -158,3 +158,11 @@
|
||||
.empty.small {
|
||||
padding: 24rpx 0;
|
||||
}
|
||||
|
||||
/* 按天分组的日期组头 */
|
||||
.day-header {
|
||||
padding: 16rpx 8rpx 8rpx;
|
||||
font-size: 24rpx;
|
||||
font-weight: 600;
|
||||
color: var(--sub);
|
||||
}
|
||||
Reference in new issue
Block a user