✨ Realm basis
This commit is contained in:
@ -15,8 +15,9 @@ import 'package:uuid/uuid.dart';
|
||||
|
||||
class ChannelEditorScreen extends StatefulWidget {
|
||||
final Channel? editing;
|
||||
final String? realm;
|
||||
|
||||
const ChannelEditorScreen({super.key, this.editing});
|
||||
const ChannelEditorScreen({super.key, this.editing, this.realm});
|
||||
|
||||
@override
|
||||
State<ChannelEditorScreen> createState() => _ChannelEditorScreenState();
|
||||
@ -39,8 +40,8 @@ class _ChannelEditorScreenState extends State<ChannelEditorScreen> {
|
||||
}
|
||||
|
||||
final uri = widget.editing == null
|
||||
? getRequestUri('messaging', '/api/channels/global')
|
||||
: getRequestUri('messaging', '/api/channels/global/${widget.editing!.id}');
|
||||
? getRequestUri('messaging', '/api/channels/${widget.realm ?? 'global'}')
|
||||
: getRequestUri('messaging', '/api/channels/${widget.realm ?? 'global'}/${widget.editing!.id}');
|
||||
|
||||
final req = Request(widget.editing == null ? 'POST' : 'PUT', uri);
|
||||
req.headers['Content-Type'] = 'application/json';
|
||||
|
@ -15,8 +15,9 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
|
||||
class ChatMemberScreen extends StatefulWidget {
|
||||
final Channel channel;
|
||||
final String realm;
|
||||
|
||||
const ChatMemberScreen({super.key, required this.channel});
|
||||
const ChatMemberScreen({super.key, required this.channel, this.realm = 'global'});
|
||||
|
||||
@override
|
||||
State<ChatMemberScreen> createState() => _ChatMemberScreenState();
|
||||
@ -36,8 +37,7 @@ class _ChatMemberScreenState extends State<ChatMemberScreen> {
|
||||
|
||||
_selfId = prof['id'];
|
||||
|
||||
var uri = getRequestUri(
|
||||
'messaging', '/api/channels/global/${widget.channel.alias}/members');
|
||||
var uri = getRequestUri('messaging', '/api/channels/${widget.realm}/${widget.channel.alias}/members');
|
||||
|
||||
var res = await auth.client!.get(uri);
|
||||
if (res.statusCode == 200) {
|
||||
@ -51,7 +51,7 @@ class _ChatMemberScreenState extends State<ChatMemberScreen> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> kickMember(ChannelMember item) async {
|
||||
Future<void> removeMember(ChannelMember item) async {
|
||||
setState(() => _isSubmitting = true);
|
||||
|
||||
final auth = context.read<AuthProvider>();
|
||||
@ -60,10 +60,9 @@ class _ChatMemberScreenState extends State<ChatMemberScreen> {
|
||||
return;
|
||||
}
|
||||
|
||||
var uri = getRequestUri(
|
||||
'messaging', '/api/channels/global/${widget.channel.alias}/kick');
|
||||
var uri = getRequestUri('messaging', '/api/channels/global/${widget.channel.alias}');
|
||||
|
||||
var res = await auth.client!.post(
|
||||
var res = await auth.client!.delete(
|
||||
uri,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
@ -82,7 +81,7 @@ class _ChatMemberScreenState extends State<ChatMemberScreen> {
|
||||
setState(() => _isSubmitting = false);
|
||||
}
|
||||
|
||||
Future<void> inviteMember(String username) async {
|
||||
Future<void> addMember(String username) async {
|
||||
setState(() => _isSubmitting = true);
|
||||
|
||||
final auth = context.read<AuthProvider>();
|
||||
@ -91,8 +90,7 @@ class _ChatMemberScreenState extends State<ChatMemberScreen> {
|
||||
return;
|
||||
}
|
||||
|
||||
var uri = getRequestUri(
|
||||
'messaging', '/api/channels/global/${widget.channel.alias}/invite');
|
||||
var uri = getRequestUri('messaging', '/api/channels/${widget.realm}/${widget.channel.alias}');
|
||||
|
||||
var res = await auth.client!.post(
|
||||
uri,
|
||||
@ -122,10 +120,10 @@ class _ChatMemberScreenState extends State<ChatMemberScreen> {
|
||||
);
|
||||
if (input == null) return;
|
||||
|
||||
await inviteMember((input as Account).name);
|
||||
await addMember((input as Account).name);
|
||||
}
|
||||
|
||||
bool getKickable(ChannelMember item) {
|
||||
bool getRemovable(ChannelMember item) {
|
||||
if (_selfId != widget.channel.account.externalId) return false;
|
||||
if (item.accountId == widget.channel.accountId) return false;
|
||||
if (item.account.externalId == _selfId) return false;
|
||||
@ -156,9 +154,7 @@ class _ChatMemberScreenState extends State<ChatMemberScreen> {
|
||||
child: CustomScrollView(
|
||||
slivers: [
|
||||
SliverToBoxAdapter(
|
||||
child: _isSubmitting
|
||||
? const LinearProgressIndicator().animate().scaleX()
|
||||
: Container(),
|
||||
child: _isSubmitting ? const LinearProgressIndicator().animate().scaleX() : Container(),
|
||||
),
|
||||
SliverList.builder(
|
||||
itemCount: _members.length,
|
||||
@ -169,9 +165,7 @@ class _ChatMemberScreenState extends State<ChatMemberScreen> {
|
||||
|
||||
return Dismissible(
|
||||
key: Key(randomId.toString()),
|
||||
direction: getKickable(element)
|
||||
? DismissDirection.startToEnd
|
||||
: DismissDirection.none,
|
||||
direction: getRemovable(element) ? DismissDirection.startToEnd : DismissDirection.none,
|
||||
background: Container(
|
||||
color: Colors.red,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 20),
|
||||
@ -179,13 +173,12 @@ class _ChatMemberScreenState extends State<ChatMemberScreen> {
|
||||
child: const Icon(Icons.remove, color: Colors.white),
|
||||
),
|
||||
child: ListTile(
|
||||
leading: AccountAvatar(
|
||||
source: element.account.avatar, direct: true),
|
||||
leading: AccountAvatar(source: element.account.avatar, direct: true),
|
||||
title: Text(element.account.nick),
|
||||
subtitle: Text(element.account.name),
|
||||
),
|
||||
onDismissed: (_) {
|
||||
kickMember(element);
|
||||
removeMember(element);
|
||||
},
|
||||
);
|
||||
},
|
||||
|
@ -21,8 +21,9 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
|
||||
class ChatScreen extends StatelessWidget {
|
||||
final String alias;
|
||||
final String realm;
|
||||
|
||||
const ChatScreen({super.key, required this.alias});
|
||||
const ChatScreen({super.key, required this.alias, this.realm = 'global'});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@ -37,31 +38,35 @@ class ChatScreen extends StatelessWidget {
|
||||
ChannelCallAction(
|
||||
call: chat.ongoingCall,
|
||||
channel: chat.focusChannel!,
|
||||
onUpdate: () => chat.fetchChannel(chat.focusChannel!.alias),
|
||||
realm: realm,
|
||||
onUpdate: () => chat.fetchChannel(chat.focusChannel!.alias, realm),
|
||||
),
|
||||
ChannelManageAction(
|
||||
channel: chat.focusChannel!,
|
||||
onUpdate: () => chat.fetchChannel(chat.focusChannel!.alias),
|
||||
realm: realm,
|
||||
onUpdate: () => chat.fetchChannel(chat.focusChannel!.alias, realm),
|
||||
),
|
||||
]
|
||||
: [],
|
||||
child: ChatScreenWidget(
|
||||
child: ChatWidget(
|
||||
alias: alias,
|
||||
realm: realm,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ChatScreenWidget extends StatefulWidget {
|
||||
class ChatWidget extends StatefulWidget {
|
||||
final String alias;
|
||||
final String realm;
|
||||
|
||||
const ChatScreenWidget({super.key, required this.alias});
|
||||
const ChatWidget({super.key, required this.alias, required this.realm});
|
||||
|
||||
@override
|
||||
State<ChatScreenWidget> createState() => _ChatScreenWidgetState();
|
||||
State<ChatWidget> createState() => _ChatWidgetState();
|
||||
}
|
||||
|
||||
class _ChatScreenWidgetState extends State<ChatScreenWidget> {
|
||||
class _ChatWidgetState extends State<ChatWidget> {
|
||||
bool _isReady = false;
|
||||
|
||||
final PagingController<int, Message> _pagingController = PagingController(firstPageKey: 0);
|
||||
@ -77,7 +82,7 @@ class _ChatScreenWidgetState extends State<ChatScreenWidget> {
|
||||
|
||||
var uri = getRequestUri(
|
||||
'messaging',
|
||||
'/api/channels/global/${widget.alias}/messages?take=$take&offset=$offset',
|
||||
'/api/channels/${widget.realm}/${widget.alias}/messages?take=$take&offset=$offset',
|
||||
);
|
||||
|
||||
var res = await auth.client!.get(uri);
|
||||
@ -147,8 +152,8 @@ class _ChatScreenWidgetState extends State<ChatScreenWidget> {
|
||||
super.initState();
|
||||
|
||||
Future.delayed(Duration.zero, () {
|
||||
_chat.fetchOngoingCall(widget.alias);
|
||||
_chat.fetchChannel(widget.alias);
|
||||
_chat.fetchOngoingCall(widget.alias, widget.realm);
|
||||
_chat.fetchChannel(widget.alias, widget.realm);
|
||||
});
|
||||
}
|
||||
|
||||
@ -232,6 +237,7 @@ class _ChatScreenWidgetState extends State<ChatScreenWidget> {
|
||||
),
|
||||
),
|
||||
ChatMessageEditor(
|
||||
realm: widget.realm,
|
||||
channel: widget.alias,
|
||||
editing: _editingItem,
|
||||
replying: _replyingItem,
|
||||
|
@ -9,8 +9,9 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
|
||||
class ChatDetailScreen extends StatefulWidget {
|
||||
final Channel channel;
|
||||
final String realm;
|
||||
|
||||
const ChatDetailScreen({super.key, required this.channel});
|
||||
const ChatDetailScreen({super.key, required this.channel, this.realm = 'global'});
|
||||
|
||||
@override
|
||||
State<ChatDetailScreen> createState() => _ChatDetailScreenState();
|
||||
@ -24,6 +25,7 @@ class _ChatDetailScreenState extends State<ChatDetailScreen> {
|
||||
context: context,
|
||||
builder: (context) => ChannelDeletion(
|
||||
channel: widget.channel,
|
||||
realm: widget.realm,
|
||||
isOwned: _isOwned,
|
||||
),
|
||||
);
|
||||
|
@ -23,29 +23,15 @@ class ChatListScreen extends StatelessWidget {
|
||||
title: AppLocalizations.of(context)!.chat,
|
||||
appBarActions: const [NotificationButton()],
|
||||
fixedAppBarColor: SolianTheme.isLargeScreen(context),
|
||||
child: ChatListWidget(
|
||||
onSelect: (item) {
|
||||
if (SolianRouter.currentRoute.name == 'chat.channel') {
|
||||
SolianRouter.router.pushReplacementNamed(
|
||||
'chat.channel',
|
||||
pathParameters: {'channel': item.alias},
|
||||
);
|
||||
} else {
|
||||
SolianRouter.router.pushNamed(
|
||||
'chat.channel',
|
||||
pathParameters: {'channel': item.alias},
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
child: const ChatListWidget(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ChatListWidget extends StatefulWidget {
|
||||
final Function(Channel item)? onSelect;
|
||||
final String? realm;
|
||||
|
||||
const ChatListWidget({super.key, this.onSelect});
|
||||
const ChatListWidget({super.key, this.realm});
|
||||
|
||||
@override
|
||||
State<ChatListWidget> createState() => _ChatListWidgetState();
|
||||
@ -58,7 +44,12 @@ class _ChatListWidgetState extends State<ChatListWidget> {
|
||||
final auth = context.read<AuthProvider>();
|
||||
if (!await auth.isAuthorized()) return;
|
||||
|
||||
var uri = getRequestUri('messaging', '/api/channels/global/me/available');
|
||||
Uri uri;
|
||||
if (widget.realm == null) {
|
||||
uri = getRequestUri('messaging', '/api/channels/global/me/available');
|
||||
} else {
|
||||
uri = getRequestUri('messaging', '/api/channels/${widget.realm}');
|
||||
}
|
||||
|
||||
var res = await auth.client!.get(uri);
|
||||
if (res.statusCode == 200) {
|
||||
@ -75,7 +66,10 @@ class _ChatListWidgetState extends State<ChatListWidget> {
|
||||
void viewNewChatAction() {
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
builder: (context) => ChatNewAction(onUpdate: () => fetchChannels()),
|
||||
builder: (context) => ChatNewAction(
|
||||
onUpdate: () => fetchChannels(),
|
||||
realm: widget.realm,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@ -128,17 +122,24 @@ class _ChatListWidgetState extends State<ChatListWidget> {
|
||||
title: Text(element.name),
|
||||
subtitle: Text(element.description),
|
||||
onTap: () async {
|
||||
if (widget.onSelect != null) {
|
||||
widget.onSelect!(element);
|
||||
return;
|
||||
String? result;
|
||||
if (SolianRouter.currentRoute.name == 'chat.channel') {
|
||||
result = await SolianRouter.router.pushReplacementNamed(
|
||||
widget.realm == null ? 'chat.channel' : 'realms.chat.channel',
|
||||
pathParameters: {
|
||||
'channel': element.alias,
|
||||
...(widget.realm == null ? {} : {'realm': widget.realm!}),
|
||||
},
|
||||
);
|
||||
} else {
|
||||
result = await SolianRouter.router.pushNamed(
|
||||
widget.realm == null ? 'chat.channel' : 'realms.chat.channel',
|
||||
pathParameters: {
|
||||
'channel': element.alias,
|
||||
...(widget.realm == null ? {} : {'realm': widget.realm!}),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
final result = await SolianRouter.router.pushNamed(
|
||||
'chat.channel',
|
||||
pathParameters: {
|
||||
'channel': element.alias,
|
||||
},
|
||||
);
|
||||
switch (result) {
|
||||
case 'refresh':
|
||||
fetchChannels();
|
||||
|
@ -25,25 +25,15 @@ class ExplorePostScreen extends StatelessWidget {
|
||||
fixedAppBarColor: SolianTheme.isLargeScreen(context),
|
||||
appBarActions: const [NotificationButton()],
|
||||
title: AppLocalizations.of(context)!.explore,
|
||||
child: ExplorePostWidget(
|
||||
onSelect: (item) {
|
||||
SolianRouter.router.pushNamed(
|
||||
'posts.screen',
|
||||
pathParameters: {
|
||||
'alias': item.alias,
|
||||
'dataset': item.dataset,
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
child: const ExplorePostWidget(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ExplorePostWidget extends StatefulWidget {
|
||||
final Function(Post item) onSelect;
|
||||
final String? realm;
|
||||
|
||||
const ExplorePostWidget({super.key, required this.onSelect});
|
||||
const ExplorePostWidget({super.key, this.realm});
|
||||
|
||||
@override
|
||||
State<ExplorePostWidget> createState() => _ExplorePostWidgetState();
|
||||
@ -58,7 +48,12 @@ class _ExplorePostWidgetState extends State<ExplorePostWidget> {
|
||||
final offset = pageKey;
|
||||
const take = 5;
|
||||
|
||||
var uri = getRequestUri('interactive', '/api/feed?take=$take&offset=$offset');
|
||||
Uri uri;
|
||||
if (widget.realm == null) {
|
||||
uri = getRequestUri('interactive', '/api/feed?take=$take&offset=$offset');
|
||||
} else {
|
||||
uri = getRequestUri('interactive', '/api/feed?realm=${widget.realm}&take=$take&offset=$offset');
|
||||
}
|
||||
|
||||
var res = await _client.get(uri);
|
||||
if (res.statusCode == 200) {
|
||||
@ -95,7 +90,10 @@ class _ExplorePostWidgetState extends State<ExplorePostWidget> {
|
||||
return FloatingActionButton(
|
||||
child: const Icon(Icons.edit),
|
||||
onPressed: () async {
|
||||
final did = await SolianRouter.router.pushNamed('posts.moments.editor');
|
||||
final did = await SolianRouter.router.pushNamed(
|
||||
'posts.moments.editor',
|
||||
queryParameters: {'realm': widget.realm},
|
||||
);
|
||||
if (did == true) _pagingController.refresh();
|
||||
},
|
||||
);
|
||||
@ -114,7 +112,16 @@ class _ExplorePostWidgetState extends State<ExplorePostWidget> {
|
||||
itemBuilder: (context, item, index) => PostItem(
|
||||
item: item,
|
||||
onUpdate: () => _pagingController.refresh(),
|
||||
onTap: () => widget.onSelect(item),
|
||||
onTap: () {
|
||||
SolianRouter.router.pushNamed(
|
||||
widget.realm == null ? 'posts.details' : 'realms.posts.details',
|
||||
pathParameters: {
|
||||
'alias': item.alias,
|
||||
'dataset': item.dataset,
|
||||
...(widget.realm == null ? {} : {'realm': widget.realm!}),
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -37,12 +37,16 @@ class _NotificationScreenState extends State<NotificationScreen> {
|
||||
markList.add(element.id);
|
||||
}
|
||||
|
||||
var uri = getRequestUri('passport', '/api/notifications/batch/read');
|
||||
await auth.client!.put(
|
||||
uri,
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode({'messages': markList}),
|
||||
);
|
||||
nty.clearRealtime();
|
||||
|
||||
if(markList.isNotEmpty) {
|
||||
var uri = getRequestUri('passport', '/api/notifications/batch/read');
|
||||
await auth.client!.put(
|
||||
uri,
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode({'messages': markList}),
|
||||
);
|
||||
}
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||||
content: Text(AppLocalizations.of(context)!.notifyMarkAllReadDone),
|
||||
@ -195,19 +199,7 @@ class NotificationItem extends StatelessWidget {
|
||||
onDismissed: (direction) {
|
||||
markAsRead(item, context).then((value) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: RichText(
|
||||
text: TextSpan(
|
||||
children: [
|
||||
TextSpan(
|
||||
text: item.subject,
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
const TextSpan(text: ' is marked as read')
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
SnackBar(content: Text('${item.subject} is marked as read')),
|
||||
);
|
||||
});
|
||||
if (onDismiss != null) {
|
||||
|
87
lib/screens/realms/realm.dart
Normal file
87
lib/screens/realms/realm.dart
Normal file
@ -0,0 +1,87 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:solian/providers/auth.dart';
|
||||
import 'package:solian/providers/realm.dart';
|
||||
import 'package:solian/screens/chat/chat_list.dart';
|
||||
import 'package:solian/screens/explore.dart';
|
||||
import 'package:solian/utils/theme.dart';
|
||||
import 'package:solian/widgets/scaffold.dart';
|
||||
|
||||
class RealmScreen extends StatelessWidget {
|
||||
final String alias;
|
||||
|
||||
const RealmScreen({super.key, required this.alias});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final realm = context.watch<RealmProvider>();
|
||||
|
||||
return IndentScaffold(
|
||||
title: realm.focusRealm?.name ?? 'Loading...',
|
||||
hideDrawer: !SolianTheme.isLargeScreen(context),
|
||||
noSafeArea: true,
|
||||
fixedAppBarColor: SolianTheme.isLargeScreen(context),
|
||||
child: RealmWidget(
|
||||
alias: alias,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class RealmWidget extends StatefulWidget {
|
||||
final String alias;
|
||||
|
||||
const RealmWidget({super.key, required this.alias});
|
||||
|
||||
@override
|
||||
State<RealmWidget> createState() => _RealmWidgetState();
|
||||
}
|
||||
|
||||
class _RealmWidgetState extends State<RealmWidget> {
|
||||
bool _isReady = false;
|
||||
|
||||
late RealmProvider _realm;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
Future.delayed(Duration.zero, () {
|
||||
final auth = context.read<AuthProvider>();
|
||||
if (_realm.focusRealm?.alias != widget.alias) {
|
||||
_realm.fetchSingle(auth, widget.alias);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (!_isReady) {
|
||||
_realm = context.watch<RealmProvider>();
|
||||
_isReady = true;
|
||||
}
|
||||
|
||||
return DefaultTabController(
|
||||
length: 2,
|
||||
child: Column(
|
||||
children: [
|
||||
TabBar(
|
||||
isScrollable: !SolianTheme.isLargeScreen(context),
|
||||
tabs: const [
|
||||
Tab(icon: Icon(Icons.newspaper)),
|
||||
Tab(icon: Icon(Icons.message)),
|
||||
],
|
||||
),
|
||||
Expanded(
|
||||
child: TabBarView(
|
||||
children: [
|
||||
ExplorePostWidget(realm: widget.alias),
|
||||
ChatListWidget(realm: widget.alias),
|
||||
],
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
208
lib/screens/realms/realm_editor.dart
Normal file
208
lib/screens/realms/realm_editor.dart
Normal file
@ -0,0 +1,208 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_animate/flutter_animate.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:solian/models/realm.dart';
|
||||
import 'package:solian/providers/auth.dart';
|
||||
import 'package:solian/router.dart';
|
||||
import 'package:solian/utils/service_url.dart';
|
||||
import 'package:solian/widgets/exts.dart';
|
||||
import 'package:solian/widgets/scaffold.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
|
||||
class RealmEditorScreen extends StatefulWidget {
|
||||
final Realm? editing;
|
||||
final String? realm;
|
||||
|
||||
const RealmEditorScreen({super.key, this.editing, this.realm});
|
||||
|
||||
@override
|
||||
State<RealmEditorScreen> createState() => _RealmEditorScreenState();
|
||||
}
|
||||
|
||||
class _RealmEditorScreenState extends State<RealmEditorScreen> {
|
||||
final _aliasController = TextEditingController();
|
||||
final _nameController = TextEditingController();
|
||||
final _descriptionController = TextEditingController();
|
||||
|
||||
bool _isPublic = false;
|
||||
bool _isCommunity = false;
|
||||
|
||||
bool _isSubmitting = false;
|
||||
|
||||
Future<void> applyChannel(BuildContext context) async {
|
||||
setState(() => _isSubmitting = true);
|
||||
|
||||
final auth = context.read<AuthProvider>();
|
||||
if (!await auth.isAuthorized()) {
|
||||
setState(() => _isSubmitting = false);
|
||||
return;
|
||||
}
|
||||
|
||||
final uri = widget.editing == null
|
||||
? getRequestUri('passport', '/api/realms')
|
||||
: getRequestUri('passport', '/api/realms/${widget.editing!.id}');
|
||||
|
||||
final req = Request(widget.editing == null ? 'POST' : 'PUT', uri);
|
||||
req.headers['Content-Type'] = 'application/json';
|
||||
req.body = jsonEncode(<String, dynamic>{
|
||||
'alias': _aliasController.value.text.toLowerCase(),
|
||||
'name': _nameController.value.text,
|
||||
'description': _descriptionController.value.text,
|
||||
'is_public': _isPublic,
|
||||
'is_community': _isCommunity,
|
||||
'realm': widget.realm,
|
||||
});
|
||||
|
||||
var res = await Response.fromStream(await auth.client!.send(req));
|
||||
if (res.statusCode != 200) {
|
||||
var message = utf8.decode(res.bodyBytes);
|
||||
context.showErrorDialog(message);
|
||||
} else {
|
||||
if (SolianRouter.router.canPop()) {
|
||||
SolianRouter.router.pop(true);
|
||||
}
|
||||
}
|
||||
setState(() => _isSubmitting = false);
|
||||
}
|
||||
|
||||
void randomizeAlias() {
|
||||
_aliasController.text = const Uuid().v4().replaceAll('-', '').substring(0, 16);
|
||||
}
|
||||
|
||||
void cancelEditing() {
|
||||
if (SolianRouter.router.canPop()) {
|
||||
SolianRouter.router.pop(false);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
if (widget.editing != null) {
|
||||
_aliasController.text = widget.editing!.alias;
|
||||
_nameController.text = widget.editing!.name;
|
||||
_descriptionController.text = widget.editing!.description;
|
||||
_isPublic = widget.editing!.isPublic;
|
||||
_isCommunity = widget.editing!.isCommunity;
|
||||
}
|
||||
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final editingBanner = MaterialBanner(
|
||||
padding: const EdgeInsets.only(top: 4, bottom: 4, left: 20),
|
||||
leading: const Icon(Icons.edit_note),
|
||||
backgroundColor: Theme.of(context).colorScheme.surfaceVariant.withOpacity(0.9),
|
||||
dividerColor: const Color.fromARGB(1, 0, 0, 0),
|
||||
content: Text(AppLocalizations.of(context)!.realmEditNotify),
|
||||
actions: [
|
||||
TextButton(
|
||||
child: Text(AppLocalizations.of(context)!.cancel),
|
||||
onPressed: () => cancelEditing(),
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
return IndentScaffold(
|
||||
hideDrawer: true,
|
||||
title: AppLocalizations.of(context)!.realmEstablish,
|
||||
appBarActions: <Widget>[
|
||||
TextButton(
|
||||
onPressed: !_isSubmitting ? () => applyChannel(context) : null,
|
||||
child: Text(AppLocalizations.of(context)!.apply.toUpperCase()),
|
||||
),
|
||||
],
|
||||
child: Column(
|
||||
children: [
|
||||
_isSubmitting ? const LinearProgressIndicator().animate().scaleX() : Container(),
|
||||
widget.editing != null ? editingBanner : Container(),
|
||||
ListTile(
|
||||
title: Text(AppLocalizations.of(context)!.realmUsage),
|
||||
subtitle: Text(AppLocalizations.of(context)!.realmUsageCaption),
|
||||
leading: const CircleAvatar(
|
||||
backgroundColor: Colors.teal,
|
||||
child: Icon(Icons.supervised_user_circle, color: Colors.white),
|
||||
),
|
||||
),
|
||||
const Divider(thickness: 0.3),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 2),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextField(
|
||||
autofocus: true,
|
||||
controller: _aliasController,
|
||||
decoration: InputDecoration.collapsed(
|
||||
hintText: AppLocalizations.of(context)!.realmAliasLabel,
|
||||
),
|
||||
onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(),
|
||||
),
|
||||
),
|
||||
TextButton(
|
||||
style: TextButton.styleFrom(
|
||||
shape: const CircleBorder(),
|
||||
visualDensity: const VisualDensity(horizontal: -2, vertical: -2),
|
||||
),
|
||||
onPressed: () => randomizeAlias(),
|
||||
child: const Icon(Icons.refresh),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
const Divider(thickness: 0.3),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
child: TextField(
|
||||
autocorrect: true,
|
||||
controller: _nameController,
|
||||
decoration: InputDecoration.collapsed(
|
||||
hintText: AppLocalizations.of(context)!.realmNameLabel,
|
||||
),
|
||||
onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(),
|
||||
),
|
||||
),
|
||||
const Divider(thickness: 0.3),
|
||||
Expanded(
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||
child: TextField(
|
||||
minLines: 5,
|
||||
maxLines: null,
|
||||
autocorrect: true,
|
||||
keyboardType: TextInputType.multiline,
|
||||
controller: _descriptionController,
|
||||
decoration: InputDecoration.collapsed(
|
||||
hintText: AppLocalizations.of(context)!.realmDescriptionLabel,
|
||||
),
|
||||
onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(),
|
||||
),
|
||||
),
|
||||
),
|
||||
const Divider(thickness: 0.3),
|
||||
CheckboxListTile(
|
||||
title: Text(AppLocalizations.of(context)!.realmPublicLabel),
|
||||
value: _isPublic,
|
||||
onChanged: (newValue) {
|
||||
setState(() => _isPublic = newValue ?? false);
|
||||
},
|
||||
controlAffinity: ListTileControlAffinity.leading,
|
||||
),
|
||||
CheckboxListTile(
|
||||
title: Text(AppLocalizations.of(context)!.realmCommunityLabel),
|
||||
value: _isCommunity,
|
||||
onChanged: (newValue) {
|
||||
setState(() => _isCommunity = newValue ?? false);
|
||||
},
|
||||
controlAffinity: ListTileControlAffinity.leading,
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
132
lib/screens/realms/realm_list.dart
Normal file
132
lib/screens/realms/realm_list.dart
Normal file
@ -0,0 +1,132 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:solian/providers/auth.dart';
|
||||
import 'package:solian/providers/realm.dart';
|
||||
import 'package:solian/router.dart';
|
||||
import 'package:solian/screens/realms/realm.dart';
|
||||
import 'package:solian/utils/theme.dart';
|
||||
import 'package:solian/widgets/notification_notifier.dart';
|
||||
import 'package:solian/widgets/realms/realm_new.dart';
|
||||
import 'package:solian/widgets/scaffold.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:solian/widgets/signin_required.dart';
|
||||
|
||||
class RealmListScreen extends StatelessWidget {
|
||||
const RealmListScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final realm = context.watch<RealmProvider>();
|
||||
|
||||
return realm.focusRealm == null
|
||||
? IndentScaffold(
|
||||
title: AppLocalizations.of(context)!.realm,
|
||||
appBarActions: const [NotificationButton()],
|
||||
fixedAppBarColor: SolianTheme.isLargeScreen(context),
|
||||
child: const RealmListWidget(),
|
||||
)
|
||||
: RealmScreen(alias: realm.focusRealm!.alias);
|
||||
}
|
||||
}
|
||||
|
||||
class RealmListWidget extends StatefulWidget {
|
||||
const RealmListWidget({super.key});
|
||||
|
||||
@override
|
||||
State<RealmListWidget> createState() => _RealmListWidgetState();
|
||||
}
|
||||
|
||||
class _RealmListWidgetState extends State<RealmListWidget> {
|
||||
void viewNewRealmAction() {
|
||||
final auth = context.read<AuthProvider>();
|
||||
final realms = context.read<RealmProvider>();
|
||||
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
builder: (context) => RealmNewAction(onUpdate: () => realms.fetch(auth)),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
Future.delayed(Duration.zero, () {
|
||||
final auth = context.read<AuthProvider>();
|
||||
final realms = context.read<RealmProvider>();
|
||||
realms.fetch(auth);
|
||||
});
|
||||
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final auth = context.read<AuthProvider>();
|
||||
final realms = context.watch<RealmProvider>();
|
||||
|
||||
return Scaffold(
|
||||
floatingActionButton: FutureBuilder(
|
||||
future: auth.isAuthorized(),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData && snapshot.data!) {
|
||||
return FloatingActionButton.extended(
|
||||
icon: const Icon(Icons.add),
|
||||
label: Text(AppLocalizations.of(context)!.realmNew),
|
||||
onPressed: () => viewNewRealmAction(),
|
||||
);
|
||||
} else {
|
||||
return Container();
|
||||
}
|
||||
},
|
||||
),
|
||||
body: FutureBuilder(
|
||||
future: auth.isAuthorized(),
|
||||
builder: (context, snapshot) {
|
||||
if (!snapshot.hasData || !snapshot.data!) {
|
||||
return const SignInRequiredScreen();
|
||||
}
|
||||
|
||||
return RefreshIndicator(
|
||||
onRefresh: () => realms.fetch(auth),
|
||||
child: ListView.builder(
|
||||
itemCount: realms.realms.length,
|
||||
itemBuilder: (context, index) {
|
||||
final element = realms.realms[index];
|
||||
return ListTile(
|
||||
leading: const CircleAvatar(
|
||||
backgroundColor: Colors.indigo,
|
||||
child: Icon(Icons.supervisor_account, color: Colors.white),
|
||||
),
|
||||
title: Text(element.name),
|
||||
subtitle: Text(element.description),
|
||||
onTap: () async {
|
||||
realms.fetchSingle(auth, element.alias);
|
||||
String? result;
|
||||
if (SolianRouter.currentRoute.name == 'chat.channel') {
|
||||
result = await SolianRouter.router.pushReplacementNamed(
|
||||
'realms.details',
|
||||
pathParameters: {
|
||||
'realm': element.alias,
|
||||
},
|
||||
);
|
||||
} else {
|
||||
result = await SolianRouter.router.pushNamed(
|
||||
'realms.details',
|
||||
pathParameters: {
|
||||
'realm': element.alias,
|
||||
},
|
||||
);
|
||||
}
|
||||
switch (result) {
|
||||
case 'refresh':
|
||||
realms.fetch(auth);
|
||||
}
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user