Skip to content

Commit 6e41359

Browse files
appflowyXazin
andauthored
feat: show indicator when importing appflowy data (#4357)
* feat: show indicator when importing appflowy data * Update frontend/appflowy_flutter/lib/workspace/presentation/settings/widgets/setting_file_import_appflowy_data_view.dart Co-authored-by: Mathias Mogensen <[email protected]> * chore: fix analyzer * chore: fix test --------- Co-authored-by: Mathias Mogensen <[email protected]>
1 parent b1cc4e4 commit 6e41359

File tree

11 files changed

+105
-64
lines changed

11 files changed

+105
-64
lines changed

frontend/appflowy_flutter/lib/plugins/database/application/defines.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ typedef OnError = void Function(FlowyError);
3434

3535
@freezed
3636
class LoadingState with _$LoadingState {
37+
const factory LoadingState.idle() = _Idle;
3738
const factory LoadingState.loading() = _Loading;
3839
const factory LoadingState.finish(
3940
Either<Unit, FlowyError> successOrFail,

frontend/appflowy_flutter/lib/plugins/database/board/presentation/board_page.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class BoardPage extends StatelessWidget {
103103
howToFix: LocaleKeys.errorDialog_howToFixFallback.tr(),
104104
),
105105
),
106+
idle: (_) => const SizedBox.shrink(),
106107
),
107108
),
108109
);

frontend/appflowy_flutter/lib/plugins/database/grid/presentation/grid_page.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class _GridPageState extends State<GridPage> {
131131
howToFix: LocaleKeys.errorDialog_howToFixFallback.tr(),
132132
),
133133
),
134+
idle: (_) => const SizedBox.shrink(),
134135
);
135136
},
136137
),

frontend/appflowy_flutter/lib/plugins/database/grid/presentation/mobile_grid_page.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class _MobileGridPageState extends State<MobileGridPage> {
9797
howToFix: LocaleKeys.errorDialog_howToFixFallback.tr(),
9898
),
9999
),
100+
idle: (_) => const SizedBox.shrink(),
100101
);
101102
},
102103
),

frontend/appflowy_flutter/lib/startup/tasks/device_info_task.dart

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,20 @@ class DeviceOrApplicationInfoTask extends LaunchTask {
1414

1515
@override
1616
Future<void> initialize(LaunchContext context) async {
17-
final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
18-
final PackageInfo packageInfo = await PackageInfo.fromPlatform();
19-
20-
if (Platform.isAndroid) {
21-
final androidInfo = await deviceInfoPlugin.androidInfo;
22-
androidSDKVersion = androidInfo.version.sdkInt;
23-
}
24-
25-
if (Platform.isAndroid || Platform.isIOS) {
26-
applicationVersion = packageInfo.version;
27-
buildNumber = packageInfo.buildNumber;
17+
// Can't get the device info from test environment
18+
if (!context.env.isTest) {
19+
final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
20+
final PackageInfo packageInfo = await PackageInfo.fromPlatform();
21+
22+
if (Platform.isAndroid) {
23+
final androidInfo = await deviceInfoPlugin.androidInfo;
24+
androidSDKVersion = androidInfo.version.sdkInt;
25+
}
26+
27+
if (Platform.isAndroid || Platform.isIOS) {
28+
applicationVersion = packageInfo.version;
29+
buildNumber = packageInfo.buildNumber;
30+
}
2831
}
2932
}
3033

frontend/appflowy_flutter/lib/user/application/encrypt_secret_bloc.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ class EncryptSecretBloc extends Bloc<EncryptSecretEvent, EncryptSecretState> {
7575
bool isLoading() {
7676
final loadingState = state.loadingState;
7777
if (loadingState != null) {
78-
return loadingState.when(loading: () => true, finish: (_) => false);
78+
return loadingState.when(
79+
loading: () => true,
80+
finish: (_) => false,
81+
idle: () => false,
82+
);
7983
}
8084
return false;
8185
}

frontend/appflowy_flutter/lib/user/presentation/screens/encrypt_secret_screen.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class _EncryptSecretScreenState extends State<EncryptSecretScreen> {
6969
child: CircularProgressIndicator.adaptive(),
7070
),
7171
finish: (result) => const SizedBox.shrink(),
72+
idle: () => const SizedBox.shrink(),
7273
) ??
7374
const SizedBox.shrink();
7475
return Center(

frontend/appflowy_flutter/lib/user/presentation/screens/workspace_error_screen.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class WorkspaceErrorScreen extends StatelessWidget {
7070
},
7171
);
7272
},
73+
idle: () {},
7374
);
7475
},
7576
),
Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:appflowy/plugins/database/application/defines.dart';
12
import 'package:appflowy_backend/dispatch/dispatch.dart';
23
import 'package:appflowy_backend/log.dart';
34
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
@@ -9,55 +10,72 @@ import 'package:freezed_annotation/freezed_annotation.dart';
910

1011
part 'setting_file_importer_bloc.freezed.dart';
1112

12-
class SettingFileImporterBloc
13+
class SettingFileImportBloc
1314
extends Bloc<SettingFileImportEvent, SettingFileImportState> {
14-
SettingFileImporterBloc() : super(SettingFileImportState.initial()) {
15-
on<SettingFileImportEvent>((event, emit) async {
16-
await event.when(
17-
importAppFlowyDataFolder: (String path) async {
18-
final formattedDate =
19-
DateFormat('yyyy-MM-dd HH:mm:ss').format(DateTime.now());
20-
final payload = ImportAppFlowyDataPB.create()
21-
..path = path
22-
..importContainerName = "appflowy_import_$formattedDate";
23-
final result =
24-
await FolderEventImportAppFlowyDataFolder(payload).send();
25-
result.fold(
26-
(l) {
27-
emit(
28-
state.copyWith(
29-
successOrFail: some(left(unit)),
30-
),
31-
);
32-
},
33-
(err) {
34-
Log.error(err);
35-
emit(
36-
state.copyWith(
37-
successOrFail: some(right(err)),
38-
),
39-
);
40-
},
41-
);
42-
},
43-
);
44-
});
15+
SettingFileImportBloc() : super(SettingFileImportState.initial()) {
16+
on<SettingFileImportEvent>(
17+
(event, emit) async {
18+
await event.when(
19+
importAppFlowyDataFolder: (String path) async {
20+
final formattedDate =
21+
DateFormat('yyyy-MM-dd HH:mm:ss').format(DateTime.now());
22+
final payload = ImportAppFlowyDataPB.create()
23+
..path = path
24+
..importContainerName = "appflowy_import_$formattedDate";
25+
emit(
26+
state.copyWith(loadingState: const LoadingState.loading()),
27+
);
28+
FolderEventImportAppFlowyDataFolder(payload).send().then((result) {
29+
if (!isClosed) {
30+
add(SettingFileImportEvent.finishImport(result));
31+
}
32+
});
33+
},
34+
finishImport: (result) {
35+
result.fold(
36+
(l) {
37+
emit(
38+
state.copyWith(
39+
successOrFail: some(left(unit)),
40+
loadingState: LoadingState.finish(left(unit)),
41+
),
42+
);
43+
},
44+
(err) {
45+
Log.error(err);
46+
emit(
47+
state.copyWith(
48+
successOrFail: some(right(err)),
49+
loadingState: LoadingState.finish(right(err)),
50+
),
51+
);
52+
},
53+
);
54+
},
55+
);
56+
},
57+
);
4558
}
4659
}
4760

4861
@freezed
4962
class SettingFileImportEvent with _$SettingFileImportEvent {
5063
const factory SettingFileImportEvent.importAppFlowyDataFolder(String path) =
5164
_ImportAppFlowyDataFolder;
65+
const factory SettingFileImportEvent.finishImport(
66+
Either<Unit, FlowyError> result,
67+
) = _ImportResult;
5268
}
5369

5470
@freezed
5571
class SettingFileImportState with _$SettingFileImportState {
5672
const factory SettingFileImportState({
73+
required LoadingState loadingState,
5774
required Option<Either<Unit, FlowyError>> successOrFail,
5875
}) = _SettingFileImportState;
5976

6077
factory SettingFileImportState.initial() => SettingFileImportState(
78+
loadingState: const LoadingState.idle(),
6179
successOrFail: none(),
6280
);
6381
}

frontend/appflowy_flutter/lib/workspace/presentation/settings/widgets/setting_file_import_appflowy_data_view.dart

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class _ImportAppFlowyDataState extends State<ImportAppFlowyData> {
3030
@override
3131
Widget build(BuildContext context) {
3232
return BlocProvider(
33-
create: (context) => SettingFileImporterBloc(),
34-
child: BlocListener<SettingFileImporterBloc, SettingFileImportState>(
33+
create: (context) => SettingFileImportBloc(),
34+
child: BlocListener<SettingFileImportBloc, SettingFileImportState>(
3535
listener: (context, state) {
3636
state.successOrFail.fold(
3737
() {},
@@ -47,7 +47,7 @@ class _ImportAppFlowyDataState extends State<ImportAppFlowyData> {
4747
},
4848
);
4949
},
50-
child: BlocBuilder<SettingFileImporterBloc, SettingFileImportState>(
50+
child: BlocBuilder<SettingFileImportBloc, SettingFileImportState>(
5151
builder: (context, state) {
5252
return const Column(
5353
children: [
@@ -120,24 +120,33 @@ class ImportAppFlowyDataButton extends StatefulWidget {
120120
class _ImportAppFlowyDataButtonState extends State<ImportAppFlowyDataButton> {
121121
@override
122122
Widget build(BuildContext context) {
123-
return SizedBox(
124-
height: 40,
125-
child: FlowyButton(
126-
text: FlowyText(LocaleKeys.settings_menu_importAppFlowyData.tr()),
127-
onTap: () async {
128-
final path = await getIt<FilePickerService>().getDirectoryPath();
129-
if (path == null) {
130-
return;
131-
}
132-
if (!mounted) {
133-
return;
134-
}
123+
return BlocBuilder<SettingFileImportBloc, SettingFileImportState>(
124+
builder: (context, state) {
125+
return Column(
126+
children: [
127+
SizedBox(
128+
height: 40,
129+
child: FlowyButton(
130+
text:
131+
FlowyText(LocaleKeys.settings_menu_importAppFlowyData.tr()),
132+
onTap: () async {
133+
final path =
134+
await getIt<FilePickerService>().getDirectoryPath();
135+
if (path == null || !mounted) {
136+
return;
137+
}
135138

136-
context
137-
.read<SettingFileImporterBloc>()
138-
.add(SettingFileImportEvent.importAppFlowyDataFolder(path));
139-
},
140-
),
139+
context.read<SettingFileImportBloc>().add(
140+
SettingFileImportEvent.importAppFlowyDataFolder(path),
141+
);
142+
},
143+
),
144+
),
145+
if (state.loadingState.isLoading())
146+
const LinearProgressIndicator(minHeight: 1),
147+
],
148+
);
149+
},
141150
);
142151
}
143152
}

0 commit comments

Comments
 (0)