Files
meshcore-open/lib/widgets/gif_message.dart
T
zjs81 25f197c979 Fix review findings, add default flood scope, and address recent issues
Review fixes:
- Keep stored message history beyond the 200-message window; stop
  messages leaking between radios; reset link/session state on disconnect
- Accept firmware's 176-byte RX frames over USB/TCP
- Hold APP_START during contact sync; contact-sync watchdog; replay DMs
  from not-yet-known senders; tombstone late command replies; finish
  channel sync on ERR_NOT_FOUND; fail region-scoped sends cleanly
- DM retry watchdog when RESP_CODE_SENT never arrives; late repeater CLI
  replies; ACK hash collisions; UTF-8-safe name truncation
- Image pipeline: delete-during-decode, lifecycle cancels, decoder memory
  release, entry cap, dead worker recovery, atomic model writes
- Storage: deterministic reaction hash across web/native, Smaz
  double-decode, per-entry parsing, per-radio path history and regions,
  review-mode data isolation
- UI: safe channel slot selection, byte-length name limits, double-send
  guard, mounted checks, localized hardcoded strings
- Skip persisted-store reload on repeat SELF_INFO

Features:
- Radio default flood scope (firmware ver 11+) and explicit unscoped
  sends (ver 12+)
- Scan for nearby nodes (#570)
- Optional distinct first-hop repeater counting for channel resend (#589)
- Optional region next to hop count (#580)

Issues:
- Message text follows system font size (#569)
- Repeater/room password field accepts 20 chars (#576)
- TCP auto-reconnect and keepalive (#571)
- Order feeds by local receive time; flag future timestamps (#584)
- Coding rate help text (#568), French "Favoris" (#575)
- iOS/macOS app badge now clears

Translations for all new strings in every locale.
2026-10-11 01:25:31 -07:00

205 lines
4.7 KiB
Dart

import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import '../l10n/l10n.dart';
class GifMessage extends StatefulWidget {
final String url;
final Color backgroundColor;
final Color fallbackTextColor;
final double maxSize;
const GifMessage({
super.key,
required this.url,
required this.backgroundColor,
required this.fallbackTextColor,
this.maxSize = 200,
});
@override
State<GifMessage> createState() => _GifMessageState();
}
class _GifMessageState extends State<GifMessage> {
ImageStream? _imageStream;
ImageStreamListener? _listener;
ui.Image? _image;
Object? _error;
bool _isLoading = true;
bool _isPaused = false;
@override
void initState() {
super.initState();
_resolveImage();
}
@override
void didUpdateWidget(covariant GifMessage oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.url != widget.url) {
_unsubscribe();
_image = null;
_error = null;
_isLoading = true;
_isPaused = false;
_resolveImage();
}
}
@override
void dispose() {
_unsubscribe();
super.dispose();
}
void _resolveImage() {
setState(() {
_isLoading = true;
_error = null;
});
final provider = NetworkImage(widget.url);
final stream = provider.resolve(ImageConfiguration.empty);
_imageStream = stream;
_listener = ImageStreamListener(
(imageInfo, _) {
if (_isPaused) {
return;
}
setState(() {
_image = imageInfo.image;
_isLoading = false;
});
},
onError: (error, _) {
setState(() {
_error = error;
_isLoading = false;
});
},
);
stream.addListener(_listener!);
}
void _retryLoad() {
_unsubscribe();
_image = null;
_isPaused = false;
_resolveImage();
}
void _unsubscribe() {
if (_imageStream != null && _listener != null) {
_imageStream!.removeListener(_listener!);
}
}
void _togglePause() {
if (_error != null) {
_retryLoad();
return;
}
if (_image == null) {
if (!_isLoading) {
_retryLoad();
}
return;
}
setState(() {
_isPaused = !_isPaused;
});
if (_listener == null || _imageStream == null) {
return;
}
if (_isPaused) {
_imageStream!.removeListener(_listener!);
} else {
_imageStream!.addListener(_listener!);
}
}
@override
Widget build(BuildContext context) {
// Calculate display size based on image aspect ratio
// Use 4:3 placeholder aspect ratio during loading to minimize layout shifts
double displayWidth = widget.maxSize;
double displayHeight = widget.maxSize * 0.75;
if (_image != null) {
final imageWidth = _image!.width.toDouble();
final imageHeight = _image!.height.toDouble();
final aspectRatio = imageWidth / imageHeight;
// Fit within maxSize, calculating dimensions from aspect ratio
if (aspectRatio >= 1) {
// Wider than tall: constrain by width
displayWidth = widget.maxSize;
displayHeight = displayWidth / aspectRatio;
} else {
// Taller than wide: constrain by height
displayHeight = widget.maxSize;
displayWidth = displayHeight * aspectRatio;
}
}
Widget content;
if (_error != null) {
content = Center(
child: Text(
context.l10n.gif_loadFailedRetry,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 12, color: widget.fallbackTextColor),
),
);
} else if (_isLoading && _image == null) {
content = const Center(
child: SizedBox(
width: 18,
height: 18,
child: CircularProgressIndicator(strokeWidth: 2),
),
);
} else if (_image == null) {
content = Center(
child: Text(
context.l10n.gif_tapToLoad,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 12, color: widget.fallbackTextColor),
),
);
} else {
content = RawImage(
image: _image,
fit: BoxFit.contain,
width: displayWidth,
height: displayHeight,
);
}
return GestureDetector(
onTap: _togglePause,
child: Container(
color: widget.backgroundColor,
width: displayWidth,
height: displayHeight,
child: Stack(
fit: StackFit.expand,
children: [
content,
if (_isPaused && _image != null)
Container(
color: Colors.black.withValues(alpha: 0.2),
child: const Center(
child: Icon(Icons.pause, color: Colors.white70, size: 28),
),
),
],
),
),
);
}
}