This repository has been archived by the owner on Aug 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
additionaly fixed some code problems
- Loading branch information
Qizot
committed
Mar 22, 2020
1 parent
7e86eb4
commit 6f9edc0
Showing
23 changed files
with
519 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
frontend/coronavirus_visualizer/lib/src/bloc/saved_countries/bloc.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export 'saved_countries_bloc.dart'; | ||
export 'saved_countries_event.dart'; | ||
export 'saved_countries_state.dart'; |
97 changes: 97 additions & 0 deletions
97
frontend/coronavirus_visualizer/lib/src/bloc/saved_countries/saved_countries_bloc.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import 'dart:async'; | ||
import 'package:coronavirus_visualizer/src/services/countries.dart'; | ||
import 'package:localstorage/localstorage.dart'; | ||
import 'package:bloc/bloc.dart'; | ||
import 'package:coronavirus_visualizer/src/bloc/saved_countries/bloc.dart'; | ||
|
||
class SavedCountriesBloc extends Bloc<SavedCountriesEvent, SavedCountriesState> { | ||
|
||
final _storage = LocalStorage('coronavirus'); | ||
int _fetched = 0; | ||
int _errors = 0; | ||
int _successes = 0; | ||
|
||
|
||
@override | ||
SavedCountriesState get initialState => SavedCountriesUninitialized(); | ||
|
||
@override | ||
Stream<SavedCountriesState> mapEventToState(SavedCountriesEvent event) async* { | ||
if (event is SavedCountriesAddCountry) { | ||
yield* _mapAddCountry(event); | ||
} | ||
if (event is SavedCountriesRemoveCountry) { | ||
yield* _mapRemoveCountry(event); | ||
} | ||
if (event is SavedCountriesFetch) { | ||
yield* _mapFetch(event); | ||
} | ||
} | ||
|
||
List<Country> _getCountries() { | ||
return (_storage.getItem('countries') as List) | ||
?.map((country) => Country.fromJson(country)) | ||
?.toList() ?? []; | ||
} | ||
|
||
Stream<SavedCountriesState> _mapAddCountry(SavedCountriesAddCountry event) async* { | ||
try { | ||
final countries = _getCountries(); | ||
|
||
if (countries.indexOf(event.country, 0) != -1) { | ||
_errors += 1; | ||
yield SavedCountriesError(error: "Country has already been saved!", version: _errors); | ||
} else { | ||
countries.add(event.country); | ||
await _storage.setItem('countries', | ||
countries | ||
.map((c) => c.toJson()) | ||
.toList() | ||
); | ||
_successes += 1; | ||
yield SavedCountriesSuccess(message: "Country has been saved!", version: _successes); | ||
add(SavedCountriesFetch()); | ||
} | ||
} catch (error) { | ||
_errors += 1; | ||
yield SavedCountriesError(error: "Failed to save country!", version: _errors); | ||
} | ||
} | ||
|
||
Stream<SavedCountriesState> _mapRemoveCountry(SavedCountriesRemoveCountry event) async* { | ||
try { | ||
final countries = _getCountries(); | ||
|
||
if (countries.indexOf(event.country, 0) == -1) { | ||
|
||
_errors += 1; | ||
yield SavedCountriesError(error: "Country was not previously saved!", version: _errors); | ||
} else { | ||
countries.remove(event.country); | ||
_storage.setItem('countries', countries.map((c) => c.toJson()).toList()); | ||
|
||
_successes += 1; | ||
yield SavedCountriesSuccess(message: "Country has been removed!", version: _successes); | ||
add(SavedCountriesFetch()); | ||
} | ||
|
||
} catch (error) { | ||
|
||
_errors += 1; | ||
yield SavedCountriesError(error: "Failed to remove country!", version: _errors); | ||
} | ||
} | ||
|
||
Stream<SavedCountriesState> _mapFetch(SavedCountriesFetch event) async* { | ||
try { | ||
final countries = _getCountries(); | ||
|
||
_fetched += 1; | ||
yield SavedCountriesFetched(countries: countries, version: _fetched); | ||
} catch (error) { | ||
|
||
_errors += 1; | ||
yield SavedCountriesError(error: "Failed to fetch saved countries!", version: _errors); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
frontend/coronavirus_visualizer/lib/src/bloc/saved_countries/saved_countries_event.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import 'package:coronavirus_visualizer/src/services/countries.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:meta/meta.dart'; | ||
|
||
|
||
abstract class SavedCountriesEvent extends Equatable { | ||
const SavedCountriesEvent(); | ||
|
||
@override | ||
List<Object> get props => []; | ||
} | ||
|
||
class SavedCountriesAddCountry extends SavedCountriesEvent { | ||
final Country country; | ||
|
||
SavedCountriesAddCountry({@required this.country}); | ||
|
||
@override | ||
List<Object> get props => [country]; | ||
|
||
@override | ||
String toString() { | ||
return 'SavedCountriesAddCountry { country: $country }'; | ||
} | ||
} | ||
|
||
class SavedCountriesRemoveCountry extends SavedCountriesEvent { | ||
final Country country; | ||
|
||
SavedCountriesRemoveCountry({@required this.country}); | ||
|
||
@override | ||
List<Object> get props => [country]; | ||
|
||
@override | ||
String toString() { | ||
return 'SavedCountriesRemoveCountry { country: $country }'; | ||
} | ||
} | ||
|
||
class SavedCountriesFetch extends SavedCountriesEvent { | ||
@override | ||
String toString() { | ||
return 'SavedCountriesFetch { }'; | ||
} | ||
} | ||
|
61 changes: 61 additions & 0 deletions
61
frontend/coronavirus_visualizer/lib/src/bloc/saved_countries/saved_countries_state.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import 'package:coronavirus_visualizer/src/services/countries.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
|
||
|
||
abstract class SavedCountriesState extends Equatable { | ||
const SavedCountriesState(); | ||
|
||
@override | ||
List<Object> get props => []; | ||
} | ||
|
||
class SavedCountriesUninitialized extends SavedCountriesState { | ||
@override | ||
String toString() => 'SavedCountriesUninitialized { }'; | ||
|
||
} | ||
|
||
class SavedCountriesError extends SavedCountriesState { | ||
final String error; | ||
final int version; | ||
|
||
SavedCountriesError({this.error, this.version}); | ||
|
||
@override | ||
List<Object> get props => [version, error]; | ||
|
||
@override | ||
String toString() => 'SavedCountriesStateError { version: $version, error: $error }'; | ||
} | ||
|
||
class SavedCountriesSuccess extends SavedCountriesState { | ||
final String message; | ||
final int version; | ||
|
||
SavedCountriesSuccess({this.message, this.version}); | ||
|
||
@override | ||
List<Object> get props => [version, message]; | ||
|
||
@override | ||
String toString() => 'SavedCountriesSuccess { version: $version, message: $message }'; | ||
} | ||
|
||
class SavedCountriesLoading extends SavedCountriesState { | ||
@override | ||
String toString() => 'SavedCountriesStateLoading { }'; | ||
} | ||
|
||
class SavedCountriesFetched extends SavedCountriesState { | ||
final List<Country> countries; | ||
final int version; | ||
|
||
SavedCountriesFetched({this.countries, this.version}); | ||
|
||
@override | ||
List<Object> get props => [version, countries]; | ||
|
||
@override | ||
String toString() => 'SavedCountriesFetched { version: $version, countries: $countries }'; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 20 additions & 3 deletions
23
frontend/coronavirus_visualizer/lib/src/services/countries.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.