fix(bootstraper): invalid logic
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
import 'dart:async';
|
||||
import 'dart:isolate';
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
@@ -48,7 +46,7 @@ class BootstrapState with _$BootstrapState {
|
||||
final StageController? controller;
|
||||
|
||||
StageFactory get currentStage => stages[currentStageIndex];
|
||||
bool get isCompleted => currentStageIndex > stages.length;
|
||||
bool get isCompleted => currentStageIndex >= stages.length;
|
||||
|
||||
BootstrapState(this.stages, {this.currentStageIndex = 0, this.controller});
|
||||
}
|
||||
@@ -81,6 +79,7 @@ class BootstrapController extends Bloc<BootstrapEvent, BootstrapState> {
|
||||
|
||||
if (newState.isCompleted) {
|
||||
talker.info("BootstrapController: All stages completed");
|
||||
add(const BootstrapEvent.finished());
|
||||
} else if (await newState.currentStage.isCompleted) {
|
||||
talker.info(
|
||||
"BootstrapController: Stage ${newState.currentStage.title} already completed, skipping",
|
||||
@@ -93,30 +92,28 @@ class BootstrapController extends Bloc<BootstrapEvent, BootstrapState> {
|
||||
emit(newState);
|
||||
}
|
||||
});
|
||||
on<FinishedEvent>((event, emit) {
|
||||
talker.info("BootstrapController: Bootstrap process finished");
|
||||
});
|
||||
}
|
||||
|
||||
StageController launchCurrentStage(Stages stages, int index) {
|
||||
final currentStage = stages[index];
|
||||
final controller = StageController(title: currentStage.title);
|
||||
|
||||
Isolate.run(
|
||||
() => currentStage
|
||||
.start(controller)
|
||||
.then((_) {
|
||||
talker.info(
|
||||
"BootstrapController: Stage ${currentStage.title} completed",
|
||||
);
|
||||
add(BootstrapEvent.stageCompleted());
|
||||
})
|
||||
.catchError((error) {
|
||||
talker.handle(
|
||||
error,
|
||||
null,
|
||||
"BootstrapController: Error in ${currentStage.title}",
|
||||
);
|
||||
addError(error);
|
||||
}),
|
||||
);
|
||||
currentStage
|
||||
.start(controller)
|
||||
.then((_) {
|
||||
add(BootstrapEvent.stageCompleted());
|
||||
})
|
||||
.catchError((error) {
|
||||
talker.handle(
|
||||
error,
|
||||
null,
|
||||
"BootstrapController: Error in ${currentStage.title}",
|
||||
);
|
||||
addError(error);
|
||||
});
|
||||
|
||||
return controller;
|
||||
}
|
||||
@@ -136,7 +133,7 @@ class BootstrapFooter extends StatelessWidget {
|
||||
return Column(
|
||||
children: [
|
||||
Text(
|
||||
"${state.currentStage.title} ${controller.state.progress.toStringAsFixed(2)}%",
|
||||
"${state.currentStage.title} ${(controller.state.progress * 100).toStringAsFixed(2)}%",
|
||||
),
|
||||
CircularProgressIndicator.adaptive(
|
||||
value: controller.state.progress,
|
||||
@@ -164,7 +161,6 @@ class Bootstrapper extends StatelessWidget {
|
||||
create: (context) {
|
||||
final controller = BootstrapController(BootstrapState(stages));
|
||||
controller.add(const BootstrapEvent.start());
|
||||
controller.launchCurrentStage(stages, 0);
|
||||
return controller;
|
||||
},
|
||||
child: BlocListener<BootstrapController, BootstrapState>(
|
||||
|
||||
Reference in New Issue
Block a user