LiveCountdownComponent.ets
4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import font from '@ohos.font'
import { LiveDetailsBean } from 'wdBean/Index'
import { DateTimeUtils, StringUtils } from 'wdKit/Index'
@Component
export struct LiveCountdownComponent {
@Consume @Watch('calculateDataStatus') liveDetailsBean: LiveDetailsBean
textTimerController: TextTimerController = new TextTimerController()
@State format: string = 'HH:mm:ss'
@State month: string = ''
@State day: string = ''
@State hour: string = ''
@State minute: string = ''
@State differenceTimeStamp: number = 0
@State isCountDownStart: boolean = false
aboutToAppear(): void {
//注册字体
font.registerFont({
familyName: 'BebasNeue_Regular',
familySrc: $rawfile('font/BebasNeue_Regular.otf')
})
setTimeout(() => {
this.textTimerController.start()
}, 2000)
}
build() {
Column() {
this.showTitle()
this.showCountDown()
this.showAppointment()
}.padding({
top: 20,
left: 20,
right: 20,
bottom: 24
})
.backgroundColor(Color.White)
.border({ radius: 6 })
.margin({ top: 16 })
}
aboutToDisappear(): void {
this.textTimerController.pause()
}
@Builder
showTitle() {
Text(this.isCountDownStart ? '距离直播开始还有' : '直播开始时间')
.maxLines(2)
.textOverflow({ overflow: TextOverflow.Ellipsis })
.fontSize('14fp')
.fontWeight(400)
.fontColor('#222222')
}
@Builder
showCountDown() {
Row() {
Text(this.month)
.showTimeStyleBold()
Text('月')
.showTimeStyleNormal()
.margin({ left: 3 })
Text(this.day)
.showTimeStyleBold()
.margin({ left: 3 })
Text('日')
.showTimeStyleNormal()
.margin({ left: 3 })
Text(this.hour)
.showTimeStyleBold()
.margin({ left: 10 })
Text(':')
.showTimeStyleBold()
Text(this.minute)
.showTimeStyleBold()
}
.margin({ top: 10 })
.visibility(this.isCountDownStart ? Visibility.None : Visibility.Visible
)
TextTimer({ isCountDown: true, count: this.differenceTimeStamp, controller: this.textTimerController })
.format(this.format)
.fontSize('40fp')
.fontWeight(FontWeight.Bold)
.fontColor('#222222')
.fontFamily('BebasNeue_Regular')
.onTimer((utc: number, elapsedTime: number) => {
console.info('textTimer notCountDown utc is:' + utc + ', elapsedTime: ' + elapsedTime)
})
.margin({ top: 10 })
.visibility(this.isCountDownStart ? Visibility.Visible : Visibility.None)
}
@Builder
showAppointment() {
Text('我要预约')
.width('100%')
.height(42)
.textAlign(TextAlign.Center)
.fontSize('16fp')
.fontWeight(400)
.fontColor(Color.White)
.margin({
top: 16
})
.border({ radius: 4 })
.backgroundColor('#ED2800')
// .backgroundColor('#CCCCCC')
}
calculateDataStatus() {
let startTimeStamp: number = DateTimeUtils.getDateTimestamp(this.liveDetailsBean.liveInfo.planStartTime)
let currentTimeStamp: number = DateTimeUtils.getTimeStamp()
let _differenceTimeStampTmp = startTimeStamp - currentTimeStamp
this.isCountDownStart = _differenceTimeStampTmp <= 4 * 60 * 60 * 1000
if (this.isCountDownStart) {
this.differenceTimeStamp = _differenceTimeStampTmp
return
}
//2024-04-01 19:44:00-trim->2024-04-0119:44:00
if (StringUtils.isNotEmpty(this.liveDetailsBean.liveInfo.planStartTime)) {
let playStartTimeTmp = this.liveDetailsBean.liveInfo.planStartTime.trim()
this.month = playStartTimeTmp.substring(5, 7)
this.day = playStartTimeTmp.substring(8, 10)
this.hour = playStartTimeTmp.substring(11, 13)
this.minute = playStartTimeTmp.substring(14, 16)
}
}
}
@Extend(Text)
function showTimeStyleNormal() {
.fontSize('16fp')
.fontWeight(500)
.fontColor('#222222')
}
@Extend(Text)
function showTimeStyleBold() {
.fontSize('40fp')
.fontFamily('BebasNeue_Regular')
.fontWeight(FontWeight.Bold)
.fontColor('#222222')
}