Skip to content

Commit

Permalink
opt.: TagSwitcher related
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Aug 16, 2024
1 parent f7ef8a3 commit 7558b48
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 198 deletions.
2 changes: 1 addition & 1 deletion lib/intro.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final class _IntroPage extends StatelessWidget {
final selected = await ctx.showPickSingleDialog(
title: libL10n.language,
items: AppLocalizations.supportedLocales,
name: (p0) => p0.nativeName,
display: (p0) => p0.nativeName,
initial: _setting.locale.fetch().toLocale,
);
if (selected != null) {
Expand Down
100 changes: 52 additions & 48 deletions lib/view/page/server/edit.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import 'dart:convert';

import 'package:choice/choice.dart';
import 'package:fl_lib/fl_lib.dart';
import 'package:flutter/material.dart';
import 'package:icons_plus/icons_plus.dart';
import 'package:server_box/core/extension/context/locale.dart';
import 'package:server_box/data/model/server/custom.dart';
import 'package:server_box/data/model/server/server.dart';
import 'package:server_box/data/model/server/wol_cfg.dart';
import 'package:server_box/data/provider/server.dart';

Expand Down Expand Up @@ -45,7 +47,7 @@ class _ServerEditPageState extends State<ServerEditPage> with AfterLayoutMixin {

final _keyIdx = ValueNotifier<int?>(null);
final _autoConnect = ValueNotifier(true);
final _jumpServer = ValueNotifier<String?>(null);
final _jumpServer = nvn<String?>();
final _pveIgnoreCert = ValueNotifier(false);
final _env = <String, String>{}.vn;
final _customCmds = <String, String>{}.vn;
Expand Down Expand Up @@ -217,39 +219,35 @@ class _ServerEditPageState extends State<ServerEditPage> with AfterLayoutMixin {
}

Widget _buildKeyAuth() {
const padding = EdgeInsets.only(left: 23, right: 13);
return PrivateKeyProvider.pkis.listenVal(
(pkis) {
final tiles = List<Widget>.generate(pkis.length, (index) {
final e = pkis[index];
return ListTile(
contentPadding: padding,
leading: Text(
'#${index + 1}',
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 15),
contentPadding: const EdgeInsets.only(left: 10, right: 15),
leading: Radio<int>(
value: index,
groupValue: _keyIdx.value,
onChanged: (value) => _keyIdx.value = value,
),
title: Text(e.id, textAlign: TextAlign.start),
subtitle: Text(
e.type ?? l10n.unknown,
textAlign: TextAlign.start,
style: UIs.textGrey,
),
trailing: Radio<int>(
value: index,
groupValue: _keyIdx.value,
onChanged: (value) => _keyIdx.value = value,
trailing: Btn.icon(
icon: const Icon(Icons.edit),
onTap: () => AppRoutes.keyEdit(pki: e).go(context),
),
onTap: () => _keyIdx.value = index,
);
});
tiles.add(
ListTile(
title: Text(libL10n.add),
contentPadding: padding,
trailing: const Padding(
padding: EdgeInsets.only(right: 13),
child: Icon(Icons.add),
),
contentPadding: const EdgeInsets.only(left: 23, right: 23),
trailing: const Icon(Icons.add),
onTap: () => AppRoutes.keyEdit().go(context),
),
);
Expand Down Expand Up @@ -433,43 +431,49 @@ class _ServerEditPageState extends State<ServerEditPage> with AfterLayoutMixin {
}

Widget _buildJumpServer() {
return ListenableBuilder(
listenable: _jumpServer,
builder: (_, __) {
final children = ServerProvider.servers.values
.map((e) => e.value)
.where((e) => e.spi.jumpId == null)
.where((e) => e.spi.id != widget.spi?.id)
.map(
(e) => ListTile(
title: Text(e.spi.name),
subtitle: Text(e.spi.id, style: UIs.textGrey),
trailing: Radio<String>(
groupValue: _jumpServer.value,
value: e.spi.id,
onChanged: (val) => _jumpServer.value = val,
),
onTap: () => _jumpServer.value = e.spi.id,
contentPadding: const EdgeInsets.symmetric(horizontal: 17),
),
)
.toList();
children.add(ListTile(
title: Text(libL10n.clear),
trailing: const Icon(Icons.clear),
onTap: () => _jumpServer.value = null,
contentPadding: const EdgeInsets.symmetric(horizontal: 17),
));
return CardX(
child: ExpandTile(
leading: const Icon(Icons.map),
initiallyExpanded: _jumpServer.value != null,
title: Text(l10n.jumpServer),
children: children,
const padding = EdgeInsets.only(left: 13, right: 13, bottom: 7);
final srvs = ServerProvider.servers.values
.map((e) => e.value)
.where((e) => e.spi.jumpId == null)
.where((e) => e.spi.id != widget.spi?.id)
.toList();
final choice = _jumpServer.listenVal(
(val) {
final srv = srvs.firstWhereOrNull((e) => e.id == _jumpServer.value);
return Choice<Server>(
multiple: false,
clearable: true,
value: srv != null ? [srv] : [],
builder: (state, _) => Wrap(
children: List<Widget>.generate(
srvs.length,
(index) {
final item = srvs[index];
return ChoiceChipX<Server>(
label: item.spi.name,
state: state,
value: item,
onSelected: (srv, on) {
if (on) {
_jumpServer.value = srv.spi.id;
} else {
_jumpServer.value = null;
}
},
);
},
),
),
);
},
);
return ExpandTile(
leading: const Icon(Icons.map),
initiallyExpanded: _jumpServer.value != null,
childrenPadding: padding,
title: Text(l10n.jumpServer),
children: [choice],
).cardx;
}

void _onSave() async {
Expand Down
54 changes: 26 additions & 28 deletions lib/view/page/server/tab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class _ServerPageState extends State<ServerPage>

Timer? _timer;

String? _tag;
final _tag = ''.vn;
bool _useDoubleColumn = false;

final _scrollController = ScrollController();
Expand Down Expand Up @@ -84,7 +84,11 @@ class _ServerPageState extends State<ServerPage>

Widget _buildPortrait() {
return Scaffold(
appBar: _buildTagsSwitcher(),
appBar: TagSwitcher(
tags: ServerProvider.tags,
onTagChanged: (p0) => _tag.value = p0,
tag: _tag,
),
body: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () => _autoHideKey.currentState?.show(),
Expand Down Expand Up @@ -182,36 +186,26 @@ class _ServerPageState extends State<ServerPage>
Widget _buildBody() {
return ServerProvider.serverOrder.listenVal(
(order) {
if (!ServerProvider.tags.value.contains(_tag)) {
_tag = null;
}
if (order.isEmpty) {
return Center(
child: Text(libL10n.empty, textAlign: TextAlign.center),
);
}

final filtered = _filterServers(order);
if (_useDoubleColumn &&
Stores.setting.doubleColumnServersPage.fetch()) {
return _buildBodyMedium(filtered);
}
return _buildBodySmall(filtered: filtered);
return _tag.listenVal(
(val) {
final filtered = _filterServers(order);
if (_useDoubleColumn &&
Stores.setting.doubleColumnServersPage.fetch()) {
return _buildBodyMedium(filtered);
}
return _buildBodySmall(filtered: filtered);
},
);
},
);
}

TagSwitcher _buildTagsSwitcher() {
return TagSwitcher(
tags: ServerProvider.tags,
width: _media.size.width,
onTagChanged: (p0) => setState(() {
_tag = p0;
}),
initTag: _tag,
);
}

Widget _buildBodySmall({
required List<String> filtered,
EdgeInsets? padding = const EdgeInsets.fromLTRB(7, 0, 7, 7),
Expand Down Expand Up @@ -259,7 +253,7 @@ class _ServerPageState extends State<ServerPage>
}

return CardX(
key: Key(srv.spi.id + (_tag ?? '')),
key: Key(srv.spi.id + _tag.value),
child: InkWell(
onTap: () {
if (srv.canViewDetails) {
Expand Down Expand Up @@ -651,11 +645,15 @@ ${ss.err?.message ?? 'null'}
ServerProvider.startAutoRefresh();
}

List<String> _filterServers(List<String> order) => order
.where((e) =>
_tag == null ||
(ServerProvider.pick(id: e)?.value.spi.tags?.contains(_tag) ?? false))
.toList();
List<String> _filterServers(List<String> order) {
final tag = _tag.value;
if (tag.isEmpty) return order;
return order.where((e) {
final tags = ServerProvider.pick(id: e)?.value.spi.tags;
if (tags == null) return false;
return tags.contains(tag);
}).toList();
}

static const _kCardHeightMin = 23.0;
static const _kCardHeightFlip = 99.0;
Expand Down
16 changes: 8 additions & 8 deletions lib/view/page/setting/entry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class _SettingPageState extends State<SettingPage> {
title: libL10n.setting,
items: List.generate(10, (idx) => idx == 1 ? null : idx),
initial: _setting.serverStatusUpdateInterval.fetch(),
name: (p0) => p0 == 0 ? l10n.manual : '$p0 ${l10n.second}',
display: (p0) => p0 == 0 ? l10n.manual : '$p0 ${l10n.second}',
);
if (val != null) {
_setting.serverStatusUpdateInterval.put(val);
Expand Down Expand Up @@ -331,7 +331,7 @@ class _SettingPageState extends State<SettingPage> {
final selected = await context.showPickSingleDialog(
title: l10n.maxRetryCount,
items: List.generate(10, (index) => index),
name: (p0) => '$p0 ${l10n.times}',
display: (p0) => '$p0 ${l10n.times}',
initial: val,
);
if (selected != null) {
Expand All @@ -356,7 +356,7 @@ class _SettingPageState extends State<SettingPage> {
final selected = await context.showPickSingleDialog(
title: libL10n.themeMode,
items: List.generate(len + 2, (index) => index),
name: (p0) => _buildThemeModeStr(p0),
display: (p0) => _buildThemeModeStr(p0),
initial: _setting.themeMode.fetch(),
);
if (selected != null) {
Expand Down Expand Up @@ -503,7 +503,7 @@ class _SettingPageState extends State<SettingPage> {
final selected = await context.showPickSingleDialog(
title: libL10n.language,
items: AppLocalizations.supportedLocales,
name: (p0) => p0.nativeName,
display: (p0) => p0.nativeName,
initial: _setting.locale.fetch().toLocale,
);
if (selected != null) {
Expand Down Expand Up @@ -543,7 +543,7 @@ class _SettingPageState extends State<SettingPage> {
final selected = await context.showPickSingleDialog(
title: l10n.theme,
items: themeMap.keys.toList(),
name: (p0) => p0,
display: (p0) => p0,
initial: _setting.editorTheme.fetch(),
);
if (selected != null) {
Expand All @@ -565,7 +565,7 @@ class _SettingPageState extends State<SettingPage> {
final selected = await context.showPickSingleDialog(
title: l10n.theme,
items: themeMap.keys.toList(),
name: (p0) => p0,
display: (p0) => p0,
initial: _setting.editorDarkTheme.fetch(),
);
if (selected != null) {
Expand Down Expand Up @@ -688,7 +688,7 @@ class _SettingPageState extends State<SettingPage> {
final selected = await context.showPickSingleDialog(
title: l10n.netViewType,
items: NetViewType.values,
name: (p0) => p0.toStr,
display: (p0) => p0.toStr,
initial: _setting.netViewType.fetch(),
);
if (selected != null) {
Expand Down Expand Up @@ -997,7 +997,7 @@ class _SettingPageState extends State<SettingPage> {
final selected = await context.showPickSingleDialog(
title: l10n.theme,
items: List.generate(3, (index) => index),
name: (p0) => index2Str(p0),
display: (p0) => index2Str(p0),
initial: _setting.termTheme.fetch(),
);
if (selected != null) {
Expand Down
2 changes: 1 addition & 1 deletion lib/view/page/snippet/edit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class _SnippetEditPageState extends State<SnippetEditPage>
final serverIds = await context.showPickDialog(
title: l10n.autoRun,
items: ServerProvider.serverOrder.value,
name: (e) => ServerProvider.pick(id: e)?.value.spi.name ?? e,
display: (e) => ServerProvider.pick(id: e)?.value.spi.name ?? e,
initial: vals,
clearable: true,
);
Expand Down
Loading

0 comments on commit 7558b48

Please sign in to comment.