misc: initial commit
This commit is contained in:
193
lib/src/bootstrapper.dart
Normal file
193
lib/src/bootstrapper.dart
Normal file
@@ -0,0 +1,193 @@
|
||||
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';
|
||||
import 'package:markettakers/main.dart';
|
||||
import 'package:markettakers/src/loaders/loader.dart';
|
||||
|
||||
part 'bootstrapper.freezed.dart';
|
||||
|
||||
abstract class StageFactory {
|
||||
String get title;
|
||||
|
||||
Future<bool> get isCompleted async => false;
|
||||
|
||||
Future<void> start(StageController controller);
|
||||
void dispose() {}
|
||||
}
|
||||
|
||||
@freezed
|
||||
class StageState with _$StageState {
|
||||
final String title;
|
||||
final double progress;
|
||||
|
||||
StageState({required this.title, required this.progress});
|
||||
}
|
||||
|
||||
class StageController extends Cubit<StageState> {
|
||||
StageController({required String title})
|
||||
: super(StageState(title: title, progress: 0.0));
|
||||
|
||||
void updateProgress(double progress) {
|
||||
emit(state.copyWith(progress: progress));
|
||||
}
|
||||
|
||||
void updateTitle(String title) {
|
||||
emit(state.copyWith(title: title));
|
||||
}
|
||||
}
|
||||
|
||||
typedef Stages = List<StageFactory>;
|
||||
|
||||
@freezed
|
||||
class BootstrapState with _$BootstrapState {
|
||||
final Stages stages;
|
||||
final int currentStageIndex;
|
||||
final StageController? controller;
|
||||
|
||||
StageFactory get currentStage => stages[currentStageIndex];
|
||||
bool get isCompleted => currentStageIndex > stages.length;
|
||||
|
||||
BootstrapState(this.stages, {this.currentStageIndex = 0, this.controller});
|
||||
}
|
||||
|
||||
@freezed
|
||||
sealed class BootstrapEvent with _$BootstrapEvent {
|
||||
const factory BootstrapEvent.start() = StartEvent;
|
||||
const factory BootstrapEvent.stageCompleted() = StageCompletedEvent;
|
||||
const factory BootstrapEvent.finished() = FinishedEvent;
|
||||
}
|
||||
|
||||
class BootstrapController extends Bloc<BootstrapEvent, BootstrapState> {
|
||||
BootstrapController(super.initialState) {
|
||||
assert(state.stages.isNotEmpty, "Stages list cannot be empty");
|
||||
|
||||
on<StartEvent>((event, emit) {
|
||||
talker.info("BootstrapController: Starting bootstrap process");
|
||||
final controller = launchCurrentStage(
|
||||
state.stages,
|
||||
state.currentStageIndex,
|
||||
);
|
||||
emit(state.copyWith(controller: controller));
|
||||
});
|
||||
on<StageCompletedEvent>((event, emit) async {
|
||||
talker.info("BootstrapController: ${state.currentStage.title} completed");
|
||||
state.currentStage.dispose();
|
||||
|
||||
final nextIndex = state.currentStageIndex + 1;
|
||||
final newState = state.copyWith(currentStageIndex: nextIndex);
|
||||
|
||||
if (newState.isCompleted) {
|
||||
talker.info("BootstrapController: All stages completed");
|
||||
} else if (await newState.currentStage.isCompleted) {
|
||||
talker.info(
|
||||
"BootstrapController: Stage ${newState.currentStage.title} already completed, skipping",
|
||||
);
|
||||
add(const BootstrapEvent.stageCompleted());
|
||||
} else {
|
||||
final nextStage = newState.currentStage;
|
||||
talker.info("BootstrapController: Starting stage ${nextStage.title}");
|
||||
launchCurrentStage(state.stages, nextIndex);
|
||||
emit(newState);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
}),
|
||||
);
|
||||
|
||||
return controller;
|
||||
}
|
||||
}
|
||||
|
||||
class BootstrapFooter extends StatelessWidget {
|
||||
const BootstrapFooter({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<BootstrapController, BootstrapState>(
|
||||
builder: (context, state) {
|
||||
final controller = state.controller;
|
||||
if (controller == null) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
return Column(
|
||||
children: [
|
||||
Text(
|
||||
"${state.currentStage.title} ${controller.state.progress.toStringAsFixed(2)}%",
|
||||
),
|
||||
CircularProgressIndicator.adaptive(
|
||||
value: controller.state.progress,
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Bootstrapper extends StatelessWidget {
|
||||
final Stages stages;
|
||||
final Completer onCompleted;
|
||||
|
||||
const Bootstrapper({
|
||||
super.key,
|
||||
required this.stages,
|
||||
required this.onCompleted,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocProvider(
|
||||
create: (context) {
|
||||
final controller = BootstrapController(BootstrapState(stages));
|
||||
controller.add(const BootstrapEvent.start());
|
||||
controller.launchCurrentStage(stages, 0);
|
||||
return controller;
|
||||
},
|
||||
child: BlocListener<BootstrapController, BootstrapState>(
|
||||
listener: (context, state) {
|
||||
if (state.isCompleted) {
|
||||
onCompleted.complete();
|
||||
}
|
||||
},
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
mainAxisSize: MainAxisSize.max,
|
||||
children: [
|
||||
Flexible(
|
||||
flex: 3,
|
||||
child: Center(
|
||||
child: Loader(flavour: LoaderFlavour.big, playing: true),
|
||||
),
|
||||
),
|
||||
Flexible(flex: 1, child: BootstrapFooter()),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user