- MessagesCubit: 1:1 conversation over MessageTransport. Filters the shared inbox to the peer; tags our own sends with our pubkey (NIP-17 wraps don't come back to the sender) so the UI can align bubbles; degrades gracefully offline. - ChatScreen (mockup 10): message bubbles (mine right / peer left) + a composer; opens a session lazily, shows 'set up sharing' when there's no relay. - Entry point: a 'Message' button on each offer card (hidden on your own listings) → /chat/:pubkey. - Routing + i18n en/es/pt. Tests: 5 cubit (send/receive/order/filter/offline) + chat offline + market, green.
216 lines
5.8 KiB
Dart
216 lines
5.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
import '../i18n/strings.g.dart';
|
|
import '../services/social_service.dart';
|
|
import '../services/social_settings.dart';
|
|
import '../state/messages_cubit.dart';
|
|
import 'theme.dart';
|
|
|
|
/// A 1:1 encrypted chat with one peer (redesign screen 10). Private and
|
|
/// metadata-hiding (NIP-17) under the hood; local-first, so it degrades to a
|
|
/// "set up sharing" note when there's no relay.
|
|
class ChatScreen extends StatefulWidget {
|
|
const ChatScreen({
|
|
required this.social,
|
|
required this.settings,
|
|
required this.peerPubkey,
|
|
super.key,
|
|
});
|
|
|
|
final SocialService social;
|
|
final SocialSettings settings;
|
|
final String peerPubkey;
|
|
|
|
@override
|
|
State<ChatScreen> createState() => _ChatScreenState();
|
|
}
|
|
|
|
class _ChatScreenState extends State<ChatScreen> {
|
|
MessagesCubit? _cubit;
|
|
bool _loading = true;
|
|
final _input = TextEditingController();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_init();
|
|
}
|
|
|
|
Future<void> _init() async {
|
|
final cubit = await createMessagesCubit(
|
|
widget.social,
|
|
widget.settings,
|
|
peerPubkey: widget.peerPubkey,
|
|
);
|
|
if (!mounted) {
|
|
await cubit.close();
|
|
return;
|
|
}
|
|
cubit.start();
|
|
setState(() {
|
|
_cubit = cubit;
|
|
_loading = false;
|
|
});
|
|
}
|
|
|
|
Future<void> _send() async {
|
|
final cubit = _cubit;
|
|
if (cubit == null || _input.text.trim().isEmpty) return;
|
|
final text = _input.text;
|
|
_input.clear();
|
|
await cubit.send(text);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_cubit?.close();
|
|
_input.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final t = context.t;
|
|
final cubit = _cubit;
|
|
return Scaffold(
|
|
appBar: AppBar(title: Text(t.chat.title)),
|
|
body: _loading || cubit == null
|
|
? const Center(child: CircularProgressIndicator())
|
|
: BlocProvider.value(
|
|
value: cubit,
|
|
child: _ChatBody(controller: _input, onSend: _send),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _ChatBody extends StatelessWidget {
|
|
const _ChatBody({required this.controller, required this.onSend});
|
|
|
|
final TextEditingController controller;
|
|
final Future<void> Function() onSend;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final t = context.t;
|
|
final cubit = context.read<MessagesCubit>();
|
|
if (!cubit.isOnline) {
|
|
return Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(32),
|
|
child: Text(
|
|
t.chat.offline,
|
|
textAlign: TextAlign.center,
|
|
style: const TextStyle(color: seedMuted, fontSize: 15),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
return Column(
|
|
children: [
|
|
Expanded(
|
|
child: BlocBuilder<MessagesCubit, ChatState>(
|
|
builder: (context, state) {
|
|
if (state.messages.isEmpty) {
|
|
return Center(
|
|
child: Text(t.chat.empty,
|
|
style: const TextStyle(color: seedMuted)),
|
|
);
|
|
}
|
|
return ListView.builder(
|
|
padding: const EdgeInsets.all(12),
|
|
itemCount: state.messages.length,
|
|
itemBuilder: (context, i) {
|
|
final m = state.messages[i];
|
|
return _Bubble(text: m.text, mine: cubit.isMine(m));
|
|
},
|
|
);
|
|
},
|
|
),
|
|
),
|
|
_Composer(controller: controller, onSend: onSend),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
class _Bubble extends StatelessWidget {
|
|
const _Bubble({required this.text, required this.mine});
|
|
|
|
final String text;
|
|
final bool mine;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Align(
|
|
alignment: mine ? AlignmentDirectional.centerEnd : AlignmentDirectional.centerStart,
|
|
child: Container(
|
|
margin: const EdgeInsets.symmetric(vertical: 4),
|
|
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10),
|
|
constraints: BoxConstraints(
|
|
maxWidth: MediaQuery.of(context).size.width * 0.75,
|
|
),
|
|
decoration: BoxDecoration(
|
|
color: mine ? seedGreen : seedPrimaryContainer,
|
|
borderRadius: BorderRadius.circular(16),
|
|
),
|
|
child: Text(
|
|
text,
|
|
style: TextStyle(
|
|
color: mine ? Colors.white : seedOnSurface,
|
|
fontSize: 15,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _Composer extends StatelessWidget {
|
|
const _Composer({required this.controller, required this.onSend});
|
|
|
|
final TextEditingController controller;
|
|
final Future<void> Function() onSend;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final t = context.t;
|
|
return SafeArea(
|
|
top: false,
|
|
child: Padding(
|
|
padding: const EdgeInsets.fromLTRB(12, 6, 12, 12),
|
|
child: Row(
|
|
children: [
|
|
Expanded(
|
|
child: TextField(
|
|
key: const Key('chat.input'),
|
|
controller: controller,
|
|
textInputAction: TextInputAction.send,
|
|
onSubmitted: (_) => onSend(),
|
|
decoration: InputDecoration(
|
|
hintText: t.chat.hint,
|
|
filled: true,
|
|
fillColor: seedField,
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(24),
|
|
borderSide: BorderSide.none,
|
|
),
|
|
contentPadding:
|
|
const EdgeInsets.symmetric(horizontal: 18, vertical: 10),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 8),
|
|
IconButton.filled(
|
|
key: const Key('chat.send'),
|
|
icon: const Icon(Icons.send),
|
|
tooltip: t.chat.send,
|
|
onPressed: onSend,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|