2019-02-08 07:01:25 +01:00
|
|
|
import 'package:flutter/cupertino.dart';
|
2019-09-02 15:52:32 +02:00
|
|
|
import 'package:git_touch/models/theme.dart';
|
2019-09-07 11:48:59 +02:00
|
|
|
import 'package:git_touch/utils/utils.dart';
|
2019-09-02 15:52:32 +02:00
|
|
|
import 'package:provider/provider.dart';
|
2022-09-17 14:35:45 +02:00
|
|
|
|
2019-02-08 13:52:10 +01:00
|
|
|
import '../widgets/error_reload.dart';
|
2022-09-17 14:35:45 +02:00
|
|
|
import '../widgets/link.dart';
|
|
|
|
import '../widgets/loading.dart';
|
2019-02-08 07:01:25 +01:00
|
|
|
|
|
|
|
class LongListPayload<T, K> {
|
2021-06-05 07:54:52 +02:00
|
|
|
T header;
|
|
|
|
int totalCount;
|
2021-06-14 08:02:52 +02:00
|
|
|
String? cursor;
|
2021-06-05 07:54:52 +02:00
|
|
|
List<K> leadingItems;
|
2021-05-16 09:16:35 +02:00
|
|
|
List<K>? trailingItems;
|
2019-02-08 07:01:25 +01:00
|
|
|
|
|
|
|
LongListPayload({
|
2021-06-05 07:54:52 +02:00
|
|
|
required this.header,
|
|
|
|
required this.totalCount,
|
|
|
|
required this.cursor,
|
|
|
|
required this.leadingItems,
|
2019-02-08 07:01:25 +01:00
|
|
|
this.trailingItems,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a scaffold for issue and pull request
|
|
|
|
// Since the list could be very long, and some users may only want to to check trailing items
|
|
|
|
// We should load leading and trailing items at first fetching, and do load more in the middle
|
|
|
|
// e.g. https://github.com/reactjs/rfcs/pull/68
|
2019-10-03 05:00:59 +02:00
|
|
|
class LongListStatefulScaffold<T, K> extends StatefulWidget {
|
2019-02-08 07:01:25 +01:00
|
|
|
final Widget title;
|
2021-05-16 09:16:35 +02:00
|
|
|
final Widget Function(T t)? trailingBuilder;
|
2021-01-17 15:08:32 +01:00
|
|
|
final Widget Function(T t) headerBuilder;
|
|
|
|
final Widget Function(K k) itemBuilder;
|
2019-02-08 07:01:25 +01:00
|
|
|
final Future<LongListPayload<T, K>> Function() onRefresh;
|
2021-05-16 09:16:35 +02:00
|
|
|
final Future<LongListPayload<T, K>> Function(String? cursor) onLoadMore;
|
2019-02-08 07:01:25 +01:00
|
|
|
|
2022-09-06 18:32:56 +02:00
|
|
|
const LongListStatefulScaffold({
|
2021-05-16 09:16:35 +02:00
|
|
|
required this.title,
|
2019-02-10 11:50:40 +01:00
|
|
|
this.trailingBuilder,
|
2021-05-16 09:16:35 +02:00
|
|
|
required this.headerBuilder,
|
|
|
|
required this.itemBuilder,
|
|
|
|
required this.onRefresh,
|
|
|
|
required this.onLoadMore,
|
2019-02-08 07:01:25 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
2019-12-03 04:26:08 +01:00
|
|
|
_LongListStatefulScaffoldState<T, K> createState() =>
|
2019-10-03 05:00:59 +02:00
|
|
|
_LongListStatefulScaffoldState();
|
2019-02-08 07:01:25 +01:00
|
|
|
}
|
|
|
|
|
2019-10-03 05:00:59 +02:00
|
|
|
class _LongListStatefulScaffoldState<T, K>
|
2021-06-05 07:54:52 +02:00
|
|
|
extends State<LongListStatefulScaffold<T, K>> {
|
2021-05-16 09:16:35 +02:00
|
|
|
late bool loading;
|
2019-02-08 07:01:25 +01:00
|
|
|
bool loadingMore = false;
|
2019-02-08 13:52:10 +01:00
|
|
|
String error = '';
|
|
|
|
|
2021-06-05 07:54:52 +02:00
|
|
|
LongListPayload<T, K>? payload;
|
2019-02-08 07:01:25 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _refresh() async {
|
2019-10-13 05:01:29 +02:00
|
|
|
// Fimber.d('long list scaffold refresh');
|
2019-02-08 07:01:25 +01:00
|
|
|
setState(() {
|
2019-02-08 13:52:10 +01:00
|
|
|
error = '';
|
2019-02-08 07:01:25 +01:00
|
|
|
loading = true;
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
payload = await widget.onRefresh();
|
2019-02-08 13:52:10 +01:00
|
|
|
} catch (err) {
|
2019-02-08 16:41:58 +01:00
|
|
|
error = err.toString();
|
2022-09-06 18:32:56 +02:00
|
|
|
rethrow;
|
2019-02-08 07:01:25 +01:00
|
|
|
} finally {
|
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
loading = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _loadMore() async {
|
2019-10-13 05:01:29 +02:00
|
|
|
// Fimber.d('long list scaffold load more');
|
2019-02-08 07:01:25 +01:00
|
|
|
setState(() {
|
|
|
|
loadingMore = true;
|
|
|
|
});
|
|
|
|
try {
|
2022-09-06 18:32:56 +02:00
|
|
|
LongListPayload<T?, K> p = await widget.onLoadMore(payload!.cursor);
|
|
|
|
payload!.totalCount = p.totalCount;
|
|
|
|
payload!.cursor = p.cursor;
|
|
|
|
payload!.leadingItems.addAll(p.leadingItems);
|
2019-02-08 07:01:25 +01:00
|
|
|
} finally {
|
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
loadingMore = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildItem(BuildContext context, int index) {
|
2019-11-08 11:29:08 +01:00
|
|
|
final theme = Provider.of<ThemeModel>(context);
|
|
|
|
|
2019-02-08 07:01:25 +01:00
|
|
|
if (index % 2 == 1) {
|
2019-10-02 10:09:54 +02:00
|
|
|
return CommonStyle.border;
|
2019-02-08 07:01:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int realIndex = index ~/ 2;
|
|
|
|
|
2021-06-05 07:54:52 +02:00
|
|
|
if (realIndex < payload!.leadingItems.length) {
|
|
|
|
return widget.itemBuilder(payload!.leadingItems[realIndex]);
|
|
|
|
} else if (realIndex == payload!.leadingItems.length) {
|
|
|
|
var count = payload!.totalCount -
|
|
|
|
payload!.leadingItems.length +
|
2021-05-16 09:16:35 +02:00
|
|
|
payload!.trailingItems!.length;
|
2019-02-08 07:01:25 +01:00
|
|
|
return Container(
|
2019-10-02 10:09:54 +02:00
|
|
|
padding: CommonStyle.padding,
|
2019-02-08 07:01:25 +01:00
|
|
|
child: Center(
|
2021-05-16 09:16:35 +02:00
|
|
|
child: LinkWidget(
|
2019-02-21 11:43:41 +01:00
|
|
|
onTap: _loadMore,
|
2019-02-08 07:01:25 +01:00
|
|
|
child: Container(
|
2019-10-02 10:09:54 +02:00
|
|
|
padding: CommonStyle.padding,
|
2019-02-08 07:01:25 +01:00
|
|
|
decoration: BoxDecoration(
|
2020-01-27 08:11:51 +01:00
|
|
|
border: Border.all(color: theme.palette.text),
|
2019-02-08 07:01:25 +01:00
|
|
|
),
|
|
|
|
child: Column(
|
|
|
|
children: <Widget>[
|
|
|
|
Text('$count hidden items',
|
2020-01-27 08:11:51 +01:00
|
|
|
style:
|
|
|
|
TextStyle(color: theme.palette.text, fontSize: 15)),
|
2022-09-06 18:32:56 +02:00
|
|
|
const Padding(padding: EdgeInsets.only(top: 4)),
|
2019-02-08 07:01:25 +01:00
|
|
|
loadingMore
|
2022-09-06 18:32:56 +02:00
|
|
|
? const CupertinoActivityIndicator()
|
2019-02-08 07:01:25 +01:00
|
|
|
: Text(
|
|
|
|
'Load more...',
|
2019-11-08 11:29:08 +01:00
|
|
|
style: TextStyle(
|
2020-01-27 08:11:51 +01:00
|
|
|
color: theme.palette.primary, fontSize: 16),
|
2019-02-08 07:01:25 +01:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
2021-05-16 09:16:35 +02:00
|
|
|
return widget.itemBuilder(payload!
|
2021-06-05 07:54:52 +02:00
|
|
|
.trailingItems![realIndex - payload!.leadingItems.length - 1]);
|
2019-02-08 07:01:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int get _itemCount {
|
2021-06-05 07:54:52 +02:00
|
|
|
int count = payload!.leadingItems.length + payload!.trailingItems!.length;
|
|
|
|
if (payload!.totalCount > count) {
|
2019-02-08 07:01:25 +01:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return 2 * count; // including bottom border
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildSliver() {
|
2019-02-08 13:52:10 +01:00
|
|
|
if (error.isNotEmpty) {
|
2019-02-09 06:29:44 +01:00
|
|
|
return SliverToBoxAdapter(
|
2019-02-21 11:43:41 +01:00
|
|
|
child: ErrorReload(text: error, onTap: _refresh));
|
2019-02-08 13:52:10 +01:00
|
|
|
} else if (loading) {
|
2019-03-02 09:51:28 +01:00
|
|
|
// TODO:
|
2022-09-06 18:32:56 +02:00
|
|
|
return const SliverToBoxAdapter(child: Loading(more: false));
|
2019-02-08 07:01:25 +01:00
|
|
|
} else {
|
|
|
|
return SliverList(
|
|
|
|
delegate:
|
|
|
|
SliverChildBuilderDelegate(_buildItem, childCount: _itemCount),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-09-17 14:35:45 +02:00
|
|
|
List<Widget> slivers = [CupertinoSliverRefreshControl(onRefresh: _refresh)];
|
|
|
|
if (payload != null) {
|
|
|
|
slivers.add(
|
|
|
|
SliverToBoxAdapter(child: widget.headerBuilder(payload!.header)),
|
|
|
|
);
|
2019-02-08 07:01:25 +01:00
|
|
|
}
|
2022-09-17 14:35:45 +02:00
|
|
|
slivers.add(_buildSliver());
|
|
|
|
|
|
|
|
return CupertinoPageScaffold(
|
|
|
|
navigationBar: CupertinoNavigationBar(
|
|
|
|
middle: widget.title,
|
|
|
|
trailing:
|
|
|
|
payload == null ? null : widget.trailingBuilder!(payload!.header),
|
|
|
|
),
|
|
|
|
child: SafeArea(
|
|
|
|
child: CupertinoScrollbar(
|
|
|
|
child: CustomScrollView(slivers: slivers),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2019-02-08 07:01:25 +01:00
|
|
|
}
|
|
|
|
}
|