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';
|
2022-08-06 14:50:30 +00:00
|
|
|
import 'package:loris/partials/thread.dart';
|
2022-07-04 20:39:25 +00:00
|
|
|
import '../../business_logic/timeline/timeline.dart' as tl;
|
2022-08-14 16:44:06 +00:00
|
|
|
import '../../global.dart' as global;
|
2022-08-22 11:32:40 +00:00
|
|
|
import 'package:loris/partials/loadingbox.dart';
|
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
|
2022-08-15 13:08:37 +00:00
|
|
|
State<Timeline> createState() => TimelineState();
|
2022-07-04 17:37:53 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 18:31:44 +00:00
|
|
|
class TimelineState extends State<Timeline> {
|
2022-08-10 16:47:34 +00:00
|
|
|
String? oldestId;
|
2022-07-04 17:37:53 +00:00
|
|
|
final controller = ScrollController();
|
|
|
|
List<Widget> children = [];
|
|
|
|
bool loading = false;
|
|
|
|
|
2022-08-14 16:44:06 +00:00
|
|
|
tl.TimelineType selectedTimelineType = tl.TimelineType.home;
|
|
|
|
String selectedId = global.settings!.activeIdentity;
|
|
|
|
|
2022-07-04 17:37:53 +00:00
|
|
|
@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-14 16:44:06 +00:00
|
|
|
final models = await tl.getTimelineFromServer(
|
|
|
|
oldestId, selectedId, selectedTimelineType);
|
2022-08-10 16:47:34 +00:00
|
|
|
|
2022-08-15 14:20:57 +00:00
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
children.removeWhere((element) {
|
|
|
|
return element.runtimeType != Thread;
|
|
|
|
});
|
|
|
|
List<Thread> threads = [];
|
2022-08-14 16:44:06 +00:00
|
|
|
|
2022-08-15 14:20:57 +00:00
|
|
|
for (int i = 0; i < models.length; i++) {
|
|
|
|
threads.add(Thread(model: models[i]));
|
|
|
|
}
|
2022-08-10 16:47:34 +00:00
|
|
|
|
2022-08-15 14:20:57 +00:00
|
|
|
if (models.isNotEmpty) {
|
|
|
|
oldestId = models.last.posts.last.id;
|
|
|
|
}
|
|
|
|
children.addAll(threads);
|
|
|
|
children.add(
|
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
TextButton.icon(
|
|
|
|
onPressed: () {
|
|
|
|
fetchMore();
|
|
|
|
},
|
|
|
|
icon: const Icon(Icons.more_horiz),
|
|
|
|
label: Text(
|
|
|
|
"load-more".i18n(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
children.add(const LoadingBox());
|
|
|
|
loading = false;
|
|
|
|
});
|
|
|
|
}
|
2022-07-04 17:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
controller.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-08-14 16:44:06 +00:00
|
|
|
List<DropdownMenuItem> identities = [];
|
|
|
|
// add identities to dropdown menu
|
|
|
|
for (int i = 0; i < global.settings!.identities.keys.length; i++) {
|
|
|
|
identities.add(DropdownMenuItem(
|
2022-09-02 21:19:25 +00:00
|
|
|
alignment: Alignment.center,
|
2022-08-14 16:44:06 +00:00
|
|
|
value: global.settings!.identities.keys.toList()[i],
|
2022-09-02 21:19:25 +00:00
|
|
|
child: Text(
|
|
|
|
global.settings!.identities.keys.toList()[i],
|
|
|
|
style: Theme.of(context).textTheme.bodyMedium,
|
|
|
|
),
|
2022-08-14 16:44:06 +00:00
|
|
|
));
|
|
|
|
}
|
2022-08-15 14:20:57 +00:00
|
|
|
selectedId = global.settings!.activeIdentity;
|
2022-08-14 16:44:06 +00:00
|
|
|
return Column(
|
2022-08-29 21:16:13 +00:00
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
2022-08-14 16:44:06 +00:00
|
|
|
children: [
|
|
|
|
Container(
|
|
|
|
color: Theme.of(context).colorScheme.surface,
|
2022-08-29 21:16:13 +00:00
|
|
|
child: Wrap(
|
|
|
|
alignment: WrapAlignment.spaceEvenly,
|
2022-08-14 16:44:06 +00:00
|
|
|
children: [
|
|
|
|
IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
reload();
|
|
|
|
},
|
|
|
|
icon: const Icon(Icons.refresh),
|
|
|
|
),
|
2022-08-29 21:16:13 +00:00
|
|
|
DropdownButtonHideUnderline(
|
|
|
|
child: DropdownButton(
|
2022-09-02 21:19:25 +00:00
|
|
|
alignment: Alignment.center,
|
2022-08-29 21:16:13 +00:00
|
|
|
borderRadius: BorderRadius.circular(8),
|
|
|
|
iconEnabledColor: Theme.of(context).colorScheme.onSurface,
|
|
|
|
value: selectedId,
|
|
|
|
items: identities,
|
|
|
|
onChanged: (dynamic value) async {
|
|
|
|
setState(() {
|
|
|
|
selectedId = value ?? global.settings!.activeIdentity;
|
|
|
|
global.settings!.saveActiveIdentity(selectedId);
|
|
|
|
reload();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
2022-08-14 16:44:06 +00:00
|
|
|
),
|
2022-08-29 21:16:13 +00:00
|
|
|
DropdownButtonHideUnderline(
|
|
|
|
child: DropdownButton(
|
2022-09-02 21:19:25 +00:00
|
|
|
alignment: Alignment.center,
|
2022-08-29 21:16:13 +00:00
|
|
|
borderRadius: BorderRadius.circular(8),
|
|
|
|
iconEnabledColor: Theme.of(context).colorScheme.onSurface,
|
|
|
|
value: selectedTimelineType,
|
|
|
|
items: [
|
|
|
|
DropdownMenuItem(
|
2022-09-02 21:19:25 +00:00
|
|
|
alignment: Alignment.center,
|
2022-08-29 21:16:13 +00:00
|
|
|
value: tl.TimelineType.home,
|
|
|
|
child: RichText(
|
|
|
|
text: TextSpan(
|
2022-09-02 21:19:25 +00:00
|
|
|
style: Theme.of(context).textTheme.bodyMedium,
|
2022-08-29 21:16:13 +00:00
|
|
|
text: "${"home-timeline".i18n()} ",
|
|
|
|
children: const [
|
|
|
|
WidgetSpan(
|
|
|
|
child: Icon(Icons.home),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2022-08-14 16:44:06 +00:00
|
|
|
),
|
|
|
|
),
|
2022-08-29 21:16:13 +00:00
|
|
|
DropdownMenuItem(
|
2022-09-02 21:19:25 +00:00
|
|
|
alignment: Alignment.center,
|
2022-08-29 21:16:13 +00:00
|
|
|
value: tl.TimelineType.local,
|
|
|
|
child: RichText(
|
|
|
|
text: TextSpan(
|
2022-09-02 21:19:25 +00:00
|
|
|
style: Theme.of(context).textTheme.bodyMedium,
|
2022-08-29 21:16:13 +00:00
|
|
|
text: "${"local-timeline".i18n()} ",
|
|
|
|
children: const [
|
|
|
|
WidgetSpan(
|
|
|
|
child: Icon(Icons.people),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
2022-08-14 16:44:06 +00:00
|
|
|
),
|
|
|
|
),
|
2022-08-29 21:16:13 +00:00
|
|
|
DropdownMenuItem(
|
2022-09-02 21:19:25 +00:00
|
|
|
alignment: Alignment.center,
|
2022-08-29 21:16:13 +00:00
|
|
|
value: tl.TimelineType.public,
|
|
|
|
child: RichText(
|
|
|
|
text: TextSpan(
|
2022-09-02 21:19:25 +00:00
|
|
|
style: Theme.of(context).textTheme.bodyMedium,
|
2022-08-29 21:16:13 +00:00
|
|
|
text: "${"public-timeline".i18n()} ",
|
|
|
|
children: const [
|
|
|
|
WidgetSpan(
|
|
|
|
child: Icon(Icons.public),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2022-08-14 16:44:06 +00:00
|
|
|
),
|
|
|
|
),
|
2022-08-29 21:16:13 +00:00
|
|
|
],
|
|
|
|
onChanged: (tl.TimelineType? value) {
|
|
|
|
setState(() {
|
|
|
|
selectedTimelineType = value ?? tl.TimelineType.home;
|
|
|
|
reload();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
2022-08-14 16:44:06 +00:00
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: 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,
|
|
|
|
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
|
|
|
|
2022-08-14 16:44:06 +00:00
|
|
|
void reload() async {
|
2022-08-15 15:13:29 +00:00
|
|
|
controller.animateTo(
|
|
|
|
0,
|
|
|
|
duration: const Duration(seconds: 1),
|
|
|
|
curve: Curves.easeOut,
|
|
|
|
);
|
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
children = [const LoadingBox()];
|
|
|
|
oldestId = null;
|
|
|
|
});
|
|
|
|
}
|
2022-08-14 16:44:06 +00:00
|
|
|
await _waitForFetchMore();
|
2022-08-15 15:13:29 +00:00
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
fetchMore();
|
|
|
|
});
|
|
|
|
}
|
2022-08-14 16:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _waitForFetchMore() async {
|
|
|
|
while (loading) {
|
|
|
|
await Future.delayed(const Duration(milliseconds: 500));
|
|
|
|
}
|
|
|
|
}
|
2022-08-10 16:47:34 +00:00
|
|
|
}
|