feat(bootstrapper): added definitie/indefinite progress and circular indicator
This commit is contained in:
@@ -1,35 +1,48 @@
|
||||
// ignore_for_file: annotate_overrides
|
||||
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:markettakers/markettakers.dart';
|
||||
import 'package:markettakers/src/loaders/loader.dart';
|
||||
import 'package:percent_indicator/circular_percent_indicator.dart';
|
||||
|
||||
part 'bootstrapper.freezed.dart';
|
||||
|
||||
abstract class StageFactory {
|
||||
String get title;
|
||||
|
||||
Future<bool> get isCompleted async => false;
|
||||
Future<bool> get isAlreadyCompleted async => false;
|
||||
|
||||
Future<void> start(StageController controller);
|
||||
void dispose() {}
|
||||
}
|
||||
|
||||
@freezed
|
||||
sealed class Progress with _$Progress {
|
||||
const factory Progress.definite(double value) = PercentageProgress;
|
||||
const factory Progress.indefinite() = IndefiniteProgress;
|
||||
}
|
||||
|
||||
@freezed
|
||||
class StageState with _$StageState {
|
||||
final String title;
|
||||
final double progress;
|
||||
final Progress progress;
|
||||
|
||||
StageState({required this.title, required this.progress});
|
||||
}
|
||||
|
||||
class StageController extends Cubit<StageState> {
|
||||
StageController({required String title})
|
||||
: super(StageState(title: title, progress: 0.0));
|
||||
: super(StageState(title: title, progress: const Progress.indefinite()));
|
||||
|
||||
void updateProgress(double progress) {
|
||||
emit(state.copyWith(progress: progress));
|
||||
void setDefiniteProgress(double value) {
|
||||
emit(state.copyWith(progress: Progress.definite(value)));
|
||||
}
|
||||
|
||||
void setIndefiniteProgress() {
|
||||
emit(state.copyWith(progress: const Progress.indefinite()));
|
||||
}
|
||||
|
||||
void updateTitle(String title) {
|
||||
@@ -46,20 +59,20 @@ class BootstrapState with _$BootstrapState {
|
||||
final StageController? controller;
|
||||
|
||||
StageFactory get currentStage => stages[currentStageIndex];
|
||||
bool get isCompleted => currentStageIndex >= stages.length;
|
||||
bool get areStagesCompleted => 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;
|
||||
sealed class _BootstrapEvent with _$BootstrapEvent {
|
||||
const factory _BootstrapEvent.start() = StartEvent;
|
||||
const factory _BootstrapEvent.stageCompleted() = StageCompletedEvent;
|
||||
}
|
||||
|
||||
class BootstrapController extends Bloc<BootstrapEvent, BootstrapState> {
|
||||
class _BootstrapController extends Bloc<_BootstrapEvent, BootstrapState> {
|
||||
final Completer<void> completer;
|
||||
BootstrapController(super.initialState, this.completer) {
|
||||
_BootstrapController(super.initialState, this.completer) {
|
||||
assert(state.stages.isNotEmpty, "Stages list cannot be empty");
|
||||
|
||||
on<StartEvent>((event, emit) {
|
||||
@@ -77,14 +90,19 @@ class BootstrapController extends Bloc<BootstrapEvent, BootstrapState> {
|
||||
final nextIndex = state.currentStageIndex + 1;
|
||||
final newState = state.copyWith(currentStageIndex: nextIndex);
|
||||
|
||||
if (newState.isCompleted) {
|
||||
// all stages completed
|
||||
if (newState.areStagesCompleted) {
|
||||
talker.info("BootstrapController: All stages completed");
|
||||
completer.complete();
|
||||
} else if (await newState.currentStage.isCompleted) {
|
||||
|
||||
// skip already completed stages
|
||||
} else if (await newState.currentStage.isAlreadyCompleted) {
|
||||
talker.info(
|
||||
"BootstrapController: Stage ${newState.currentStage.title} already completed, skipping",
|
||||
);
|
||||
add(const BootstrapEvent.stageCompleted());
|
||||
add(const _BootstrapEvent.stageCompleted());
|
||||
|
||||
// move to next stage
|
||||
} else {
|
||||
final nextStage = newState.currentStage;
|
||||
talker.info("BootstrapController: Starting stage ${nextStage.title}");
|
||||
@@ -92,9 +110,6 @@ class BootstrapController extends Bloc<BootstrapEvent, BootstrapState> {
|
||||
emit(newState);
|
||||
}
|
||||
});
|
||||
on<FinishedEvent>((event, emit) {
|
||||
talker.info("BootstrapController: Bootstrap process finished");
|
||||
});
|
||||
}
|
||||
|
||||
StageController launchCurrentStage(Stages stages, int index) {
|
||||
@@ -104,7 +119,7 @@ class BootstrapController extends Bloc<BootstrapEvent, BootstrapState> {
|
||||
currentStage
|
||||
.start(controller)
|
||||
.then((_) {
|
||||
add(BootstrapEvent.stageCompleted());
|
||||
add(_BootstrapEvent.stageCompleted());
|
||||
})
|
||||
.catchError((error) {
|
||||
talker.handle(
|
||||
@@ -119,27 +134,64 @@ class BootstrapController extends Bloc<BootstrapEvent, BootstrapState> {
|
||||
}
|
||||
}
|
||||
|
||||
class BootstrapFooter extends StatelessWidget {
|
||||
const BootstrapFooter({super.key});
|
||||
class _DefiniteIndicator extends StatelessWidget {
|
||||
final double progress; // 0.0 to 1.0
|
||||
|
||||
const _DefiniteIndicator({required this.progress});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<BootstrapController, BootstrapState>(
|
||||
final formattedProgress = "${(progress * 100).toStringAsFixed(0)}%";
|
||||
return CircularPercentIndicator(
|
||||
radius: 40.0,
|
||||
lineWidth: 5.0,
|
||||
percent: progress.clamp(0.0, 1.0),
|
||||
center: Text(formattedProgress),
|
||||
progressColor: Theme.of(context).colorScheme.secondary,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _BootstrapFooter extends StatelessWidget {
|
||||
const _BootstrapFooter();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<_BootstrapController, BootstrapState>(
|
||||
builder: (context, state) {
|
||||
final controller = state.controller;
|
||||
if (controller == null) {
|
||||
if (state.areStagesCompleted) {
|
||||
return Text(
|
||||
"All stages completed",
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
);
|
||||
} else if (state.controller != null) {
|
||||
return BlocBuilder<StageController, StageState>(
|
||||
bloc: state.controller,
|
||||
builder: (context, state) {
|
||||
final progressIndicator = state.progress.when(
|
||||
definite: (definite) => _DefiniteIndicator(progress: definite),
|
||||
indefinite: () => const CircularProgressIndicator(),
|
||||
);
|
||||
|
||||
return Column(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
mainAxisSize: MainAxisSize.max,
|
||||
children: [
|
||||
Flexible(
|
||||
flex: 1,
|
||||
child: Text(
|
||||
state.title,
|
||||
style: Theme.of(context).textTheme.titleLarge,
|
||||
),
|
||||
),
|
||||
Flexible(flex: 1, child: progressIndicator),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
} else {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
return Column(
|
||||
children: [
|
||||
Text(
|
||||
"${state.currentStage.title} ${(controller.state.progress * 100).toStringAsFixed(2)}%",
|
||||
),
|
||||
CircularProgressIndicator.adaptive(
|
||||
value: controller.state.progress,
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -159,30 +211,30 @@ class Bootstrapper extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
return BlocProvider(
|
||||
create: (context) {
|
||||
final controller = BootstrapController(
|
||||
final controller = _BootstrapController(
|
||||
BootstrapState(stages),
|
||||
onCompleted,
|
||||
);
|
||||
controller.add(const BootstrapEvent.start());
|
||||
controller.add(const _BootstrapEvent.start());
|
||||
return controller;
|
||||
},
|
||||
child: BlocListener<BootstrapController, BootstrapState>(
|
||||
child: BlocListener<_BootstrapController, BootstrapState>(
|
||||
listener: (context, state) {
|
||||
if (state.isCompleted) {
|
||||
if (state.areStagesCompleted) {
|
||||
onCompleted.complete();
|
||||
}
|
||||
},
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
mainAxisSize: MainAxisSize.max,
|
||||
children: [
|
||||
Flexible(
|
||||
flex: 2,
|
||||
child: Center(child: Loader.playing(flavour: LoaderFlavour.big)),
|
||||
),
|
||||
Flexible(flex: 1, child: Container()),
|
||||
Flexible(flex: 1, child: BootstrapFooter()),
|
||||
Flexible(flex: 1, child: _BootstrapFooter()),
|
||||
Flexible(flex: 1, child: Container()),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user