Files
buzz/mobile/lib/features/activity/compose_drafts_provider.dart
T
cls 9dfa06ffee
Docker image / Build (linux/amd64) (push) Has been cancelled
Docker image / Build (linux/arm64) (push) Has been cancelled
Docker image / Merge release multi-arch manifest (push) Has been cancelled
Docker image / Merge debug multi-arch manifest (push) Has been cancelled
Docker image / Build public push gateway (linux/amd64) (push) Has been cancelled
Docker image / Build public push gateway (linux/arm64) (push) Has been cancelled
Docker image / Publish public push gateway image (push) Has been cancelled
Sprig image / Build (linux/amd64) (push) Has been cancelled
Sprig image / Build (linux/arm64) (push) Has been cancelled
Sprig image / Merge multi-arch manifest (push) Has been cancelled
Harbor Buzz Orchestra / Python tests and lint (push) Has been cancelled
CI / Detect Changed Paths (push) Has been cancelled
CI / Rust Lint (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
CI / Desktop Core (push) Has been cancelled
CI / Desktop Smoke E2E (1) (push) Has been cancelled
CI / Desktop Smoke E2E (2) (push) Has been cancelled
CI / Desktop Smoke E2E (3) (push) Has been cancelled
CI / Desktop Smoke E2E (4) (push) Has been cancelled
CI / Desktop (push) Has been cancelled
CI / Desktop E2E Relay (push) Has been cancelled
CI / Desktop E2E Integration (1/2) (push) Has been cancelled
CI / Desktop E2E Integration (2/2) (push) Has been cancelled
CI / Desktop E2E Integration (push) Has been cancelled
CI / Backend Integration (relay e2e) (push) Has been cancelled
CI / Relay E2E (push) Has been cancelled
CI / Web (push) Has been cancelled
CI / Mobile (push) Has been cancelled
CI / Security (push) Has been cancelled
CI / Dead Token Reference Guard (push) Has been cancelled
CI / Server Cross-Compile (aarch64-unknown-linux-musl) (push) Has been cancelled
CI / Server Cross-Compile (x86_64-unknown-linux-musl) (push) Has been cancelled
CI / Windows Rust (x86_64-pc-windows-msvc) (push) Has been cancelled
CI / Desktop Build (macOS) (push) Has been cancelled
helm chart / lint + unittest + render matrix (push) Has been cancelled
helm chart / install on kind (gated) (push) Has been cancelled
helm chart / publish chart to GHCR (push) Has been cancelled
Mesh Lifecycle / Relay-Driven Mesh Lifecycle Smoke (push) Has been cancelled
Sprig / Build (aarch64-unknown-linux-musl) (push) Has been cancelled
Sprig / Build (x86_64-unknown-linux-musl) (push) Has been cancelled
Sprig / Publish rolling release (push) Has been cancelled
Sprig / Publish tagged release (push) Has been cancelled
feat: import Chinese-localized Buzz source snapshot
Signed-off-by: cls_宁波本机 <908705107@qq.com>
2026-08-13 18:34:25 +08:00

151 lines
4.5 KiB
Dart

import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import '../../shared/relay/relay.dart';
import '../../shared/theme/theme_provider.dart';
const _draftsPrefsKey = 'compose_drafts_v1';
const _maxDrafts = 50;
/// A locally persisted, unsent composer draft.
///
/// Mobile has no durable relay-backed draft store (desktop keeps drafts
/// locally too), so drafts are device-local by design: the inbox Drafts
/// filter reflects what this device's composer has in flight.
@immutable
class ComposeDraft {
/// `<channelId>` for channel composers, `<channelId>:<threadHeadId>` for
/// thread composers.
final String key;
final String channelId;
final String? threadHeadId;
final String text;
final int updatedAt; // unix seconds
const ComposeDraft({
required this.key,
required this.channelId,
required this.threadHeadId,
required this.text,
required this.updatedAt,
});
Map<String, dynamic> toJson() => {
'key': key,
'channel_id': channelId,
if (threadHeadId != null) 'thread_head_id': threadHeadId,
'text': text,
'updated_at': updatedAt,
};
static ComposeDraft? fromJson(Object? raw) {
if (raw is! Map<String, dynamic>) return null;
final key = raw['key'];
final channelId = raw['channel_id'];
final text = raw['text'];
final updatedAt = raw['updated_at'];
if (key is! String || channelId is! String || text is! String) return null;
if (text.trim().isEmpty) return null;
return ComposeDraft(
key: key,
channelId: channelId,
threadHeadId: raw['thread_head_id'] as String?,
text: text,
updatedAt: updatedAt is int ? updatedAt : 0,
);
}
}
String composeDraftKey(String channelId, {String? threadHeadId}) =>
threadHeadId == null ? channelId : '$channelId:$threadHeadId';
/// SharedPreferences-backed store of unsent composer drafts, newest first.
///
/// Drafts are plaintext, so persistence is namespaced by community (relay)
/// and account pubkey: switching community or account rebuilds this provider
/// against that identity's own store and can never surface another
/// identity's draft text.
class ComposeDraftsNotifier extends Notifier<List<ComposeDraft>> {
late String _prefsKey;
@override
List<ComposeDraft> build() {
// Rebuild (and re-read the identity-scoped store) whenever the active
// community or the derived account pubkey changes.
final config = ref.watch(relayConfigProvider);
final pubkey = ref.watch(myPubkeyProvider) ?? 'anon';
_prefsKey = '$_draftsPrefsKey:${config.baseUrl}:$pubkey';
final prefs = ref.read(savedPrefsProvider);
final raw = readMigratedPref<String>(
prefs,
canonicalKey: _prefsKey,
legacyKey: '$_draftsPrefsKey:${config.storedOrigin}:$pubkey',
read: prefs.getString,
write: prefs.setString,
);
if (raw == null) return const [];
try {
final decoded = jsonDecode(raw);
if (decoded is! List) return const [];
final drafts = decoded
.map(ComposeDraft.fromJson)
.whereType<ComposeDraft>()
.toList();
drafts.sort((a, b) => b.updatedAt.compareTo(a.updatedAt));
return drafts;
} catch (_) {
return const [];
}
}
/// Persist [text] for the composer identified by [key]. Empty text removes
/// the draft.
void save({
required String key,
required String channelId,
String? threadHeadId,
required String text,
}) {
if (text.trim().isEmpty) {
remove(key);
return;
}
final existing = state.where((d) => d.key == key).firstOrNull;
if (existing?.text == text) return;
final draft = ComposeDraft(
key: key,
channelId: channelId,
threadHeadId: threadHeadId,
text: text,
updatedAt: DateTime.now().millisecondsSinceEpoch ~/ 1000,
);
final next = [draft, ...state.where((d) => d.key != key)];
_persist(next.length > _maxDrafts ? next.sublist(0, _maxDrafts) : next);
}
void remove(String key) {
if (!state.any((d) => d.key == key)) return;
_persist([...state.where((d) => d.key != key)]);
}
String? textFor(String key) =>
state.where((d) => d.key == key).firstOrNull?.text;
void _persist(List<ComposeDraft> drafts) {
state = List.unmodifiable(drafts);
final prefs = ref.read(savedPrefsProvider);
prefs.setString(
_prefsKey,
jsonEncode([for (final d in drafts) d.toJson()]),
);
}
}
final composeDraftsProvider =
NotifierProvider<ComposeDraftsNotifier, List<ComposeDraft>>(
ComposeDraftsNotifier.new,
);