mirror of
https://github.com/twonlyapp/twonly-app.git
synced 2026-07-18 02:24:07 +00:00
fix timer issues
This commit is contained in:
parent
2cee00aff6
commit
cda52a5da8
3 changed files with 53 additions and 26 deletions
|
|
@ -1,5 +1,10 @@
|
||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 0.3.5
|
||||||
|
|
||||||
|
- Fix: Performance issue caused by an out-of-sync Signal session
|
||||||
|
- Fix: Multiple smaller bug fixes
|
||||||
|
|
||||||
## 0.3.3
|
## 0.3.3
|
||||||
|
|
||||||
- Fix: Multiple UI issues
|
- Fix: Multiple UI issues
|
||||||
|
|
|
||||||
|
|
@ -259,6 +259,7 @@ class _UserListItem extends State<GroupListItemComp> {
|
||||||
: Row(
|
: Row(
|
||||||
children: [
|
children: [
|
||||||
LastMessageTimeComp(
|
LastMessageTimeComp(
|
||||||
|
key: ValueKey(widget.group.groupId),
|
||||||
dateTime: widget.group.lastMessageExchange,
|
dateTime: widget.group.lastMessageExchange,
|
||||||
),
|
),
|
||||||
FlameCounterWidget(
|
FlameCounterWidget(
|
||||||
|
|
@ -281,7 +282,10 @@ class _UserListItem extends State<GroupListItemComp> {
|
||||||
const Text('•'),
|
const Text('•'),
|
||||||
const SizedBox(width: 5),
|
const SizedBox(width: 5),
|
||||||
if (_currentMessage != null)
|
if (_currentMessage != null)
|
||||||
LastMessageTimeComp(message: _currentMessage),
|
LastMessageTimeComp(
|
||||||
|
key: ValueKey(widget.group.groupId),
|
||||||
|
message: _currentMessage,
|
||||||
|
),
|
||||||
FlameCounterWidget(
|
FlameCounterWidget(
|
||||||
groupId: widget.group.groupId,
|
groupId: widget.group.groupId,
|
||||||
prefix: true,
|
prefix: true,
|
||||||
|
|
|
||||||
|
|
@ -19,41 +19,59 @@ class LastMessageTimeComp extends StatefulWidget {
|
||||||
class _LastMessageTimeCompState extends State<LastMessageTimeComp> {
|
class _LastMessageTimeCompState extends State<LastMessageTimeComp> {
|
||||||
Timer? updateTime;
|
Timer? updateTime;
|
||||||
int lastMessageInSeconds = 0;
|
int lastMessageInSeconds = 0;
|
||||||
|
DateTime? targetTime;
|
||||||
|
StreamSubscription<MessageAction?>? _actionSubscription;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
// Change the color every 200 milliseconds
|
_loadTargetTime();
|
||||||
updateTime = Timer.periodic(const Duration(milliseconds: 500), (
|
|
||||||
timer,
|
updateTime = Timer.periodic(
|
||||||
) async {
|
const Duration(milliseconds: 500),
|
||||||
if (widget.message != null) {
|
(_) => _updateSeconds(),
|
||||||
final lastAction = await twonlyDB.messagesDao.getLastMessageAction(
|
|
||||||
widget.message!.messageId,
|
|
||||||
);
|
);
|
||||||
lastMessageInSeconds = clock
|
|
||||||
.now()
|
|
||||||
.difference(lastAction?.actionAt ?? widget.message!.createdAt)
|
|
||||||
.inSeconds;
|
|
||||||
} else if (widget.dateTime != null) {
|
|
||||||
lastMessageInSeconds = clock
|
|
||||||
.now()
|
|
||||||
.difference(widget.dateTime!)
|
|
||||||
.inSeconds;
|
|
||||||
}
|
}
|
||||||
if (mounted) {
|
|
||||||
setState(() {
|
@override
|
||||||
if (lastMessageInSeconds < 0) {
|
void didUpdateWidget(LastMessageTimeComp oldWidget) {
|
||||||
lastMessageInSeconds = 0;
|
super.didUpdateWidget(oldWidget);
|
||||||
|
if (oldWidget.message?.messageId != widget.message?.messageId ||
|
||||||
|
oldWidget.dateTime != widget.dateTime) {
|
||||||
|
_loadTargetTime();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _loadTargetTime() {
|
||||||
|
_actionSubscription?.cancel();
|
||||||
|
_actionSubscription = null;
|
||||||
|
|
||||||
|
if (widget.message != null) {
|
||||||
|
_actionSubscription = twonlyDB.messagesDao
|
||||||
|
.watchLastMessageAction(widget.message!.messageId)
|
||||||
|
.listen((lastAction) {
|
||||||
|
targetTime = lastAction?.actionAt ?? widget.message!.createdAt;
|
||||||
|
_updateSeconds();
|
||||||
});
|
});
|
||||||
|
} else if (widget.dateTime != null) {
|
||||||
|
targetTime = widget.dateTime;
|
||||||
|
_updateSeconds();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _updateSeconds() {
|
||||||
|
if (targetTime == null || !mounted) return;
|
||||||
|
|
||||||
|
final seconds = clock.now().difference(targetTime!).inSeconds;
|
||||||
|
setState(() {
|
||||||
|
lastMessageInSeconds = seconds < 0 ? 0 : seconds;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
updateTime?.cancel();
|
updateTime?.cancel();
|
||||||
|
_actionSubscription?.cancel();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue