Skip to content

Commit

Permalink
Merge pull request #162 from getlantern/crdzbird/status-updates
Browse files Browse the repository at this point in the history
Workaround on svg change dynamically
  • Loading branch information
cosmicespresso authored Jun 18, 2021
2 parents 5de9802 + 272154a commit f80eb0b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 35 deletions.
82 changes: 47 additions & 35 deletions lib/messaging/widgets/message_types/status_row.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:lantern/package_store.dart';
import 'package:lantern/model/model.dart';
import 'package:lantern/messaging/widgets/message_utils.dart';
import 'package:lantern/utils/humanize.dart';
import 'package:lantern/utils/int_extension.dart';

class StatusRow extends StatefulWidget {
final bool outbound;
Expand All @@ -25,52 +26,63 @@ class StatusRowState extends State<StatusRow> {
@override
Widget build(BuildContext context) {
final statusIcon = getStatusIcon(widget.inbound, widget.msg);
final segments = widget.msg.firstViewedAt
.toInt()
.segments(iterations: 12, endTime: widget.msg.disappearAt.toInt());
final begin = widget.msg.firstViewedAt.toInt();
final end = widget.msg.disappearAt.toInt();
final lifeSpan = end - begin;
final step = lifeSpan / 12 + 1; // adding + 1 to avoid NaN below

return TweenAnimationBuilder<int>(
tween: IntTween(begin: begin, end: lifeSpan),
duration: Duration(milliseconds: lifeSpan),
curve: Curves.linear,
builder: (BuildContext context, int time, Widget? child) {
var index = (time / step).floor();
var index = begin.position(segments: segments, extraTime: time);
return Container(
child: Opacity(
opacity: 0.8,
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
mainAxisSize: MainAxisSize.min,
children: [
Container(
child: Opacity(
opacity: 0.8,
child: Flex(
direction: Axis.horizontal,
mainAxisAlignment: MainAxisAlignment.end,
mainAxisSize: MainAxisSize.min,
children: [
Flexible(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 1),
child: Text(
widget.message.value.ts.toInt().humanizeDate(),
style: tsMessageStatus(widget.outbound),
),
),
),
if (statusIcon != null)
Flexible(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 1),
child: Text(
widget.message.value.ts.toInt().humanizeDate(),
style: tsMessageStatus(widget.outbound),
),
),
if (statusIcon != null)
Container(
padding:
const EdgeInsets.symmetric(horizontal: 1),
child: Icon(
statusIcon,
size: 12,
color: widget.outbound
? outboundMsgColor
: inboundMsgColor,
)),
Container(
padding: const EdgeInsets.symmetric(horizontal: 1),
child: CustomAssetImage(
path: ImagePaths.countdownPaths[0],
size: 12,
color: widget.outbound
? outboundMsgColor
: inboundMsgColor)),
Text(index.toString()),
])));
child: Icon(
statusIcon,
size: 12,
color: widget.outbound
? outboundMsgColor
: inboundMsgColor,
)),
),
Flexible(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 1),
child: CustomAssetImage(
path: ImagePaths.countdownPaths[index],
size: 12,
color: widget.outbound
? outboundMsgColor
: inboundMsgColor),
),
),
],
),
),
);
});
}
}
38 changes: 38 additions & 0 deletions lib/utils/int_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
extension IntExtension on int {
/// Returns a `List<int>` with the time segments
List<int> segments({int iterations = 0, required int endTime}) {
var segments = <int>[];
var totalTime = (endTime - this);
int timePerSegment;

/// if the calculation gave an error then we can safetly asumme that the timePerSegment
/// is gonna be always 0.
try {
timePerSegment = (totalTime ~/ iterations);
} catch (_) {
timePerSegment = 0;
}

/// Once we have the time per segment, we iterate and check if the array is empty (only on first position) then
/// we just sum the current value, if not then we retrieve the previous position and just add it to the next iteration.
for (var i = 0; i < iterations; i++) {
segments.isEmpty
? segments.add(this + timePerSegment)
: segments.add(segments[i - 1] + timePerSegment);
}
return segments;
}

/// return the position of the element, based on a list of integers.
int position({required List<int> segments, int extraTime = 0}) {
var currentTime = extraTime != 0 ? (this + extraTime) : this;
var position = 0;
for (var i = 0; i < segments.length; i++) {
if (currentTime < segments[i]) {
position = i;
break;
}
}
return position;
}
}

0 comments on commit f80eb0b

Please # to comment.