refa: msg top page

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
bggRGjQaUbCoE
2025-03-04 12:32:20 +08:00
parent 8ef163dd38
commit 4642eda98d
11 changed files with 478 additions and 561 deletions

View File

@@ -1,5 +1,10 @@
import 'dart:math'; import 'dart:math';
import 'package:PiliPlus/http/constants.dart'; import 'package:PiliPlus/http/constants.dart';
import 'package:PiliPlus/http/loading_state.dart';
import 'package:PiliPlus/models/msg/msgfeed_at_me.dart';
import 'package:PiliPlus/models/msg/msgfeed_like_me.dart';
import 'package:PiliPlus/models/msg/msgfeed_reply_me.dart';
import 'package:PiliPlus/models/msg/msgfeed_sys_msg.dart';
import 'package:PiliPlus/pages/dynamics/view.dart' show ReplyOption; import 'package:PiliPlus/pages/dynamics/view.dart' show ReplyOption;
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@@ -12,7 +17,8 @@ import 'api.dart';
import 'init.dart'; import 'init.dart';
class MsgHttp { class MsgHttp {
static Future msgFeedReplyMe({int cursor = -1, int cursorTime = -1}) async { static Future<LoadingState> msgFeedReplyMe(
{int cursor = -1, int cursorTime = -1}) async {
var res = await Request().get(Api.msgFeedReply, queryParameters: { var res = await Request().get(Api.msgFeedReply, queryParameters: {
'id': cursor == -1 ? null : cursor, 'id': cursor == -1 ? null : cursor,
'reply_time': cursorTime == -1 ? null : cursorTime, 'reply_time': cursorTime == -1 ? null : cursorTime,
@@ -21,20 +27,15 @@ class MsgHttp {
'build': '8350200', 'build': '8350200',
}); });
if (res.data['code'] == 0) { if (res.data['code'] == 0) {
return { MsgFeedReplyMe data = MsgFeedReplyMe.fromJson(res.data['data']);
'status': true, return LoadingState.success(data);
'data': res.data['data'],
};
} else { } else {
return { return LoadingState.error(res.data['message']);
'status': false,
'date': [],
'msg': res.data['message'],
};
} }
} }
static Future msgFeedAtMe({int cursor = -1, int cursorTime = -1}) async { static Future<LoadingState> msgFeedAtMe(
{int cursor = -1, int cursorTime = -1}) async {
var res = await Request().get(Api.msgFeedAt, queryParameters: { var res = await Request().get(Api.msgFeedAt, queryParameters: {
'id': cursor == -1 ? null : cursor, 'id': cursor == -1 ? null : cursor,
'at_time': cursorTime == -1 ? null : cursorTime, 'at_time': cursorTime == -1 ? null : cursorTime,
@@ -43,20 +44,15 @@ class MsgHttp {
'build': '8350200', 'build': '8350200',
}); });
if (res.data['code'] == 0) { if (res.data['code'] == 0) {
return { MsgFeedAtMe data = MsgFeedAtMe.fromJson(res.data['data']);
'status': true, return LoadingState.success(data);
'data': res.data['data'],
};
} else { } else {
return { return LoadingState.error(res.data['message']);
'status': false,
'date': [],
'msg': res.data['message'],
};
} }
} }
static Future msgFeedLikeMe({int cursor = -1, int cursorTime = -1}) async { static Future<LoadingState> msgFeedLikeMe(
{int cursor = -1, int cursorTime = -1}) async {
var res = await Request().get(Api.msgFeedLike, queryParameters: { var res = await Request().get(Api.msgFeedLike, queryParameters: {
'id': cursor == -1 ? null : cursor, 'id': cursor == -1 ? null : cursor,
'like_time': cursorTime == -1 ? null : cursorTime, 'like_time': cursorTime == -1 ? null : cursorTime,
@@ -65,35 +61,26 @@ class MsgHttp {
'build': '8350200', 'build': '8350200',
}); });
if (res.data['code'] == 0) { if (res.data['code'] == 0) {
return { MsgFeedLikeMe data = MsgFeedLikeMe.fromJson(res.data['data']);
'status': true, return LoadingState.success(data);
'data': res.data['data'],
};
} else { } else {
return { return LoadingState.error(res.data['message']);
'status': false,
'date': [],
'msg': res.data['message'],
};
} }
} }
static Future msgFeedNotify({int cursor = -1, int pageSize = 20}) async { static Future<LoadingState> msgFeedNotify(
{int cursor = -1, int pageSize = 20}) async {
var res = await Request().get(Api.msgSysNotify, queryParameters: { var res = await Request().get(Api.msgSysNotify, queryParameters: {
'cursor': cursor == -1 ? null : cursor, 'cursor': cursor == -1 ? null : cursor,
'page_size': pageSize, 'page_size': pageSize,
}); });
if (res.data['code'] == 0) { if (res.data['code'] == 0) {
return { List<SystemNotifyList>? list = (res.data['data'] as List?)
'status': true, ?.map((e) => SystemNotifyList.fromJson(e))
'data': res.data['data'], .toList();
}; return LoadingState.success(list);
} else { } else {
return { return LoadingState.error(res.data['message']);
'status': false,
'date': [],
'msg': res.data['message'],
};
} }
} }

View File

@@ -5,8 +5,7 @@ class MsgFeedLikeMe {
MsgFeedLikeMe({latest, total}); MsgFeedLikeMe({latest, total});
MsgFeedLikeMe.fromJson(Map<String, dynamic> json) { MsgFeedLikeMe.fromJson(Map<String, dynamic> json) {
latest = latest = json['latest'] != null ? Latest.fromJson(json['latest']) : null;
json['latest'] != null ? Latest.fromJson(json['latest']) : null;
total = json['total'] != null ? Total.fromJson(json['total']) : null; total = json['total'] != null ? Total.fromJson(json['total']) : null;
} }
@@ -50,13 +49,7 @@ class LikeMeItems {
int? likeTime; int? likeTime;
int? noticeState; int? noticeState;
LikeMeItems( LikeMeItems({id, users, item, counts, likeTime, noticeState});
{id,
users,
item,
counts,
likeTime,
noticeState});
LikeMeItems.fromJson(Map<String, dynamic> json) { LikeMeItems.fromJson(Map<String, dynamic> json) {
id = json['id']; id = json['id'];
@@ -92,13 +85,7 @@ class Users {
String? midLink; String? midLink;
bool? follow; bool? follow;
Users( Users({mid, fans, nickname, avatar, midLink, follow});
{mid,
fans,
nickname,
avatar,
midLink,
follow});
Users.fromJson(Map<String, dynamic> json) { Users.fromJson(Map<String, dynamic> json) {
mid = json['mid']; mid = json['mid'];
@@ -197,8 +184,7 @@ class Total {
Total({cursor, items}); Total({cursor, items});
Total.fromJson(Map<String, dynamic> json) { Total.fromJson(Map<String, dynamic> json) {
cursor = cursor = json['cursor'] != null ? Cursor.fromJson(json['cursor']) : null;
json['cursor'] != null ? Cursor.fromJson(json['cursor']) : null;
if (json['items'] != null) { if (json['items'] != null) {
items = <LikeMeItems>[]; items = <LikeMeItems>[];
json['items'].forEach((v) { json['items'].forEach((v) {

View File

@@ -1,43 +1,43 @@
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:PiliPlus/http/loading_state.dart';
import 'package:get/get.dart'; import 'package:PiliPlus/pages/common/common_controller.dart';
import 'package:PiliPlus/utils/extension.dart';
import 'package:PiliPlus/http/msg.dart'; import 'package:PiliPlus/http/msg.dart';
import 'package:PiliPlus/models/msg/msgfeed_at_me.dart'; import 'package:PiliPlus/models/msg/msgfeed_at_me.dart';
class AtMeController extends GetxController { class AtMeController extends CommonController {
RxList<AtMeItems> msgFeedAtMeList = <AtMeItems>[].obs;
bool isLoading = false;
int cursor = -1; int cursor = -1;
int cursorTime = -1; int cursorTime = -1;
bool isEnd = false;
Future queryMsgFeedAtMe() async { @override
if (isLoading) return; void onInit() {
isLoading = true; super.onInit();
var res = await MsgHttp.msgFeedAtMe(cursor: cursor, cursorTime: cursorTime); queryData();
isLoading = false; }
if (res['status']) {
MsgFeedAtMe data = MsgFeedAtMe.fromJson(res['data']); @override
isEnd = data.cursor?.isEnd ?? false; bool customHandleResponse(Success response) {
if (cursor == -1) { MsgFeedAtMe data = response.response;
msgFeedAtMeList.assignAll(data.items!); if (data.cursor?.isEnd == true || data.items.isNullOrEmpty) {
} else { isEnd = true;
msgFeedAtMeList.addAll(data.items!);
} }
cursor = data.cursor?.id ?? -1; cursor = data.cursor?.id ?? -1;
cursorTime = data.cursor?.time ?? -1; cursorTime = data.cursor?.time ?? -1;
} else { if (currentPage != 1 && loadingState.value is Success) {
SmartDialog.showToast(res['msg']); data.items ??= <AtMeItems>[];
data.items!.insert(0, (loadingState.value as Success).response);
} }
loadingState.value = LoadingState.success(data.items);
return true;
} }
Future onLoad() async { @override
if (isEnd) return; Future onRefresh() {
queryMsgFeedAtMe();
}
Future onRefresh() async {
cursor = -1; cursor = -1;
cursorTime = -1; cursorTime = -1;
queryMsgFeedAtMe(); return super.onRefresh();
} }
@override
Future<LoadingState> customGetData() =>
MsgHttp.msgFeedAtMe(cursor: cursor, cursorTime: cursorTime);
} }

View File

@@ -1,11 +1,11 @@
import 'package:PiliPlus/common/widgets/loading_widget.dart'; import 'package:PiliPlus/common/widgets/loading_widget.dart';
import 'package:PiliPlus/common/widgets/network_img_layer.dart';
import 'package:PiliPlus/common/widgets/refresh_indicator.dart'; import 'package:PiliPlus/common/widgets/refresh_indicator.dart';
import 'package:easy_debounce/easy_throttle.dart'; import 'package:PiliPlus/http/loading_state.dart';
import 'package:PiliPlus/utils/app_scheme.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:PiliPlus/common/widgets/network_img_layer.dart';
import '../../../utils/app_scheme.dart';
import 'controller.dart'; import 'controller.dart';
class AtMePage extends StatefulWidget { class AtMePage extends StatefulWidget {
@@ -17,31 +17,6 @@ class AtMePage extends StatefulWidget {
class _AtMePageState extends State<AtMePage> { class _AtMePageState extends State<AtMePage> {
late final AtMeController _atMeController = Get.put(AtMeController()); late final AtMeController _atMeController = Get.put(AtMeController());
final ScrollController _scrollController = ScrollController();
@override
void initState() {
_atMeController.queryMsgFeedAtMe();
super.initState();
_scrollController.addListener(_scrollListener);
}
@override
void dispose() {
_scrollController.removeListener(_scrollListener);
_scrollController.dispose();
super.dispose();
}
Future _scrollListener() async {
if (_scrollController.position.pixels >=
_scrollController.position.maxScrollExtent - 200) {
EasyThrottle.throttle('my-throttler', const Duration(milliseconds: 800),
() async {
await _atMeController.onLoad();
});
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@@ -53,53 +28,51 @@ class _AtMePageState extends State<AtMePage> {
onRefresh: () async { onRefresh: () async {
await _atMeController.onRefresh(); await _atMeController.onRefresh();
}, },
child: Obx( child: Obx(() => _buildBody(_atMeController.loadingState.value)),
() { ),
// TODO: refactor
if (_atMeController.msgFeedAtMeList.isEmpty) {
if (_atMeController.cursor == -1 &&
_atMeController.cursorTime == -1) {
return const Center(
child: CircularProgressIndicator(),
); );
} else {
return scrollErrorWidget(
callback: _atMeController.queryMsgFeedAtMe);
} }
}
return ListView.separated( Widget _buildBody(LoadingState loadingState) {
controller: _scrollController, return switch (loadingState) {
itemCount: _atMeController.msgFeedAtMeList.length, Loading() => loadingWidget,
Success() => (loadingState.response as List?)?.isNotEmpty == true
? ListView.separated(
itemCount: loadingState.response.length,
physics: const AlwaysScrollableScrollPhysics(), physics: const AlwaysScrollableScrollPhysics(),
itemBuilder: (context, int i) { padding: EdgeInsets.only(
bottom: MediaQuery.paddingOf(context).bottom + 80),
itemBuilder: (context, int index) {
if (index == loadingState.response.length - 1) {
_atMeController.onLoadMore();
}
return ListTile( return ListTile(
onTap: () { onTap: () {
String? nativeUri = String? nativeUri =
_atMeController.msgFeedAtMeList[i].item?.nativeUri; loadingState.response[index].item?.nativeUri;
if (nativeUri != null) { if (nativeUri != null) {
PiliScheme.routePushFromUrl(nativeUri); PiliScheme.routePushFromUrl(nativeUri);
} }
// SmartDialog.showToast("跳转至:$nativeUri暂未实现");
}, },
leading: NetworkImgLayer( leading: NetworkImgLayer(
width: 45, width: 45,
height: 45, height: 45,
type: 'avatar', type: 'avatar',
src: _atMeController.msgFeedAtMeList[i].user?.avatar, src: loadingState.response[index].user?.avatar,
), ),
title: Text( title: Text(
"${_atMeController.msgFeedAtMeList[i].user?.nickname} " "${loadingState.response[index].user?.nickname} "
"${_atMeController.msgFeedAtMeList[i].item?.business}中@了我", "${loadingState.response[index].item?.business}中@了我",
style: Theme.of(context).textTheme.titleMedium!.copyWith( style: Theme.of(context).textTheme.titleMedium!.copyWith(
color: Theme.of(context).colorScheme.primary, color: Theme.of(context).colorScheme.primary,
)), ),
),
subtitle: Column( subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
const SizedBox(height: 4), const SizedBox(height: 4),
Text( Text(
_atMeController loadingState.response[index].item?.sourceContent ??
.msgFeedAtMeList[i].item?.sourceContent ??
"", "",
maxLines: 3, maxLines: 3,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
@@ -110,14 +83,13 @@ class _AtMePageState extends State<AtMePage> {
color: Theme.of(context).colorScheme.outline)) color: Theme.of(context).colorScheme.outline))
], ],
), ),
trailing: _atMeController.msgFeedAtMeList[i].item?.image != trailing: loadingState.response[index].item?.image != null &&
null && loadingState.response[index].item?.image != ""
_atMeController.msgFeedAtMeList[i].item?.image != ""
? NetworkImgLayer( ? NetworkImgLayer(
width: 45, width: 45,
height: 45, height: 45,
type: 'cover', type: 'cover',
src: _atMeController.msgFeedAtMeList[i].item?.image, src: loadingState.response[index].item?.image,
) )
: null, : null,
); );
@@ -130,10 +102,13 @@ class _AtMePageState extends State<AtMePage> {
color: Colors.grey.withOpacity(0.1), color: Colors.grey.withOpacity(0.1),
); );
}, },
); )
}, : scrollErrorWidget(callback: _atMeController.onReload),
Error() => scrollErrorWidget(
errMsg: loadingState.errMsg,
callback: _atMeController.onReload,
), ),
), LoadingState() => throw UnimplementedError(),
); };
} }
} }

View File

@@ -1,47 +1,57 @@
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:PiliPlus/common/widgets/pair.dart';
import 'package:get/get.dart'; import 'package:PiliPlus/http/loading_state.dart';
import 'package:PiliPlus/pages/common/common_controller.dart';
import 'package:PiliPlus/http/msg.dart'; import 'package:PiliPlus/http/msg.dart';
import 'package:PiliPlus/utils/extension.dart';
import '../../../models/msg/msgfeed_like_me.dart'; import '../../../models/msg/msgfeed_like_me.dart';
class LikeMeController extends GetxController { class LikeMeController extends CommonController {
RxList<LikeMeItems> msgFeedLikeMeLatestList = <LikeMeItems>[].obs;
RxList<LikeMeItems> msgFeedLikeMeTotalList = <LikeMeItems>[].obs;
bool isLoading = false;
int cursor = -1; int cursor = -1;
int cursorTime = -1; int cursorTime = -1;
bool isEnd = false;
Future queryMsgFeedLikeMe() async { @override
if (isLoading) return; void onInit() {
isLoading = true; super.onInit();
var res = queryData();
await MsgHttp.msgFeedLikeMe(cursor: cursor, cursorTime: cursorTime); }
isLoading = false;
if (res['status']) { @override
MsgFeedLikeMe data = MsgFeedLikeMe.fromJson(res['data']); bool customHandleResponse(Success response) {
isEnd = data.total?.cursor?.isEnd ?? false; MsgFeedLikeMe data = response.response;
if (cursor == -1) { if (data.total?.cursor?.isEnd == true ||
msgFeedLikeMeLatestList.assignAll(data.latest?.items ?? []); data.total?.items.isNullOrEmpty == true) {
msgFeedLikeMeTotalList.assignAll(data.total?.items ?? []); isEnd = true;
} else {
msgFeedLikeMeLatestList.addAll(data.latest?.items ?? []);
msgFeedLikeMeTotalList.addAll(data.total?.items ?? []);
} }
cursor = data.total?.cursor?.id ?? -1; cursor = data.total?.cursor?.id ?? -1;
cursorTime = data.total?.cursor?.time ?? -1; cursorTime = data.total?.cursor?.time ?? -1;
} else { List<LikeMeItems> latest = <LikeMeItems>[];
SmartDialog.showToast(res['msg']); List<LikeMeItems> total = <LikeMeItems>[];
if (data.latest?.items?.isNotEmpty == true) {
latest.addAll(data.latest!.items!);
} }
if (data.total?.items?.isNotEmpty == true) {
total.addAll(data.total!.items!);
}
if (currentPage != 1 && loadingState.value is Success) {
Pair<List<LikeMeItems>, List<LikeMeItems>> pair =
(loadingState.value as Success).response;
latest.insertAll(0, pair.first);
total.insertAll(0, pair.second);
}
loadingState.value = LoadingState.success(
Pair(first: latest, second: total),
);
return true;
} }
Future onLoad() async { @override
if (isEnd) return; Future onRefresh() {
queryMsgFeedLikeMe();
}
Future onRefresh() async {
cursor = -1; cursor = -1;
cursorTime = -1; cursorTime = -1;
queryMsgFeedLikeMe(); return super.onRefresh();
} }
@override
Future<LoadingState> customGetData() =>
MsgHttp.msgFeedLikeMe(cursor: cursor, cursorTime: cursorTime);
} }

View File

@@ -1,10 +1,12 @@
import 'package:PiliPlus/common/widgets/loading_widget.dart';
import 'package:PiliPlus/common/widgets/pair.dart';
import 'package:PiliPlus/common/widgets/refresh_indicator.dart'; import 'package:PiliPlus/common/widgets/refresh_indicator.dart';
import 'package:easy_debounce/easy_throttle.dart'; import 'package:PiliPlus/http/loading_state.dart';
import 'package:PiliPlus/models/msg/msgfeed_like_me.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:PiliPlus/common/widgets/network_img_layer.dart'; import 'package:PiliPlus/common/widgets/network_img_layer.dart';
import '../../../models/msg/msgfeed_like_me.dart';
import '../../../utils/app_scheme.dart'; import '../../../utils/app_scheme.dart';
import 'controller.dart'; import 'controller.dart';
@@ -17,31 +19,6 @@ class LikeMePage extends StatefulWidget {
class _LikeMePageState extends State<LikeMePage> { class _LikeMePageState extends State<LikeMePage> {
late final LikeMeController _likeMeController = Get.put(LikeMeController()); late final LikeMeController _likeMeController = Get.put(LikeMeController());
final ScrollController _scrollController = ScrollController();
@override
void initState() {
_likeMeController.queryMsgFeedLikeMe();
super.initState();
_scrollController.addListener(_scrollListener);
}
@override
void dispose() {
_scrollController.removeListener(_scrollListener);
_scrollController.dispose();
super.dispose();
}
Future _scrollListener() async {
if (_scrollController.position.pixels >=
_scrollController.position.maxScrollExtent - 200) {
EasyThrottle.throttle('my-throttler', const Duration(milliseconds: 800),
() async {
await _likeMeController.onLoad();
});
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@@ -53,79 +30,85 @@ class _LikeMePageState extends State<LikeMePage> {
onRefresh: () async { onRefresh: () async {
await _likeMeController.onRefresh(); await _likeMeController.onRefresh();
}, },
// TODO: refactor child: Obx(() => _buildBody(_likeMeController.loadingState.value)),
child: SingleChildScrollView( ),
controller: _scrollController,
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Obx(
() {
if (_likeMeController.msgFeedLikeMeLatestList.isEmpty &&
_likeMeController.msgFeedLikeMeTotalList.isEmpty) {
return const Center(
child: CircularProgressIndicator(),
); );
} }
return Column(
crossAxisAlignment: CrossAxisAlignment.start, Widget _buildBody(LoadingState loadingState) {
children: [ return switch (loadingState) {
if (_likeMeController Loading() => loadingWidget,
.msgFeedLikeMeLatestList.isNotEmpty) ...<Widget>[ Success() => () {
Text(" 最新", Pair<List<LikeMeItems>, List<LikeMeItems>> pair =
loadingState.response;
int length = pair.first.length + pair.second.length;
if (pair.first.isNotEmpty) {
length++;
}
if (pair.second.isNotEmpty) {
length++;
}
LikeMeItems getCurrentItem(int index) {
if (pair.first.isEmpty) {
return pair.second[index - 1];
} else {
return index <= pair.first.length
? pair.first[index - 1]
: pair.second[index - pair.first.length - 2];
}
}
return length > 0
? ListView.separated(
itemCount: length,
padding: EdgeInsets.only(
bottom: MediaQuery.paddingOf(context).bottom + 80),
physics: const AlwaysScrollableScrollPhysics(),
itemBuilder: (context, int index) {
if (index == length - 1) {
_likeMeController.onLoadMore();
}
// title
if (index == 0) {
return Padding(
padding: const EdgeInsets.only(left: 16),
child: Text(
pair.first.isNotEmpty ? '最新' : '累计',
style: Theme.of(context) style: Theme.of(context)
.textTheme .textTheme
.labelMedium! .labelLarge!
.copyWith( .copyWith(
color: color: Theme.of(context).colorScheme.secondary,
Theme.of(context).colorScheme.outline)), ),
LikeMeList( ),
msgFeedLikeMeList:
_likeMeController.msgFeedLikeMeLatestList),
],
if (_likeMeController
.msgFeedLikeMeTotalList.isNotEmpty) ...<Widget>[
Text(" 累计",
style: Theme.of(context)
.textTheme
.labelMedium!
.copyWith(
color:
Theme.of(context).colorScheme.outline)),
LikeMeList(
msgFeedLikeMeList:
_likeMeController.msgFeedLikeMeTotalList),
]
]);
},
); );
}), } else if (pair.first.isNotEmpty &&
index == pair.first.length + 1) {
return Padding(
padding: const EdgeInsets.only(left: 16),
child: Text(
"累计",
style: Theme.of(context)
.textTheme
.labelLarge!
.copyWith(
color: Theme.of(context).colorScheme.secondary,
), ),
), ),
); );
} }
}
class LikeMeList extends StatelessWidget { // item
const LikeMeList({ final item = getCurrentItem(index);
super.key,
required this.msgFeedLikeMeList,
});
final RxList<LikeMeItems> msgFeedLikeMeList;
@override
Widget build(BuildContext context) {
return ListView.separated(
itemCount: msgFeedLikeMeList.length,
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemBuilder: (context, int i) {
return ListTile( return ListTile(
onTap: () { onTap: () {
String? nativeUri = msgFeedLikeMeList[i].item?.nativeUri; String? nativeUri = item.item?.nativeUri;
if (nativeUri != null) { if (nativeUri != null) {
PiliScheme.routePushFromUrl(nativeUri); PiliScheme.routePushFromUrl(nativeUri);
} }
// SmartDialog.showToast("跳转至:$nativeUri暂未实现");
}, },
leading: Column( leading: Column(
children: [ children: [
@@ -136,20 +119,18 @@ class LikeMeList extends StatelessWidget {
child: Stack( child: Stack(
children: [ children: [
for (var j = 0; for (var j = 0;
j < msgFeedLikeMeList[i].users!.length && j < 4; j < item.users!.length && j < 4;
j++) ...<Widget>[ j++) ...<Widget>[
Positioned( Positioned(
left: 15 * (j % 2).toDouble(), left: 15 * (j % 2).toDouble(),
top: 15 * (j ~/ 2).toDouble(), top: 15 * (j ~/ 2).toDouble(),
child: NetworkImgLayer( child: NetworkImgLayer(
width: msgFeedLikeMeList[i].users!.length > 1 width:
? 30 item.users!.length > 1 ? 30 : 45,
: 45, height:
height: msgFeedLikeMeList[i].users!.length > 1 item.users!.length > 1 ? 30 : 45,
? 30
: 45,
type: 'avatar', type: 'avatar',
src: msgFeedLikeMeList[i].users![j].avatar, src: item.users![j].avatar,
)), )),
] ]
], ],
@@ -159,37 +140,43 @@ class LikeMeList extends StatelessWidget {
), ),
title: Text( title: Text(
// "${msgFeedLikeMeList[i].users!.map((e) => e.nickname).join("/")}" // "${msgFeedLikeMeList[i].users!.map((e) => e.nickname).join("/")}"
"${msgFeedLikeMeList[i].users?[0].nickname}" "${item.users?[0].nickname}"
"${msgFeedLikeMeList[i].users!.length > 1 ? '' + msgFeedLikeMeList[i].users![1].nickname.toString() + '' : ''} " "${item.users!.length > 1 ? '${item.users![1].nickname} 等' : ''} "
"${msgFeedLikeMeList[i].counts! > 1 ? '' + msgFeedLikeMeList[i].counts.toString() + '' : ''}" "${item.counts! > 1 ? '${item.counts} 人' : ''}"
"赞了我的${msgFeedLikeMeList[i].item?.business}", "赞了我的${item.item?.business}",
style: Theme.of(context).textTheme.titleSmall!.copyWith( style: Theme.of(context).textTheme.titleSmall!.copyWith(
height: 1.5, color: Theme.of(context).colorScheme.primary), height: 1.5,
color: Theme.of(context).colorScheme.primary),
maxLines: 2, maxLines: 2,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
), ),
subtitle: msgFeedLikeMeList[i].item?.title != null && subtitle:
msgFeedLikeMeList[i].item?.title != "" item.item?.title != null && item.item?.title != ""
? Column( ? Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
const SizedBox(height: 4), const SizedBox(height: 4),
Text(msgFeedLikeMeList[i].item?.title ?? "", Text(item.item?.title ?? "",
maxLines: 3, maxLines: 3,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
style: Theme.of(context).textTheme.bodyMedium!.copyWith( style: Theme.of(context)
color: Theme.of(context).colorScheme.outline, .textTheme
.bodyMedium!
.copyWith(
color: Theme.of(context)
.colorScheme
.outline,
height: 1.5)) height: 1.5))
], ],
) )
: null, : null,
trailing: msgFeedLikeMeList[i].item?.image != null && trailing:
msgFeedLikeMeList[i].item?.image != "" item.item?.image != null && item.item?.image != ""
? NetworkImgLayer( ? NetworkImgLayer(
width: 45, width: 45,
height: 45, height: 45,
type: 'cover', type: 'cover',
src: msgFeedLikeMeList[i].item?.image, src: item.item?.image,
) )
: null, : null,
); );
@@ -202,6 +189,14 @@ class LikeMeList extends StatelessWidget {
color: Colors.grey.withOpacity(0.1), color: Colors.grey.withOpacity(0.1),
); );
}, },
); )
: scrollErrorWidget(callback: _likeMeController.onReload);
}(),
Error() => scrollErrorWidget(
errMsg: loadingState.errMsg,
callback: _likeMeController.onReload,
),
LoadingState() => throw UnimplementedError(),
};
} }
} }

View File

@@ -1,45 +1,44 @@
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:PiliPlus/http/loading_state.dart';
import 'package:get/get.dart'; import 'package:PiliPlus/pages/common/common_controller.dart';
import 'package:PiliPlus/utils/extension.dart';
import 'package:PiliPlus/http/msg.dart'; import 'package:PiliPlus/http/msg.dart';
import '../../../models/msg/msgfeed_reply_me.dart'; import '../../../models/msg/msgfeed_reply_me.dart';
class ReplyMeController extends GetxController { class ReplyMeController extends CommonController {
RxList<ReplyMeItems> msgFeedReplyMeList = <ReplyMeItems>[].obs;
bool isLoading = false;
int cursor = -1; int cursor = -1;
int cursorTime = -1; int cursorTime = -1;
bool isEnd = false;
Future queryMsgFeedReplyMe() async { @override
if (isLoading) return; void onInit() {
isLoading = true; super.onInit();
var res = queryData();
await MsgHttp.msgFeedReplyMe(cursor: cursor, cursorTime: cursorTime); }
isLoading = false;
if (res['status']) { @override
MsgFeedReplyMe data = MsgFeedReplyMe.fromJson(res['data']); bool customHandleResponse(Success response) {
isEnd = data.cursor?.isEnd ?? false; MsgFeedReplyMe data = response.response;
if (cursor == -1) { if (data.cursor?.isEnd == true || data.items.isNullOrEmpty) {
msgFeedReplyMeList.assignAll(data.items!); isEnd = true;
} else {
msgFeedReplyMeList.addAll(data.items!);
} }
cursor = data.cursor?.id ?? -1; cursor = data.cursor?.id ?? -1;
cursorTime = data.cursor?.time ?? -1; cursorTime = data.cursor?.time ?? -1;
} else { if (currentPage != 1 && loadingState.value is Success) {
SmartDialog.showToast(res['msg']); data.items ??= <ReplyMeItems>[];
data.items!.insert(0, (loadingState.value as Success).response);
} }
loadingState.value = LoadingState.success(data.items);
return true;
} }
Future onLoad() async { @override
if (isEnd) return; Future onRefresh() {
queryMsgFeedReplyMe();
}
Future onRefresh() async {
cursor = -1; cursor = -1;
cursorTime = -1; cursorTime = -1;
queryMsgFeedReplyMe(); return super.onRefresh();
} }
@override
Future<LoadingState> customGetData() =>
MsgHttp.msgFeedReplyMe(cursor: cursor, cursorTime: cursorTime);
} }

View File

@@ -1,5 +1,6 @@
import 'package:PiliPlus/common/widgets/loading_widget.dart';
import 'package:PiliPlus/common/widgets/refresh_indicator.dart'; import 'package:PiliPlus/common/widgets/refresh_indicator.dart';
import 'package:easy_debounce/easy_throttle.dart'; import 'package:PiliPlus/http/loading_state.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:PiliPlus/common/widgets/network_img_layer.dart'; import 'package:PiliPlus/common/widgets/network_img_layer.dart';
@@ -15,33 +16,7 @@ class ReplyMePage extends StatefulWidget {
} }
class _ReplyMePageState extends State<ReplyMePage> { class _ReplyMePageState extends State<ReplyMePage> {
late final ReplyMeController _replyMeController = late final _replyMeController = Get.put(ReplyMeController());
Get.put(ReplyMeController());
final ScrollController _scrollController = ScrollController();
@override
void initState() {
_replyMeController.queryMsgFeedReplyMe();
super.initState();
_scrollController.addListener(_scrollListener);
}
@override
void dispose() {
_scrollController.removeListener(_scrollListener);
_scrollController.dispose();
super.dispose();
}
Future _scrollListener() async {
if (_scrollController.position.pixels >=
_scrollController.position.maxScrollExtent - 200) {
EasyThrottle.throttle('my-throttler', const Duration(milliseconds: 800),
() async {
await _replyMeController.onLoad();
});
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@@ -51,37 +26,42 @@ class _ReplyMePageState extends State<ReplyMePage> {
onRefresh: () async { onRefresh: () async {
await _replyMeController.onRefresh(); await _replyMeController.onRefresh();
}, },
// TODO: refactor child: Obx(() => _buildBody(_replyMeController.loadingState.value)),
child: Obx( ),
() {
if (_replyMeController.msgFeedReplyMeList.isEmpty) {
return const Center(
child: CircularProgressIndicator(),
); );
} }
return ListView.separated(
controller: _scrollController, Widget _buildBody(LoadingState loadingState) {
itemCount: _replyMeController.msgFeedReplyMeList.length, return switch (loadingState) {
Loading() => loadingWidget,
Success() => (loadingState.response as List?)?.isNotEmpty == true
? ListView.separated(
itemCount: loadingState.response.length,
physics: const AlwaysScrollableScrollPhysics(), physics: const AlwaysScrollableScrollPhysics(),
itemBuilder: (context, int i) { padding: EdgeInsets.only(
bottom: MediaQuery.paddingOf(context).bottom + 80),
itemBuilder: (context, int index) {
if (index == loadingState.response.length - 1) {
_replyMeController.onLoadMore();
}
return ListTile( return ListTile(
onTap: () { onTap: () {
String? nativeUri = _replyMeController String? nativeUri =
.msgFeedReplyMeList[i].item?.nativeUri; loadingState.response[index].item?.nativeUri;
if (nativeUri != null) { if (nativeUri != null) {
PiliScheme.routePushFromUrl(nativeUri); PiliScheme.routePushFromUrl(nativeUri);
} }
// SmartDialog.showToast("跳转至:$nativeUri暂未实现");
}, },
leading: NetworkImgLayer( leading: NetworkImgLayer(
width: 45, width: 45,
height: 45, height: 45,
type: 'avatar', type: 'avatar',
src: _replyMeController.msgFeedReplyMeList[i].user?.avatar, src: loadingState.response[index].user?.avatar,
), ),
title: Text( title: Text(
"${_replyMeController.msgFeedReplyMeList[i].user?.nickname} " "${loadingState.response[index].user?.nickname} "
"回复了我的${_replyMeController.msgFeedReplyMeList[i].item?.business}", "回复了我的${loadingState.response[index].item?.business}",
style: Theme.of(context) style: Theme.of(context)
.textTheme .textTheme
.bodyMedium! .bodyMedium!
@@ -93,19 +73,18 @@ class _ReplyMePageState extends State<ReplyMePage> {
children: [ children: [
const SizedBox(height: 4), const SizedBox(height: 4),
Text( Text(
_replyMeController.msgFeedReplyMeList[i].item loadingState.response[index].item?.sourceContent ??
?.sourceContent ??
"", "",
style: Theme.of(context).textTheme.bodyMedium), style: Theme.of(context).textTheme.bodyMedium),
const SizedBox(height: 4), const SizedBox(height: 4),
if (_replyMeController.msgFeedReplyMeList[i].item if (loadingState
?.targetReplyContent != .response[index].item?.targetReplyContent !=
null && null &&
_replyMeController.msgFeedReplyMeList[i].item loadingState
?.targetReplyContent != .response[index].item?.targetReplyContent !=
"") "")
Text( Text(
"| ${_replyMeController.msgFeedReplyMeList[i].item?.targetReplyContent}", "| ${loadingState.response[index].item?.targetReplyContent}",
maxLines: 1, maxLines: 1,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
style: Theme.of(context) style: Theme.of(context)
@@ -115,22 +94,24 @@ class _ReplyMePageState extends State<ReplyMePage> {
color: color:
Theme.of(context).colorScheme.outline, Theme.of(context).colorScheme.outline,
height: 1.5)), height: 1.5)),
if (_replyMeController.msgFeedReplyMeList[i].item if (loadingState
?.rootReplyContent != .response[index].item?.rootReplyContent !=
null && null &&
_replyMeController.msgFeedReplyMeList[i].item loadingState
?.rootReplyContent != .response[index].item?.rootReplyContent !=
"") "")
Text( Text(
" | ${_replyMeController.msgFeedReplyMeList[i].item?.rootReplyContent}", " | ${loadingState.response[index].item?.rootReplyContent}",
maxLines: 1, maxLines: 1,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
style: Theme.of(context) style:
Theme.of(context)
.textTheme .textTheme
.labelMedium! .labelMedium!
.copyWith( .copyWith(
color: color: Theme.of(context)
Theme.of(context).colorScheme.outline, .colorScheme
.outline,
height: 1.5)), height: 1.5)),
]), ]),
); );
@@ -143,10 +124,13 @@ class _ReplyMePageState extends State<ReplyMePage> {
color: Colors.grey.withOpacity(0.1), color: Colors.grey.withOpacity(0.1),
); );
}, },
); )
}, : scrollErrorWidget(callback: _replyMeController.onReload),
Error() => scrollErrorWidget(
errMsg: loadingState.errMsg,
callback: _replyMeController.onReload,
), ),
), LoadingState() => throw UnimplementedError(),
); };
} }
} }

View File

@@ -1,38 +1,27 @@
import 'package:PiliPlus/http/loading_state.dart';
import 'package:PiliPlus/pages/common/common_controller.dart';
import 'package:PiliPlus/utils/extension.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:PiliPlus/http/msg.dart'; import 'package:PiliPlus/http/msg.dart';
import '../../../models/msg/msgfeed_sys_msg.dart'; class SysMsgController extends CommonController {
final pageSize = 20;
class SysMsgController extends GetxController {
static const pageSize = 20;
RxList<SystemNotifyList> msgFeedSysMsgList = <SystemNotifyList>[].obs;
bool isLoading = false;
int cursor = -1; int cursor = -1;
bool isEnd = false;
Future queryMsgFeedSysMsg() async { @override
if (isLoading) return; void onInit() {
isLoading = true; super.onInit();
final res = await MsgHttp.msgFeedNotify(cursor: cursor, pageSize: pageSize); queryData();
isLoading = false;
if (res['status']) {
final data = (res['data'] as List)
.map((i) => SystemNotifyList.fromJson(i))
.toList();
isEnd = data.length + 1 < pageSize; // data.length会比pageSize小1
if (data.isNotEmpty) {
if (cursor == -1) {
msgFeedSysMsgList.assignAll(data);
} else {
msgFeedSysMsgList.addAll(data);
} }
cursor = data.last.cursor ?? -1;
msgSysUpdateCursor(msgFeedSysMsgList.first.cursor!); @override
} List? handleListResponse(List currentList, List dataList) {
} else { cursor = dataList.last.cursor ?? -1;
SmartDialog.showToast(res['msg']); msgSysUpdateCursor(dataList.first.cursor!);
if (isEnd.not && dataList.length + 1 < pageSize) {
isEnd = true;
} }
return null;
} }
Future msgSysUpdateCursor(int cursor) async { Future msgSysUpdateCursor(int cursor) async {
@@ -46,23 +35,27 @@ class SysMsgController extends GetxController {
} }
} }
Future onLoad() async { @override
if (isEnd) return; Future onRefresh() {
queryMsgFeedSysMsg();
}
Future onRefresh() async {
cursor = -1; cursor = -1;
queryMsgFeedSysMsg(); return super.onRefresh();
} }
Future onRemove(int index) async { Future onRemove(dynamic id, int index) async {
var res = await MsgHttp.removeSysMsg(msgFeedSysMsgList[index].id); try {
var res = await MsgHttp.removeSysMsg(id);
if (res['status']) { if (res['status']) {
msgFeedSysMsgList.removeAt(index); List list = (loadingState.value as Success).response;
list.removeAt(index);
loadingState.value = LoadingState.success(list);
SmartDialog.showToast('删除成功'); SmartDialog.showToast('删除成功');
} else { } else {
SmartDialog.showToast(res['msg']); SmartDialog.showToast(res['msg']);
} }
} catch (_) {}
} }
@override
Future<LoadingState> customGetData() =>
MsgHttp.msgFeedNotify(cursor: cursor, pageSize: pageSize);
} }

View File

@@ -1,10 +1,11 @@
import 'dart:convert'; import 'dart:convert';
import 'package:PiliPlus/common/widgets/loading_widget.dart';
import 'package:PiliPlus/common/widgets/refresh_indicator.dart'; import 'package:PiliPlus/common/widgets/refresh_indicator.dart';
import 'package:PiliPlus/http/loading_state.dart';
import 'package:PiliPlus/utils/app_scheme.dart'; import 'package:PiliPlus/utils/app_scheme.dart';
import 'package:PiliPlus/utils/id_utils.dart'; import 'package:PiliPlus/utils/id_utils.dart';
import 'package:PiliPlus/utils/utils.dart'; import 'package:PiliPlus/utils/utils.dart';
import 'package:easy_debounce/easy_throttle.dart';
import 'package:flutter/gestures.dart'; import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
@@ -20,32 +21,7 @@ class SysMsgPage extends StatefulWidget {
} }
class _SysMsgPageState extends State<SysMsgPage> { class _SysMsgPageState extends State<SysMsgPage> {
late final SysMsgController _sysMsgController = Get.put(SysMsgController()); late final _sysMsgController = Get.put(SysMsgController());
final ScrollController _scrollController = ScrollController();
@override
void initState() {
_sysMsgController.queryMsgFeedSysMsg();
super.initState();
_scrollController.addListener(_scrollListener);
}
@override
void dispose() {
_scrollController.removeListener(_scrollListener);
_scrollController.dispose();
super.dispose();
}
Future _scrollListener() async {
if (_scrollController.position.pixels >=
_scrollController.position.maxScrollExtent - 200) {
EasyThrottle.throttle('my-throttler', const Duration(milliseconds: 800),
() async {
await _sysMsgController.onLoad();
});
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@@ -57,21 +33,26 @@ class _SysMsgPageState extends State<SysMsgPage> {
onRefresh: () async { onRefresh: () async {
await _sysMsgController.onRefresh(); await _sysMsgController.onRefresh();
}, },
// TODO: refactor child: Obx(() => _buildBody(_sysMsgController.loadingState.value)),
child: Obx( ),
() {
if (_sysMsgController.msgFeedSysMsgList.isEmpty) {
return const Center(
child: CircularProgressIndicator(),
); );
} }
return ListView.separated(
controller: _scrollController, Widget _buildBody(LoadingState loadingState) {
itemCount: _sysMsgController.msgFeedSysMsgList.length, return switch (loadingState) {
Loading() => loadingWidget,
Success() => (loadingState.response as List?)?.isNotEmpty == true
? ListView.separated(
itemCount: loadingState.response.length,
physics: const AlwaysScrollableScrollPhysics(), physics: const AlwaysScrollableScrollPhysics(),
itemBuilder: (context, int i) { padding: EdgeInsets.only(
String? content = bottom: MediaQuery.paddingOf(context).bottom + 80),
_sysMsgController.msgFeedSysMsgList[i].content; itemBuilder: (context, int index) {
if (index == loadingState.response.length - 1) {
_sysMsgController.onLoadMore();
}
String? content = loadingState.response[index].content;
if (content != null) { if (content != null) {
try { try {
dynamic jsonContent = json.decode(content); dynamic jsonContent = json.decode(content);
@@ -101,7 +82,10 @@ class _SysMsgPageState extends State<SysMsgPage> {
TextButton( TextButton(
onPressed: () { onPressed: () {
Get.back(); Get.back();
_sysMsgController.onRemove(i); _sysMsgController.onRemove(
loadingState.response[index].id,
index,
);
}, },
child: const Text('确定'), child: const Text('确定'),
), ),
@@ -109,7 +93,7 @@ class _SysMsgPageState extends State<SysMsgPage> {
)); ));
}, },
title: Text( title: Text(
"${_sysMsgController.msgFeedSysMsgList[i].title}", "${loadingState.response[index].title}",
style: Theme.of(context).textTheme.titleMedium, style: Theme.of(context).textTheme.titleMedium,
), ),
subtitle: Column( subtitle: Column(
@@ -130,7 +114,7 @@ class _SysMsgPageState extends State<SysMsgPage> {
SizedBox( SizedBox(
width: double.infinity, width: double.infinity,
child: Text( child: Text(
"${_sysMsgController.msgFeedSysMsgList[i].timeAt}", "${loadingState.response[index].timeAt}",
maxLines: 1, maxLines: 1,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
style: Theme.of(context) style: Theme.of(context)
@@ -154,11 +138,14 @@ class _SysMsgPageState extends State<SysMsgPage> {
color: Colors.grey.withOpacity(0.1), color: Colors.grey.withOpacity(0.1),
); );
}, },
); )
}, : scrollErrorWidget(callback: _sysMsgController.onReload),
Error() => scrollErrorWidget(
errMsg: loadingState.errMsg,
callback: _sysMsgController.onReload,
), ),
), LoadingState() => throw UnimplementedError(),
); };
} }
InlineSpan _buildContent(String content) { InlineSpan _buildContent(String content) {

View File

@@ -86,6 +86,7 @@ class _NoteListPageState extends State<NoteListPage> {
}, },
child: CustomScrollView( child: CustomScrollView(
controller: ScrollController(), controller: ScrollController(),
physics: const AlwaysScrollableScrollPhysics(),
slivers: [ slivers: [
SliverList.separated( SliverList.separated(
itemBuilder: (context, index) { itemBuilder: (context, index) {