This repository has been archived on 2024-06-08. You can view files and clone it, but cannot push or open issues or pull requests.
SolarAgent/lib/screens/explore.dart

105 lines
3.0 KiB
Dart
Raw Normal View History

2024-03-23 15:05:04 +00:00
import 'dart:convert';
import 'package:flutter/material.dart';
2024-03-23 17:22:01 +00:00
import 'package:go_router/go_router.dart';
2024-03-23 15:05:04 +00:00
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
2024-03-23 17:22:01 +00:00
import 'package:solaragent/auth.dart';
2024-03-23 15:05:04 +00:00
import 'package:solaragent/models/feed.dart';
import 'package:solaragent/models/pagination.dart';
import 'package:http/http.dart' as http;
2024-03-23 17:22:01 +00:00
import 'package:solaragent/router.dart';
2024-03-23 15:05:04 +00:00
import 'package:solaragent/widgets/feed.dart';
2024-03-23 17:22:01 +00:00
import 'package:solaragent/screens/publish/moment_editor.dart';
2024-03-23 15:05:04 +00:00
class ExploreScreen extends StatefulWidget {
const ExploreScreen({super.key});
@override
State<ExploreScreen> createState() => _ExploreScreenState();
}
class _ExploreScreenState extends State<ExploreScreen> {
static const pageSize = 5;
final client = http.Client();
final PagingController<int, Feed> paginationController =
PagingController(firstPageKey: 0);
List<Feed> feed = List.empty();
@override
void initState() {
super.initState();
paginationController.addPageRequestListener((pageKey) {
pullFeed(pageKey);
});
}
Future<void> pullFeed(int pageKey) async {
var offset = pageKey;
var take = pageSize;
var uri =
Uri.parse('https://co.solsynth.dev/api/feed?take=$take&offset=$offset');
var res = await client.get(uri);
if (res.statusCode == 200) {
final result =
PaginationResult.fromJson(jsonDecode(utf8.decode(res.bodyBytes)));
2024-03-24 03:07:36 +00:00
final isLastPage = (result.count - pageKey) < pageSize;
2024-03-24 04:12:13 +00:00
final items =
result.data?.map((x) => Feed.fromJson(x)).toList() ?? List.empty();
2024-03-24 03:07:36 +00:00
if (isLastPage || result.data == null) {
2024-03-24 04:12:13 +00:00
paginationController.appendLastPage(items);
2024-03-23 15:05:04 +00:00
} else {
2024-03-24 04:12:13 +00:00
final nextPageKey = pageKey + items.length;
paginationController.appendPage(items, nextPageKey);
2024-03-23 15:05:04 +00:00
}
} else {
paginationController.error = utf8.decode(res.bodyBytes);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
2024-03-24 03:35:18 +00:00
body: RefreshIndicator(
onRefresh: () async {
paginationController.refresh();
},
child: PagedListView<int, Feed>(
pagingController: paginationController,
builderDelegate: PagedChildBuilderDelegate<Feed>(
itemBuilder: (context, item, index) => FeedItem(
item: item,
),
2024-03-23 15:05:04 +00:00
),
),
),
2024-03-23 17:22:01 +00:00
floatingActionButton: FutureBuilder(
future: authClient.isAuthorized(),
builder: (context, snapshot) {
if (snapshot.hasData && snapshot.data == true) {
return FloatingActionButton(
child: const Icon(Icons.edit),
onPressed: () {
2024-03-24 04:32:24 +00:00
router.push("/post/moments").then((value) {
if (value == true) paginationController.refresh();
});
2024-03-23 17:22:01 +00:00
},
);
} else {
return Container();
}
}),
2024-03-23 15:05:04 +00:00
);
}
@override
void dispose() {
paginationController.dispose();
super.dispose();
}
}