From a0e45d81ffe97c272a78329b040a5aa70d49c6a3 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Tue, 19 Apr 2022 02:03:38 +0300 Subject: [PATCH] Implement thread view update feature --- lib/thread_model.dart | 4 +++ lib/thread_screen.dart | 56 +++++++++++++++++++++++++++++------------- 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/lib/thread_model.dart b/lib/thread_model.dart index 95c8e28..5c5d3f3 100644 --- a/lib/thread_model.dart +++ b/lib/thread_model.dart @@ -53,4 +53,8 @@ class ThreadModel extends ChangeNotifier { msg.addHeader("Newsgroups", client!.currentGroup!); return await client!.postArticle(msg); } + + void update() { + notifyListeners(); + } } diff --git a/lib/thread_screen.dart b/lib/thread_screen.dart index 3181723..0c58804 100644 --- a/lib/thread_screen.dart +++ b/lib/thread_screen.dart @@ -22,36 +22,55 @@ class ThreadScreen extends StatefulWidget { class ThreadScreenState extends State { ThreadScreenState(this.threadNumber); + + late ThreadModel model; late int threadNumber; @override Widget build(BuildContext context) { + model = Provider.of(context, listen: false); + return Scaffold( 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( child: Container( width: 650, - child: FutureBuilder>( - future: _fetch(context), - builder: (context, snapshot) { - if (snapshot.hasData) { - List data = List.from(snapshot.data!); - data.insert(1, MessageItem("reply", 0, "", "", "", "")); // reply - return _listView(data); - } else if (snapshot.hasError) { - return Text("${snapshot.error}"); - } - return Center(child: CircularProgressIndicator()); - }, - ), + child: Consumer( + builder: ((context, value, child) => + FutureBuilder>( + future: _fetch(context), + builder: (context, snapshot) { + if (snapshot.hasData) { + List data = List.from(snapshot.data!); + data.insert( + 1, MessageItem("reply", 0, "", "", "", "")); // reply + return _listView(data); + } else if (snapshot.hasError) { + return Text("${snapshot.error}"); + } + return Center(child: CircularProgressIndicator()); + }, + ))), )), ); } Future> _fetch(BuildContext context) async { - var model = context.read(); List posts = []; var threadPosts = await model.getThread(threadNumber); @@ -145,13 +164,16 @@ class SendMessageFormState extends State { listen: false) .postMessage(opPost, value.commentTextController.text) - .then((value) { - if (value == 240) { + .then((responseCode) { + if (responseCode == 240) { ScaffoldMessenger.of(context) .showSnackBar( const SnackBar( content: Text('Пост отправлен!')), ); + Provider.of(context, + listen: false) + .update(); } }); value.commentTextController.text = "";