Implement thread view update feature

This commit is contained in:
ChronosX88 2022-04-19 02:03:38 +03:00
parent 987c48d530
commit a0e45d81ff
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
2 changed files with 43 additions and 17 deletions

View File

@ -53,4 +53,8 @@ class ThreadModel extends ChangeNotifier {
msg.addHeader("Newsgroups", client!.currentGroup!); msg.addHeader("Newsgroups", client!.currentGroup!);
return await client!.postArticle(msg); return await client!.postArticle(msg);
} }
void update() {
notifyListeners();
}
} }

View File

@ -22,36 +22,55 @@ class ThreadScreen extends StatefulWidget {
class ThreadScreenState extends State<ThreadScreen> { class ThreadScreenState extends State<ThreadScreen> {
ThreadScreenState(this.threadNumber); ThreadScreenState(this.threadNumber);
late ThreadModel model;
late int threadNumber; late int threadNumber;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
model = Provider.of<ThreadModel>(context, listen: false);
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: Text("Thread #${this.threadNumber}"), title: Text("Тред #${this.threadNumber}"),
actions: [
TextButton.icon(
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Обновление треда...')),
);
model.update();
},
label: const Text("Обновить"),
style: TextButton.styleFrom(
primary: Theme.of(context).colorScheme.onPrimary),
icon: Icon(Icons.sync))
],
), ),
body: Center( body: Center(
child: Container( child: Container(
width: 650, width: 650,
child: FutureBuilder<List<MessageItem>>( child: Consumer<ThreadModel>(
future: _fetch(context), builder: ((context, value, child) =>
builder: (context, snapshot) { FutureBuilder<List<MessageItem>>(
if (snapshot.hasData) { future: _fetch(context),
List<MessageItem> data = List.from(snapshot.data!); builder: (context, snapshot) {
data.insert(1, MessageItem("reply", 0, "", "", "", "")); // reply if (snapshot.hasData) {
return _listView(data); List<MessageItem> data = List.from(snapshot.data!);
} else if (snapshot.hasError) { data.insert(
return Text("${snapshot.error}"); 1, MessageItem("reply", 0, "", "", "", "")); // reply
} return _listView(data);
return Center(child: CircularProgressIndicator()); } else if (snapshot.hasError) {
}, return Text("${snapshot.error}");
), }
return Center(child: CircularProgressIndicator());
},
))),
)), )),
); );
} }
Future<List<MessageItem>> _fetch(BuildContext context) async { Future<List<MessageItem>> _fetch(BuildContext context) async {
var model = context.read<ThreadModel>();
List<MessageItem> posts = []; List<MessageItem> posts = [];
var threadPosts = await model.getThread(threadNumber); var threadPosts = await model.getThread(threadNumber);
@ -145,13 +164,16 @@ class SendMessageFormState extends State<SendMessageForm> {
listen: false) listen: false)
.postMessage(opPost, .postMessage(opPost,
value.commentTextController.text) value.commentTextController.text)
.then((value) { .then((responseCode) {
if (value == 240) { if (responseCode == 240) {
ScaffoldMessenger.of(context) ScaffoldMessenger.of(context)
.showSnackBar( .showSnackBar(
const SnackBar( const SnackBar(
content: Text('Пост отправлен!')), content: Text('Пост отправлен!')),
); );
Provider.of<ThreadModel>(context,
listen: false)
.update();
} }
}); });
value.commentTextController.text = ""; value.commentTextController.text = "";