Merge pull request 'new message counter' (#24) from msgcounter into trunk

Reviewed-on: #24
This commit is contained in:
Sarah Jamie Lewis 2021-03-17 16:01:21 -07:00
commit 20d3d2458f
7 changed files with 31 additions and 59 deletions

View File

@ -63,7 +63,7 @@ class FlwtchState extends State<Flwtch> {
appStatus = AppModel(cwtch: cwtch);
}
ChangeNotifierProvider<Settings> getSettingsProvider() => ChangeNotifierProvider(create: (context) => globalSettings);
ChangeNotifierProvider<Settings> getSettingsProvider() => ChangeNotifierProvider.value(value: globalSettings);
Provider<FlwtchState> getFlwtchStateProvider() => Provider<FlwtchState>(create: (_) => this);
ChangeNotifierProvider<ProfileListState> getProfileListProvider() => ChangeNotifierProvider(create: (context) => profs);

View File

@ -145,7 +145,9 @@ class ProfileInfoState extends ChangeNotifier {
status: contact["status"],
imagePath: contact["picture"],
isBlocked: contact["authorization"] == "blocked",
savePeerHistory: contact["saveConversationHistory"]);
savePeerHistory: contact["saveConversationHistory"],
numMessages: contact["numMessages"],
numUnread: contact["numUnread"]);
}));
}
}
@ -188,14 +190,17 @@ class ContactInfoState extends ChangeNotifier {
String _imagePath;
String _savePeerHistory;
int _unreadMessages = 0;
int _totalMessages = 0;
ContactInfoState({this.profileOnion, this.onion, nickname = "", isGroup = false, isInvitation = false, isBlocked = false, status = "", imagePath = "", savePeerHistory = "DeleteHistoryConfirmed"}) {
ContactInfoState({this.profileOnion, this.onion, nickname = "", isGroup = false, isInvitation = false, isBlocked = false, status = "", imagePath = "", savePeerHistory = "DeleteHistoryConfirmed", numMessages = 0, numUnread = 0,}) {
this._nickname = nickname;
this._isGroup = isGroup;
this._isInvitation = isInvitation;
this._isBlocked = isBlocked;
this._status = status;
this._imagePath = imagePath;
this._totalMessages = numMessages;
this._unreadMessages = numUnread;
this._savePeerHistory = savePeerHistory;
}
@ -236,6 +241,12 @@ class ContactInfoState extends ChangeNotifier {
notifyListeners();
}
get totalMessages => this._totalMessages;
set totalMessages(int newVal) {
this._totalMessages = newVal;
notifyListeners();
}
get imagePath => this._imagePath;
set imagePath(String newVal) {
this._imagePath = newVal;

View File

@ -26,7 +26,7 @@ class _DoubleColumnViewState extends State<DoubleColumnView> {
child: flwtch.selectedConversation == ""
? Center(child: Text("pick a contact"))
: //dev
Container(child: MessageView(profile: flwtch.selectedProfile, conversationHandle: flwtch.selectedConversation)),
Container(child: MessageView()),
),
],
);

View File

@ -8,10 +8,6 @@ import '../opaque.dart';
import '../widgets/messagelist.dart';
class MessageView extends StatefulWidget {
const MessageView({Key key, this.profile, this.conversationHandle}) : super(key: key);
final ProfileInfoState profile;
final String conversationHandle;
@override
_MessageViewState createState() => _MessageViewState();
}
@ -29,7 +25,7 @@ class _MessageViewState extends State<MessageView> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.conversationHandle),
title: Text(Provider.of<ContactInfoState>(context).onion),
actions: [
IconButton(icon: Icon(Icons.chat), onPressed: _pushContactSettings),
IconButton(icon: Icon(Icons.list), onPressed: _pushContactSettings),
@ -37,7 +33,7 @@ class _MessageViewState extends State<MessageView> {
IconButton(icon: Icon(Icons.settings), onPressed: _pushContactSettings),
],
),
body: MessageList(profile: widget.profile, conversationHandle: widget.conversationHandle),
body: MessageList(),
bottomSheet: _buildComposeBox(),
);
}

View File

@ -29,7 +29,7 @@ class _TripleColumnViewState extends State<TripleColumnView> {
child: flwtch.selectedConversation == ""
? Center(child: Text("pick a contact"))
: //dev
Container(child: MessageView(profile: flwtch.selectedProfile, conversationHandle: flwtch.selectedConversation)),
Container(child: MessageView()),
),
]);
}

View File

@ -63,7 +63,7 @@ class _ContactRowState extends State<ContactRow> {
ChangeNotifierProvider.value(value: Provider.of<ProfileInfoState>(context)),
ChangeNotifierProvider.value(value: Provider.of<ContactInfoState>(context)),
],
child: MessageView(conversationHandle: handle),
child: MessageView(),
);
},
),

View File

@ -1,61 +1,26 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../main.dart';
import '../model.dart';
import 'messagebubble.dart';
class MessageList extends StatefulWidget {
final ProfileInfoState profile;
final String conversationHandle;
const MessageList({Key key, this.profile, this.conversationHandle}) : super(key: key);
@override
_MessageListState createState() => _MessageListState();
}
class _MessageListState extends State<MessageList> {
int conversationNumMessages = 0;
Timer timer;
@override
Widget build(BuildContext context) {
_updateMessageCount(context);
if (timer == null) {
timer = Timer.periodic(Duration(seconds: 1), (Timer t) {
_updateMessageCount(context);
});
}
return ProxyProvider0(
update: (_, __) => MessageCounter(conversationNumMessages),
child: ListView.builder(
itemCount: conversationNumMessages,
itemBuilder: (context, index) {
return MessageBubble(
profile: Provider.of<ProfileInfoState>(context),
contactOnion: widget.conversationHandle,
messageIndex: index,
);
},
));
Widget build(BuildContext outerContext) {
return ListView.builder(
itemCount: Provider.of<ContactInfoState>(context).totalMessages,
itemBuilder: (context, index) {
return MessageBubble(
profile: Provider.of<ProfileInfoState>(outerContext),
contactOnion: Provider.of<ContactInfoState>(outerContext).onion,
messageIndex: index,
);
},
);
}
Future _updateMessageCount(BuildContext context) async {
if (!mounted) {
if (timer != null && timer.isActive) timer.cancel();
return;
}
Provider.of<FlwtchState>(context, listen: false).cwtch.NumMessages(Provider.of<ProfileInfoState>(context, listen: false).onion, widget.conversationHandle).then((n) {
if (n != conversationNumMessages) setState(() => conversationNumMessages = n);
});
}
}
class MessageCounter {
MessageCounter(this.x);
int x = 0;
}
}