WithLastDateHeader.js
2.22 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
/*
*
* Created by Stone
* https://github.com/bolan9999
* Email: shanshang130@gmail.com
* Date: 2019/1/25
*
*/
import React from "react";
import { Text, StyleSheet } from "react-native";
import { NormalHeader } from "../NormalHeader";
import { HeaderStatus } from "../RefreshHeader";
import AsyncStorage from '@react-native-async-storage/async-storage';
const RefreshKey = "org.bolan9999.spring_scrollview.refresh_time";
export class WithLastDateHeader extends NormalHeader {
lastTime: Date;
constructor(props) {
super(props);
AsyncStorage.getItem(RefreshKey)
.then(value => {
if (value) this.lastTime = new Date(value);
})
.catch();
}
onStateChange(oldStatus: HeaderStatus, newStatus: HeaderStatus) {
if (newStatus === "refreshing") {
const now = new Date();
AsyncStorage.setItem(RefreshKey, now.toString())
.then(() => {
this.lastTime = now;
})
.catch();
}
super.onStateChange(oldStatus, newStatus);
}
renderContent() {
return (
<Text style={styles.text}>
{this.getText()}
{this.getTimeDescription()}
</Text>
);
}
getText() {
return "Last Updated: ";
}
getTimeDescription() {
if (!this.lastTime) {
return "never";
}
const now = new Date();
let timeInterval = Math.floor(
(now.getTime() - this.lastTime.getTime()) / 1000
);
if (timeInterval < 60) {
return `${timeInterval}s ago`;
} else if (timeInterval < 3600) {
return `${Math.floor(timeInterval / 60)}m ago`;
} else if (
now.getFullYear() === this.lastTime.getFullYear() &&
now.getMonth() === this.lastTime.getMonth() &&
now.getDate() === this.lastTime.getDate()
) {
return `${this.lastTime.getHours()}:${this.lastTime.getMinutes()}`;
} else if (now.getFullYear() === this.lastTime.getFullYear()) {
return `${this.lastTime.getMonth() + 1}-${this.lastTime.getDate()}`;
} else {
return `${this.lastTime.getMonth() +
1}-${this.lastTime.getDate()}-${this.lastTime.getFullYear()}`;
}
}
}
const styles = StyleSheet.create({
text: {
marginVertical: 5,
fontSize: 12,
color: "#666",
textAlign: "center",
width: 130
}
});