An open-source cross-platform alternative to AirDrop
refactor: create receive tab vm
Tien Do Namcb2247c17b4d
diff --git a/app/lib/pages/tabs/receive_tab.dart b/app/lib/pages/tabs/receive_tab.dartindex d616c243..839f85f7 100644--- a/app/lib/pages/tabs/receive_tab.dart+++ b/app/lib/pages/tabs/receive_tab.dart@@ -2,43 +2,24 @@ import 'package:flutter/material.dart';import 'package:localsend_app/gen/strings.g.dart';import 'package:localsend_app/pages/home_page.dart';import 'package:localsend_app/pages/receive_history_page.dart';+import 'package:localsend_app/pages/tabs/receive_tab_vm.dart';import 'package:localsend_app/provider/animation_provider.dart';-import 'package:localsend_app/provider/local_ip_provider.dart';-import 'package:localsend_app/provider/network/server/server_provider.dart';-import 'package:localsend_app/provider/settings_provider.dart';import 'package:localsend_app/provider/ui/home_tab_provider.dart';import 'package:localsend_app/util/ip_helper.dart';-import 'package:localsend_app/util/sleep.dart';import 'package:localsend_app/widget/animations/initial_fade_transition.dart';import 'package:localsend_app/widget/custom_icon_button.dart';-import 'package:localsend_app/widget/dialogs/quick_save_notice.dart';import 'package:localsend_app/widget/local_send_logo.dart';import 'package:localsend_app/widget/responsive_list_view.dart';import 'package:localsend_app/widget/rotating_widget.dart';import 'package:refena_flutter/refena_flutter.dart';import 'package:routerino/routerino.dart';-class ReceiveTab extends StatefulWidget {- const ReceiveTab({Key? key}) : super(key: key);-- @override- State<ReceiveTab> createState() => _ReceiveTagState();-}--class _ReceiveTagState extends State<ReceiveTab> with AutomaticKeepAliveClientMixin, Refena {- bool _advanced = false;- bool _showHistoryButton = true;-- @override- bool get wantKeepAlive => true;+class ReceiveTab extends StatelessWidget {+ const ReceiveTab();@overrideWidget build(BuildContext context) {- super.build(context);-- final settings = ref.watch(settingsProvider);- final networkInfo = ref.watch(localIpProvider);- final serverState = ref.watch(serverProvider);+ final vm = context.ref.watch(receiveTabVmProvider);return Stack(children: [@@ -65,20 +46,20 @@ class _ReceiveTagState extends State<ReceiveTab> with AutomaticKeepAliveClientMifinal activeTab = ref.watch(homeTabProvider);return RotatingWidget(duration: const Duration(seconds: 15),- spinning: serverState != null && animations && activeTab == HomeTab.receive,+ spinning: vm.serverState != null && animations && activeTab == HomeTab.receive,child: const LocalSendLogo(withText: false),);}),),FittedBox(fit: BoxFit.scaleDown,- child: Text(serverState?.alias ?? settings.alias, style: const TextStyle(fontSize: 48)),+ child: Text(vm.serverState?.alias ?? vm.aliasSettings, style: const TextStyle(fontSize: 48)),),InitialFadeTransition(duration: const Duration(milliseconds: 300),delay: const Duration(milliseconds: 500),child: Text(- serverState == null ? t.general.offline : networkInfo.localIps.map((ip) => '#${ip.visualId}').toSet().join(' '),+ vm.serverState == null ? t.general.offline : vm.localIps.map((ip) => '#${ip.visualId}').toSet().join(' '),style: const TextStyle(fontSize: 24),textAlign: TextAlign.center,),@@ -92,25 +73,20 @@ class _ReceiveTagState extends State<ReceiveTab> with AutomaticKeepAliveClientMiPadding(padding: const EdgeInsets.only(top: 10),child: Center(- child: settings.quickSave+ child: vm.quickSaveSettings? ElevatedButton(style: ElevatedButton.styleFrom(backgroundColor: Theme.of(context).buttonTheme.colorScheme!.primary,foregroundColor: Theme.of(context).buttonTheme.colorScheme!.onPrimary,),- onPressed: () async => ref.notifier(settingsProvider).setQuickSave(false),+ onPressed: () async => vm.onSetQuickSave(context, false),child: Text('${t.general.quickSave}: ${t.general.on}'),): TextButton(style: TextButton.styleFrom(foregroundColor: Colors.grey,),- onPressed: () async {- await ref.notifier(settingsProvider).setQuickSave(true);- if (mounted) {- await QuickSaveNotice.open(context);- }- },+ onPressed: () async => vm.onSetQuickSave(context, true),child: Text('${t.general.quickSave}: ${t.general.off}'),),),@@ -122,7 +98,7 @@ class _ReceiveTagState extends State<ReceiveTab> with AutomaticKeepAliveClientMi),),AnimatedCrossFade(- crossFadeState: _advanced ? CrossFadeState.showSecond : CrossFadeState.showFirst,+ crossFadeState: vm.showAdvanced ? CrossFadeState.showSecond : CrossFadeState.showFirst,duration: const Duration(milliseconds: 200),firstChild: Container(),secondChild: Align(@@ -145,7 +121,7 @@ class _ReceiveTagState extends State<ReceiveTab> with AutomaticKeepAliveClientMiconst SizedBox(width: 10),Padding(padding: const EdgeInsets.only(right: 30),- child: SelectableText(serverState?.alias ?? '-'),+ child: SelectableText(vm.serverState?.alias ?? '-'),),],),@@ -156,8 +132,8 @@ class _ReceiveTagState extends State<ReceiveTab> with AutomaticKeepAliveClientMiColumn(crossAxisAlignment: CrossAxisAlignment.start,children: [- if (networkInfo.localIps.isEmpty) Text(t.general.unknown),- ...networkInfo.localIps.map((ip) => SelectableText(ip)),+ if (vm.localIps.isEmpty) Text(t.general.unknown),+ ...vm.localIps.map((ip) => SelectableText(ip)),],),],@@ -166,7 +142,7 @@ class _ReceiveTagState extends State<ReceiveTab> with AutomaticKeepAliveClientMichildren: [Text(t.receiveTab.infoBox.port),const SizedBox(width: 10),- SelectableText(serverState?.port.toString() ?? '-'),+ SelectableText(vm.serverState?.port.toString() ?? '-'),],),],@@ -183,9 +159,9 @@ class _ReceiveTagState extends State<ReceiveTab> with AutomaticKeepAliveClientMichild: Row(mainAxisAlignment: MainAxisAlignment.end,children: [- if (!_advanced)+ if (!vm.showAdvanced)AnimatedOpacity(- opacity: _showHistoryButton ? 1 : 0,+ opacity: vm.showHistoryButton ? 1 : 0,duration: const Duration(milliseconds: 200),child: CustomIconButton(onPressed: () async {@@ -196,20 +172,7 @@ class _ReceiveTagState extends State<ReceiveTab> with AutomaticKeepAliveClientMi),CustomIconButton(key: const ValueKey('info-btn'),- onPressed: () async {- if (_advanced) {- setState(() => _advanced = false);- await sleepAsync(200);- if (mounted) {- setState(() => _showHistoryButton = true);- }- } else {- setState(() {- _advanced = true;- _showHistoryButton = false;- });- }- },+ onPressed: vm.toggleAdvanced,child: const Icon(Icons.info),),],diff --git a/app/lib/pages/tabs/receive_tab_vm.dart b/app/lib/pages/tabs/receive_tab_vm.dartnew file mode 100644index 00000000..d0433643--- /dev/null+++ b/app/lib/pages/tabs/receive_tab_vm.dart@@ -0,0 +1,70 @@+import 'package:flutter/material.dart';+import 'package:localsend_app/model/state/server/server_state.dart';+import 'package:localsend_app/provider/local_ip_provider.dart';+import 'package:localsend_app/provider/network/server/server_provider.dart';+import 'package:localsend_app/provider/settings_provider.dart';+import 'package:localsend_app/util/sleep.dart';+import 'package:localsend_app/widget/dialogs/quick_save_notice.dart';+import 'package:refena_flutter/refena_flutter.dart';++/// Whether the advanced network info is shown+final _showAdvancedProvider = StateProvider<bool>((ref) => false);++/// Whether the history button is shown+/// This extra boolean is needed to delay the animation+final _showHistoryButtonProvider = StateProvider<bool>((ref) => true);++class ReceiveTabVm {+ final String aliasSettings;+ final bool quickSaveSettings;+ final ServerState? serverState;+ final List<String> localIps;+ final bool showAdvanced;+ final bool showHistoryButton;+ final Future<void> Function() toggleAdvanced;+ final Future<void> Function(BuildContext context, bool enable) onSetQuickSave;++ const ReceiveTabVm({+ required this.aliasSettings,+ required this.quickSaveSettings,+ required this.serverState,+ required this.localIps,+ required this.showAdvanced,+ required this.showHistoryButton,+ required this.toggleAdvanced,+ required this.onSetQuickSave,+ });+}++final receiveTabVmProvider = ViewProvider((ref) {+ final (alias, quickSave) = ref.watch(settingsProvider.select((s) => (s.alias, s.quickSave)));+ final networkInfo = ref.watch(localIpProvider).localIps;+ final serverState = ref.watch(serverProvider);+ final showAdvanced = ref.watch(_showAdvancedProvider);+ final showHistoryButton = ref.watch(_showHistoryButtonProvider);++ return ReceiveTabVm(+ aliasSettings: alias,+ quickSaveSettings: quickSave,+ serverState: serverState,+ localIps: networkInfo,+ showAdvanced: showAdvanced,+ showHistoryButton: showHistoryButton,+ toggleAdvanced: () async {+ if (showAdvanced) {+ ref.notifier(_showAdvancedProvider).setState((_) => false);+ await sleepAsync(200);+ ref.notifier(_showHistoryButtonProvider).setState((_) => true);+ } else {+ ref.notifier(_showAdvancedProvider).setState((_) => true);+ ref.notifier(_showHistoryButtonProvider).setState((_) => false);+ }+ },+ onSetQuickSave: (context, enable) async {+ await ref.notifier(settingsProvider).setQuickSave(enable);+ if (enable && context.mounted) {+ await QuickSaveNotice.open(context);+ }+ },+ );+});