subscription list+view
This commit is contained in:
115
flutter/lib/pages/subscription_list/subscription_list.dart
Normal file
115
flutter/lib/pages/subscription_list/subscription_list.dart
Normal file
@@ -0,0 +1,115 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:simplecloudnotifier/api/api_client.dart';
|
||||
import 'package:simplecloudnotifier/components/layout/scaffold.dart';
|
||||
import 'package:simplecloudnotifier/models/channel.dart';
|
||||
import 'package:simplecloudnotifier/models/subscription.dart';
|
||||
import 'package:simplecloudnotifier/models/user.dart';
|
||||
import 'package:simplecloudnotifier/state/application_log.dart';
|
||||
import 'package:simplecloudnotifier/state/app_auth.dart';
|
||||
import 'package:simplecloudnotifier/pages/subscription_list/subscription_list_item.dart';
|
||||
import 'package:simplecloudnotifier/state/scn_data_cache.dart';
|
||||
|
||||
class SubscriptionListPage extends StatefulWidget {
|
||||
const SubscriptionListPage({super.key});
|
||||
|
||||
@override
|
||||
State<SubscriptionListPage> createState() => _SubscriptionListPageState();
|
||||
}
|
||||
|
||||
class _SubscriptionListPageState extends State<SubscriptionListPage> {
|
||||
final PagingController<int, Subscription> _pagingController = PagingController.fromValue(PagingState(nextPageKey: null, itemList: [], error: null), firstPageKey: 0);
|
||||
|
||||
final userCache = Map<String, UserPreview>();
|
||||
final channelCache = Map<String, ChannelPreview>();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
for (var v in SCNDataCache().getChannelMap().entries) channelCache[v.key] = v.value.toPreview(null);
|
||||
|
||||
_pagingController.addPageRequestListener(_fetchPage);
|
||||
|
||||
_pagingController.refresh();
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
ApplicationLog.debug('SubscriptionListPage::dispose');
|
||||
_pagingController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _fetchPage(int pageKey) async {
|
||||
final acc = Provider.of<AppAuth>(context, listen: false);
|
||||
|
||||
ApplicationLog.debug('Start SubscriptionListPage::_pagingController::_fetchPage [ ${pageKey} ]');
|
||||
|
||||
if (!acc.isAuth()) {
|
||||
_pagingController.error = 'Not logged in';
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
final items = (await APIClient.getSubscriptionList(acc)).toList();
|
||||
|
||||
items.sort((a, b) => -1 * a.timestampCreated.compareTo(b.timestampCreated));
|
||||
|
||||
var promises = Map<String, Future<UserPreview>>();
|
||||
|
||||
for (var item in items) {
|
||||
if (userCache[item.subscriberUserID] == null && !promises.containsKey(item.subscriberUserID)) {
|
||||
promises[item.subscriberUserID] = APIClient.getUserPreview(acc, item.subscriberUserID).then((p) => userCache[p.userID] = p);
|
||||
}
|
||||
if (userCache[item.channelOwnerUserID] == null && !promises.containsKey(item.channelOwnerUserID)) {
|
||||
promises[item.channelOwnerUserID] = APIClient.getUserPreview(acc, item.channelOwnerUserID).then((p) => userCache[p.userID] = p);
|
||||
}
|
||||
if (channelCache[item.channelID] == null && !promises.containsKey(item.channelID)) {
|
||||
channelCache[item.channelID] = await APIClient.getChannelPreview(acc, item.channelID).then((p) => channelCache[p.channelID] = p);
|
||||
}
|
||||
}
|
||||
|
||||
await Future.wait(promises.values);
|
||||
|
||||
_pagingController.value = PagingState(nextPageKey: null, itemList: items, error: null);
|
||||
} catch (exc, trace) {
|
||||
_pagingController.error = exc.toString();
|
||||
ApplicationLog.error('Failed to list subscriptions: ' + exc.toString(), trace: trace);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SCNScaffold(
|
||||
title: "Subscriptions",
|
||||
showSearch: false,
|
||||
showShare: false,
|
||||
child: Padding(
|
||||
padding: EdgeInsets.fromLTRB(8, 4, 8, 4),
|
||||
child: RefreshIndicator(
|
||||
onRefresh: () => Future.sync(
|
||||
() => _pagingController.refresh(),
|
||||
),
|
||||
child: PagedListView<int, Subscription>(
|
||||
pagingController: _pagingController,
|
||||
builderDelegate: PagedChildBuilderDelegate<Subscription>(
|
||||
itemBuilder: (context, item, index) => SubscriptionListItem(item: item, userCache: userCache, channelCache: channelCache, needsReload: fullRefresh),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void fullRefresh() {
|
||||
ApplicationLog.debug('SubscriptionListPage::fullRefresh');
|
||||
_pagingController.refresh();
|
||||
}
|
||||
}
|
||||
116
flutter/lib/pages/subscription_list/subscription_list_item.dart
Normal file
116
flutter/lib/pages/subscription_list/subscription_list_item.dart
Normal file
@@ -0,0 +1,116 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:simplecloudnotifier/models/channel.dart';
|
||||
import 'package:simplecloudnotifier/models/subscription.dart';
|
||||
import 'package:simplecloudnotifier/models/user.dart';
|
||||
import 'package:simplecloudnotifier/pages/subscription_view/subscription_view.dart';
|
||||
import 'package:simplecloudnotifier/state/app_auth.dart';
|
||||
import 'package:simplecloudnotifier/utils/navi.dart';
|
||||
|
||||
enum SubscriptionListItemMode {
|
||||
Messages,
|
||||
Extended,
|
||||
}
|
||||
|
||||
class SubscriptionListItem extends StatelessWidget {
|
||||
static final _dateFormat = DateFormat('yyyy-MM-dd HH:mm'); //TODO setting
|
||||
|
||||
const SubscriptionListItem({
|
||||
required this.item,
|
||||
required this.userCache,
|
||||
required this.channelCache,
|
||||
required this.needsReload,
|
||||
super.key,
|
||||
});
|
||||
|
||||
final Subscription item;
|
||||
final Map<String, UserPreview> userCache;
|
||||
final Map<String, ChannelPreview> channelCache;
|
||||
|
||||
final void Function()? needsReload;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final channelOwner = userCache[item.channelOwnerUserID];
|
||||
final subscriber = userCache[item.subscriberUserID];
|
||||
final channel = channelCache[item.channelID];
|
||||
|
||||
return Card.filled(
|
||||
margin: EdgeInsets.fromLTRB(0, 4, 0, 4),
|
||||
shape: BeveledRectangleBorder(borderRadius: BorderRadius.circular(0)),
|
||||
color: Theme.of(context).cardTheme.color,
|
||||
child: InkWell(
|
||||
onTap: () {
|
||||
Navi.push(context, () => SubscriptionViewPage(subscriptionID: item.subscriptionID, preloadedData: (item, channelOwner, subscriber, channel), needsReload: this.needsReload));
|
||||
},
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(FontAwesomeIcons.solidDiagramSubtask, color: Theme.of(context).colorScheme.outline, size: 32),
|
||||
SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Text(
|
||||
subscriber?.username ?? item.subscriberUserID,
|
||||
style: const TextStyle(),
|
||||
),
|
||||
SizedBox(height: 4),
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
Text(
|
||||
"@" + (channel?.displayName ?? item.channelID),
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
SizedBox(width: 10),
|
||||
Text(
|
||||
"(" + (channelOwner?.username ?? item.channelOwnerUserID) + ")",
|
||||
style: const TextStyle(fontStyle: FontStyle.italic),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
SizedBox(width: 4),
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: _buildIcon(context),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildIcon(BuildContext context) {
|
||||
final acc = Provider.of<AppAuth>(context, listen: false);
|
||||
|
||||
final colorFull = Theme.of(context).colorScheme.onPrimaryContainer;
|
||||
final colorHalf = Theme.of(context).colorScheme.onPrimaryContainer.withAlpha(75);
|
||||
|
||||
final isOwned = item.channelOwnerUserID == acc.userID && item.subscriberUserID == acc.userID;
|
||||
final isIncoming = item.channelOwnerUserID == acc.userID && item.subscriberUserID != acc.userID;
|
||||
final isOutgoing = item.channelOwnerUserID != acc.userID && item.subscriberUserID == acc.userID;
|
||||
|
||||
if (isOutgoing && !item.confirmed) return Icon(FontAwesomeIcons.solidSquareEnvelope, color: colorHalf, size: 24);
|
||||
if (isOutgoing && !item.active) return Icon(FontAwesomeIcons.solidSquareShareNodes, color: colorHalf, size: 24);
|
||||
if (isOutgoing && item.active) return Icon(FontAwesomeIcons.solidSquareShareNodes, color: colorFull, size: 24);
|
||||
|
||||
if (isIncoming && !item.confirmed) return Icon(FontAwesomeIcons.solidSquareQuestion, color: colorHalf, size: 24);
|
||||
if (isIncoming && !item.active) return Icon(FontAwesomeIcons.solidSquareCheck, color: colorHalf, size: 24);
|
||||
if (isIncoming && item.active) return Icon(FontAwesomeIcons.solidSquareCheck, color: colorFull, size: 24);
|
||||
|
||||
if (isOwned && !item.confirmed) return Icon(FontAwesomeIcons.solidSquare, color: colorHalf, size: 24); // should not be possible
|
||||
if (isOwned && !item.active) return Icon(FontAwesomeIcons.solidSquareRss, color: colorHalf, size: 24);
|
||||
if (isOwned && item.active) return Icon(FontAwesomeIcons.solidSquareRss, color: colorFull, size: 24);
|
||||
|
||||
return SizedBox(width: 24, height: 24); // should also not be possible
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user