|
| 1 | +import 'dart:math'; |
| 2 | + |
1 | 3 | import 'package:collection/collection.dart'; |
2 | 4 | import 'package:dpip/api/model/crowdin/localization_progress.dart'; |
3 | 5 | import 'package:dpip/core/i18n.dart'; |
4 | 6 | import 'package:dpip/global.dart'; |
5 | 7 | import 'package:dpip/models/settings/ui.dart'; |
6 | 8 | import 'package:dpip/utils/extensions/build_context.dart'; |
| 9 | +import 'package:dpip/utils/extensions/color_scheme.dart'; |
7 | 10 | import 'package:dpip/utils/extensions/locale.dart'; |
8 | 11 | import 'package:dpip/widgets/list/segmented_list.dart'; |
9 | 12 | import 'package:flutter/material.dart'; |
10 | 13 | import 'package:go_router/go_router.dart'; |
11 | 14 | import 'package:i18n_extension/i18n_extension.dart'; |
12 | 15 | import 'package:intl/intl.dart'; |
| 16 | +import 'package:m3e_collection/m3e_collection.dart'; |
13 | 17 | import 'package:material_symbols_icons/material_symbols_icons.dart'; |
14 | | -import 'package:provider/provider.dart'; |
| 18 | +import 'package:option_result/result.dart'; |
15 | 19 |
|
16 | 20 | class SettingsLocaleSelectPage extends StatefulWidget { |
17 | 21 | const SettingsLocaleSelectPage({super.key}); |
18 | 22 |
|
19 | | - static const route = '/settings/locale/select'; |
20 | | - |
21 | 23 | @override |
22 | 24 | State<StatefulWidget> createState() => _SettingsLocaleSelectPageState(); |
23 | 25 | } |
24 | 26 |
|
25 | | -class _SettingsLocaleSelectPageState extends State<SettingsLocaleSelectPage> { |
26 | | - List<CrowdinLocalizationProgress> progress = []; |
| 27 | +class _SettingsLocaleSelectPageState extends State<SettingsLocaleSelectPage> |
| 28 | + with TickerProviderStateMixin { |
| 29 | + late final _animationController = |
| 30 | + AnimationController( |
| 31 | + vsync: this, |
| 32 | + duration: const Duration(milliseconds: 1200), |
| 33 | + ) |
| 34 | + ..addListener(() { |
| 35 | + if (mounted) setState(() {}); |
| 36 | + }) |
| 37 | + ..repeat(); |
| 38 | + |
| 39 | + Result<List<CrowdinLocalizationProgress>, String>? progress; |
27 | 40 | List<Locale> localeList = I18n.supportedLocales |
28 | 41 | .where((e) => !['zh'].contains(e.toLanguageTag())) |
29 | 42 | .toList(); |
30 | 43 |
|
| 44 | + Future<void> _refresh() async { |
| 45 | + final result = await Global.api.getLocalizationProgress(); |
| 46 | + setState(() => progress = result); |
| 47 | + } |
| 48 | + |
31 | 49 | @override |
32 | 50 | void initState() { |
33 | 51 | super.initState(); |
34 | | - Global.api.getLocalizationProgress().then((value) { |
35 | | - setState(() => progress = value); |
36 | | - }); |
| 52 | + _refresh(); |
37 | 53 | } |
38 | 54 |
|
39 | 55 | @override |
40 | 56 | Widget build(BuildContext context) { |
41 | | - return ListView( |
42 | | - padding: EdgeInsets.only(top: 8, bottom: 16 + context.padding.bottom), |
43 | | - children: [ |
44 | | - SegmentedList( |
45 | | - label: Text('選擇語言'.i18n), |
46 | | - children: [ |
47 | | - for (final (index, item) in localeList.indexed) |
48 | | - Selector<SettingsUserInterfaceModel, Locale?>( |
49 | | - selector: (_, model) => model.locale, |
50 | | - builder: (context, locale, child) { |
51 | | - final p = progress.firstWhereOrNull( |
52 | | - (e) => e.id == item.toLanguageTag(), |
53 | | - ); |
54 | | - |
55 | | - final translated = p != null |
56 | | - ? NumberFormat('#.#%').format(p.translation / 100) |
57 | | - : '...'; |
58 | | - final approved = p != null |
59 | | - ? NumberFormat('#.#%').format(p.approval / 100) |
60 | | - : '...'; |
61 | | - |
62 | | - final isSelected = |
63 | | - item.toLanguageTag() == locale?.toLanguageTag(); |
64 | | - |
65 | | - return SegmentedListTile( |
66 | | - isFirst: index == 0, |
67 | | - isLast: index == localeList.length - 1, |
68 | | - title: Text(item.nativeName), |
69 | | - subtitle: (item.toLanguageTag() != 'zh-Hant') |
70 | | - ? Column( |
71 | | - crossAxisAlignment: CrossAxisAlignment.start, |
72 | | - children: [ |
73 | | - Text( |
74 | | - '已翻譯 {translated}・已校對 {approved}'.i18n.args({ |
75 | | - 'translated': translated, |
76 | | - 'approved': approved, |
77 | | - }), |
78 | | - ), |
79 | | - Padding( |
80 | | - padding: const EdgeInsets.symmetric( |
81 | | - vertical: 4, |
82 | | - ), |
83 | | - child: p != null |
84 | | - ? Stack( |
85 | | - children: [ |
86 | | - LinearProgressIndicator( |
87 | | - value: p.translation / 100, |
88 | | - color: Colors.blue, |
89 | | - ), |
90 | | - LinearProgressIndicator( |
91 | | - value: p.approval / 100, |
92 | | - color: Colors.lightGreen, |
93 | | - backgroundColor: Colors.transparent, |
94 | | - ), |
95 | | - ], |
96 | | - ) |
97 | | - : const LinearProgressIndicator(), |
98 | | - ), |
99 | | - ], |
100 | | - ) |
101 | | - : Text('來源語言'.i18n), |
102 | | - leading: Container( |
103 | | - height: 28, |
104 | | - width: 40, |
105 | | - decoration: BoxDecoration( |
106 | | - color: context.colors.secondaryContainer, |
107 | | - borderRadius: BorderRadius.circular(6), |
108 | | - ), |
109 | | - child: Center( |
110 | | - child: Text( |
111 | | - item.iconLabel, |
112 | | - style: context.texts.labelLarge?.copyWith( |
113 | | - color: context.colors.onSecondaryContainer, |
114 | | - height: 1, |
115 | | - ), |
116 | | - ), |
117 | | - ), |
| 57 | + final locale = context.useUserInterface.locale; |
| 58 | + final data = switch (progress) { |
| 59 | + Ok(:final value)? => value, |
| 60 | + _ => null, |
| 61 | + }; |
| 62 | + |
| 63 | + final phaseValue = _animationController.value * 2 * pi; |
| 64 | + |
| 65 | + final children = localeList.mapIndexed((index, item) { |
| 66 | + final p = data?.firstWhereOrNull( |
| 67 | + (e) => e.id == item.toLanguageTag(), |
| 68 | + ); |
| 69 | + |
| 70 | + final translated = p != null |
| 71 | + ? NumberFormat('#.#%').format(p.translation / 100) |
| 72 | + : '...'; |
| 73 | + final approved = p != null |
| 74 | + ? NumberFormat('#.#%').format(p.approval / 100) |
| 75 | + : '...'; |
| 76 | + |
| 77 | + final isSelected = item.toLanguageTag() == locale?.toLanguageTag(); |
| 78 | + |
| 79 | + final progressBar = IgnorePointer( |
| 80 | + child: SizedBox( |
| 81 | + height: 10, |
| 82 | + child: switch (p) { |
| 83 | + null => LinearProgressIndicatorM3E( |
| 84 | + size: .s, |
| 85 | + phase: phaseValue, |
| 86 | + ), |
| 87 | + _ => SliderTheme( |
| 88 | + data: SliderThemeData( |
| 89 | + thumbSize: .all(.zero), |
| 90 | + trackGap: 1, |
| 91 | + trackHeight: 5, |
| 92 | + padding: .zero, |
| 93 | + year2023: false, |
| 94 | + ), |
| 95 | + child: Slider( |
| 96 | + activeColor: context.theme.extendedColors.green, |
| 97 | + secondaryActiveColor: context.theme.extendedColors.blue, |
| 98 | + value: p.approval / 100, |
| 99 | + secondaryTrackValue: p.translation / 100, |
| 100 | + onChanged: (_) {}, |
| 101 | + ), |
| 102 | + ), |
| 103 | + }, |
| 104 | + ), |
| 105 | + ); |
| 106 | + |
| 107 | + return SegmentedListTile( |
| 108 | + isFirst: index == 0, |
| 109 | + isLast: index == localeList.length - 1, |
| 110 | + title: Text(item.nativeName), |
| 111 | + subtitle: (item.toLanguageTag() != 'zh-Hant') |
| 112 | + ? Column( |
| 113 | + crossAxisAlignment: .start, |
| 114 | + children: [ |
| 115 | + Text( |
| 116 | + '已翻譯 {translated}・已校對 {approved}'.i18n.args({ |
| 117 | + 'translated': translated, |
| 118 | + 'approved': approved, |
| 119 | + }), |
| 120 | + ), |
| 121 | + RepaintBoundary( |
| 122 | + child: Padding( |
| 123 | + padding: const .symmetric(vertical: 4), |
| 124 | + child: progressBar, |
118 | 125 | ), |
119 | | - trailing: Icon(isSelected ? Symbols.check_rounded : null), |
120 | | - onTap: () { |
121 | | - context.locale = item; |
122 | | - context.read<SettingsUserInterfaceModel>().setLocale( |
123 | | - item, |
124 | | - ); |
125 | | - context.pop(); |
126 | | - }, |
127 | | - ); |
128 | | - }, |
| 126 | + ), |
| 127 | + ], |
| 128 | + ) |
| 129 | + : Text('來源語言'.i18n), |
| 130 | + leading: Container( |
| 131 | + height: 28, |
| 132 | + width: 40, |
| 133 | + decoration: BoxDecoration( |
| 134 | + color: context.colors.secondaryContainer, |
| 135 | + borderRadius: .circular(6), |
| 136 | + ), |
| 137 | + child: Center( |
| 138 | + child: Text( |
| 139 | + item.iconLabel, |
| 140 | + style: context.texts.labelLarge?.copyWith( |
| 141 | + color: context.colors.onSecondaryContainer, |
| 142 | + height: 1, |
129 | 143 | ), |
130 | | - ], |
| 144 | + ), |
| 145 | + ), |
131 | 146 | ), |
132 | | - ], |
| 147 | + trailing: Icon(isSelected ? Symbols.check_rounded : null), |
| 148 | + onTap: () { |
| 149 | + context.locale = item; |
| 150 | + context.userInterface.setLocale(item); |
| 151 | + context.pop(); |
| 152 | + }, |
| 153 | + ); |
| 154 | + }).toList(); |
| 155 | + |
| 156 | + return ExpressiveRefreshIndicator.contained( |
| 157 | + onRefresh: _refresh, |
| 158 | + backgroundColor: context.colors.primaryContainer, |
| 159 | + child: ListView( |
| 160 | + padding: EdgeInsets.only(top: 8, bottom: 16 + context.padding.bottom), |
| 161 | + children: [ |
| 162 | + SegmentedList( |
| 163 | + label: Text('選擇語言'.i18n), |
| 164 | + children: children, |
| 165 | + ), |
| 166 | + ], |
| 167 | + ), |
133 | 168 | ); |
134 | 169 | } |
| 170 | + |
| 171 | + @override |
| 172 | + void dispose() { |
| 173 | + _animationController.dispose(); |
| 174 | + super.dispose(); |
| 175 | + } |
135 | 176 | } |
0 commit comments