Skip to content

Commit

Permalink
refactor: localization maps for stepbars
Browse files Browse the repository at this point in the history
  • Loading branch information
parodyBit committed Oct 25, 2023
1 parent f6f5bfb commit 0873740
Show file tree
Hide file tree
Showing 9 changed files with 524 additions and 508 deletions.
36 changes: 35 additions & 1 deletion lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:my_wit_wallet/screens/create_wallet/bloc/create_wallet_bloc.dart';
import 'package:my_wit_wallet/screens/dashboard/view/dashboard_screen.dart';
import 'package:my_wit_wallet/screens/preferences/preferences_screen.dart';
import 'package:my_wit_wallet/screens/send_transaction/send_vtt_screen.dart';
import 'package:my_wit_wallet/util/storage/database/wallet.dart';
import 'package:my_wit_wallet/globals.dart' as globals;
import 'package:witnet/data_structures.dart';

typedef String LocalizationCallback(String value);

Expand All @@ -16,6 +20,36 @@ Map<WalletType, String> walletTypeToLabel(BuildContext context) => {
WalletType.single: AppLocalizations.of(context)!.walletTypeNodeLabel,
};

Map<DashboardViewSteps, String> localizedDashboardSteps = {
DashboardViewSteps.transactions:
localization.dashboardViewSteps('transactions'),
DashboardViewSteps.stats: localization.dashboardViewSteps('stats'),
};

Map<VTTsteps, String> localizedVTTsteps = {
VTTsteps.Transaction: localization.vttSendSteps('Transaction'),
VTTsteps.MinerFee: localization.vttSendSteps('MinerFee'),
VTTsteps.Review: localization.vttSendSteps('Review'),
};
Map<EstimatedFeeOptions, String> localizedFeeOptions = {
EstimatedFeeOptions.Stinky: localization.estimatedFeeOptions('stinky'),
EstimatedFeeOptions.Low: localization.estimatedFeeOptions('low'),
EstimatedFeeOptions.Medium: localization.estimatedFeeOptions('medium'),
EstimatedFeeOptions.High: localization.estimatedFeeOptions('high'),
EstimatedFeeOptions.Opulent: localization.estimatedFeeOptions('opulent'),
EstimatedFeeOptions.Custom: localization.estimatedFeeOptions('custom'),
};

Map<FeeType, String> localizedFeeTypeOptions = {
FeeType.Absolute: localization.feeTypeOptions('absolute'),
FeeType.Weighted: localization.feeTypeOptions('weighted'),
};

Map<ConfigSteps, String> localizedConfigSteps = {
ConfigSteps.general: localization.preferenceTabs('general'),
ConfigSteps.wallet: localization.preferenceTabs('wallet'),
};

/// Explorer Settings
const bool USE_EXPLORER_DEV = false;
// ignore: non_constant_identifier_names
Expand Down Expand Up @@ -59,7 +93,7 @@ Map<EstimatedFeeOptions, String?> DEFAULT_MINER_FEE_OPTIONS = {
EstimatedFeeOptions.Medium: '0',
EstimatedFeeOptions.High: '0',
EstimatedFeeOptions.Opulent: '0',
EstimatedFeeOptions.Custom: null,
EstimatedFeeOptions.Custom: '1',
};

Map<ImportOrigin, String> importOriginToLabel(BuildContext context) {
Expand Down
385 changes: 197 additions & 188 deletions lib/l10n/app_en.arb

Large diffs are not rendered by default.

375 changes: 190 additions & 185 deletions lib/l10n/app_es.arb

Large diffs are not rendered by default.

31 changes: 11 additions & 20 deletions lib/screens/dashboard/view/dashboard_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ enum DashboardViewSteps {
stats,
}

Map<String, DashboardViewSteps> _localizedDashboardSteps(BuildContext context) {
return {
localization.dashboardViewSteps('transactions'):
DashboardViewSteps.transactions,
localization.dashboardViewSteps('stats'): DashboardViewSteps.stats,
};
}

class DashboardScreen extends StatefulWidget {
static final route = '/dashboard';
@override
Expand All @@ -49,8 +41,8 @@ class DashboardScreenState extends State<DashboardScreen>
ApiDatabase database = Locator.instance.get<ApiDatabase>();
ScrollController scrollController = ScrollController(keepScrollOffset: true);
ExplorerBloc? explorerBlock;

String? selectedItem;
String selectedItem =
localizedDashboardSteps[DashboardViewSteps.transactions]!;

@override
void initState() {
Expand Down Expand Up @@ -133,8 +125,8 @@ class DashboardScreenState extends State<DashboardScreen>
}

Widget buildMainDashboardContent(ThemeData theme) {
if (_localizedDashboardSteps(context)[selectedItem]! ==
DashboardViewSteps.transactions) {
if (localizedDashboardSteps[DashboardViewSteps.transactions]! ==
selectedItem) {
return buildTransactionsView();
}
return Stats(currentWallet: currentWallet!);
Expand All @@ -151,13 +143,16 @@ class DashboardScreenState extends State<DashboardScreen>
crossAxisAlignment: CrossAxisAlignment.start,
children: [
StepBar(
selectedItem: selectedItem ??
_localizedDashboardSteps(context).keys.first,
listItems: _localizedDashboardSteps(context).keys.toList(),
selectedItem: selectedItem,
listItems: localizedDashboardSteps.values.toList(),
actionable: true,
onChanged: (item) => {
scrollController.jumpTo(0.0),
setState(() => selectedItem = item),
setState(
() => selectedItem = localizedDashboardSteps.entries
.firstWhere((element) => element.value == item)
.value,
),
}),
SizedBox(height: 16),
buildMainDashboardContent(theme)
Expand All @@ -168,10 +163,6 @@ class DashboardScreenState extends State<DashboardScreen>

@override
Widget build(BuildContext context) {
if (selectedItem == null) {
selectedItem = _localizedDashboardSteps(context).keys.first;
}

return BlocConsumer<ExplorerBloc, ExplorerState>(
builder: (BuildContext context, ExplorerState state) {
return DashboardLayout(
Expand Down
7 changes: 4 additions & 3 deletions lib/screens/preferences/export_signed_msg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:convert';
import 'package:dart_date/dart_date.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:my_wit_wallet/constants.dart';
import 'package:my_wit_wallet/util/storage/path_provider_interface.dart';
import 'package:my_wit_wallet/widgets/PaddedButton.dart';
import 'package:my_wit_wallet/widgets/dashed_rect.dart';
Expand Down Expand Up @@ -63,7 +64,7 @@ class ExportSignMessageState extends State<ExportSignMessage> {
}),
SizedBox(height: 16),
PaddedButton(
text: 'Export JSON',
text: localization.exportJson,
type: ButtonType.primary,
isLoading: false,
padding: EdgeInsets.only(bottom: 8),
Expand All @@ -73,7 +74,7 @@ class ExportSignMessageState extends State<ExportSignMessage> {
),
SizedBox(height: 8),
PaddedButton(
text: 'Copy JSON',
text: localization.copyJson,
type: ButtonType.secondary,
isLoading: false,
padding: EdgeInsets.only(bottom: 16),
Expand All @@ -83,7 +84,7 @@ class ExportSignMessageState extends State<ExportSignMessage> {
if (await Clipboard.hasStrings()) {
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context)
.showSnackBar(buildCopiedSnackbar(theme, 'JSON copied!'));
.showSnackBar(buildCopiedSnackbar(theme, localization.jsonCopied));
}
},
),
Expand Down
34 changes: 14 additions & 20 deletions lib/screens/preferences/preferences_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,11 @@ enum ConfigSteps {
wallet,
}

Map<String, ConfigSteps> _localizedConfigSteps(BuildContext context) {
return {
localization.preferenceTabs('general'): ConfigSteps.general,
localization.preferenceTabs('wallet'): ConfigSteps.wallet,
};
}

class _PreferencePageState extends State<PreferencePage> {
ScrollController scrollController = ScrollController(keepScrollOffset: false);

String? selectedItem;

ConfigSteps currentStep = ConfigSteps.general;
String selectedItem = localizedConfigSteps[ConfigSteps.general]!;
@override
void initState() {
super.initState();
Expand All @@ -41,14 +34,9 @@ class _PreferencePageState extends State<PreferencePage> {

Widget _buildConfigView() {
Widget view = GeneralConfig();
if (selectedItem == null) {
selectedItem = _localizedConfigSteps(context).keys.first;
view = GeneralConfig();
} else if (_localizedConfigSteps(context)[selectedItem]! ==
ConfigSteps.general) {
if (localizedConfigSteps[ConfigSteps.general] == selectedItem) {
view = GeneralConfig();
} else if (_localizedConfigSteps(context)[selectedItem]! ==
ConfigSteps.wallet) {
} else if (localizedConfigSteps[ConfigSteps.wallet] == selectedItem) {
view = WalletConfig(scrollController: scrollController);
} else {
return GeneralConfig();
Expand All @@ -58,13 +46,19 @@ class _PreferencePageState extends State<PreferencePage> {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
StepBar(
selectedItem:
selectedItem ?? _localizedConfigSteps(context).keys.first,
listItems: _localizedConfigSteps(context).keys.toList(),
selectedItem: selectedItem,
listItems: localizedConfigSteps.values.toList(),
actionable: true,
onChanged: (item) => {
scrollController.jumpTo(0.0),
setState(() => selectedItem = item),
setState(() {
selectedItem = localizedConfigSteps.entries
.firstWhere((element) => element.value == item)
.value;
currentStep = localizedConfigSteps.entries
.firstWhere((element) => element.value == item)
.key;
}),
}),
SizedBox(height: 16),
view,
Expand Down
44 changes: 15 additions & 29 deletions lib/screens/send_transaction/send_vtt_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ enum VTTsteps {
Review,
}

Map<String, VTTsteps> _localizedVTTsteps(BuildContext context) {
return {
localization.vttSendSteps('Transaction'): VTTsteps.Transaction,
localization.vttSendSteps('MinerFee'): VTTsteps.MinerFee,
localization.vttSendSteps('Review'): VTTsteps.Review,
};
}

class CreateVttScreenState extends State<CreateVttScreen>
with TickerProviderStateMixin {
GlobalKey<RecipientStepState> transactionFormState =
Expand All @@ -52,7 +44,7 @@ class CreateVttScreenState extends State<CreateVttScreen>
FeeType? savedFeeType;
ScrollController scrollController = ScrollController(keepScrollOffset: false);

String? selectedItem;
String selectedItem = localizedVTTsteps[VTTsteps.Transaction]!;

@override
void initState() {
Expand Down Expand Up @@ -81,16 +73,11 @@ class CreateVttScreenState extends State<CreateVttScreen>
}

void goToNextStep() {
setState(() {
setOngoingTransaction();
});
VTTsteps currentStep = _localizedVTTsteps(context)[selectedItem]!;
if (currentStep.index + 1 < VTTsteps.values.length) {
int currentStep = localizedVTTsteps.values.toList().indexOf(selectedItem);
if (currentStep + 1 < VTTsteps.values.length) {
scrollController.jumpTo(0.0);
setState(() {
selectedItem =
_localizedVTTsteps(context).keys.elementAt(currentStep.index + 1);
setOngoingTransaction();
selectedItem = localizedVTTsteps.values.elementAt(currentStep + 1);
});
}
}
Expand All @@ -108,7 +95,9 @@ class CreateVttScreenState extends State<CreateVttScreen>
}

bool _isNextStepAllow() {
VTTsteps currentStep = _localizedVTTsteps(context)[selectedItem]!;
VTTsteps currentStep = localizedVTTsteps.entries
.firstWhere((element) => element.value == selectedItem)
.key;
bool isTransactionFormValid = currentStep == VTTsteps.Transaction &&
(transactionFormState.currentState != null &&
transactionFormState.currentState!.validateForm(force: true));
Expand Down Expand Up @@ -194,10 +183,9 @@ class CreateVttScreenState extends State<CreateVttScreen>
}

Widget stepToBuild() {
if (selectedItem == null) {
return _recipientStep();
}
VTTsteps currentStep = _localizedVTTsteps(context)[selectedItem]!;
VTTsteps currentStep = localizedVTTsteps.entries
.firstWhere((element) => element.value == selectedItem)
.key;
if (currentStep == VTTsteps.Transaction) {
return _recipientStep();
} else if (currentStep == VTTsteps.MinerFee) {
Expand All @@ -212,13 +200,14 @@ class CreateVttScreenState extends State<CreateVttScreen>
crossAxisAlignment: CrossAxisAlignment.start,
children: [
StepBar(
listItems: _localizedVTTsteps(context).keys.toList(),
selectedItem:
selectedItem ?? _localizedVTTsteps(context).keys.first,
listItems: localizedVTTsteps.values.toList(),
selectedItem: selectedItem,
actionable: false,
onChanged: (item) => {
setState(() {
selectedItem = item;
selectedItem = localizedVTTsteps.entries
.firstWhere((element) => element.value == item)
.value;
setOngoingTransaction();
}),
}),
Expand Down Expand Up @@ -272,9 +261,6 @@ class CreateVttScreenState extends State<CreateVttScreen>

@override
Widget build(BuildContext context) {
if (selectedItem == null) {
selectedItem = _localizedVTTsteps(context).keys.first;
}
return BlocBuilder<VTTCreateBloc, VTTCreateState>(
builder: (context, state) {
return _dashboardBlocListener();
Expand Down
Loading

0 comments on commit 0873740

Please sign in to comment.