feat: 消息分页

This commit is contained in:
guozhigq
2023-12-17 14:55:52 +08:00
parent a43c071eb5
commit a6ab72cadd
8 changed files with 259 additions and 144 deletions

View File

@ -6,10 +6,13 @@ import 'package:pilipala/models/msg/session.dart';
class WhisperController extends GetxController {
RxList<SessionList> sessionList = <SessionList>[].obs;
RxList<AccountListModel> accountList = <AccountListModel>[].obs;
bool isLoading = false;
Future querySessionList() async {
var res = await MsgHttp.sessionList();
if (res['data'].sessionList.isNotEmpty) {
Future querySessionList(String? type) async {
if (isLoading) return;
var res = await MsgHttp.sessionList(
endTs: type == 'onLoad' ? sessionList.last.sessionTs : null);
if (res['data'].sessionList != null && res['data'].sessionList.isNotEmpty) {
await queryAccountList(res['data'].sessionList);
// 将 accountList 转换为 Map 结构
Map<int, dynamic> accountMap = {};
@ -32,10 +35,14 @@ class WhisperController extends GetxController {
}
}
}
if (res['status']) {
sessionList.value = res['data'].sessionList;
if (res['status'] && res['data'].sessionList != null) {
if (type == 'onLoad') {
sessionList.addAll(res['data'].sessionList);
} else {
sessionList.value = res['data'].sessionList;
}
}
isLoading = false;
return res;
}
@ -47,4 +54,12 @@ class WhisperController extends GetxController {
}
return res;
}
Future onLoad() async {
querySessionList('onLoad');
}
Future onRefresh() async {
querySessionList('onRefresh');
}
}

View File

@ -1,3 +1,4 @@
import 'package:easy_debounce/easy_throttle.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pilipala/common/widgets/network_img_layer.dart';
@ -16,18 +17,31 @@ class _WhisperPageState extends State<WhisperPage> {
late final WhisperController _whisperController =
Get.put(WhisperController());
late Future _futureBuilderFuture;
final ScrollController _scrollController = ScrollController();
@override
void initState() {
super.initState();
_futureBuilderFuture = _whisperController.querySessionList();
_futureBuilderFuture = _whisperController.querySessionList('init');
_scrollController.addListener(_scrollListener);
}
Future _scrollListener() async {
if (_scrollController.position.pixels >=
_scrollController.position.maxScrollExtent - 200) {
EasyThrottle.throttle('my-throttler', const Duration(milliseconds: 800),
() async {
await _whisperController.onLoad();
_whisperController.isLoading = true;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
scrolledUnderElevation: 0,
title: const Text('消息'),
),
body: Column(
children: [
@ -82,95 +96,133 @@ class _WhisperPageState extends State<WhisperPage> {
// },
// ),
Expanded(
child: FutureBuilder(
future: _futureBuilderFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
Map data = snapshot.data as Map;
if (data['status']) {
List sessionList = _whisperController.sessionList;
return Obx(
() => sessionList.isEmpty
? const SizedBox()
: ListView.builder(
itemCount: sessionList.length,
shrinkWrap: true,
itemBuilder: (_, int i) {
return ListTile(
onTap: () {
Get.toNamed(
'/whisperDetail?talkerId=${sessionList[i].talkerId}&name=${sessionList[i].accountInfo.name}');
},
leading: NetworkImgLayer(
width: 45,
height: 45,
type: 'avatar',
src: sessionList[i].accountInfo.face,
),
title: Text(
sessionList[i].accountInfo.name,
style:
Theme.of(context).textTheme.titleSmall,
),
subtitle: Text(
sessionList[i].lastMsg.content['text'] ??
sessionList[i]
.lastMsg
.content['content'] ??
sessionList[i]
.lastMsg
.content['title'],
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: Theme.of(context)
.textTheme
.labelMedium!
.copyWith(
color: Theme.of(context)
.colorScheme
.outline)),
trailing: Text(
Utils.dateFormat(
sessionList[i].lastMsg.timestamp),
style: Theme.of(context)
.textTheme
.labelSmall!
.copyWith(
color: Theme.of(context)
.colorScheme
.outline),
),
);
},
),
);
} else {
// 请求错误
return SizedBox();
}
} else {
// 骨架屏
return SizedBox();
}
child: RefreshIndicator(
onRefresh: () async {
await _whisperController.onRefresh();
},
child: SingleChildScrollView(
controller: _scrollController,
child: Column(
children: [
FutureBuilder(
future: _futureBuilderFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
Map data = snapshot.data as Map;
if (data['status']) {
List sessionList = _whisperController.sessionList;
return Obx(
() => sessionList.isEmpty
? const SizedBox()
: ListView.separated(
itemCount: sessionList.length,
shrinkWrap: true,
physics:
const NeverScrollableScrollPhysics(),
itemBuilder: (_, int i) {
return ListTile(
onTap: () => Get.toNamed(
'/whisperDetail',
parameters: {
'talkerId': sessionList[i]
.talkerId
.toString(),
'name': sessionList[i]
.accountInfo
.name,
'face': sessionList[i]
.accountInfo
.face,
'mid': sessionList[i]
.accountInfo
.mid
.toString(),
},
),
leading: Badge(
isLabelVisible: false,
backgroundColor: Theme.of(context)
.colorScheme
.primary,
label: Text(sessionList[i]
.unreadCount
.toString()),
alignment: Alignment.bottomRight,
child: NetworkImgLayer(
width: 45,
height: 45,
type: 'avatar',
src: sessionList[i]
.accountInfo
.face,
),
),
title: Text(
sessionList[i].accountInfo.name),
subtitle: Text(
sessionList[i]
.lastMsg
.content['text'] ??
sessionList[i]
.lastMsg
.content['content'] ??
sessionList[i]
.lastMsg
.content['title'] ??
sessionList[i]
.lastMsg
.content[
'reply_content'] ??
'',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: Theme.of(context)
.textTheme
.labelMedium!
.copyWith(
color: Theme.of(context)
.colorScheme
.outline)),
trailing: Text(
Utils.dateFormat(sessionList[i]
.lastMsg
.timestamp),
style: Theme.of(context)
.textTheme
.labelSmall!
.copyWith(
color: Theme.of(context)
.colorScheme
.outline),
),
);
},
separatorBuilder:
(BuildContext context, int index) {
return Divider(
indent: 72,
endIndent: 20,
height: 6,
color: Colors.grey.withOpacity(0.1),
);
},
),
);
} else {
// 请求错误
return SizedBox();
}
} else {
// 骨架屏
return SizedBox();
}
},
)
],
),
),
),
),
// ListTile(
// onTap: () {},
// leading: CircleAvatar(),
// title: Text('钱瑞昌'),
// subtitle: Text('没事', style: Theme.of(context).textTheme.bodySmall),
// trailing: Text('昨天'),
// ),
// ListTile(
// onTap: () {},
// leading: CircleAvatar(),
// title: Text('李天'),
// subtitle:
// Text('明天有空吗', style: Theme.of(context).textTheme.bodySmall),
// trailing: Text('现在'),
// )
],
),
);