2020-10-06 00:19:34 +02:00
|
|
|
import 'dart:math' show max;
|
|
|
|
|
2020-10-05 23:09:36 +02:00
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
2020-10-06 01:04:49 +02:00
|
|
|
import 'package:flutter/cupertino.dart';
|
2020-10-04 21:50:40 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
|
|
|
import 'package:lemmy_api_client/lemmy_api_client.dart';
|
|
|
|
|
|
|
|
import '../hooks/infinite_scroll.dart';
|
2020-10-05 23:09:36 +02:00
|
|
|
import '../hooks/memo_future.dart';
|
2020-10-04 21:50:40 +02:00
|
|
|
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';
|
2020-10-06 01:04:49 +02:00
|
|
|
import 'add_account.dart';
|
2020-10-04 21:50:40 +02:00
|
|
|
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
|
2020-10-04 21:50:40 +02:00
|
|
|
class HomeTab extends HookWidget {
|
|
|
|
@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));
|
|
|
|
// TODO: needs to be an observer? for accounts changes
|
2020-10-04 21:50:40 +02:00
|
|
|
final isc = useInfiniteScrollController();
|
|
|
|
final theme = Theme.of(context);
|
2020-10-05 23:09:36 +02:00
|
|
|
final instancesIcons = useMemoFuture(() async {
|
|
|
|
final map = <String, String>{};
|
|
|
|
final instances = accStore.instances.toList(growable: false);
|
|
|
|
final sites = await Future.wait(instances
|
|
|
|
.map((e) => LemmyApi(e).v1.getSite().catchError((e) => null)));
|
|
|
|
for (var i in Iterable.generate(sites.length)) {
|
|
|
|
map[instances[i]] = sites[i].site.icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
return map;
|
|
|
|
});
|
2020-10-04 21:50:40 +02:00
|
|
|
|
|
|
|
handleListChange() async {
|
2020-10-06 17:28:11 +02:00
|
|
|
final val = await showModalBottomSheet<_SelectedList>(
|
2020-10-04 21:50:40 +02:00
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
isScrollControlled: true,
|
|
|
|
context: context,
|
|
|
|
builder: (context) {
|
2020-10-06 17:28:11 +02:00
|
|
|
pop(_SelectedList thing) => Navigator.of(context).pop(thing);
|
2020-10-04 21:50:40 +02:00
|
|
|
return BottomModal(
|
2020-10-06 16:23:29 +02:00
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
SizedBox(height: 5),
|
2020-10-04 21:50:40 +02:00
|
|
|
ListTile(
|
2020-10-06 16:23:29 +02:00
|
|
|
title: Text('EVERYTHING'),
|
2020-10-04 21:50:40 +02:00
|
|
|
dense: true,
|
|
|
|
contentPadding: EdgeInsets.zero,
|
|
|
|
visualDensity:
|
|
|
|
VisualDensity(vertical: VisualDensity.minimumDensity),
|
2020-10-06 16:23:29 +02:00
|
|
|
leading: SizedBox.shrink(),
|
2020-10-04 21:50:40 +02:00
|
|
|
),
|
|
|
|
ListTile(
|
2020-10-06 16:23:29 +02:00
|
|
|
title: Text('Subscribed'),
|
|
|
|
leading: SizedBox(width: 20, height: 20),
|
|
|
|
onTap: () => pop(
|
2020-10-06 17:28:11 +02:00
|
|
|
_SelectedList(listingType: PostListingType.subscribed)),
|
2020-10-04 21:50:40 +02:00
|
|
|
),
|
|
|
|
ListTile(
|
|
|
|
title: Text('All'),
|
2020-10-06 16:23:29 +02:00
|
|
|
leading: SizedBox(width: 20, height: 20),
|
|
|
|
onTap: () =>
|
2020-10-06 17:28:11 +02:00
|
|
|
pop(_SelectedList(listingType: PostListingType.all)),
|
2020-10-04 21:50:40 +02:00
|
|
|
),
|
2020-10-06 16:23:29 +02:00
|
|
|
for (final instance in accStore.instances) ...[
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 10),
|
|
|
|
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,
|
|
|
|
visualDensity:
|
|
|
|
VisualDensity(vertical: VisualDensity.minimumDensity),
|
|
|
|
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
|
|
|
)
|
|
|
|
: SizedBox(width: 30),
|
|
|
|
),
|
|
|
|
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(instanceUrl: instance))
|
2020-10-06 17:28:11 +02:00
|
|
|
: () => pop(_SelectedList(
|
2020-10-06 16:23:29 +02:00
|
|
|
listingType: PostListingType.subscribed,
|
|
|
|
instanceUrl: instance,
|
|
|
|
)),
|
|
|
|
leading: SizedBox(width: 20),
|
|
|
|
),
|
|
|
|
ListTile(
|
|
|
|
title: Text('All'),
|
2020-10-06 17:28:11 +02:00
|
|
|
onTap: () => pop(_SelectedList(
|
2020-10-06 16:23:29 +02:00
|
|
|
listingType: PostListingType.all,
|
|
|
|
instanceUrl: instance,
|
|
|
|
)),
|
|
|
|
leading: SizedBox(width: 20),
|
|
|
|
),
|
|
|
|
]
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
2020-10-04 21:50:40 +02:00
|
|
|
},
|
|
|
|
);
|
|
|
|
if (val != null) {
|
|
|
|
selectedList.value = val;
|
|
|
|
isc.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 21:55:33 +02:00
|
|
|
final title = () {
|
|
|
|
final first = selectedList.value.listingType == PostListingType.subscribed
|
|
|
|
? 'Subscribed'
|
|
|
|
: 'All';
|
|
|
|
final last = selectedList.value.instanceUrl == null
|
|
|
|
? ''
|
|
|
|
: '@${selectedList.value.instanceUrl}';
|
|
|
|
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,
|
|
|
|
children: [
|
|
|
|
Center(child: Text('there needs to be at least one instance')),
|
|
|
|
],
|
|
|
|
),
|
2020-12-03 22:46:25 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-04 21:50:40 +02:00
|
|
|
return Scaffold(
|
2020-10-05 23:14:36 +02:00
|
|
|
// TODO: make appbar autohide when scrolling down
|
2020-10-04 21:50:40 +02:00
|
|
|
appBar: AppBar(
|
|
|
|
actions: [
|
|
|
|
IconButton(
|
|
|
|
icon: Icon(Icons.notifications),
|
|
|
|
onPressed: () => goTo(context, (_) => InboxPage()),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
centerTitle: true,
|
|
|
|
title: TextButton(
|
2020-10-06 00:54:17 +02:00
|
|
|
style: TextButton.styleFrom(
|
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.all(Radius.circular(10))),
|
|
|
|
padding: 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
|
|
|
),
|
2020-10-06 00:54:17 +02:00
|
|
|
),
|
|
|
|
Icon(
|
|
|
|
Icons.arrow_drop_down,
|
|
|
|
color: theme.primaryTextTheme.headline6.color,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2020-10-04 21:50:40 +02:00
|
|
|
),
|
|
|
|
body: InfiniteHomeList(
|
|
|
|
controller: isc,
|
|
|
|
selectedList: selectedList.value,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 16:22:49 +02:00
|
|
|
/// Infinite list of posts
|
2020-10-04 21:50:40 +02:00
|
|
|
class InfiniteHomeList extends HookWidget {
|
|
|
|
final Function onStyleChange;
|
|
|
|
final InfiniteScrollController controller;
|
2020-10-06 17:28:11 +02:00
|
|
|
final _SelectedList selectedList;
|
2020-10-04 21:50:40 +02:00
|
|
|
InfiniteHomeList({
|
|
|
|
@required this.selectedList,
|
|
|
|
this.onStyleChange,
|
|
|
|
this.controller,
|
|
|
|
}) : assert(selectedList != null);
|
2020-10-06 16:28:38 +02:00
|
|
|
|
2020-10-04 21:50:40 +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(
|
2020-10-04 21:50:40 +02:00
|
|
|
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 =
|
|
|
|
instances.map((instanceUrl) => LemmyApi(instanceUrl).v1.getPosts(
|
|
|
|
type: listingType,
|
|
|
|
sort: sort,
|
|
|
|
page: page,
|
|
|
|
limit: limit,
|
|
|
|
auth: accStore.defaultTokenFor(instanceUrl)?.raw,
|
|
|
|
));
|
2020-10-06 16:29:19 +02:00
|
|
|
final posts = await Future.wait(futures);
|
2020-10-06 00:19:34 +02:00
|
|
|
final newPosts = <PostView>[];
|
|
|
|
for (final i
|
|
|
|
in Iterable.generate(posts.map((e) => e.length).reduce(max))) {
|
|
|
|
for (final el in posts) {
|
|
|
|
if (el.elementAt(i) != null) {
|
|
|
|
newPosts.add(el[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newPosts;
|
2020-10-04 21:50:40 +02:00
|
|
|
}
|
|
|
|
|
2020-10-06 16:30:52 +02:00
|
|
|
Future<List<PostView>> Function(int, int) fetcherFromInstance(
|
2020-10-06 12:06:29 +02:00
|
|
|
String instanceUrl, PostListingType listingType, SortType sort) =>
|
|
|
|
(page, batchSize) => LemmyApi(instanceUrl).v1.getPosts(
|
|
|
|
type: listingType,
|
|
|
|
sort: sort,
|
|
|
|
page: page,
|
|
|
|
limit: batchSize,
|
|
|
|
auth: accStore.defaultTokenFor(instanceUrl)?.raw,
|
|
|
|
);
|
2020-10-04 21:50:40 +02:00
|
|
|
|
|
|
|
return InfiniteScroll<PostView>(
|
|
|
|
prepend: Column(
|
|
|
|
children: [
|
|
|
|
PostListOptions(
|
|
|
|
onChange: changeSorting,
|
|
|
|
defaultSort: SortType.active,
|
|
|
|
styleButton: onStyleChange != null,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
builder: (post) => Column(
|
|
|
|
children: [
|
|
|
|
Post(post),
|
|
|
|
SizedBox(height: 20),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
padding: EdgeInsets.zero,
|
|
|
|
fetchMore: selectedList.instanceUrl == null
|
|
|
|
? (page, limit) =>
|
2020-10-06 16:30:52 +02:00
|
|
|
generalFetcher(page, limit, sort.value, selectedList.listingType)
|
|
|
|
: fetcherFromInstance(
|
2020-10-04 21:50:40 +02:00
|
|
|
selectedList.instanceUrl,
|
|
|
|
selectedList.listingType,
|
|
|
|
sort.value,
|
|
|
|
),
|
|
|
|
controller: controller,
|
|
|
|
batchSize: 20,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 17:28:11 +02:00
|
|
|
class _SelectedList {
|
2020-10-04 21:50:40 +02:00
|
|
|
final String instanceUrl;
|
|
|
|
final PostListingType listingType;
|
2020-10-06 17:28:11 +02:00
|
|
|
_SelectedList({
|
2020-10-04 21:50:40 +02:00
|
|
|
@required this.listingType,
|
|
|
|
this.instanceUrl,
|
|
|
|
});
|
|
|
|
|
|
|
|
String toString() =>
|
|
|
|
'SelectedList({instanceUrl: $instanceUrl, listingType: $listingType})';
|
|
|
|
}
|