2024-04-13 11:47:31 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2024-04-12 16:38:20 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-04-19 09:51:00 +00:00
|
|
|
import 'package:provider/provider.dart';
|
2024-04-13 11:47:31 +00:00
|
|
|
import 'package:solian/models/pagination.dart';
|
|
|
|
import 'package:solian/models/post.dart';
|
2024-04-19 09:51:00 +00:00
|
|
|
import 'package:solian/providers/auth.dart';
|
2024-04-13 17:07:57 +00:00
|
|
|
import 'package:solian/router.dart';
|
2024-04-13 11:47:31 +00:00
|
|
|
import 'package:solian/utils/service_url.dart';
|
|
|
|
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
|
2024-04-12 16:38:20 +00:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2024-04-13 11:47:31 +00:00
|
|
|
import 'package:http/http.dart' as http;
|
2024-05-03 05:51:44 +00:00
|
|
|
import 'package:solian/utils/theme.dart';
|
2024-05-03 05:39:52 +00:00
|
|
|
import 'package:solian/widgets/scaffold.dart';
|
2024-04-24 15:19:26 +00:00
|
|
|
import 'package:solian/widgets/notification_notifier.dart';
|
2024-05-03 05:39:52 +00:00
|
|
|
import 'package:solian/widgets/posts/post.dart';
|
2024-04-12 16:38:20 +00:00
|
|
|
|
2024-05-03 05:51:44 +00:00
|
|
|
class ExplorePostScreen extends StatelessWidget {
|
|
|
|
const ExplorePostScreen({super.key});
|
2024-05-01 16:49:38 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-05-03 05:39:52 +00:00
|
|
|
return IndentScaffold(
|
2024-05-01 16:49:38 +00:00
|
|
|
noSafeArea: true,
|
2024-05-03 05:51:44 +00:00
|
|
|
fixedAppBarColor: SolianTheme.isLargeScreen(context),
|
2024-05-01 16:49:38 +00:00
|
|
|
appBarActions: const [NotificationButton()],
|
|
|
|
title: AppLocalizations.of(context)!.explore,
|
2024-05-03 05:51:44 +00:00
|
|
|
child: ExplorePostWidget(
|
|
|
|
onSelect: (item) {
|
|
|
|
SolianRouter.router.pushNamed(
|
|
|
|
'posts.screen',
|
|
|
|
pathParameters: {
|
|
|
|
'alias': item.alias,
|
|
|
|
'dataset': item.dataset,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2024-05-01 16:49:38 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-03 05:51:44 +00:00
|
|
|
class ExplorePostWidget extends StatefulWidget {
|
2024-05-01 16:49:38 +00:00
|
|
|
final Function(Post item) onSelect;
|
|
|
|
|
2024-05-03 05:51:44 +00:00
|
|
|
const ExplorePostWidget({super.key, required this.onSelect});
|
2024-05-01 16:49:38 +00:00
|
|
|
|
|
|
|
@override
|
2024-05-03 05:51:44 +00:00
|
|
|
State<ExplorePostWidget> createState() => _ExplorePostWidgetState();
|
2024-05-01 16:49:38 +00:00
|
|
|
}
|
|
|
|
|
2024-05-03 05:51:44 +00:00
|
|
|
class _ExplorePostWidgetState extends State<ExplorePostWidget> {
|
2024-05-01 16:49:38 +00:00
|
|
|
final PagingController<int, Post> _pagingController = PagingController(firstPageKey: 0);
|
2024-04-13 11:47:31 +00:00
|
|
|
|
|
|
|
final http.Client _client = http.Client();
|
|
|
|
|
|
|
|
Future<void> fetchFeed(int pageKey) async {
|
|
|
|
final offset = pageKey;
|
|
|
|
const take = 5;
|
|
|
|
|
2024-05-01 16:49:38 +00:00
|
|
|
var uri = getRequestUri('interactive', '/api/feed?take=$take&offset=$offset');
|
2024-04-13 11:47:31 +00:00
|
|
|
|
|
|
|
var res = await _client.get(uri);
|
|
|
|
if (res.statusCode == 200) {
|
2024-05-01 16:49:38 +00:00
|
|
|
final result = PaginationResult.fromJson(jsonDecode(utf8.decode(res.bodyBytes)));
|
|
|
|
final items = result.data?.map((x) => Post.fromJson(x)).toList() ?? List.empty();
|
2024-04-13 11:47:31 +00:00
|
|
|
final isLastPage = (result.count - pageKey) < take;
|
|
|
|
if (isLastPage || result.data == null) {
|
|
|
|
_pagingController.appendLastPage(items);
|
|
|
|
} else {
|
|
|
|
final nextPageKey = pageKey + items.length;
|
|
|
|
_pagingController.appendPage(items, nextPageKey);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_pagingController.error = utf8.decode(res.bodyBytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-12 16:38:20 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2024-04-13 11:47:31 +00:00
|
|
|
|
|
|
|
_pagingController.addPageRequestListener((pageKey) => fetchFeed(pageKey));
|
2024-04-12 16:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-04-19 09:51:00 +00:00
|
|
|
final auth = context.read<AuthProvider>();
|
|
|
|
|
2024-05-01 16:49:38 +00:00
|
|
|
return Scaffold(
|
2024-04-19 09:51:00 +00:00
|
|
|
floatingActionButton: FutureBuilder(
|
|
|
|
future: auth.isAuthorized(),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.hasData && snapshot.data!) {
|
|
|
|
return FloatingActionButton(
|
|
|
|
child: const Icon(Icons.edit),
|
|
|
|
onPressed: () async {
|
2024-05-03 05:39:52 +00:00
|
|
|
final did = await SolianRouter.router.pushNamed('posts.moments.editor');
|
2024-04-19 09:51:00 +00:00
|
|
|
if (did == true) _pagingController.refresh();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return Container();
|
|
|
|
}
|
2024-04-14 07:58:27 +00:00
|
|
|
},
|
|
|
|
),
|
2024-05-01 16:49:38 +00:00
|
|
|
body: RefreshIndicator(
|
2024-04-13 16:03:50 +00:00
|
|
|
onRefresh: () => Future.sync(
|
|
|
|
() => _pagingController.refresh(),
|
|
|
|
),
|
2024-04-25 15:03:16 +00:00
|
|
|
child: PagedListView<int, Post>(
|
|
|
|
pagingController: _pagingController,
|
|
|
|
builderDelegate: PagedChildBuilderDelegate<Post>(
|
|
|
|
itemBuilder: (context, item, index) => PostItem(
|
|
|
|
item: item,
|
|
|
|
onUpdate: () => _pagingController.refresh(),
|
2024-05-01 16:49:38 +00:00
|
|
|
onTap: () => widget.onSelect(item),
|
2024-04-13 11:47:31 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2024-04-12 16:38:20 +00:00
|
|
|
);
|
|
|
|
}
|
2024-04-13 17:07:57 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_pagingController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
2024-04-12 16:38:20 +00:00
|
|
|
}
|