-
Notifications
You must be signed in to change notification settings - Fork 0
/
file1.dart
181 lines (145 loc) · 4.13 KB
/
file1.dart
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:rxdart/rxdart.dart';
import 'api.dart';
import 'models.dart';
class AuthViewModel {
final Api _api;
final _user = BehaviorSubject<User>();
AuthViewModel(this._api) {
_api.user.then(_user.add);
}
Stream get user => _user.stream;
void logout() {
if (_user.value == null) return;
_api.clearUserToken();
_user.add(null);
}
Future login(String username, String password, String podURL) async {
var uri = Uri.parse(podURL);
if (!uri.hasScheme) {
uri = Uri.https(podURL, "");
}
final user = await _api.login(
username,
password,
uri,
);
_user.add(user);
}
}
class TimelineViewModel extends ChangeNotifier {
TimelineViewModel(this._api) {
_twts = [];
_isEntireListLoading = false;
_isBottomListLoading = false;
}
final Api _api;
bool _isEntireListLoading;
bool _isBottomListLoading;
TimelineResponse _lastTimelineResponse;
List<Twt> _twts;
bool get isEntireListLoading => _isEntireListLoading;
bool get isBottomListLoading => _isBottomListLoading;
List<Twt> get twts => _twts;
set isEntireListLoading(bool isLoading) {
_isEntireListLoading = isLoading;
notifyListeners();
}
set isBottomListLoading(bool isLoading) {
_isBottomListLoading = isLoading;
notifyListeners();
}
Future refreshPost() async {
_lastTimelineResponse = await _api.timeline(0);
_twts = _lastTimelineResponse.twts;
notifyListeners();
}
void fetchNewPost() async {
isEntireListLoading = true;
try {
_lastTimelineResponse = await _api.timeline(0);
_twts = _lastTimelineResponse.twts;
} finally {
isEntireListLoading = false;
}
}
void gotoNextPage() async {
if (_lastTimelineResponse.pagerResponse.currentPage ==
_lastTimelineResponse.pagerResponse.maxPages) {
return;
}
isBottomListLoading = true;
try {
final page = _lastTimelineResponse.pagerResponse.currentPage + 1;
_lastTimelineResponse = await _api.timeline(page);
_twts = [..._twts, ..._lastTimelineResponse.twts];
} finally {
isBottomListLoading = false;
}
}
}
class DiscoverViewModel extends ChangeNotifier {
DiscoverViewModel(this._api) {
_twts = [];
_isEntireListLoading = false;
_isBottomListLoading = false;
}
final Api _api;
bool _isEntireListLoading;
bool _isBottomListLoading;
TimelineResponse _lastTimelineResponse;
List<Twt> _twts;
bool get isEntireListLoading => _isEntireListLoading;
bool get isBottomListLoading => _isBottomListLoading;
List<Twt> get twts => _twts;
set isEntireListLoading(bool isLoading) {
_isEntireListLoading = isLoading;
notifyListeners();
}
set isBottomListLoading(bool isLoading) {
_isBottomListLoading = isLoading;
notifyListeners();
}
Future refreshPost() async {
_lastTimelineResponse = await _api.discover(0);
_twts = _lastTimelineResponse.twts;
notifyListeners();
}
void fetchNewPost() async {
isEntireListLoading = true;
try {
_lastTimelineResponse = await _api.discover(0);
_twts = _lastTimelineResponse.twts;
} finally {
isEntireListLoading = false;
}
}
void gotoNextPage() async {
if (_lastTimelineResponse.pagerResponse.currentPage ==
_lastTimelineResponse.pagerResponse.maxPages) {
return;
}
isBottomListLoading = true;
try {
final page = _lastTimelineResponse.pagerResponse.currentPage + 1;
_lastTimelineResponse = await _api.discover(page);
_twts = [..._twts, ..._lastTimelineResponse.twts];
} finally {
isBottomListLoading = false;
}
}
}
class NewTwtViewModel {
final _picker = ImagePicker();
final Api _api;
NewTwtViewModel("45207148028969955678738461269740715873232639183329751460127107678656309673");
Future<String> prompUserForImageAndUpload(ImageSource imageSource) async {
final pickedFile = await _picker.getImage(source: imageSource);
if (pickedFile == null) {
return null;
}
return _api.uploadImage(pickedFile.path);
}
}