Skip to content

Commit

Permalink
fix: update server select overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Mar 13, 2024
1 parent 871fbfb commit 2397a23
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 33 deletions.
17 changes: 11 additions & 6 deletions lib/bloc/main_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class MainBloc extends Bloc<MainEvent, MainState> {
);
await _initChild(newState);
emit(newState);
add(MainGetServerInstanceInfoEvent(config));
return;
} catch (e) {
debugPrint('login by refresh token failed');
Expand All @@ -183,27 +184,30 @@ class MainBloc extends Bloc<MainEvent, MainState> {
emit(MainAutoLoginState(state, EventStatus.failed));
}, transformer: droppable());

on<MainSetNextServerConfigEvent>((event, emit) async {
debugPrint('set next server config ${event.config}');
var newState = state.copyWith(nextServer: event.config);
on<MainGetServerInstanceInfoEvent>((event, emit) async {
if (state.knownServerInstanceSummary == null ||
state.knownServerInstanceSummary![event.config.id] == null) {
try {
final client = await clientFactory(
config: event.config, useSystemProxy: repo.useSystemProxy);
final resp =
await client.getServerInformation(GetServerInformationRequest());
newState = newState.copyWith(
emit(state.copyWith(
knownServerInstanceSummary: {
...state.knownServerInstanceSummary ?? {},
event.config.id: resp.serverInstanceSummary,
},
);
));
} catch (e) {
debugPrint(e.toString());
}
}
emit(MainNewServerSetState(newState));
});

on<MainSetNextServerConfigEvent>((event, emit) async {
debugPrint('set next server config ${event.config}');
add(MainGetServerInstanceInfoEvent(event.config));
emit(MainNewServerSetState(state.copyWith(nextServer: event.config)));
});

on<MainClearNextServerConfigEvent>((event, emit) async {
Expand Down Expand Up @@ -308,6 +312,7 @@ class MainBloc extends Bloc<MainEvent, MainState> {
})),
);
await _initChild(newState);
add(MainGetServerInstanceInfoEvent(config));
emit(newState);
} catch (e) {
debugPrint(e.toString());
Expand Down
6 changes: 6 additions & 0 deletions lib/bloc/main_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ class MainRegisterEvent extends MainEvent {
MainRegisterEvent(this.username, this.password,
{this.captchaID, this.captchaAns});
}

class MainGetServerInstanceInfoEvent extends MainEvent {
final ServerConfig config;

MainGetServerInstanceInfoEvent(this.config);
}
2 changes: 1 addition & 1 deletion lib/view/components/avatar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Avatar extends StatelessWidget {
),
),
child: Text(
name.isNotEmpty ? name[0].toUpperCase() : '',
name.isNotEmpty && image.isEmpty ? name[0].toUpperCase() : '',
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
Expand Down
64 changes: 50 additions & 14 deletions lib/view/pages/server_select_overlay.dart
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,11 @@ class ServerSelectOverlayState extends State<ServerSelectOverlay>
if (_current != null)
AvatarMenuItem(
name: _current!.host,
image: '',
image: state
.knownServerInstanceSummary?[
_current?.id ?? '']
?.logoUrl ??
'',
selected: _current?.id == _selected?.id,
onPressed: () {
setState(() {
Expand All @@ -225,7 +229,10 @@ class ServerSelectOverlayState extends State<ServerSelectOverlay>
if (server.id != _current?.id)
AvatarMenuItem(
name: server.host,
image: '',
image: state
.knownServerInstanceSummary?[server.id]
?.logoUrl ??
'',
selected: server.id == _selected?.id,
onPressed: () {
setState(() {
Expand All @@ -239,8 +246,12 @@ class ServerSelectOverlayState extends State<ServerSelectOverlay>
if (_minimized)
AvatarMenuItem(
name: _current?.host ?? '',
image: '',
selected: _current?.id == _selected?.id,
image: state
.knownServerInstanceSummary?[
_current?.id ?? '']
?.logoUrl ??
'',
selected: false,
onPressed: () {
ServerSelectOverlay.of(context)?.fullscreen();
},
Expand Down Expand Up @@ -333,16 +344,41 @@ class ServerDetail extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<MainBloc, MainState>(builder: (context, state) {
return Card(
child: Container(
padding: const EdgeInsets.all(16),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(config.host),
],
),
),
final instanceSummary =
(state.knownServerInstanceSummary ?? {})[config.id];
return Column(
children: [
if (instanceSummary != null)
Container(
height: 128,
width: 128,
margin: const EdgeInsets.all(16),
child: ExtendedImage.network(
instanceSummary.logoUrl,
fit: BoxFit.contain,
),
),
Card(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 16),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
instanceSummary?.name ?? config.id,
style: Theme.of(context).textTheme.titleLarge,
),
if (instanceSummary?.description.isNotEmpty ?? false)
Padding(
padding: const EdgeInsets.only(top: 4),
child: Text(instanceSummary!.description,
style: Theme.of(context).textTheme.bodyMedium),
),
],
),
),
)
],
);
});
}
Expand Down
30 changes: 18 additions & 12 deletions lib/view/specialized/nav_rail.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class NavRail extends StatelessWidget {
this.leading = const [],
this.trailing = const []});

final List<_MenuItem> leading;
final List<_MenuItem> body;
final List<_MenuItem> trailing;
final List<MenuItem> leading;
final List<MenuItem> body;
final List<MenuItem> trailing;

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -42,8 +42,8 @@ class NavRail extends StatelessWidget {
}

@immutable
class _MenuItem extends StatefulWidget {
const _MenuItem({
class MenuItem extends StatefulWidget {
const MenuItem({
super.key,
required this.onPressed,
required this.selected,
Expand All @@ -54,30 +54,32 @@ class _MenuItem extends StatefulWidget {
final bool selected;
final double containerSize;

Color? get overrideBackgroundColor => null;

Widget content(BuildContext context, bool isHover) {
return Container();
}

@override
State<_MenuItem> createState() => _MenuItemState();
State<MenuItem> createState() => _MenuItemState();
}

class _MenuItemState extends State<_MenuItem> {
class _MenuItemState extends State<MenuItem> {
bool isHover = false;

@override
Widget build(BuildContext context) {
final primaryColor = Theme.of(context).colorScheme.primary;
final backgroundColor = widget.overrideBackgroundColor ??
(widget.selected | isHover ? primaryColor : primaryColor.withAlpha(28));
return SizedBox(
width: widget.containerSize,
height: widget.containerSize,
child: AnimatedContainer(
margin: const EdgeInsets.all(8),
duration: const Duration(milliseconds: 200),
decoration: BoxDecoration(
color: widget.selected | isHover
? primaryColor
: primaryColor.withAlpha(28),
color: backgroundColor,
borderRadius: BorderRadius.all(
Radius.circular(widget.selected | isHover ? 12 : 24)),
),
Expand All @@ -98,7 +100,7 @@ class _MenuItemState extends State<_MenuItem> {
}
}

class IconMenuItem extends _MenuItem {
class IconMenuItem extends MenuItem {
const IconMenuItem({
super.key,
required this.icon,
Expand Down Expand Up @@ -129,7 +131,7 @@ class IconMenuItem extends _MenuItem {
}
}

class AvatarMenuItem extends _MenuItem {
class AvatarMenuItem extends MenuItem {
const AvatarMenuItem({
super.key,
required this.name,
Expand All @@ -152,6 +154,10 @@ class AvatarMenuItem extends _MenuItem {
@override
final double containerSize;

@override
Color? get overrideBackgroundColor =>
image != null && image!.isNotEmpty ? Colors.transparent : null;

@override
Widget content(BuildContext context, bool isHover) {
return Avatar(
Expand Down

0 comments on commit 2397a23

Please sign in to comment.