lemmur-app-android/lib/pages/home_tab.dart

381 lines
12 KiB
Dart
Raw Normal View History

import 'dart:math' show max;
2020-10-05 23:09:36 +02:00
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
2021-01-24 20:01:55 +01:00
import 'package:lemmy_api_client/v2.dart';
import '../hooks/infinite_scroll.dart';
2020-10-05 23:09:36 +02:00
import '../hooks/memo_future.dart';
import '../hooks/stores.dart';
import '../util/goto.dart';
import '../widgets/bottom_modal.dart';
import '../widgets/infinite_scroll.dart';
import '../widgets/post.dart';
import '../widgets/post_list_options.dart';
import 'add_account.dart';
import 'inbox.dart';
2020-10-06 16:22:49 +02:00
/// First thing users sees when opening the app
/// Shows list of posts from all or just specific instances
class HomeTab extends HookWidget {
2021-01-03 19:43:39 +01:00
const HomeTab();
@override
Widget build(BuildContext context) {
final accStore = useAccountsStore();
2020-12-03 22:46:25 +01:00
final selectedList = useState(_SelectedList(
listingType: accStore.hasNoAccount
? PostListingType.all
: PostListingType.subscribed));
final isc = useInfiniteScrollController();
final theme = Theme.of(context);
2020-10-05 23:09:36 +02:00
final instancesIcons = useMemoFuture(() async {
final instances = accStore.instances.toList(growable: false);
2021-01-31 14:38:47 +01:00
final sites = await Future.wait(instances
.map((e) => LemmyApiV2(e).run(GetSite()).catchError((e) => null)));
2020-10-05 23:09:36 +02:00
2021-01-03 21:25:05 +01:00
return {
2021-01-24 20:01:55 +01:00
for (var i = 0; i < sites.length; i++)
instances[i]: sites[i].siteView.site.icon
2021-01-03 21:25:05 +01:00
};
2020-10-05 23:09:36 +02:00
});
2021-01-30 15:26:48 +01:00
// if the current SelectedList points to something that no longer exists
// switch it to something else
// cases include:
// - listingType == subscribed on an instance that has no longer a logged in account
// - instanceHost of a removed instance
useEffect(() {
if (accStore.isAnonymousFor(selectedList.value.instanceHost) &&
selectedList.value.listingType == PostListingType.subscribed ||
!accStore.instances.contains(selectedList.value.instanceHost)) {
selectedList.value = _SelectedList(
listingType: accStore.hasNoAccount
? PostListingType.all
: PostListingType.subscribed,
);
}
return null;
}, [
accStore.isAnonymousFor(selectedList.value.instanceHost),
accStore.hasNoAccount,
accStore.instances.length,
]);
handleListChange() async {
final val = await showModalBottomSheet<_SelectedList>(
backgroundColor: Colors.transparent,
isScrollControlled: true,
context: context,
builder: (context) {
pop(_SelectedList thing) => Navigator.of(context).pop(thing);
2021-01-30 15:26:48 +01:00
return BottomModal(
2020-10-06 16:23:29 +02:00
child: Column(
children: [
2021-01-03 19:43:39 +01:00
const SizedBox(height: 5),
const ListTile(
2020-10-06 16:23:29 +02:00
title: Text('EVERYTHING'),
dense: true,
contentPadding: EdgeInsets.zero,
visualDensity:
VisualDensity(vertical: VisualDensity.minimumDensity),
2020-10-06 16:23:29 +02:00
leading: SizedBox.shrink(),
),
2021-01-30 15:26:48 +01:00
ListTile(
title: Text(
'Subscribed',
style: TextStyle(
color: accStore.hasNoAccount
? theme.textTheme.bodyText1.color.withOpacity(0.4)
: null,
),
),
onTap: accStore.hasNoAccount
? null
: () => pop(
const _SelectedList(
listingType: PostListingType.subscribed,
),
),
leading: const SizedBox(width: 20),
),
for (final listingType in [
PostListingType.local,
PostListingType.all,
])
ListTile(
title: Text(listingType.value),
leading: const SizedBox(width: 20, height: 20),
onTap: () => pop(_SelectedList(listingType: listingType)),
),
2020-10-06 16:23:29 +02:00
for (final instance in accStore.instances) ...[
2021-01-03 19:43:39 +01:00
const Padding(
padding: EdgeInsets.symmetric(horizontal: 10),
2020-10-06 16:23:29 +02:00
child: Divider(),
),
ListTile(
title: Text(
instance.toUpperCase(),
style: TextStyle(
color:
theme.textTheme.bodyText1.color.withOpacity(0.7)),
),
onTap: () => goToInstance(context, instance),
dense: true,
contentPadding: EdgeInsets.zero,
2021-01-03 19:43:39 +01:00
visualDensity: const VisualDensity(
vertical: VisualDensity.minimumDensity),
2020-10-06 16:23:29 +02:00
leading: (instancesIcons.hasData &&
instancesIcons.data[instance] != null)
2020-10-06 16:27:35 +02:00
? Padding(
padding: const EdgeInsets.only(left: 20),
child: SizedBox(
width: 25,
height: 25,
child: CachedNetworkImage(
imageUrl: instancesIcons.data[instance],
2020-10-06 16:23:29 +02:00
height: 25,
2020-10-06 16:27:35 +02:00
width: 25,
2020-10-06 16:23:29 +02:00
),
2020-10-06 16:27:35 +02:00
),
2020-10-06 16:23:29 +02:00
)
2021-01-03 19:43:39 +01:00
: const SizedBox(width: 30),
2020-10-06 16:23:29 +02:00
),
ListTile(
title: Text(
'Subscribed',
style: TextStyle(
color: accStore.isAnonymousFor(instance)
? theme.textTheme.bodyText1.color.withOpacity(0.4)
: null),
),
onTap: accStore.isAnonymousFor(instance)
? () => showCupertinoModalPopup(
context: context,
builder: (_) =>
AddAccountPage(instanceHost: instance))
: () => pop(_SelectedList(
2020-10-06 16:23:29 +02:00
listingType: PostListingType.subscribed,
instanceHost: instance,
2020-10-06 16:23:29 +02:00
)),
2021-01-03 19:43:39 +01:00
leading: const SizedBox(width: 20),
2020-10-06 16:23:29 +02:00
),
ListTile(
title: const Text('Local'),
onTap: () => pop(_SelectedList(
listingType: PostListingType.local,
instanceHost: instance,
)),
leading: const SizedBox(width: 20),
),
2020-10-06 16:23:29 +02:00
ListTile(
2021-01-03 19:43:39 +01:00
title: const Text('All'),
onTap: () => pop(_SelectedList(
2020-10-06 16:23:29 +02:00
listingType: PostListingType.all,
instanceHost: instance,
2020-10-06 16:23:29 +02:00
)),
2021-01-03 19:43:39 +01:00
leading: const SizedBox(width: 20),
2020-10-06 16:23:29 +02:00
),
]
],
),
);
},
);
if (val != null) {
selectedList.value = val;
isc.clear();
}
}
2020-10-05 21:55:33 +02:00
final title = () {
2021-01-16 14:48:15 +01:00
final first = selectedList.value.listingType.value;
final last = selectedList.value.instanceHost == null
2020-10-05 21:55:33 +02:00
? ''
: '@${selectedList.value.instanceHost}';
2020-10-05 21:55:33 +02:00
return '$first$last';
}();
2020-12-03 22:46:25 +01:00
if (accStore.instances.isEmpty) {
return Scaffold(
2020-12-04 13:58:17 +01:00
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
2021-01-03 18:21:56 +01:00
children: const [
2020-12-04 13:58:17 +01:00
Center(child: Text('there needs to be at least one instance')),
],
),
2020-12-03 22:46:25 +01:00
);
}
return Scaffold(
2020-10-05 23:14:36 +02:00
// TODO: make appbar autohide when scrolling down
appBar: AppBar(
actions: [
IconButton(
2021-01-03 19:43:39 +01:00
icon: const Icon(Icons.notifications),
onPressed: () => goTo(context, (_) => const InboxPage()),
)
],
centerTitle: true,
title: TextButton(
style: TextButton.styleFrom(
2021-01-03 19:43:39 +01:00
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10))),
2021-01-03 19:43:39 +01:00
padding: const EdgeInsets.symmetric(horizontal: 15),
primary: theme.buttonColor,
textStyle: theme.primaryTextTheme.headline6,
),
onPressed: handleListChange,
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Flexible(
child: Text(
title,
style: theme.primaryTextTheme.headline6,
overflow: TextOverflow.ellipsis,
2020-10-05 21:55:33 +02:00
),
),
Icon(
Icons.arrow_drop_down,
color: theme.primaryTextTheme.headline6.color,
),
],
),
),
),
body: InfiniteHomeList(
controller: isc,
selectedList: selectedList.value,
),
);
}
}
2020-10-06 16:22:49 +02:00
/// Infinite list of posts
class InfiniteHomeList extends HookWidget {
final Function onStyleChange;
final InfiniteScrollController controller;
final _SelectedList selectedList;
2021-01-03 18:21:56 +01:00
const InfiniteHomeList({
@required this.selectedList,
this.onStyleChange,
this.controller,
}) : assert(selectedList != null);
2020-10-06 16:28:38 +02:00
@override
Widget build(BuildContext context) {
final accStore = useAccountsStore();
final sort = useState(SortType.active);
void changeSorting(SortType newSort) {
sort.value = newSort;
controller.clear();
}
2020-10-06 16:30:52 +02:00
/// fetches post from many instances at once and combines them into a single
/// list
///
/// Process of combining them works sort of like zip function in python
Future<List<PostView>> generalFetcher(
int page,
int limit,
SortType sort,
PostListingType listingType,
) async {
assert(
listingType != PostListingType.community, 'only subscribed or all');
final instances = () {
if (listingType == PostListingType.all) {
return accStore.instances;
} else {
return accStore.loggedInInstances;
}
}();
final futures =
2021-01-24 20:01:55 +01:00
instances.map((instanceHost) => LemmyApiV2(instanceHost).run(GetPosts(
type: listingType,
sort: sort,
page: page,
limit: limit,
auth: accStore.defaultTokenFor(instanceHost)?.raw,
2021-01-24 20:01:55 +01:00
)));
2020-10-06 16:29:19 +02:00
final posts = await Future.wait(futures);
final newPosts = <PostView>[];
2021-01-03 21:25:05 +01:00
final longest = posts.map((e) => e.length).reduce(max);
for (var i = 0; i < longest; i++) {
for (final el in posts) {
if (el.elementAt(i) != null) {
newPosts.add(el[i]);
}
}
}
2021-01-03 21:25:05 +01:00
return newPosts;
}
2020-10-06 16:30:52 +02:00
Future<List<PostView>> Function(int, int) fetcherFromInstance(
String instanceHost, PostListingType listingType, SortType sort) =>
2021-01-24 20:01:55 +01:00
(page, batchSize) => LemmyApiV2(instanceHost).run(GetPosts(
2020-10-06 12:06:29 +02:00
type: listingType,
sort: sort,
page: page,
limit: batchSize,
auth: accStore.defaultTokenFor(instanceHost)?.raw,
2021-01-24 20:01:55 +01:00
));
return InfiniteScroll<PostView>(
prepend: Column(
children: [
PostListOptions(
onChange: changeSorting,
styleButton: onStyleChange != null,
),
],
),
builder: (post) => Column(
children: [
2021-01-24 20:01:55 +01:00
PostWidget(post),
2021-01-03 19:43:39 +01:00
const SizedBox(height: 20),
],
),
padding: EdgeInsets.zero,
fetchMore: selectedList.instanceHost == null
? (page, limit) =>
2020-10-06 16:30:52 +02:00
generalFetcher(page, limit, sort.value, selectedList.listingType)
: fetcherFromInstance(
selectedList.instanceHost,
selectedList.listingType,
sort.value,
),
controller: controller,
batchSize: 20,
);
}
}
class _SelectedList {
2021-01-30 15:26:48 +01:00
/// when null it implies the 'EVERYTHING' mode
final String instanceHost;
final PostListingType listingType;
2021-01-03 19:43:39 +01:00
const _SelectedList({
@required this.listingType,
this.instanceHost,
2021-01-30 15:26:48 +01:00
}) : assert(listingType != null);
String toString() =>
2021-01-30 15:26:48 +01:00
'SelectedList(instanceHost: $instanceHost, listingType: $listingType)';
}