105 lines
3.0 KiB
Dart
105 lines
3.0 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
|
|
import 'package:solaragent/auth.dart';
|
|
import 'package:solaragent/models/feed.dart';
|
|
import 'package:solaragent/models/pagination.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:solaragent/router.dart';
|
|
import 'package:solaragent/widgets/feed.dart';
|
|
import 'package:solaragent/screens/publish/moment_editor.dart';
|
|
|
|
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)));
|
|
final isLastPage = (result.count - pageKey) < pageSize;
|
|
final items =
|
|
result.data?.map((x) => Feed.fromJson(x)).toList() ?? List.empty();
|
|
if (isLastPage || result.data == null) {
|
|
paginationController.appendLastPage(items);
|
|
} else {
|
|
final nextPageKey = pageKey + items.length;
|
|
paginationController.appendPage(items, nextPageKey);
|
|
}
|
|
} else {
|
|
paginationController.error = utf8.decode(res.bodyBytes);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: RefreshIndicator(
|
|
onRefresh: () async {
|
|
paginationController.refresh();
|
|
},
|
|
child: PagedListView<int, Feed>(
|
|
pagingController: paginationController,
|
|
builderDelegate: PagedChildBuilderDelegate<Feed>(
|
|
itemBuilder: (context, item, index) => FeedItem(
|
|
item: item,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
floatingActionButton: FutureBuilder(
|
|
future: authClient.isAuthorized(),
|
|
builder: (context, snapshot) {
|
|
if (snapshot.hasData && snapshot.data == true) {
|
|
return FloatingActionButton(
|
|
child: const Icon(Icons.edit),
|
|
onPressed: () {
|
|
router.push("/post/moments").then((value) {
|
|
if (value == true) paginationController.refresh();
|
|
});
|
|
},
|
|
);
|
|
} else {
|
|
return Container();
|
|
}
|
|
}),
|
|
);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
paginationController.dispose();
|
|
super.dispose();
|
|
}
|
|
}
|