mirror of
https://github.com/nostrlabs-io/zap-stream-flutter.git
synced 2025-06-16 11:58:50 +00:00
@ -7,46 +7,46 @@ import 'package:ndk/domain_layer/entities/account.dart';
|
||||
import 'package:ndk/shared/nips/nip01/bip340.dart';
|
||||
import 'package:ndk/shared/nips/nip19/nip19.dart';
|
||||
|
||||
class Account {
|
||||
class LoginAccount {
|
||||
final AccountType type;
|
||||
final String pubkey;
|
||||
final String? privateKey;
|
||||
|
||||
Account._({required this.type, required this.pubkey, this.privateKey});
|
||||
LoginAccount._({required this.type, required this.pubkey, this.privateKey});
|
||||
|
||||
static Account nip19(String key) {
|
||||
static LoginAccount nip19(String key) {
|
||||
final keyData = Nip19.decode(key);
|
||||
final pubkey =
|
||||
Nip19.isKey("nsec", key) ? Bip340.getPublicKey(keyData) : keyData;
|
||||
final privateKey = Nip19.isKey("npub", key) ? null : keyData;
|
||||
return Account._(
|
||||
type: AccountType.privateKey,
|
||||
return LoginAccount._(
|
||||
type: Nip19.isKey("npub", key) ? AccountType.publicKey : AccountType.privateKey,
|
||||
pubkey: pubkey,
|
||||
privateKey: privateKey,
|
||||
);
|
||||
}
|
||||
|
||||
static Account privateKeyHex(String key) {
|
||||
return Account._(
|
||||
static LoginAccount privateKeyHex(String key) {
|
||||
return LoginAccount._(
|
||||
type: AccountType.privateKey,
|
||||
privateKey: key,
|
||||
pubkey: Bip340.getPublicKey(key),
|
||||
);
|
||||
}
|
||||
|
||||
static Account externalPublicKeyHex(String key) {
|
||||
return Account._(type: AccountType.externalSigner, pubkey: key);
|
||||
static LoginAccount externalPublicKeyHex(String key) {
|
||||
return LoginAccount._(type: AccountType.externalSigner, pubkey: key);
|
||||
}
|
||||
|
||||
static Map<String, dynamic> toJson(Account? acc) => {
|
||||
static Map<String, dynamic> toJson(LoginAccount? acc) => {
|
||||
"type": acc?.type.name,
|
||||
"pubKey": acc?.pubkey,
|
||||
"privateKey": acc?.privateKey,
|
||||
};
|
||||
|
||||
static Account? fromJson(Map<String, dynamic> json) {
|
||||
static LoginAccount? fromJson(Map<String, dynamic> json) {
|
||||
if (json.length > 2 && json.containsKey("pubKey")) {
|
||||
return Account._(
|
||||
return LoginAccount._(
|
||||
type: AccountType.values.firstWhere(
|
||||
(v) => v.toString().endsWith(json["type"] as String),
|
||||
),
|
||||
@ -58,13 +58,13 @@ class Account {
|
||||
}
|
||||
}
|
||||
|
||||
class LoginData extends ValueNotifier<Account?> {
|
||||
class LoginData extends ValueNotifier<LoginAccount?> {
|
||||
final _storage = FlutterSecureStorage();
|
||||
static const String _storageKey = "accounts";
|
||||
|
||||
LoginData() : super(null) {
|
||||
super.addListener(() async {
|
||||
final data = json.encode(Account.toJson(value));
|
||||
final data = json.encode(LoginAccount.toJson(value));
|
||||
await _storage.write(key: _storageKey, value: data);
|
||||
});
|
||||
}
|
||||
@ -78,7 +78,7 @@ class LoginData extends ValueNotifier<Account?> {
|
||||
final acc = await _storage.read(key: _storageKey);
|
||||
if (acc?.isNotEmpty ?? false) {
|
||||
try {
|
||||
super.value = Account.fromJson(json.decode(acc!));
|
||||
super.value = LoginAccount.fromJson(json.decode(acc!));
|
||||
} catch (e) {
|
||||
developer.log(e.toString());
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ import 'package:ndk_amber/ndk_amber.dart';
|
||||
import 'package:ndk_objectbox/ndk_objectbox.dart';
|
||||
import 'package:ndk_rust_verifier/ndk_rust_verifier.dart';
|
||||
import 'package:zap_stream_flutter/pages/login.dart';
|
||||
import 'package:zap_stream_flutter/pages/login_input.dart';
|
||||
import 'package:zap_stream_flutter/pages/new_account.dart';
|
||||
import 'package:zap_stream_flutter/pages/profile.dart';
|
||||
import 'package:zap_stream_flutter/pages/stream.dart';
|
||||
import 'package:zap_stream_flutter/theme.dart';
|
||||
@ -87,7 +89,43 @@ Future<void> main() async {
|
||||
StatefulShellBranch(
|
||||
routes: [
|
||||
GoRoute(path: "/", builder: (ctx, state) => HomePage()),
|
||||
GoRoute(path: "/login", builder: (ctx, state) => LoginPage()),
|
||||
ShellRoute(
|
||||
builder: (context, state, child) {
|
||||
return Container(
|
||||
margin: EdgeInsets.only(top: 50),
|
||||
padding: EdgeInsets.symmetric(horizontal: 5),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
spacing: 20,
|
||||
children: [
|
||||
Center(
|
||||
child: Image.asset(
|
||||
"assets/logo.png",
|
||||
height: 150,
|
||||
),
|
||||
),
|
||||
child,
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: "/login",
|
||||
builder: (ctx, state) => LoginPage(),
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: "key",
|
||||
builder: (ctx, state) => LoginInputPage(),
|
||||
),
|
||||
GoRoute(
|
||||
path: "new",
|
||||
builder: (context, state) => NewAccountPage(),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
GoRoute(
|
||||
path: "/e/:id",
|
||||
builder: (ctx, state) {
|
||||
|
@ -4,56 +4,53 @@ import 'package:go_router/go_router.dart';
|
||||
import 'package:ndk/shared/nips/nip19/nip19.dart';
|
||||
import 'package:zap_stream_flutter/login.dart';
|
||||
import 'package:zap_stream_flutter/main.dart';
|
||||
import 'package:zap_stream_flutter/theme.dart';
|
||||
import 'package:zap_stream_flutter/widgets/button.dart';
|
||||
|
||||
class LoginPage extends StatefulWidget {
|
||||
class LoginPage extends StatelessWidget {
|
||||
const LoginPage({super.key});
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _LoginPage();
|
||||
}
|
||||
|
||||
class _LoginPage extends State<LoginPage> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
margin: EdgeInsets.only(top: 50),
|
||||
child: Column(
|
||||
spacing: 10,
|
||||
children: [
|
||||
Image.asset("assets/logo.png", height: 150),
|
||||
FutureBuilder(
|
||||
future: Amberflutter().isAppInstalled(),
|
||||
builder: (ctx, state) {
|
||||
if (state.data ?? false) {
|
||||
return BasicButton.text(
|
||||
"Login with Amber",
|
||||
onTap: () async {
|
||||
final amber = Amberflutter();
|
||||
final result = await amber.getPublicKey();
|
||||
if (result['signature'] != null) {
|
||||
final key = Nip19.decode(result['signature']);
|
||||
loginData.value = Account.externalPublicKeyHex(key);
|
||||
ctx.go("/");
|
||||
}
|
||||
},
|
||||
);
|
||||
} else {
|
||||
return SizedBox.shrink();
|
||||
}
|
||||
},
|
||||
return Column(
|
||||
spacing: 20,
|
||||
children: [
|
||||
FutureBuilder(
|
||||
future: Amberflutter().isAppInstalled(),
|
||||
builder: (ctx, state) {
|
||||
if (state.data ?? false) {
|
||||
return BasicButton.text(
|
||||
"Login with Amber",
|
||||
onTap: () async {
|
||||
final amber = Amberflutter();
|
||||
final result = await amber.getPublicKey();
|
||||
if (result['signature'] != null) {
|
||||
final key = Nip19.decode(result['signature']);
|
||||
loginData.value = LoginAccount.externalPublicKeyHex(key);
|
||||
ctx.go("/");
|
||||
}
|
||||
},
|
||||
);
|
||||
} else {
|
||||
return SizedBox.shrink();
|
||||
}
|
||||
},
|
||||
),
|
||||
BasicButton.text(
|
||||
"Login with Key",
|
||||
onTap: () => context.push("/login/key"),
|
||||
),
|
||||
Container(
|
||||
margin: EdgeInsets.symmetric(vertical: 20),
|
||||
height: 1,
|
||||
decoration: BoxDecoration(
|
||||
border: Border(bottom: BorderSide(color: LAYER_2)),
|
||||
),
|
||||
/*BasicButton.text("Login with Key"),
|
||||
Container(
|
||||
margin: EdgeInsets.symmetric(vertical: 20),
|
||||
height: 1,
|
||||
decoration: BoxDecoration(
|
||||
border: Border(bottom: BorderSide(color: LAYER_1)),
|
||||
),
|
||||
),
|
||||
Text("Create Account"),*/
|
||||
],
|
||||
),
|
||||
),
|
||||
BasicButton.text(
|
||||
"Create Account",
|
||||
onTap: () => context.push("/login/new"),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
56
lib/pages/login_input.dart
Normal file
56
lib/pages/login_input.dart
Normal file
@ -0,0 +1,56 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:ndk/shared/nips/nip19/nip19.dart';
|
||||
import 'package:zap_stream_flutter/login.dart';
|
||||
import 'package:zap_stream_flutter/main.dart';
|
||||
import 'package:zap_stream_flutter/theme.dart';
|
||||
import 'package:zap_stream_flutter/widgets/button.dart';
|
||||
|
||||
class LoginInputPage extends StatefulWidget {
|
||||
const LoginInputPage({super.key});
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _LoginInputPage();
|
||||
}
|
||||
|
||||
class _LoginInputPage extends State<LoginInputPage> {
|
||||
final TextEditingController _controller = TextEditingController();
|
||||
String? _error;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
spacing: 20,
|
||||
children: [
|
||||
TextFormField(
|
||||
controller: _controller,
|
||||
decoration: InputDecoration(labelText: "npub/nsec"),
|
||||
),
|
||||
BasicButton.text(
|
||||
"Login",
|
||||
onTap: () async {
|
||||
try {
|
||||
final keyData = Nip19.decode(_controller.text);
|
||||
if (keyData.isNotEmpty) {
|
||||
loginData.value = LoginAccount.nip19(_controller.text);
|
||||
context.go("/");
|
||||
} else {
|
||||
throw "Invalid key";
|
||||
}
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_error = e.toString();
|
||||
});
|
||||
}
|
||||
},
|
||||
),
|
||||
|
||||
if (_error != null)
|
||||
Text(
|
||||
_error!,
|
||||
style: TextStyle(color: WARNING, fontWeight: FontWeight.bold),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
121
lib/pages/new_account.dart
Normal file
121
lib/pages/new_account.dart
Normal file
@ -0,0 +1,121 @@
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:ndk/ndk.dart';
|
||||
import 'package:ndk/shared/nips/nip01/bip340.dart';
|
||||
import 'package:ndk/shared/nips/nip01/key_pair.dart';
|
||||
import 'package:zap_stream_flutter/login.dart';
|
||||
import 'package:zap_stream_flutter/main.dart';
|
||||
import 'package:zap_stream_flutter/theme.dart';
|
||||
import 'package:zap_stream_flutter/widgets/button.dart';
|
||||
|
||||
class NewAccountPage extends StatefulWidget {
|
||||
const NewAccountPage({super.key});
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _NewAccountPage();
|
||||
}
|
||||
|
||||
class _NewAccountPage extends State<NewAccountPage> {
|
||||
final TextEditingController _name = TextEditingController();
|
||||
String? _avatar;
|
||||
String? _error;
|
||||
final KeyPair _privateKey = Bip340.generatePrivateKey();
|
||||
|
||||
Future<void> _uploadAvatar() async {
|
||||
ndk.accounts.loginPrivateKey(
|
||||
pubkey: _privateKey.publicKey,
|
||||
privkey: _privateKey.privateKey!,
|
||||
);
|
||||
|
||||
final file = await ImagePicker().pickImage(source: ImageSource.gallery);
|
||||
if (file != null) {
|
||||
final upload = await ndk.blossom.uploadBlob(
|
||||
serverUrls: ["https://nostr.download"],
|
||||
data: await file.readAsBytes(),
|
||||
);
|
||||
setState(() {
|
||||
_avatar = upload.first.descriptor!.url;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _login() async {
|
||||
if (ndk.accounts.isNotLoggedIn) {
|
||||
ndk.accounts.loginPrivateKey(
|
||||
pubkey: _privateKey.publicKey,
|
||||
privkey: _privateKey.privateKey!,
|
||||
);
|
||||
}
|
||||
|
||||
await ndk.metadata.broadcastMetadata(
|
||||
Metadata(
|
||||
pubKey: _privateKey.publicKey,
|
||||
name: _name.text,
|
||||
picture: _avatar,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
spacing: 20,
|
||||
children: [
|
||||
GestureDetector(
|
||||
onTap: () {
|
||||
_uploadAvatar().catchError((e) {
|
||||
setState(() {
|
||||
if (e is String) {
|
||||
_error = e;
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
child: Container(
|
||||
width: 200,
|
||||
height: 200,
|
||||
clipBehavior: Clip.antiAlias,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.all(Radius.circular(200)),
|
||||
color: Color.fromARGB(100, 50, 50, 50),
|
||||
),
|
||||
child:
|
||||
_avatar == null
|
||||
? Center(child: Text("Upload Avatar"))
|
||||
: CachedNetworkImage(imageUrl: _avatar!),
|
||||
),
|
||||
),
|
||||
TextField(
|
||||
controller: _name,
|
||||
decoration: InputDecoration(labelText: "Username"),
|
||||
),
|
||||
BasicButton.text(
|
||||
"Login",
|
||||
onTap: () {
|
||||
_login()
|
||||
.then((_) {
|
||||
loginData.value = LoginAccount.privateKeyHex(
|
||||
_privateKey.privateKey!,
|
||||
);
|
||||
context.go("/");
|
||||
})
|
||||
.catchError((e) {
|
||||
setState(() {
|
||||
if (e is String) {
|
||||
_error = e;
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
),
|
||||
if (_error != null)
|
||||
Text(
|
||||
_error!,
|
||||
style: TextStyle(color: WARNING, fontWeight: FontWeight.bold),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
@ -1,7 +1,9 @@
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import 'package:chewie/chewie.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:video_player/video_player.dart';
|
||||
import 'package:wakelock_plus/wakelock_plus.dart';
|
||||
import 'package:zap_stream_flutter/imgproxy.dart';
|
||||
import 'package:zap_stream_flutter/main.dart';
|
||||
import 'package:zap_stream_flutter/theme.dart';
|
||||
import 'package:zap_stream_flutter/utils.dart';
|
||||
@ -38,17 +40,20 @@ class _StreamPage extends State<StreamPage> {
|
||||
_controller = VideoPlayerController.networkUrl(
|
||||
Uri.parse(url),
|
||||
httpHeaders: Map.from({"user-agent": userAgent}),
|
||||
videoPlayerOptions: VideoPlayerOptions(allowBackgroundPlayback: true),
|
||||
);
|
||||
() async {
|
||||
await _controller!.initialize();
|
||||
|
||||
_chewieController = ChewieController(
|
||||
videoPlayerController: _controller!,
|
||||
autoPlay: true,
|
||||
);
|
||||
setState(() {
|
||||
// nothing
|
||||
_chewieController = ChewieController(
|
||||
videoPlayerController: _controller!,
|
||||
autoPlay: true,
|
||||
placeholder:
|
||||
(widget.stream.info.image?.isNotEmpty ?? false)
|
||||
? CachedNetworkImage(
|
||||
imageUrl: proxyImg(context, widget.stream.info.image!),
|
||||
)
|
||||
: null,
|
||||
);
|
||||
});
|
||||
}();
|
||||
}
|
||||
@ -75,7 +80,18 @@ class _StreamPage extends State<StreamPage> {
|
||||
child:
|
||||
_chewieController != null
|
||||
? Chewie(controller: _chewieController!)
|
||||
: Container(color: LAYER_1),
|
||||
: Container(
|
||||
color: LAYER_1,
|
||||
child:
|
||||
(widget.stream.info.image?.isNotEmpty ?? false)
|
||||
? CachedNetworkImage(
|
||||
imageUrl: proxyImg(
|
||||
context,
|
||||
widget.stream.info.image!,
|
||||
),
|
||||
)
|
||||
: null,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
widget.stream.info.title ?? "",
|
||||
|
@ -15,3 +15,4 @@ Color LAYER_5 = Color.fromARGB(255, 173, 173, 173);
|
||||
Color PRIMARY_1 = Color.fromARGB(255, 248, 56, 217);
|
||||
Color SECONDARY_1 = Color.fromARGB(255, 52, 210, 254);
|
||||
Color ZAP_1 = Color.fromARGB(255, 255, 141, 43);
|
||||
Color WARNING = Color.fromARGB(255, 255, 86, 63);
|
||||
|
@ -207,6 +207,7 @@ class _WriteMessageWidget extends State<WriteMessageWidget> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final canSign = ndk.accounts.canSign;
|
||||
final isLogin = ndk.accounts.isLoggedIn;
|
||||
|
||||
return Container(
|
||||
@ -214,7 +215,7 @@ class _WriteMessageWidget extends State<WriteMessageWidget> {
|
||||
padding: EdgeInsets.symmetric(horizontal: 8),
|
||||
decoration: BoxDecoration(color: LAYER_2, borderRadius: DEFAULT_BR),
|
||||
child:
|
||||
isLogin
|
||||
canSign
|
||||
? Row(
|
||||
children: [
|
||||
Expanded(
|
||||
@ -240,7 +241,15 @@ class _WriteMessageWidget extends State<WriteMessageWidget> {
|
||||
)
|
||||
: Container(
|
||||
padding: EdgeInsets.symmetric(vertical: 12),
|
||||
child: Row(children: [Text("Please login to send messages")]),
|
||||
child: Row(
|
||||
children: [
|
||||
Text(
|
||||
isLogin
|
||||
? "Can't write messages with npub login"
|
||||
: "Please login to send messages",
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
@ -23,8 +23,8 @@ class StreamGrid extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
final streams = events
|
||||
.map((e) => StreamEvent(e))
|
||||
.where((e) => e.info.stream?.isNotEmpty ?? false)
|
||||
.sortedBy((a) => a.info.starts ?? a.event.createdAt);
|
||||
.sortedBy((a) => a.info.starts ?? a.event.createdAt)
|
||||
.reversed;
|
||||
final live = streams.where((s) => s.info.status == StreamStatus.live);
|
||||
final ended = streams.where((s) => s.info.status == StreamStatus.ended);
|
||||
final planned = streams.where((s) => s.info.status == StreamStatus.planned);
|
||||
|
Reference in New Issue
Block a user