Skip to content

Commit

Permalink
feat: basic dialog rectangle tab
Browse files Browse the repository at this point in the history
  • Loading branch information
SiongSng committed Jul 28, 2023
1 parent e386365 commit b5d935e
Show file tree
Hide file tree
Showing 18 changed files with 587 additions and 66 deletions.
7 changes: 6 additions & 1 deletion app/assets/i18n/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@
"main_page.side_bar.universe_chat": "Universe Chat",
"main_page.side_bar.home": "Home",
"main_page.side_bar.library": "Library",
"main_page.size_bar.multiplayer": "Multiplayer"
"main_page.side_bar.multiplayer": "Multiplayer",
"main_page.side_bar.create_collection": "Create Collection",
"main_page.side_bar.explore": "Explore Collections",
"main_page.side_bar.manage_downloads": "Manage Downloads",
"main_page.side_bar.manage_accounts": "Manage Accounts",
"dialog.setup.01.title": "Welcome"
}
9 changes: 8 additions & 1 deletion app/assets/i18n/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,12 @@
"main_page.side_bar.create_collection": "建立收藏",
"main_page.side_bar.explore": "探索收藏",
"main_page.side_bar.manage_downloads": "管理下載",
"main_page.side_bar.manage_accounts": "管理帳號"
"main_page.side_bar.manage_accounts": "管理帳號",
"dialog.setup.01.title": "歡迎使用",
"dialog.setup.01.description": "歡迎你來到 Era Connect!\n開始使用啟動器之前,讓我們協助你進行快速設定,就可以盡情地暢玩遊戲啦。",
"dialog.setup.01.content.title": "匯入或是新建設定",
"dialog.setup.01.content.description": "你可以重新開始設定啟動器或是匯入先前的設定",
"dialog.setup.01.content.tab.title": "選擇方式",
"dialog.setup.01.content.tabs.empty.title": "從頭開始",
"dialog.setup.01.content.tabs.import.title": "匯入設定"
}
57 changes: 57 additions & 0 deletions app/lib/dialog/setup_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import 'package:era_connect_i18n/era_connect_i18n.dart';
import 'package:era_connect_ui/era_connect_ui.dart';
import 'package:flutter/material.dart';

class SetupDialog extends StatelessWidget {
const SetupDialog({super.key});

@override
Widget build(BuildContext context) {
return InteractiveDialog(
title: context.i18n['dialog.setup.01.title'],
description: context.i18n['dialog.setup.01.description'],
logoBoxText: '01',
body: Padding(
padding:
const EdgeInsets.only(top: 45, bottom: 35, left: 45, right: 45),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
context.i18n['dialog.setup.01.content.title'],
style: TextStyle(
color: context.theme.textColor,
fontSize: 40,
fontWeight: FontWeight.w700,
),
),
Text(
context.i18n['dialog.setup.01.content.description'],
style: TextStyle(
color: context.theme.tertiaryTextColor, fontSize: 15),
),
const SizedBox(height: 25),
DialogRectangleTab(
title: '選擇方式',
tabs: [
TabItem(
title:
context.i18n['dialog.setup.01.content.tabs.empty.title'],
icon: 'deployed_code',
content:
const Text('這是從頭開始的頁面', style: TextStyle(fontSize: 30)),
),
TabItem(
title: context
.i18n['dialog.setup.01.content.tabs.import.title'],
icon: 'deployed_code_update',
content: const Text('這是匯入設定的頁面',
style: TextStyle(fontSize: 50))),
],
)
],
),
),
);
}
}
3 changes: 2 additions & 1 deletion app/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:era_connect_i18n/era_connect_i18n.dart';
import 'package:era_connect_ui/era_connect_ui.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
// import 'ffi.dart' show api;
import 'pages/main_page.dart';
Expand Down Expand Up @@ -96,7 +97,7 @@ class _AppHomeState extends State<_AppHome> {
final i18n = context.i18n;
i18n.setLocale(I18nLocale.getFromSystemLocale(
WidgetsBinding.instance.platformDispatcher));
await i18n.load();
await i18n.load(rootBundle);
setState(() {
isLoaded = true;
});
Expand Down
9 changes: 2 additions & 7 deletions app/lib/pages/main_page.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:era_connect/dialog/setup_dialog.dart';
import 'package:era_connect_i18n/era_connect_i18n.dart';
import 'package:era_connect_ui/era_connect_ui.dart';
import 'package:flutter/material.dart';
Expand All @@ -19,13 +20,7 @@ class _MainPageState extends State<MainPage> {
showEraDialog(
context: context,
barrierDismissible: true,
dialog: const InteractiveDialog(
title: '歡迎使用',
description:
'歡迎你來到 Era Connect!\n開始使用啟動器之前,讓我們協助你進行快速設定,就可以盡情地暢玩遊戲啦。',
logoBoxText: '01',
child: Placeholder(),
),
dialog: const SetupDialog(),
);
}
});
Expand Down
4 changes: 2 additions & 2 deletions packages/era_connect_i18n/lib/i18n_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class I18nManager extends ChangeNotifier implements ReassembleHandler {
/// en-US.json
/// zh-TW.json
/// ```
Future<void> load() async {
Future<void> load(AssetBundle rootBundle) async {
_data.clear();

for (final locale in I18nLocale.values) {
Expand All @@ -56,7 +56,7 @@ class I18nManager extends ChangeNotifier implements ReassembleHandler {

@override
void reassemble() {
load();
load(rootBundle);
}
}

Expand Down
2 changes: 0 additions & 2 deletions packages/era_connect_i18n/test/i18n_locale_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import 'i18n_locale_test.mocks.dart';
@GenerateMocks([ui.PlatformDispatcher])
void main() {
group('I18nLocale', () {
setUp(() => WidgetsFlutterBinding.ensureInitialized());

test(
'getFromSystemLocale returns americanEnglish when system locale is not supported',
() {
Expand Down
129 changes: 129 additions & 0 deletions packages/era_connect_i18n/test/i18n_manager_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import 'package:era_connect_i18n/i18n_locale.dart';
import 'package:era_connect_i18n/i18n_manager.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:provider/provider.dart';

import 'i18n_manager_test.mocks.dart';

@GenerateNiceMocks(
[MockSpec<AssetBundle>(onMissingStub: OnMissingStub.throwException)])
void main() {
group('I18nManager', () {
test('loads translations from JSON files', () async {
// Arrange
final i18nManager = I18nManager(
path: 'assets/i18n',
defaultLocale: I18nLocale.americanEnglish,
);
final mockAssetBundle = MockAssetBundle();
const jsonString = '{"hello": "Hello", "world": "World"}';

when(mockAssetBundle.loadString('assets/i18n/en-US.json'))
.thenAnswer((_) => Future.value(jsonString));
when(mockAssetBundle.loadString('assets/i18n/zh-TW.json'))
.thenAnswer((_) => Future.value('{}'));

// Act
await i18nManager.load(mockAssetBundle);

// Assert
expect(i18nManager.defaultLocale, equals(I18nLocale.americanEnglish));
expect(i18nManager['hello'], equals('Hello'));
expect(i18nManager['world'], equals('World'));
});

test('returns the key if no translation is found', () async {
// Arrange
final i18nManager = I18nManager(
path: 'assets/i18n',
defaultLocale: I18nLocale.americanEnglish,
);

final mockAssetBundle = MockAssetBundle();
when(mockAssetBundle.loadString('assets/i18n/en-US.json'))
.thenAnswer((_) => Future.value('{}'));
when(mockAssetBundle.loadString('assets/i18n/zh-TW.json'))
.thenAnswer((_) => Future.value('{}'));

// Act
await i18nManager.load(mockAssetBundle);

// Assert
expect(i18nManager['missing_key'], equals('missing_key'));
});

test('sets the current locale', () {
// Arrange
final i18nManager = I18nManager(
path: 'assets/i18n',
defaultLocale: I18nLocale.americanEnglish,
);

// Act
i18nManager.setLocale(I18nLocale.traditionalChineseTW);

// Assert
expect(i18nManager.locale, equals(I18nLocale.traditionalChineseTW));
});

test('notifies listeners when the locale is changed', () {
// Arrange
final i18nManager = I18nManager(
path: 'assets/i18n',
defaultLocale: I18nLocale.americanEnglish,
);
var notified = false;
i18nManager.addListener(() {
notified = true;
});

// Act
i18nManager.setLocale(I18nLocale.traditionalChineseTW);

// Assert
expect(notified, isTrue);
});

testWidgets('get i18n manager from build context', (tester) async {
// Arrange
final i18nManager = I18nManager(
path: 'assets/i18n',
defaultLocale: I18nLocale.americanEnglish,
);
final mockAssetBundle = MockAssetBundle();
const jsonString = '{"hello": "Hello", "world": "World"}';

when(mockAssetBundle.loadString('assets/i18n/en-US.json'))
.thenAnswer((_) => Future.value(jsonString));
when(mockAssetBundle.loadString('assets/i18n/zh-TW.json'))
.thenAnswer((_) => Future.value('{}'));

// Act
await i18nManager.load(mockAssetBundle);
await tester.pumpWidget(
MaterialApp(
home: ChangeNotifierProvider(
create: (context) => i18nManager,
child: Builder(
builder: (context) {
final i18nManager = context.i18n;
return Column(
children: [
Text(i18nManager['hello']),
Text(i18nManager['world']),
],
);
},
),
)),
);

// Assert
expect(find.text('Hello'), findsOneWidget);
expect(find.text('World'), findsOneWidget);
});
});
}
Loading

0 comments on commit b5d935e

Please sign in to comment.