Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate bloc to v8 #37

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
24 changes: 14 additions & 10 deletions lib/bloc/dashboard_bloc/dashboard_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:file_selector/file_selector.dart';
import 'package:bloc_concurrency/bloc_concurrency.dart';

part 'dashboard_event.dart';
part 'dashboard_state.dart';

/// Handles selecting and removing files.
class DashboardBloc extends Bloc<DashboardEvent, DashboardState> {
DashboardBloc() : super(DashboardState(files: [], alreadyPresent: false));
@override
Stream<DashboardState> mapEventToState(DashboardEvent event) async* {
DashboardBloc() : super(DashboardState(files: [], alreadyPresent: false)) {
on<DashboardEvent>(_onEvent, transformer: sequential());
}
FutureOr<void> _onEvent(
DashboardEvent event, Emitter<DashboardState> emit) async {
// TODO: logic goes here...
if (event is NewFileAdded) {
List<XFile> finalEventList = List.from(event.files);
bool alreadyPresent = false;
Expand All @@ -25,21 +29,21 @@ class DashboardBloc extends Bloc<DashboardEvent, DashboardState> {
}
}
if (alreadyPresent) {
yield state.copyWith(
emit(state.copyWith(
files: List.from(state.files)..addAll(finalEventList),
alreadyPresent: true);
alreadyPresent: true));
} else {
yield state.copyWith(
emit(state.copyWith(
files: List.from(state.files)..addAll(event.files),
alreadyPresent: false);
alreadyPresent: false));
;
}
} else if (event is FileRemoved) {
yield state.copyWith(
emit(state.copyWith(
files: List.from(state.files)..remove(event.file),
alreadyPresent: false);
alreadyPresent: false));
} else if (event is RemoveAllFiles) {
yield state.copyWith(files: [], alreadyPresent: false);
emit(state.copyWith(files: [], alreadyPresent: false));
}
}
}
82 changes: 44 additions & 38 deletions lib/bloc/process_bloc/process_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import 'dart:async';

import 'package:equatable/equatable.dart';
import 'package:file_selector/file_selector.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:pedantic/pedantic.dart';
import 'package:bloc_concurrency/bloc_concurrency.dart';

import 'package:ccxgui/repositories/ccextractor.dart';

Expand All @@ -22,9 +25,11 @@ class ProcessBloc extends Bloc<ProcessEvent, ProcessState> {
progress: '0',
current: null,
version: '0',
));
)) {
on<ProcessEvent>(_onEvent, transformer: sequential());
}

Stream<ProcessState> _extractNext() async* {
Stream<ProcessState> _extractNext(Emitter<ProcessState> emit) async* {
if (!state.started || state.current != null || state.queue.isEmpty) {
if (state.queue.isEmpty) {
// We need to show user that all files have finished processing
Expand Down Expand Up @@ -75,8 +80,8 @@ class ProcessBloc extends Bloc<ProcessEvent, ProcessState> {
);
}

Stream<ProcessState> _extractOnNetwork(
String type, String location, String tcppassword, String tcpdesc) async* {
Stream<ProcessState> _extractOnNetwork(String type, String location,
String tcppassword, String tcpdesc, Emitter<ProcessState> emit) async* {
unawaited(
_extractor
.extractFileOverNetwork(
Expand All @@ -100,7 +105,8 @@ class ProcessBloc extends Bloc<ProcessEvent, ProcessState> {
);
}

Stream<ProcessState> _extractFilesInSplitMode() async* {
Stream<ProcessState> _extractFilesInSplitMode(
Emitter<ProcessState> emit) async* {
unawaited(
_extractor
.extractFilesInSplitMode(
Expand All @@ -122,10 +128,10 @@ class ProcessBloc extends Bloc<ProcessEvent, ProcessState> {
);
}

@override
Stream<ProcessState> mapEventToState(ProcessEvent event) async* {
FutureOr<void> _onEvent(
ProcessEvent event, Emitter<ProcessState> emit) async {
if (event is StartAllProcess) {
yield state.copyWith(
emit(state.copyWith(
current: state.current,
// This equality checks if the queue and originalList are same which
// means that the user has not added any new y files or they have been
Expand All @@ -137,39 +143,39 @@ class ProcessBloc extends Bloc<ProcessEvent, ProcessState> {
// proccessed x files as it is.
processed: state.queue == state.orignalList ? [] : state.processed,
started: true,
);
yield* _extractNext();
));
_extractNext(emit);
} else if (event is StartProcessInSplitMode) {
yield state.copyWith(current: state.current, started: true);
yield* _extractFilesInSplitMode();
emit(state.copyWith(current: state.current, started: true));
_extractFilesInSplitMode(emit);
} else if (event is StopAllProcess) {
// stops everything
try {
_extractor.cancelRun();
} catch (_) {}
yield state.copyWith(
emit(state.copyWith(
current: null,
queue: state.orignalList,
processed: [], // We don't need ticks when we stop so discard processed files list.
progress: '0',
started: false,
);
));
} else if (event is ProcessKill) {
try {
_extractor.cancelRun();
} catch (_) {}
yield state.copyWith(
emit(state.copyWith(
current: state.current,
orignalList: state.orignalList
.where((element) => element != event.file)
.toList(),
queue: state.queue.where((element) => element != event.file).toList(),
);
));
} else if (event is ProcessRemoveAll) {
try {
_extractor.cancelRun();
} catch (_) {}
yield state.copyWith(
emit(state.copyWith(
current: null,
progress: '0',
processed: [],
Expand All @@ -179,36 +185,36 @@ class ProcessBloc extends Bloc<ProcessEvent, ProcessState> {
exitCode: null,
log: [],
videoDetails: [],
);
));
} else if (event is ProcessFileExtractorProgress) {
yield state.copyWith(current: state.current, progress: event.progress);
emit(state.copyWith(current: state.current, progress: event.progress));
} else if (event is ProcessFileVideoDetails) {
yield state.copyWith(
current: state.current, videoDetails: event.videoDetails);
emit(state.copyWith(
current: state.current, videoDetails: event.videoDetails));
} else if (event is ProcessFileExtractorOutput) {
yield state.copyWith(
emit(state.copyWith(
current: state.current,
log: state.log.followedBy([event.log]).toList(),
);
));
} else if (event is ProcessFileComplete) {
if (state.current == event.file) {
yield state.copyWith(
emit(state.copyWith(
current: null,
log: state.queue.isNotEmpty ? [] : state.log,
processed: state.processed.followedBy([event.file]).toList(),
exitCode: null,
);
yield* _extractNext();
));
_extractNext(emit);
}
} else if (event is SplitModeProcessComplete) {
yield state.copyWith(
emit(state.copyWith(
current: null,
log: state.log,
exitCode: null,
started: false,
progress: '100');
progress: '100'));
} else if (event is ProcessFilesSubmitted) {
yield state.copyWith(
emit(state.copyWith(
current: state.current,
orignalList: List.from(state.orignalList)..addAll(event.files),
processed: state.processed,
Expand All @@ -234,28 +240,28 @@ class ProcessBloc extends Bloc<ProcessEvent, ProcessState> {
queue: state.started || state.processed.isEmpty
? state.queue.followedBy(event.files).toList()
: event.files,
);
));
} else if (event is ProcessFileRemoved) {
yield state.copyWith(
emit(state.copyWith(
current: state.current,
orignalList: state.orignalList
.where((element) => element != event.file)
.toList(),
queue: state.queue.where((element) => element != event.file).toList(),
);
));
} else if (event is GetCCExtractorVersion) {
String ccxVersion = await _extractor.getCCExtractorVersion;
yield state.copyWith(
emit(state.copyWith(
current: state.current,
version: ccxVersion,
);
));
} else if (event is ProcessError) {
yield state.copyWith(current: state.current, exitCode: event.exitCode);
emit(state.copyWith(current: state.current, exitCode: event.exitCode));
} else if (event is ResetProcessError) {
yield state.copyWith(current: state.current, exitCode: null);
emit(state.copyWith(current: state.current, exitCode: null));
} else if (event is ProcessOnNetwork) {
yield* _extractOnNetwork(
event.type, event.location, event.tcppassword, event.tcpdesc);
_extractOnNetwork(
event.type, event.location, event.tcppassword, event.tcpdesc, emit);
}
}
}
31 changes: 16 additions & 15 deletions lib/bloc/settings_bloc/settings_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:bloc_concurrency/bloc_concurrency.dart';

import 'package:ccxgui/models/settings_model.dart';
import 'package:ccxgui/repositories/settings_repository.dart';
Expand All @@ -11,49 +12,49 @@ part 'settings_state.dart';

class SettingsBloc extends Bloc<SettingsEvent, SettingsState> {
final SettingsRepository _settingsRepository;
SettingsBloc(this._settingsRepository) : super(SettingsInitial());

@override
Stream<SettingsState> mapEventToState(
SettingsEvent event,
) async* {
SettingsBloc(this._settingsRepository) : super(SettingsInitial()) {
on<SettingsEvent>(_onEvent, transformer: sequential());
}
FutureOr<void> _onEvent(
SettingsEvent event, Emitter<SettingsState> emit) async {
// TODO: logic goes here...
if (event is CheckSettingsEvent) {
bool settingsStatus = await _settingsRepository.checkValidJSON();
if (settingsStatus) {
add(GetSettingsEvent());
} else {
yield SettingsErrorState("Couldn't parse json file");
emit(SettingsErrorState("Couldn't parse json file"));
}
} else if (event is GetSettingsEvent) {
try {
final _settings = await _settingsRepository.getSettings();
yield CurrentSettingsState(_settings);
emit(CurrentSettingsState(_settings));
} catch (e) {
yield SettingsErrorState('Error getting settings.');
emit(SettingsErrorState('Error getting settings.'));
}
} else if (event is ResetSettingsEvent) {
await _settingsRepository.resetSettings();
final _settings = await _settingsRepository.getSettings();

yield CurrentSettingsState(_settings);
emit(CurrentSettingsState(_settings));
} else if (event is SettingsUpdatedEvent) {
yield CurrentSettingsState(event.settingsModel);
emit(CurrentSettingsState(event.settingsModel));
add(SaveSettingsEvent(event.settingsModel));
// improve
} else if (event is SaveSettingsEvent) {
if (await _settingsRepository.checkValidJSON()) {
try {
await _settingsRepository.saveSettings(event.settingsModel);
final _settings = await _settingsRepository.getSettings();
yield CurrentSettingsState(_settings);
emit(CurrentSettingsState(_settings));
} catch (e) {
yield SettingsErrorState('Error saving settings.');
emit(SettingsErrorState('Error saving settings.'));
}
} else {
// only possible when app is open and use manually messes up config.json.
yield SettingsErrorState('Corrupted config.json detected, rewriting.');
emit(SettingsErrorState('Corrupted config.json detected, rewriting.'));
final _settings = await _settingsRepository.getSettings();
yield CurrentSettingsState(_settings);
emit(CurrentSettingsState(_settings));
}
}
}
Expand Down
15 changes: 8 additions & 7 deletions lib/bloc/updater_bloc/updater_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:convert';

import 'package:bloc/bloc.dart';
import 'package:bloc_concurrency/bloc_concurrency.dart';
import 'package:equatable/equatable.dart';
import 'package:http/http.dart' as http;
import 'package:url_launcher/url_launcher.dart';
Expand All @@ -10,13 +11,13 @@ part 'updater_event.dart';
part 'updater_state.dart';

class UpdaterBloc extends Bloc<UpdaterEvent, UpdaterState> {
UpdaterBloc() : super(UpdaterState('0.0', '0.0', false, '', ''));
UpdaterBloc() : super(UpdaterState('0.0', '0.0', false, '', '')) {
on<UpdaterEvent>(_onEvent, transformer: sequential());
}
static const URL =
'https://api.github.com/repos/CCExtractor/ccextractor/releases';
@override
Stream<UpdaterState> mapEventToState(
UpdaterEvent event,
) async* {
FutureOr<void> _onEvent(
UpdaterEvent event, Emitter<UpdaterState> emit) async {
if (event is CheckForUpdates) {
var url = Uri.parse(URL);
String changelog = '';
Expand All @@ -38,13 +39,13 @@ class UpdaterBloc extends Bloc<UpdaterEvent, UpdaterState> {

bool updateAvailable =
double.parse(latestVersion) > double.parse(event.currentVersion);
yield state.copyWith(
emit(state.copyWith(
currentVersion: event.currentVersion,
latestVersion: latestVersion,
updateAvailable: updateAvailable,
downloadURL: downloadURL,
changelog: changelog,
);
));
}
if (event is DownloadUpdate) {
await launch(event.downloadURl);
Expand Down
20 changes: 12 additions & 8 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ import 'bloc_observer.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
Bloc.observer = SimpleBlocObserver();
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
setWindowTitle('CCExtractor');
setWindowMinSize(const Size(800, 800));
setWindowMaxSize(const Size(10000, 10000));
}
runApp(
MyApp(),
BlocOverrides.runZoned(
() {
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
techno-disaster marked this conversation as resolved.
Show resolved Hide resolved
setWindowTitle('CCExtractor');
setWindowMinSize(const Size(800, 800));
setWindowMaxSize(const Size(10000, 10000));
}
runApp(
MyApp(),
);
},
blocObserver: SimpleBlocObserver(),
);
}

Expand Down
Loading