page_info.dart
3.75 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 'dart:convert';
import 'package:crypto/crypto.dart';
import 'package:flutter/material.dart';
import '../config/config.dart';
import '../config/manager.dart';
import '../utils/element_util.dart';
class PageInfo {
PageInfo._(this.timer);
factory PageInfo.fromElement(Element element, Route route) {
AutoTrackPageConfig pageConfig = AutoTrackConfigManager.instance.getPageConfig(element.widget);
PageInfo pageInfo = PageInfo._(PageTimer());
pageInfo._pageKey = element.widget.runtimeType.toString();
pageInfo._pagePath = pageConfig.pagePath ?? route.settings.name ?? '';
pageInfo._pageTitle = pageConfig.pageTitle ?? pageInfo._findTitle(element) ?? '';
pageInfo._pageManualKey = pageConfig.pageID ?? md5.convert(utf8.encode('${pageInfo._pageKey}${pageInfo._pagePath}${pageInfo._pageTitle}')).toString();
pageInfo.ignore = pageConfig.ignore;
return pageInfo;
}
final PageTimer timer;
bool isBack = false;
bool ignore = false;
String _pageKey = '';
String get pageKey => _pageKey;
String _pageTitle = '';
String get pageTitle => _pageTitle;
String _pageManualKey = '';
String get pageManualKey => _pageManualKey;
String _pagePath = '';
String get pagePath => _pagePath;
String? _findTitle(Element element) {
String? title;
ElementUtil.walkElement(element, (child, _) {
if (child.widget is NavigationToolbar) {
NavigationToolbar toolBar = child.widget as NavigationToolbar;
if (toolBar.middle == null) {
return false;
}
if (toolBar.middle is Text) {
title = (toolBar.middle as Text).data;
return false;
}
int layoutIndex = 0;
if (toolBar.leading != null) {
layoutIndex += 1;
}
title = _findTextsInMiddle(child, layoutIndex);
return false;
}
return true;
});
return title;
}
String? _findTextsInMiddle(Element element, int layoutIndex) {
String? text;
int index = 0;
ElementUtil.walkElement(element, ((child, _) {
if (child.widget is LayoutId) {
if (index == layoutIndex) {
text = ElementUtil.findTexts(child).join('');
return false;
}
index += 1;
}
return true;
}));
return text;
}
@override
String toString() => '{ pageKey: $pageKey, pageTitle: $pageTitle, pageManualKey: $pageManualKey, pagePath: $pagePath, isBack: $isBack }';
}
enum PageTimerState {
Init,
Start,
Pause,
Resume,
End,
}
class PageTimer {
PageTimer();
PageTimerState _state = PageTimerState.Init;
PageTimerState get state => _state;
int _lastTimeStamp = 0;
Duration _duration = const Duration();
Duration get duration => _duration;
int _computeMilliseconds() {
return DateTime.now().millisecondsSinceEpoch - _lastTimeStamp;
}
start() {
if (_state != PageTimerState.Init && _state != PageTimerState.End) {
return;
}
_state = PageTimerState.Start;
_lastTimeStamp = DateTime.now().millisecondsSinceEpoch;
_duration = const Duration();
}
pause() {
if (_state != PageTimerState.Start && _state != PageTimerState.Resume) {
return;
}
_state = PageTimerState.Pause;
_duration = Duration(milliseconds: _duration.inMilliseconds + _computeMilliseconds());
}
resume() {
if (_state != PageTimerState.Pause) {
return;
}
_state = PageTimerState.Resume;
_lastTimeStamp = DateTime.now().millisecondsSinceEpoch;
}
end() {
if (_state == PageTimerState.Pause) {
_state = PageTimerState.End;
return;
}
if (_state == PageTimerState.Start || _state == PageTimerState.Resume) {
_state = PageTimerState.End;
_duration = Duration(milliseconds: _duration.inMilliseconds + _computeMilliseconds());
}
}
}