mirror of
https://github.com/nostrlabs-io/zap-stream-flutter.git
synced 2025-06-17 20:28:52 +00:00
refactor: performance
This commit is contained in:
@ -32,7 +32,7 @@ var ndk = Ndk(
|
|||||||
eventVerifier: eventVerifier,
|
eventVerifier: eventVerifier,
|
||||||
cache: ndkCache,
|
cache: ndkCache,
|
||||||
bootstrapRelays: defaultRelays,
|
bootstrapRelays: defaultRelays,
|
||||||
engine: NdkEngine.JIT,
|
//engine: NdkEngine.JIT,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@ class HomePage extends StatelessWidget {
|
|||||||
children: [
|
children: [
|
||||||
HeaderWidget(),
|
HeaderWidget(),
|
||||||
RxFilter<Nip01Event>(
|
RxFilter<Nip01Event>(
|
||||||
|
Key("home-page"),
|
||||||
filters: [
|
filters: [
|
||||||
Filter(kinds: [30_311], limit: 50),
|
Filter(kinds: [30_311], limit: 50),
|
||||||
],
|
],
|
||||||
|
@ -81,7 +81,7 @@ class ProfilePage extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
|
|
||||||
RxFilter<Nip01Event>(
|
RxFilter<Nip01Event>(
|
||||||
key: Key("profile-streams:$hexPubkey"),
|
Key("profile-streams:$hexPubkey"),
|
||||||
relays: defaultRelays,
|
relays: defaultRelays,
|
||||||
filters: [
|
filters: [
|
||||||
Filter(kinds: [30_311], limit: 200, pTags: [hexPubkey]),
|
Filter(kinds: [30_311], limit: 200, pTags: [hexPubkey]),
|
||||||
|
@ -75,6 +75,7 @@ class _StreamPage extends State<StreamPage> {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return RxFilter<Nip01Event>(
|
return RxFilter<Nip01Event>(
|
||||||
|
Key("stream:event:${widget.stream.aTag}"),
|
||||||
relays: widget.stream.info.relays,
|
relays: widget.stream.info.relays,
|
||||||
filters: [
|
filters: [
|
||||||
Filter(
|
Filter(
|
||||||
|
@ -14,14 +14,14 @@ class RxFilter<T> extends StatefulWidget {
|
|||||||
final T Function(Nip01Event)? mapper;
|
final T Function(Nip01Event)? mapper;
|
||||||
final List<String>? relays;
|
final List<String>? relays;
|
||||||
|
|
||||||
const RxFilter({
|
const RxFilter(
|
||||||
super.key,
|
Key key, {
|
||||||
required this.filters,
|
required this.filters,
|
||||||
required this.builder,
|
required this.builder,
|
||||||
this.mapper,
|
this.mapper,
|
||||||
this.leaveOpen = true,
|
this.leaveOpen = true,
|
||||||
this.relays,
|
this.relays,
|
||||||
});
|
}) : super(key: key);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<StatefulWidget> createState() => _RxFilter<T>();
|
State<StatefulWidget> createState() => _RxFilter<T>();
|
||||||
@ -105,14 +105,14 @@ class RxFutureFilter<T> extends StatelessWidget {
|
|||||||
final Widget? loadingWidget;
|
final Widget? loadingWidget;
|
||||||
final T Function(Nip01Event)? mapper;
|
final T Function(Nip01Event)? mapper;
|
||||||
|
|
||||||
const RxFutureFilter({
|
const RxFutureFilter(
|
||||||
super.key,
|
Key key, {
|
||||||
required this.filterBuilder,
|
required this.filterBuilder,
|
||||||
required this.builder,
|
required this.builder,
|
||||||
this.mapper,
|
this.mapper,
|
||||||
this.leaveOpen = true,
|
this.leaveOpen = true,
|
||||||
this.loadingWidget,
|
this.loadingWidget,
|
||||||
});
|
}) : super(key: key);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@ -121,6 +121,7 @@ class RxFutureFilter<T> extends StatelessWidget {
|
|||||||
builder: (ctx, data) {
|
builder: (ctx, data) {
|
||||||
if (data.hasData) {
|
if (data.hasData) {
|
||||||
return RxFilter<T>(
|
return RxFilter<T>(
|
||||||
|
super.key!,
|
||||||
filters: data.data!,
|
filters: data.data!,
|
||||||
mapper: mapper,
|
mapper: mapper,
|
||||||
builder: builder,
|
builder: builder,
|
||||||
|
@ -6,15 +6,14 @@ import 'package:zap_stream_flutter/rx_filter.dart';
|
|||||||
import 'package:zap_stream_flutter/theme.dart';
|
import 'package:zap_stream_flutter/theme.dart';
|
||||||
import 'package:zap_stream_flutter/utils.dart';
|
import 'package:zap_stream_flutter/utils.dart';
|
||||||
import 'package:zap_stream_flutter/widgets/avatar.dart';
|
import 'package:zap_stream_flutter/widgets/avatar.dart';
|
||||||
import 'package:zap_stream_flutter/widgets/nostr_text.dart';
|
import 'package:zap_stream_flutter/widgets/chat_message.dart';
|
||||||
|
import 'package:zap_stream_flutter/widgets/chat_write.dart';
|
||||||
import 'package:zap_stream_flutter/widgets/profile.dart';
|
import 'package:zap_stream_flutter/widgets/profile.dart';
|
||||||
import 'package:zap_stream_flutter/widgets/chat_modal.dart';
|
|
||||||
|
|
||||||
class ChatWidget extends StatelessWidget {
|
class ChatWidget extends StatelessWidget {
|
||||||
final StreamEvent stream;
|
final StreamEvent stream;
|
||||||
|
|
||||||
const ChatWidget({super.key, required this.stream});
|
const ChatWidget({super.key, required this.stream});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
var muteLists = [stream.info.host];
|
var muteLists = [stream.info.host];
|
||||||
@ -24,9 +23,10 @@ class ChatWidget extends StatelessWidget {
|
|||||||
|
|
||||||
var filters = [
|
var filters = [
|
||||||
Filter(kinds: [1311, 9735], limit: 200, aTags: [stream.aTag]),
|
Filter(kinds: [1311, 9735], limit: 200, aTags: [stream.aTag]),
|
||||||
//Filter(kinds: [Nip51List.kMute], authors: muteLists), // bugged
|
Filter(kinds: [Nip51List.kMute], authors: muteLists),
|
||||||
];
|
];
|
||||||
return RxFilter<Nip01Event>(
|
return RxFilter<Nip01Event>(
|
||||||
|
Key("stream:chat:${stream.aTag}"),
|
||||||
relays: stream.info.relays,
|
relays: stream.info.relays,
|
||||||
filters: filters,
|
filters: filters,
|
||||||
builder: (ctx, state) {
|
builder: (ctx, state) {
|
||||||
@ -69,37 +69,26 @@ class ChatWidget extends StatelessWidget {
|
|||||||
Expanded(
|
Expanded(
|
||||||
child: ListView.builder(
|
child: ListView.builder(
|
||||||
reverse: true,
|
reverse: true,
|
||||||
shrinkWrap: true,
|
|
||||||
primary: true,
|
primary: true,
|
||||||
itemCount: filteredChat.length,
|
itemCount: filteredChat.length,
|
||||||
itemBuilder:
|
itemBuilder:
|
||||||
(ctx, idx) => switch (filteredChat[idx].kind) {
|
(ctx, idx) => switch (filteredChat[idx].kind) {
|
||||||
1311 => Padding(
|
1311 => ChatMessageWidget(
|
||||||
padding: EdgeInsets.symmetric(
|
key: Key("chat:${filteredChat[idx].id}"),
|
||||||
horizontal: 2,
|
|
||||||
vertical: 2,
|
|
||||||
),
|
|
||||||
child: _ChatMessageWidget(
|
|
||||||
stream: stream,
|
stream: stream,
|
||||||
msg: filteredChat[idx],
|
msg: filteredChat[idx],
|
||||||
),
|
),
|
||||||
),
|
9735 => _ChatZapWidget(
|
||||||
9735 => Padding(
|
key: Key("chat:${filteredChat[idx].id}"),
|
||||||
padding: EdgeInsets.symmetric(
|
|
||||||
horizontal: 2,
|
|
||||||
vertical: 2,
|
|
||||||
),
|
|
||||||
child: _ChatZapWidget(
|
|
||||||
stream: stream,
|
stream: stream,
|
||||||
zap: filteredChat[idx],
|
zap: filteredChat[idx],
|
||||||
),
|
),
|
||||||
),
|
|
||||||
_ => SizedBox.shrink(),
|
_ => SizedBox.shrink(),
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
if (stream.info.status == StreamStatus.live)
|
if (stream.info.status == StreamStatus.live)
|
||||||
_WriteMessageWidget(stream: stream),
|
WriteMessageWidget(stream: stream),
|
||||||
if (stream.info.status == StreamStatus.ended)
|
if (stream.info.status == StreamStatus.ended)
|
||||||
Container(
|
Container(
|
||||||
padding: EdgeInsets.all(8),
|
padding: EdgeInsets.all(8),
|
||||||
@ -126,6 +115,8 @@ class _StreamGoalWidget extends StatelessWidget {
|
|||||||
|
|
||||||
static Widget id(String id) {
|
static Widget id(String id) {
|
||||||
return RxFilter<Nip01Event>(
|
return RxFilter<Nip01Event>(
|
||||||
|
Key("stream:goal:$id"),
|
||||||
|
leaveOpen: false,
|
||||||
filters: [
|
filters: [
|
||||||
Filter(kinds: [9041], ids: [id]),
|
Filter(kinds: [9041], ids: [id]),
|
||||||
],
|
],
|
||||||
@ -142,6 +133,7 @@ class _StreamGoalWidget extends StatelessWidget {
|
|||||||
return Container(
|
return Container(
|
||||||
padding: EdgeInsets.symmetric(horizontal: 4, vertical: 4),
|
padding: EdgeInsets.symmetric(horizontal: 4, vertical: 4),
|
||||||
child: RxFilter<Nip01Event>(
|
child: RxFilter<Nip01Event>(
|
||||||
|
Key("stream:goal:$id:zaps"),
|
||||||
filters: [
|
filters: [
|
||||||
Filter(kinds: [9735], eTags: [goal.id]),
|
Filter(kinds: [9735], eTags: [goal.id]),
|
||||||
],
|
],
|
||||||
@ -163,7 +155,7 @@ class _StreamGoalWidget extends StatelessWidget {
|
|||||||
if (remaining > 0)
|
if (remaining > 0)
|
||||||
Text(
|
Text(
|
||||||
"Remaining: ${formatSats(remaining)}",
|
"Remaining: ${formatSats(remaining)}",
|
||||||
style: TextStyle(fontSize: 12, color: LAYER_5),
|
style: TextStyle(fontSize: 10, color: LAYER_5),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
@ -273,12 +265,13 @@ class _ChatZapWidget extends StatelessWidget {
|
|||||||
final StreamEvent stream;
|
final StreamEvent stream;
|
||||||
final Nip01Event zap;
|
final Nip01Event zap;
|
||||||
|
|
||||||
const _ChatZapWidget({required this.stream, required this.zap});
|
const _ChatZapWidget({required this.stream, required this.zap, super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final parsed = ZapReceipt.fromEvent(zap);
|
final parsed = ZapReceipt.fromEvent(zap);
|
||||||
return Container(
|
return Container(
|
||||||
|
margin: EdgeInsets.symmetric(vertical: 4),
|
||||||
padding: EdgeInsets.all(8),
|
padding: EdgeInsets.all(8),
|
||||||
decoration: BoxDecoration(
|
decoration: BoxDecoration(
|
||||||
border: Border.all(color: ZAP_1),
|
border: Border.all(color: ZAP_1),
|
||||||
@ -338,203 +331,3 @@ class _ChatZapWidget extends StatelessWidget {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class _ChatMessageWidget extends StatelessWidget {
|
|
||||||
final StreamEvent stream;
|
|
||||||
final Nip01Event msg;
|
|
||||||
|
|
||||||
const _ChatMessageWidget({required this.stream, required this.msg});
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
return ProfileLoaderWidget(msg.pubKey, (ctx, state) {
|
|
||||||
final profile = state.data ?? Metadata(pubKey: msg.pubKey);
|
|
||||||
return GestureDetector(
|
|
||||||
onLongPress: () {
|
|
||||||
if (ndk.accounts.canSign) {
|
|
||||||
showModalBottomSheet(
|
|
||||||
context: context,
|
|
||||||
constraints: BoxConstraints.expand(),
|
|
||||||
builder: (ctx) => ChatModalWidget(profile: profile, event: msg),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
child: Column(
|
|
||||||
spacing: 2,
|
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
|
||||||
children: [
|
|
||||||
_chatText(profile),
|
|
||||||
RxFilter<Nip01Event>(
|
|
||||||
key: Key("chat:reactions:${msg.id}"),
|
|
||||||
filters: [
|
|
||||||
Filter(kinds: [9735, 7], eTags: [msg.id]),
|
|
||||||
],
|
|
||||||
builder: (ctx, data) => _chatReactions(data),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Widget _chatText(Metadata profile) {
|
|
||||||
return RichText(
|
|
||||||
text: TextSpan(
|
|
||||||
children: [
|
|
||||||
WidgetSpan(
|
|
||||||
child: AvatarWidget(profile: profile, size: 24),
|
|
||||||
alignment: PlaceholderAlignment.middle,
|
|
||||||
),
|
|
||||||
TextSpan(text: " "),
|
|
||||||
WidgetSpan(
|
|
||||||
alignment: PlaceholderAlignment.middle,
|
|
||||||
child: ProfileNameWidget(
|
|
||||||
profile: profile,
|
|
||||||
style: TextStyle(
|
|
||||||
color: msg.pubKey == stream.info.host ? PRIMARY_1 : SECONDARY_1,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
TextSpan(text: " "),
|
|
||||||
...textToSpans(msg.content, msg.tags, msg.pubKey),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
Widget _chatReactions(List<Nip01Event>? events) {
|
|
||||||
if ((events?.length ?? 0) == 0) return SizedBox.shrink();
|
|
||||||
|
|
||||||
// reactions must have e tag pointing to msg
|
|
||||||
final filteredEvents = events!.where((e) => e.getEId() == msg.id);
|
|
||||||
final zaps = filteredEvents
|
|
||||||
.where((e) => e.kind == 9735)
|
|
||||||
.map((e) => ZapReceipt.fromEvent(e));
|
|
||||||
final reactions = filteredEvents.where((e) => e.kind == 7);
|
|
||||||
|
|
||||||
return Row(
|
|
||||||
spacing: 8,
|
|
||||||
children: [
|
|
||||||
if (zaps.isNotEmpty)
|
|
||||||
Container(
|
|
||||||
padding: EdgeInsets.symmetric(horizontal: 4, vertical: 2),
|
|
||||||
decoration: BoxDecoration(color: LAYER_2, borderRadius: DEFAULT_BR),
|
|
||||||
child: Row(
|
|
||||||
children: [
|
|
||||||
Icon(Icons.bolt, color: ZAP_1, size: 16),
|
|
||||||
Text(
|
|
||||||
formatSats(
|
|
||||||
zaps.fold(0, (acc, v) => acc + (v.amountSats ?? 0)),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
if (reactions.isNotEmpty)
|
|
||||||
...reactions
|
|
||||||
.fold(<String, Set<String>>{}, (acc, v) {
|
|
||||||
// ignore: prefer_collection_literals
|
|
||||||
acc[v.content] ??= Set();
|
|
||||||
acc[v.content]!.add(v.pubKey);
|
|
||||||
return acc;
|
|
||||||
})
|
|
||||||
.entries
|
|
||||||
.map(
|
|
||||||
(v) => Container(
|
|
||||||
padding: EdgeInsets.symmetric(horizontal: 4, vertical: 2),
|
|
||||||
decoration: BoxDecoration(
|
|
||||||
color: LAYER_2,
|
|
||||||
borderRadius: DEFAULT_BR,
|
|
||||||
),
|
|
||||||
child: Center(child: Text(v.key)),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class _WriteMessageWidget extends StatefulWidget {
|
|
||||||
final StreamEvent stream;
|
|
||||||
|
|
||||||
const _WriteMessageWidget({required this.stream});
|
|
||||||
|
|
||||||
@override
|
|
||||||
State<StatefulWidget> createState() => __WriteMessageWidget();
|
|
||||||
}
|
|
||||||
|
|
||||||
class __WriteMessageWidget extends State<_WriteMessageWidget> {
|
|
||||||
late final TextEditingController _controller;
|
|
||||||
|
|
||||||
@override
|
|
||||||
void initState() {
|
|
||||||
super.initState();
|
|
||||||
_controller = TextEditingController();
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _sendMessage() async {
|
|
||||||
final login = ndk.accounts.getLoggedAccount();
|
|
||||||
if (login == null) return;
|
|
||||||
|
|
||||||
final chatMsg = Nip01Event(
|
|
||||||
pubKey: login.pubkey,
|
|
||||||
kind: 1311,
|
|
||||||
content: _controller.text,
|
|
||||||
tags: [
|
|
||||||
["a", widget.stream.aTag],
|
|
||||||
],
|
|
||||||
);
|
|
||||||
_controller.text = "";
|
|
||||||
final res = ndk.broadcast.broadcast(nostrEvent: chatMsg);
|
|
||||||
await res.broadcastDoneFuture;
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
final canSign = ndk.accounts.canSign;
|
|
||||||
final isLogin = ndk.accounts.isLoggedIn;
|
|
||||||
|
|
||||||
return Container(
|
|
||||||
margin: EdgeInsets.fromLTRB(4, 8, 4, 0),
|
|
||||||
padding: EdgeInsets.symmetric(horizontal: 8),
|
|
||||||
decoration: BoxDecoration(color: LAYER_2, borderRadius: DEFAULT_BR),
|
|
||||||
child:
|
|
||||||
canSign
|
|
||||||
? Row(
|
|
||||||
children: [
|
|
||||||
Expanded(
|
|
||||||
child: TextField(
|
|
||||||
controller: _controller,
|
|
||||||
onSubmitted: (_) => _sendMessage(),
|
|
||||||
decoration: InputDecoration(
|
|
||||||
labelText: "Write message",
|
|
||||||
contentPadding: EdgeInsets.symmetric(vertical: 4),
|
|
||||||
labelStyle: TextStyle(color: LAYER_4, fontSize: 14),
|
|
||||||
border: InputBorder.none,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
//IconButton(onPressed: () {}, icon: Icon(Icons.mood)),
|
|
||||||
IconButton(
|
|
||||||
onPressed: () {
|
|
||||||
_sendMessage();
|
|
||||||
},
|
|
||||||
icon: Icon(Icons.send),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
: Container(
|
|
||||||
padding: EdgeInsets.symmetric(vertical: 12),
|
|
||||||
child: Row(
|
|
||||||
children: [
|
|
||||||
Text(
|
|
||||||
isLogin
|
|
||||||
? "Can't write messages with npub login"
|
|
||||||
: "Please login to send messages",
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
136
lib/widgets/chat_message.dart
Normal file
136
lib/widgets/chat_message.dart
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:ndk/ndk.dart';
|
||||||
|
import 'package:zap_stream_flutter/main.dart';
|
||||||
|
import 'package:zap_stream_flutter/rx_filter.dart';
|
||||||
|
import 'package:zap_stream_flutter/theme.dart';
|
||||||
|
import 'package:zap_stream_flutter/utils.dart';
|
||||||
|
import 'package:zap_stream_flutter/widgets/avatar.dart';
|
||||||
|
import 'package:zap_stream_flutter/widgets/chat_modal.dart';
|
||||||
|
import 'package:zap_stream_flutter/widgets/nostr_text.dart';
|
||||||
|
import 'package:zap_stream_flutter/widgets/profile.dart';
|
||||||
|
|
||||||
|
class ChatMessageWidget extends StatelessWidget {
|
||||||
|
final StreamEvent stream;
|
||||||
|
final Nip01Event msg;
|
||||||
|
|
||||||
|
const ChatMessageWidget({super.key, required this.stream, required this.msg});
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return ProfileLoaderWidget(msg.pubKey, (ctx, state) {
|
||||||
|
final profile = state.data ?? Metadata(pubKey: msg.pubKey);
|
||||||
|
return GestureDetector(
|
||||||
|
onLongPress: () {
|
||||||
|
if (ndk.accounts.canSign) {
|
||||||
|
showModalBottomSheet(
|
||||||
|
context: context,
|
||||||
|
constraints: BoxConstraints.expand(),
|
||||||
|
builder: (ctx) => ChatModalWidget(profile: profile, event: msg),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
child: Padding(
|
||||||
|
padding: EdgeInsets.symmetric(horizontal: 2, vertical: 4),
|
||||||
|
child: Column(
|
||||||
|
spacing: 2,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [_chatText(profile), ChatReactions(msg: msg)],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}, key: Key("chat:${msg.id}:profile"));
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget _chatText(Metadata profile) {
|
||||||
|
return RichText(
|
||||||
|
text: TextSpan(
|
||||||
|
children: [
|
||||||
|
WidgetSpan(
|
||||||
|
child: AvatarWidget(profile: profile, size: 24),
|
||||||
|
alignment: PlaceholderAlignment.middle,
|
||||||
|
),
|
||||||
|
TextSpan(text: " "),
|
||||||
|
WidgetSpan(
|
||||||
|
alignment: PlaceholderAlignment.middle,
|
||||||
|
child: ProfileNameWidget(
|
||||||
|
profile: profile,
|
||||||
|
style: TextStyle(
|
||||||
|
color: msg.pubKey == stream.info.host ? PRIMARY_1 : SECONDARY_1,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
TextSpan(text: " "),
|
||||||
|
...textToSpans(msg.content, msg.tags, msg.pubKey),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ChatReactions extends StatelessWidget {
|
||||||
|
final Nip01Event msg;
|
||||||
|
|
||||||
|
const ChatReactions({super.key, required this.msg});
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return RxFilter<Nip01Event>(
|
||||||
|
Key("chat:${msg.id}:reactions"),
|
||||||
|
filters: [
|
||||||
|
Filter(kinds: [9735, 7], eTags: [msg.id]),
|
||||||
|
],
|
||||||
|
builder: (ctx, data) => _chatReactions(data),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget _chatReactions(List<Nip01Event>? events) {
|
||||||
|
if ((events?.length ?? 0) == 0) return SizedBox.shrink();
|
||||||
|
|
||||||
|
// reactions must have e tag pointing to msg
|
||||||
|
final filteredEvents = events!.where((e) => e.getEId() == msg.id);
|
||||||
|
final zaps = filteredEvents
|
||||||
|
.where((e) => e.kind == 9735)
|
||||||
|
.map((e) => ZapReceipt.fromEvent(e));
|
||||||
|
final reactions = filteredEvents.where((e) => e.kind == 7);
|
||||||
|
|
||||||
|
return Row(
|
||||||
|
spacing: 8,
|
||||||
|
children: [
|
||||||
|
if (zaps.isNotEmpty)
|
||||||
|
Container(
|
||||||
|
padding: EdgeInsets.symmetric(horizontal: 4, vertical: 2),
|
||||||
|
decoration: BoxDecoration(color: LAYER_2, borderRadius: DEFAULT_BR),
|
||||||
|
child: Row(
|
||||||
|
children: [
|
||||||
|
Icon(Icons.bolt, color: ZAP_1, size: 16),
|
||||||
|
Text(
|
||||||
|
formatSats(
|
||||||
|
zaps.fold(0, (acc, v) => acc + (v.amountSats ?? 0)),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
if (reactions.isNotEmpty)
|
||||||
|
...reactions
|
||||||
|
.fold(<String, Set<String>>{}, (acc, v) {
|
||||||
|
// ignore: prefer_collection_literals
|
||||||
|
acc[v.content] ??= Set();
|
||||||
|
acc[v.content]!.add(v.pubKey);
|
||||||
|
return acc;
|
||||||
|
})
|
||||||
|
.entries
|
||||||
|
.map(
|
||||||
|
(v) => Container(
|
||||||
|
padding: EdgeInsets.symmetric(horizontal: 4, vertical: 2),
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: LAYER_2,
|
||||||
|
borderRadius: DEFAULT_BR,
|
||||||
|
),
|
||||||
|
child: Center(child: Text(v.key)),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -75,7 +75,12 @@ class _ChatModalWidget extends State<ChatModalWidget> {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
if (_showEmojiPicker) ReactionWidget(event: widget.event),
|
if (_showEmojiPicker) ReactionWidget(event: widget.event),
|
||||||
MuteButton(pubkey: widget.event.pubKey),
|
MuteButton(
|
||||||
|
pubkey: widget.event.pubKey,
|
||||||
|
onTap: () {
|
||||||
|
Navigator.pop(context);
|
||||||
|
},
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
90
lib/widgets/chat_write.dart
Normal file
90
lib/widgets/chat_write.dart
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:ndk/ndk.dart';
|
||||||
|
import 'package:zap_stream_flutter/main.dart';
|
||||||
|
import 'package:zap_stream_flutter/theme.dart';
|
||||||
|
import 'package:zap_stream_flutter/utils.dart';
|
||||||
|
|
||||||
|
class WriteMessageWidget extends StatefulWidget {
|
||||||
|
final StreamEvent stream;
|
||||||
|
|
||||||
|
const WriteMessageWidget({super.key, required this.stream});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<StatefulWidget> createState() => __WriteMessageWidget();
|
||||||
|
}
|
||||||
|
|
||||||
|
class __WriteMessageWidget extends State<WriteMessageWidget> {
|
||||||
|
late final TextEditingController _controller;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
_controller = TextEditingController();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _sendMessage() async {
|
||||||
|
final login = ndk.accounts.getLoggedAccount();
|
||||||
|
if (login == null) return;
|
||||||
|
|
||||||
|
final chatMsg = Nip01Event(
|
||||||
|
pubKey: login.pubkey,
|
||||||
|
kind: 1311,
|
||||||
|
content: _controller.text,
|
||||||
|
tags: [
|
||||||
|
["a", widget.stream.aTag],
|
||||||
|
],
|
||||||
|
);
|
||||||
|
_controller.text = "";
|
||||||
|
final res = ndk.broadcast.broadcast(nostrEvent: chatMsg);
|
||||||
|
await res.broadcastDoneFuture;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final canSign = ndk.accounts.canSign;
|
||||||
|
final isLogin = ndk.accounts.isLoggedIn;
|
||||||
|
|
||||||
|
return Container(
|
||||||
|
margin: EdgeInsets.fromLTRB(4, 8, 4, 0),
|
||||||
|
padding: EdgeInsets.symmetric(horizontal: 8),
|
||||||
|
decoration: BoxDecoration(color: LAYER_2, borderRadius: DEFAULT_BR),
|
||||||
|
child:
|
||||||
|
canSign
|
||||||
|
? Row(
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: TextField(
|
||||||
|
controller: _controller,
|
||||||
|
onSubmitted: (_) => _sendMessage(),
|
||||||
|
decoration: InputDecoration(
|
||||||
|
labelText: "Write message",
|
||||||
|
contentPadding: EdgeInsets.symmetric(vertical: 4),
|
||||||
|
labelStyle: TextStyle(color: LAYER_4, fontSize: 14),
|
||||||
|
border: InputBorder.none,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
//IconButton(onPressed: () {}, icon: Icon(Icons.mood)),
|
||||||
|
IconButton(
|
||||||
|
onPressed: () {
|
||||||
|
_sendMessage();
|
||||||
|
},
|
||||||
|
icon: Icon(Icons.send),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
: Container(
|
||||||
|
padding: EdgeInsets.symmetric(vertical: 12),
|
||||||
|
child: Row(
|
||||||
|
children: [
|
||||||
|
Text(
|
||||||
|
isLogin
|
||||||
|
? "Can't write messages with npub login"
|
||||||
|
: "Please login to send messages",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -6,8 +6,17 @@ import 'package:zap_stream_flutter/widgets/button.dart';
|
|||||||
|
|
||||||
class MuteButton extends StatelessWidget {
|
class MuteButton extends StatelessWidget {
|
||||||
final String pubkey;
|
final String pubkey;
|
||||||
|
final void Function()? onTap;
|
||||||
|
final void Function()? onMute;
|
||||||
|
final void Function()? onUnmute;
|
||||||
|
|
||||||
const MuteButton({super.key, required this.pubkey});
|
const MuteButton({
|
||||||
|
super.key,
|
||||||
|
required this.pubkey,
|
||||||
|
this.onTap,
|
||||||
|
this.onMute,
|
||||||
|
this.onUnmute,
|
||||||
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@ -32,6 +41,9 @@ class MuteButton extends StatelessWidget {
|
|||||||
padding: EdgeInsets.symmetric(vertical: 4, horizontal: 12),
|
padding: EdgeInsets.symmetric(vertical: 4, horizontal: 12),
|
||||||
decoration: BoxDecoration(color: WARNING, borderRadius: DEFAULT_BR),
|
decoration: BoxDecoration(color: WARNING, borderRadius: DEFAULT_BR),
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
|
if (onTap != null) {
|
||||||
|
onTap!();
|
||||||
|
}
|
||||||
if (isMuted) {
|
if (isMuted) {
|
||||||
await ndk.lists.broadcastRemoveNip51ListElement(
|
await ndk.lists.broadcastRemoveNip51ListElement(
|
||||||
Nip51List.kMute,
|
Nip51List.kMute,
|
||||||
@ -39,6 +51,9 @@ class MuteButton extends StatelessWidget {
|
|||||||
pubkey,
|
pubkey,
|
||||||
null,
|
null,
|
||||||
);
|
);
|
||||||
|
if (onUnmute != null) {
|
||||||
|
onUnmute!();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
await ndk.lists.broadcastAddNip51ListElement(
|
await ndk.lists.broadcastAddNip51ListElement(
|
||||||
Nip51List.kMute,
|
Nip51List.kMute,
|
||||||
@ -46,9 +61,10 @@ class MuteButton extends StatelessWidget {
|
|||||||
pubkey,
|
pubkey,
|
||||||
null,
|
null,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (onMute != null) {
|
||||||
|
onMute!();
|
||||||
}
|
}
|
||||||
if (ctx.mounted) {
|
|
||||||
Navigator.pop(ctx);
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
@ -14,6 +14,7 @@ class ProfileLoaderWidget extends StatelessWidget {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return FutureBuilder(
|
return FutureBuilder(
|
||||||
|
key: super.key ?? Key("profile-loader:$pubkey"),
|
||||||
future: ndk.metadata.loadMetadata(pubkey),
|
future: ndk.metadata.loadMetadata(pubkey),
|
||||||
builder: builder,
|
builder: builder,
|
||||||
);
|
);
|
||||||
@ -26,14 +27,14 @@ class ProfileNameWidget extends StatelessWidget {
|
|||||||
|
|
||||||
const ProfileNameWidget({super.key, required this.profile, this.style});
|
const ProfileNameWidget({super.key, required this.profile, this.style});
|
||||||
|
|
||||||
static Widget pubkey(String pubkey, {TextStyle? style}) {
|
static Widget pubkey(String pubkey, {Key? key, TextStyle? style}) {
|
||||||
return FutureBuilder(
|
return ProfileLoaderWidget(
|
||||||
future: ndk.metadata.loadMetadata(pubkey),
|
pubkey,
|
||||||
builder:
|
|
||||||
(ctx, data) => ProfileNameWidget(
|
(ctx, data) => ProfileNameWidget(
|
||||||
profile: data.data ?? Metadata(pubKey: pubkey),
|
profile: data.data ?? Metadata(pubKey: pubkey),
|
||||||
style: style,
|
style: style,
|
||||||
),
|
),
|
||||||
|
key: key,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,6 +85,7 @@ class ProfileWidget extends StatelessWidget {
|
|||||||
List<Widget>? children,
|
List<Widget>? children,
|
||||||
bool? showName,
|
bool? showName,
|
||||||
double? spacing,
|
double? spacing,
|
||||||
|
Key? key,
|
||||||
}) {
|
}) {
|
||||||
return ProfileLoaderWidget(pubkey, (ctx, state) {
|
return ProfileLoaderWidget(pubkey, (ctx, state) {
|
||||||
return ProfileWidget(
|
return ProfileWidget(
|
||||||
@ -91,6 +93,7 @@ class ProfileWidget extends StatelessWidget {
|
|||||||
size: size,
|
size: size,
|
||||||
showName: showName,
|
showName: showName,
|
||||||
spacing: spacing,
|
spacing: spacing,
|
||||||
|
key: key,
|
||||||
children: children,
|
children: children,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@ -102,7 +105,7 @@ class ProfileWidget extends StatelessWidget {
|
|||||||
spacing: spacing ?? 8,
|
spacing: spacing ?? 8,
|
||||||
children: [
|
children: [
|
||||||
AvatarWidget(profile: profile, size: size),
|
AvatarWidget(profile: profile, size: size),
|
||||||
if (showName ?? true) ProfileNameWidget(profile: profile),
|
if (showName ?? true) ProfileNameWidget(profile: profile, key: key),
|
||||||
...(children ?? []),
|
...(children ?? []),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
30
pubspec.lock
30
pubspec.lock
@ -580,26 +580,28 @@ packages:
|
|||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
path: "packages/ndk"
|
path: "packages/ndk"
|
||||||
ref: bbf2aa9c2468b2301de65734199649d56bb0fd74
|
ref: "919f35866f4b9d84565f7f08ebbbcd5fd0ef0b6a"
|
||||||
resolved-ref: bbf2aa9c2468b2301de65734199649d56bb0fd74
|
resolved-ref: "919f35866f4b9d84565f7f08ebbbcd5fd0ef0b6a"
|
||||||
url: "https://github.com/relaystr/ndk"
|
url: "https://github.com/relaystr/ndk"
|
||||||
source: git
|
source: git
|
||||||
version: "0.3.2"
|
version: "0.3.2"
|
||||||
ndk_amber:
|
ndk_amber:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
name: ndk_amber
|
path: "packages/amber"
|
||||||
sha256: "6f525e2bcdea08ecdd1815e2fdfc6e53c4bb86335927d8c333c1f4513dc1c099"
|
ref: "919f35866f4b9d84565f7f08ebbbcd5fd0ef0b6a"
|
||||||
url: "https://pub.dev"
|
resolved-ref: "919f35866f4b9d84565f7f08ebbbcd5fd0ef0b6a"
|
||||||
source: hosted
|
url: "https://github.com/relaystr/ndk"
|
||||||
|
source: git
|
||||||
version: "0.3.0"
|
version: "0.3.0"
|
||||||
ndk_objectbox:
|
ndk_objectbox:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
name: ndk_objectbox
|
path: "packages/objectbox"
|
||||||
sha256: f2bd04299ed34b99a01957c46eb6ff495c0bdcde068d382cbb8b8a222f67e132
|
ref: "919f35866f4b9d84565f7f08ebbbcd5fd0ef0b6a"
|
||||||
url: "https://pub.dev"
|
resolved-ref: "919f35866f4b9d84565f7f08ebbbcd5fd0ef0b6a"
|
||||||
source: hosted
|
url: "https://github.com/relaystr/ndk"
|
||||||
|
source: git
|
||||||
version: "0.2.3"
|
version: "0.2.3"
|
||||||
ndk_rust_verifier:
|
ndk_rust_verifier:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
@ -1198,6 +1200,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "6.5.0"
|
version: "6.5.0"
|
||||||
|
xxh3:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: xxh3
|
||||||
|
sha256: "399a0438f5d426785723c99da6b16e136f4953fb1e9db0bf270bd41dd4619916"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.2.0"
|
||||||
sdks:
|
sdks:
|
||||||
dart: ">=3.7.2 <4.0.0"
|
dart: ">=3.7.2 <4.0.0"
|
||||||
flutter: ">=3.27.0"
|
flutter: ">=3.27.0"
|
||||||
|
12
pubspec.yaml
12
pubspec.yaml
@ -38,7 +38,17 @@ dependency_overrides:
|
|||||||
git:
|
git:
|
||||||
url: https://github.com/relaystr/ndk
|
url: https://github.com/relaystr/ndk
|
||||||
path: packages/ndk
|
path: packages/ndk
|
||||||
ref: bbf2aa9c2468b2301de65734199649d56bb0fd74
|
ref: 919f35866f4b9d84565f7f08ebbbcd5fd0ef0b6a
|
||||||
|
ndk_objectbox:
|
||||||
|
git:
|
||||||
|
url: https://github.com/relaystr/ndk
|
||||||
|
path: packages/objectbox
|
||||||
|
ref: 919f35866f4b9d84565f7f08ebbbcd5fd0ef0b6a
|
||||||
|
ndk_amber:
|
||||||
|
git:
|
||||||
|
url: https://github.com/relaystr/ndk
|
||||||
|
path: packages/amber
|
||||||
|
ref: 919f35866f4b9d84565f7f08ebbbcd5fd0ef0b6a
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
Reference in New Issue
Block a user