channel list
This commit is contained in:
parent
f9dbbf4638
commit
6b7bf600f8
@ -146,12 +146,16 @@ class APIClient {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<(String, List<Message>)> getMessageList(KeyTokenAuth auth, String pageToken, int? pageSize) async {
|
static Future<(String, List<Message>)> getMessageList(KeyTokenAuth auth, String pageToken, {int? pageSize, List<String>? channelIDs}) async {
|
||||||
return await _request(
|
return await _request(
|
||||||
name: 'getMessageList',
|
name: 'getMessageList',
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
relURL: 'messages',
|
relURL: 'messages',
|
||||||
query: {'next_page_token': pageToken, if (pageSize != null) 'page_size': pageSize.toString()},
|
query: {
|
||||||
|
'next_page_token': pageToken,
|
||||||
|
if (pageSize != null) 'page_size': pageSize.toString(),
|
||||||
|
if (channelIDs != null) 'channel_id': channelIDs.join(","),
|
||||||
|
},
|
||||||
fn: (json) => Message.fromPaginatedJsonArray(json, 'messages', 'next_page_token'),
|
fn: (json) => Message.fromPaginatedJsonArray(json, 'messages', 'next_page_token'),
|
||||||
auth: auth,
|
auth: auth,
|
||||||
);
|
);
|
||||||
|
@ -44,6 +44,8 @@ class _ChannelRootPageState extends State<ChannelRootPage> {
|
|||||||
try {
|
try {
|
||||||
final items = await APIClient.getChannelList(acc.auth!, ChannelSelector.all);
|
final items = await APIClient.getChannelList(acc.auth!, ChannelSelector.all);
|
||||||
|
|
||||||
|
items.sort((a, b) => -1 * (a.timestampLastSent ?? '').compareTo(b.timestampLastSent ?? ''));
|
||||||
|
|
||||||
_pagingController.appendLastPage(items);
|
_pagingController.appendLastPage(items);
|
||||||
} catch (exc, trace) {
|
} catch (exc, trace) {
|
||||||
_pagingController.error = exc.toString();
|
_pagingController.error = exc.toString();
|
||||||
@ -53,11 +55,17 @@ class _ChannelRootPageState extends State<ChannelRootPage> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return PagedListView<int, Channel>(
|
return RefreshIndicator(
|
||||||
pagingController: _pagingController,
|
onRefresh: () => Future.sync(
|
||||||
builderDelegate: PagedChildBuilderDelegate<Channel>(
|
() => _pagingController.refresh(),
|
||||||
itemBuilder: (context, item, index) => ChannelListItem(
|
),
|
||||||
channel: item,
|
child: PagedListView<int, Channel>(
|
||||||
|
pagingController: _pagingController,
|
||||||
|
builderDelegate: PagedChildBuilderDelegate<Channel>(
|
||||||
|
itemBuilder: (context, item, index) => ChannelListItem(
|
||||||
|
channel: item,
|
||||||
|
onPressed: () {/*TODO*/},
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
@ -1,17 +1,92 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:intl/intl.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
import 'package:simplecloudnotifier/api/api_client.dart';
|
||||||
import 'package:simplecloudnotifier/models/channel.dart';
|
import 'package:simplecloudnotifier/models/channel.dart';
|
||||||
|
import 'package:simplecloudnotifier/models/message.dart';
|
||||||
|
import 'package:simplecloudnotifier/state/user_account.dart';
|
||||||
|
|
||||||
|
class ChannelListItem extends StatefulWidget {
|
||||||
|
static final _dateFormat = DateFormat('yyyy-MM-dd kk:mm');
|
||||||
|
|
||||||
class ChannelListItem extends StatelessWidget {
|
|
||||||
const ChannelListItem({
|
const ChannelListItem({
|
||||||
required this.channel,
|
required this.channel,
|
||||||
|
required this.onPressed,
|
||||||
super.key,
|
super.key,
|
||||||
});
|
});
|
||||||
|
|
||||||
final Channel channel;
|
final Channel channel;
|
||||||
|
final Null Function() onPressed;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) => ListTile(
|
State<ChannelListItem> createState() => _ChannelListItemState();
|
||||||
leading: const SizedBox(width: 40, height: 40, child: const Placeholder()),
|
}
|
||||||
title: Text(channel.internalName),
|
|
||||||
);
|
class _ChannelListItemState extends State<ChannelListItem> {
|
||||||
|
Message? lastMessage;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
|
||||||
|
final acc = Provider.of<UserAccount>(context, listen: false);
|
||||||
|
|
||||||
|
if (acc.auth != null) {
|
||||||
|
() async {
|
||||||
|
final (_, channelMessages) = await APIClient.getMessageList(acc.auth!, '@start', pageSize: 1, channelIDs: [widget.channel.channelID]);
|
||||||
|
setState(() {
|
||||||
|
lastMessage = channelMessages.firstOrNull;
|
||||||
|
});
|
||||||
|
}();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
//TODO subscription status
|
||||||
|
return Card.filled(
|
||||||
|
margin: EdgeInsets.fromLTRB(0, 4, 0, 4),
|
||||||
|
shape: BeveledRectangleBorder(borderRadius: BorderRadius.circular(0)),
|
||||||
|
color: Theme.of(context).cardTheme.color,
|
||||||
|
child: InkWell(
|
||||||
|
splashColor: Theme.of(context).splashColor,
|
||||||
|
onTap: widget.onPressed,
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.all(8),
|
||||||
|
child: Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
|
children: [
|
||||||
|
Row(
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: Text(
|
||||||
|
widget.channel.displayName,
|
||||||
|
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
Text(
|
||||||
|
(widget.channel.timestampLastSent == null) ? '' : ChannelListItem._dateFormat.format(DateTime.parse(widget.channel.timestampLastSent!).toLocal()),
|
||||||
|
style: const TextStyle(fontSize: 14),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
SizedBox(height: 4),
|
||||||
|
Row(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.end,
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: Text(
|
||||||
|
lastMessage?.title ?? '...',
|
||||||
|
style: TextStyle(color: Theme.of(context).textTheme.bodyLarge?.color?.withAlpha(160)),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
Text(widget.channel.messagesSent.toString(), style: const TextStyle(fontSize: 14, fontWeight: FontWeight.bold)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@ class _MessageListPageState extends State<MessageListPage> {
|
|||||||
_channels = <String, ChannelWithSubscription>{for (var v in channels) v.channelID: v};
|
_channels = <String, ChannelWithSubscription>{for (var v in channels) v.channelID: v};
|
||||||
}
|
}
|
||||||
|
|
||||||
final (npt, newItems) = await APIClient.getMessageList(acc.auth!, thisPageToken, _pageSize);
|
final (npt, newItems) = await APIClient.getMessageList(acc.auth!, thisPageToken, pageSize: _pageSize);
|
||||||
|
|
||||||
if (npt == '@end') {
|
if (npt == '@end') {
|
||||||
_pagingController.appendLastPage(newItems);
|
_pagingController.appendLastPage(newItems);
|
||||||
@ -68,15 +68,20 @@ class _MessageListPageState extends State<MessageListPage> {
|
|||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Padding(
|
return Padding(
|
||||||
padding: EdgeInsets.fromLTRB(8, 4, 8, 4),
|
padding: EdgeInsets.fromLTRB(8, 4, 8, 4),
|
||||||
child: PagedListView<String, Message>(
|
child: RefreshIndicator(
|
||||||
pagingController: _pagingController,
|
onRefresh: () => Future.sync(
|
||||||
builderDelegate: PagedChildBuilderDelegate<Message>(
|
() => _pagingController.refresh(),
|
||||||
itemBuilder: (context, item, index) => MessageListItem(
|
),
|
||||||
message: item,
|
child: PagedListView<String, Message>(
|
||||||
allChannels: _channels ?? {},
|
pagingController: _pagingController,
|
||||||
onPressed: () {
|
builderDelegate: PagedChildBuilderDelegate<Message>(
|
||||||
Navigator.push(context, MaterialPageRoute<MessageViewPage>(builder: (context) => MessageViewPage(message: item)));
|
itemBuilder: (context, item, index) => MessageListItem(
|
||||||
},
|
message: item,
|
||||||
|
allChannels: _channels ?? {},
|
||||||
|
onPressed: () {
|
||||||
|
Navigator.push(context, MaterialPageRoute<MessageViewPage>(builder: (context) => MessageViewPage(message: item)));
|
||||||
|
},
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
Loading…
Reference in New Issue
Block a user