loris/lib/pages/timeline/timeline.dart

117 lines
2.9 KiB
Dart
Raw Normal View History

2022-06-16 20:30:02 +00:00
import 'package:flutter/material.dart';
2022-07-04 17:37:53 +00:00
import 'package:localization/localization.dart';
import 'package:loris/partials/thread.dart';
2022-07-04 20:39:25 +00:00
import '../../business_logic/timeline/timeline.dart' as tl;
2022-06-16 20:30:02 +00:00
2022-07-04 17:37:53 +00:00
class Timeline extends StatefulWidget {
const Timeline({Key? key}) : super(key: key);
@override
State<Timeline> createState() => _TimelineState();
}
2022-08-10 16:47:34 +00:00
class _TimelineState extends State<Timeline>
with AutomaticKeepAliveClientMixin {
String? oldestId;
2022-07-04 17:37:53 +00:00
final controller = ScrollController();
List<Widget> children = [];
bool loading = false;
@override
void initState() {
super.initState();
2022-08-10 16:47:34 +00:00
children.add(const LoadingBox());
fetchMore();
2022-07-04 17:37:53 +00:00
controller.addListener(() {
2022-08-09 18:58:11 +00:00
if (controller.position.maxScrollExtent <=
2022-08-10 16:47:34 +00:00
controller.offset + 2 * MediaQuery.of(context).size.height &&
2022-07-04 17:37:53 +00:00
!loading) {
fetchMore();
}
});
}
Future fetchMore() async {
2022-08-09 18:58:11 +00:00
while (loading) {
await Future.delayed(const Duration(milliseconds: 500));
}
2022-07-04 17:37:53 +00:00
loading = true;
2022-08-01 08:29:43 +00:00
2022-08-10 16:47:34 +00:00
final models = await tl.getTimelineFromServer(oldestId);
2022-07-04 17:37:53 +00:00
setState(() {
2022-08-10 16:47:34 +00:00
children.removeWhere((element) {
return element.runtimeType != Thread;
});
List<Thread> threads = [];
for (int i = 0; i < models.length; i++) {
threads.add(Thread(model: models[i]));
2022-07-04 17:37:53 +00:00
}
2022-08-10 16:47:34 +00:00
oldestId = models.last.posts.last.id;
children.addAll(threads);
2022-07-04 17:37:53 +00:00
children.add(
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton.icon(
onPressed: () {
fetchMore();
},
icon: const Icon(Icons.more_horiz),
label: Text("load-more".i18n()),
)
],
),
);
2022-08-10 16:47:34 +00:00
children.add(const LoadingBox());
2022-07-04 17:37:53 +00:00
loading = false;
});
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
2022-08-10 16:47:34 +00:00
super.build(context);
2022-07-04 17:37:53 +00:00
return ListView.separated(
physics: const AlwaysScrollableScrollPhysics(),
controller: controller,
itemBuilder: (context, index) {
return children[index];
},
separatorBuilder: (context, index) {
return const Divider(
color: Colors.transparent,
);
},
itemCount: children.length,
2022-07-03 13:47:24 +00:00
padding: const EdgeInsets.fromLTRB(24, 0, 24, 64),
addAutomaticKeepAlives: false,
2022-07-04 17:37:53 +00:00
);
}
2022-08-10 16:47:34 +00:00
@override
bool get wantKeepAlive => true;
}
class LoadingBox extends StatelessWidget {
const LoadingBox({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return SizedBox.fromSize(
size: Size(double.infinity, MediaQuery.of(context).size.height),
child: Center(
child: SizedBox.fromSize(
size: const Size(128, 128),
child: const CircularProgressIndicator(),
),
));
}
2022-06-16 20:30:02 +00:00
}