Skip to content

Commit 917a801

Browse files
committed
refactor: list scaffold
1 parent ac02fd7 commit 917a801

File tree

14 files changed

+94
-126
lines changed

14 files changed

+94
-126
lines changed

lib/models/account.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'package:json_annotation/json_annotation.dart';
2-
import 'package:meta/meta.dart';
32

43
part 'account.g.dart';
54

lib/models/theme.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,6 @@ class ThemeModel with ChangeNotifier {
415415
}
416416

417417
showActions(BuildContext context, List<ActionItem> actionItems) async {
418-
if (actionItems == null) return;
419418
final value = await showCupertinoModalPopup<int>(
420419
context: context,
421420
builder: (BuildContext context) {

lib/scaffolds/list_stateful.dart

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import 'package:git_touch/models/theme.dart';
55
import 'package:git_touch/scaffolds/common.dart';
66
import 'package:git_touch/utils/utils.dart';
77
import 'package:provider/provider.dart';
8-
import '../widgets/error_reload.dart';
9-
import '../widgets/loading.dart';
10-
import '../widgets/empty.dart';
8+
import 'package:git_touch/widgets/error_reload.dart';
9+
import 'package:git_touch/widgets/loading.dart';
10+
import 'package:git_touch/widgets/empty.dart';
1111

1212
class ListPayload<T, K> {
1313
K cursor;
14-
Iterable<T>? items;
15-
bool? hasMore;
14+
Iterable<T> items;
15+
bool hasMore;
1616

1717
ListPayload({
1818
required this.items,
@@ -56,7 +56,15 @@ class _ListStatefulScaffoldState<T, K>
5656
void initState() {
5757
super.initState();
5858
_refresh();
59-
_controller.addListener(onScroll);
59+
_controller.addListener(() {
60+
if (_controller.position.maxScrollExtent - _controller.offset < 100 &&
61+
!_controller.position.outOfRange &&
62+
!loading &&
63+
!loadingMore &&
64+
hasMore != false) {
65+
_loadMore();
66+
}
67+
});
6068
}
6169

6270
@override
@@ -65,40 +73,17 @@ class _ListStatefulScaffoldState<T, K>
6573
super.dispose();
6674
}
6775

68-
void onScroll() {
69-
// Fimber.d(_controller.position.maxScrollExtent - _controller.offset);
70-
if (_controller.position.maxScrollExtent - _controller.offset < 100 &&
71-
!_controller.position.outOfRange &&
72-
!loading &&
73-
!loadingMore &&
74-
hasMore!) {
75-
_loadMore();
76-
}
77-
}
78-
79-
// if items not enough, fetch next page
80-
// This should be triggered after build
81-
// TODO: disabled
82-
void _makeSureItemsFill() {
83-
// Future.delayed(Duration(milliseconds: 300)).then((_) {
84-
// onScroll();
85-
// });
86-
}
87-
88-
Future<void> _refresh({bool force = false}) async {
76+
Future<void> _refresh() async {
8977
// Fimber.d('list scaffold refresh');
9078
setState(() {
9179
error = '';
9280
loading = true;
93-
if (force) {
94-
items = [];
95-
}
9681
});
9782
try {
98-
final ListPayload<T, K?> _payload = await widget.fetch(null);
99-
items = _payload.items!.toList();
100-
cursor = _payload.cursor;
101-
hasMore = _payload.hasMore;
83+
final ListPayload<T, K> p = await widget.fetch(null);
84+
items = p.items.toList();
85+
cursor = p.cursor;
86+
hasMore = p.hasMore;
10287
} catch (err) {
10388
error = err.toString();
10489
throw err;
@@ -107,7 +92,6 @@ class _ListStatefulScaffoldState<T, K>
10792
setState(() {
10893
loading = false;
10994
});
110-
_makeSureItemsFill();
11195
}
11296
}
11397
}
@@ -118,10 +102,10 @@ class _ListStatefulScaffoldState<T, K>
118102
loadingMore = true;
119103
});
120104
try {
121-
ListPayload<T, K?> _payload = await widget.fetch(cursor);
122-
items.addAll(_payload.items!);
123-
cursor = _payload.cursor;
124-
hasMore = _payload.hasMore;
105+
ListPayload<T, K> p = await widget.fetch(cursor);
106+
items.addAll(p.items);
107+
cursor = p.cursor;
108+
hasMore = p.hasMore;
125109
} catch (err) {
126110
error = err.toString();
127111
throw err;
@@ -130,25 +114,22 @@ class _ListStatefulScaffoldState<T, K>
130114
setState(() {
131115
loadingMore = false;
132116
});
133-
_makeSureItemsFill();
134117
}
135118
}
136119
}
137120

138121
Widget _buildItem(BuildContext context, int index) {
139122
if (index == 2 * items.length) {
140-
if (hasMore!) {
123+
if (hasMore != false) {
141124
return Loading(more: true);
142125
} else {
143126
return Container();
144127
}
145-
}
146-
147-
if (index % 2 == 1) {
128+
} else if (index % 2 == 1) {
148129
return CommonStyle.border;
130+
} else {
131+
return widget.itemBuilder(items[index ~/ 2]);
149132
}
150-
151-
return widget.itemBuilder(items[index ~/ 2]);
152133
}
153134

154135
Widget _buildCupertinoSliver() {

lib/screens/bb_repo.dart

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -82,34 +82,33 @@ class BbRepoScreen extends StatelessWidget {
8282
url:
8383
'/bitbucket/$owner/$name/commits/${branch ?? p.mainbranch!.name}',
8484
),
85-
if (branches != null)
86-
TableViewItem(
87-
leftIconData: Octicons.git_branch,
88-
text: Text(AppLocalizations.of(context)!.branches),
89-
rightWidget: Text((branch ?? p.mainbranch!.name)! +
90-
' • ' +
91-
branches.length.toString()),
92-
onTap: () async {
93-
if (branches.length < 2) return;
85+
TableViewItem(
86+
leftIconData: Octicons.git_branch,
87+
text: Text(AppLocalizations.of(context)!.branches),
88+
rightWidget: Text((branch ?? p.mainbranch!.name)! +
89+
' • ' +
90+
branches.length.toString()),
91+
onTap: () async {
92+
if (branches.length < 2) return;
9493

95-
await theme.showPicker(
96-
context,
97-
PickerGroupItem(
98-
value: branch,
99-
items: branches
100-
.map((b) => PickerItem(b.name, text: b.name))
101-
.toList(),
102-
onClose: (ref) {
103-
if (ref != branch) {
104-
theme.push(context,
105-
'/bitbucket/$owner/$name?branch=$ref',
106-
replace: true);
107-
}
108-
},
109-
),
110-
);
111-
},
112-
),
94+
await theme.showPicker(
95+
context,
96+
PickerGroupItem(
97+
value: branch,
98+
items: branches
99+
.map((b) => PickerItem(b.name, text: b.name))
100+
.toList(),
101+
onClose: (ref) {
102+
if (ref != branch) {
103+
theme.push(
104+
context, '/bitbucket/$owner/$name?branch=$ref',
105+
replace: true);
106+
}
107+
},
108+
),
109+
);
110+
},
111+
),
113112
],
114113
),
115114
CommonStyle.verticalGap,

lib/screens/ge_repo.dart

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -159,34 +159,33 @@ class GeRepoScreen extends StatelessWidget {
159159
url:
160160
'/gitee/$owner/$name/commits?branch=${branch ?? p.defaultBranch}',
161161
),
162-
if (branches != null)
163-
TableViewItem(
164-
leftIconData: Octicons.git_branch,
165-
text: Text(AppLocalizations.of(context)!.branches),
166-
rightWidget: Text((branch ?? p.defaultBranch)! +
167-
' • ' +
168-
branches.length.toString()),
169-
onTap: () async {
170-
if (branches.length < 2) return;
162+
TableViewItem(
163+
leftIconData: Octicons.git_branch,
164+
text: Text(AppLocalizations.of(context)!.branches),
165+
rightWidget: Text((branch ?? p.defaultBranch)! +
166+
' • ' +
167+
branches.length.toString()),
168+
onTap: () async {
169+
if (branches.length < 2) return;
171170

172-
await theme.showPicker(
173-
context,
174-
PickerGroupItem(
175-
value: branch,
176-
items: branches
177-
.map((b) => PickerItem(b.name, text: b.name))
178-
.toList(),
179-
onClose: (ref) {
180-
if (ref != branch) {
181-
theme.push(
182-
context, '/gitee/$owner/$name?branch=$ref',
183-
replace: true);
184-
}
185-
},
186-
),
187-
);
188-
},
189-
),
171+
await theme.showPicker(
172+
context,
173+
PickerGroupItem(
174+
value: branch,
175+
items: branches
176+
.map((b) => PickerItem(b.name, text: b.name))
177+
.toList(),
178+
onClose: (ref) {
179+
if (ref != branch) {
180+
theme.push(
181+
context, '/gitee/$owner/$name?branch=$ref',
182+
replace: true);
183+
}
184+
},
185+
),
186+
);
187+
},
188+
),
190189
TableViewItem(
191190
leftIconData: Octicons.organization,
192191
text: Text('Contributors'),

lib/screens/gh_commits.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class GhCommits extends StatelessWidget {
5050
return ListPayload(
5151
cursor: history.pageInfo.endCursor,
5252
hasMore: history.pageInfo.hasNextPage,
53-
items: history.nodes,
53+
items: history.nodes ?? [],
5454
);
5555
},
5656
itemBuilder: (p) {

lib/screens/gh_gists.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class GhGistsScreen extends StatelessWidget {
2828
final gists = res.data!.user!.gists;
2929
return ListPayload(
3030
cursor: gists.pageInfo.endCursor,
31-
items: gists.nodes,
31+
items: gists.nodes ?? [],
3232
hasMore: gists.pageInfo.hasNextPage,
3333
);
3434
},

lib/screens/gh_releases.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class GhReleasesScreen extends StatelessWidget {
3030
final releases = res.data!.repository!.releases;
3131
return ListPayload(
3232
cursor: releases.pageInfo.endCursor,
33-
items: releases.nodes,
33+
items: releases.nodes ?? [],
3434
hasMore: releases.pageInfo.hasNextPage,
3535
);
3636
},

lib/screens/gh_repo.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class GhRepoScreen extends StatelessWidget {
7575
return res.body;
7676
}).catchError((err) {
7777
// 404
78-
return null;
78+
return '';
7979
});
8080
};
8181
};

lib/screens/gh_repos.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class GhRepos extends StatelessWidget {
3030
return ListPayload(
3131
cursor: p.pageInfo.endCursor,
3232
hasMore: p.pageInfo.hasNextPage,
33-
items: p.nodes,
33+
items: p.nodes!,
3434
);
3535
},
3636
itemBuilder: (p) {
@@ -61,7 +61,7 @@ class GhStars extends StatelessWidget {
6161
return ListPayload(
6262
cursor: p.pageInfo.endCursor,
6363
hasMore: p.pageInfo.hasNextPage,
64-
items: p.nodes,
64+
items: p.nodes!,
6565
);
6666
},
6767
itemBuilder: (p) {

0 commit comments

Comments
 (0)