-
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.
- Loading branch information
Showing
31 changed files
with
953 additions
and
92 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,19 @@ | ||
abstract class UseCase<Type, String>{ | ||
Future<Type> call({required String cityName}); | ||
} | ||
|
||
abstract class SaveCityUseCase{ | ||
Future<bool> call({required String cityName, required String colorValue}); | ||
} | ||
|
||
abstract class GetCitiesUseCase{ | ||
List<String> call(); | ||
} | ||
|
||
abstract class GetColorsUseCase{ | ||
List<String> call(); | ||
} | ||
|
||
abstract class DeleteCityUseCase{ | ||
Future<void> call(String cityName); | ||
} |
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,86 @@ | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
abstract class Storage { | ||
SharedPreferences? prefs; | ||
Future<bool> saveCity(String cityName, String colorValue); | ||
List<String> getCities(); | ||
List<String> getColors(); | ||
Future<void> deleteCity(String cityName); | ||
} | ||
|
||
class StorageImplementation implements Storage{ | ||
SharedPreferences? prefs; | ||
|
||
StorageImplementation(){ | ||
_initPrefs(); | ||
} | ||
|
||
Future<void> _initPrefs() async { | ||
prefs = await SharedPreferences.getInstance(); | ||
await _fillEmptyStorage(); | ||
} | ||
|
||
Future<void> ensurePrefsInitialized() async { | ||
if (prefs == null) { | ||
await _initPrefs(); | ||
} | ||
} | ||
|
||
Future<void> _fillEmptyStorage() async{ | ||
List<String> defaultCities = ["Paris", "New York", "Sydney"]; | ||
List<String> defaultColors = ["4294959426", "4282566399", "4294927572"]; | ||
|
||
List<String> cities = getCities(); | ||
List<String> colors = getColors(); | ||
|
||
if(cities.length == 0 || colors.length == 0){ | ||
await prefs!.setStringList('cities', defaultCities); | ||
await prefs!.setStringList('colors', defaultColors); | ||
} | ||
} | ||
|
||
@override | ||
Future<bool> saveCity(String cityName, String colorValue) async { | ||
final List<String> cities = getCities(); | ||
final List<String> colors = getColors(); | ||
|
||
if(!cities.contains(cityName)){ | ||
cities.add(cityName); | ||
colors.add(colorValue); | ||
await prefs!.setStringList('cities', cities); | ||
await prefs!.setStringList('colors', colors); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@override | ||
List<String> getCities() { | ||
final List<String> cities = prefs!.getStringList('cities') ?? []; | ||
|
||
return cities; | ||
} | ||
|
||
List<String> getColors(){ | ||
final List<String> colors = prefs!.getStringList('colors') ?? []; | ||
|
||
return colors; | ||
} | ||
|
||
@override | ||
Future<void> deleteCity(String cityName) async { | ||
List<String> cities = getCities(); | ||
List<String> colors = getColors(); | ||
int colorIndex = cities.indexWhere((element) => element == cityName); | ||
|
||
cities.remove(cityName); | ||
colors.removeAt(colorIndex); | ||
|
||
await prefs!.remove('cities'); | ||
await prefs!.remove('colors'); | ||
await prefs!.setStringList('cities', cities); | ||
await prefs!.setStringList('colors', colors); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
lib/features/app/data/repository/storage_repository_implementation.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,28 @@ | ||
import 'package:weathque/features/app/data/data_sources/local/storage.dart'; | ||
import 'package:weathque/features/app/domain/repository/storage_repository.dart'; | ||
|
||
class StorageRepositoryImplementation implements StorageRepository{ | ||
final Storage _storage; | ||
|
||
StorageRepositoryImplementation(this._storage); | ||
|
||
@override | ||
Future<bool> saveCity(String cityName, String colorValue) async{ | ||
return await _storage.saveCity(cityName, colorValue); | ||
} | ||
|
||
@override | ||
List<String> getCities() { | ||
return _storage.getCities(); | ||
} | ||
|
||
@override | ||
List<String> getColors() { | ||
return _storage.getColors(); | ||
} | ||
|
||
@override | ||
Future<void> deleteCity(String cityName) async { | ||
await _storage.deleteCity(cityName); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
abstract class StorageRepository{ | ||
Future<bool> saveCity(String cityName, String colorValue); | ||
List<String> getCities(); | ||
List<String> getColors(); | ||
Future<void> deleteCity(String cityName); | ||
} |
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,13 @@ | ||
import 'package:weathque/core/usecases/usecase.dart'; | ||
import 'package:weathque/features/app/domain/repository/storage_repository.dart'; | ||
|
||
class DeleteCityUseCaseImplementation implements DeleteCityUseCase{ | ||
final StorageRepository _storage; | ||
|
||
DeleteCityUseCaseImplementation(this._storage); | ||
|
||
@override | ||
Future<void> call(String cityName) { | ||
return _storage.deleteCity(cityName); | ||
} | ||
} |
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,13 @@ | ||
import 'package:weathque/core/usecases/usecase.dart'; | ||
import 'package:weathque/features/app/domain/repository/storage_repository.dart'; | ||
|
||
class GetCitiesUseCaseImplementation implements GetCitiesUseCase{ | ||
final StorageRepository _storage; | ||
|
||
GetCitiesUseCaseImplementation(this._storage); | ||
|
||
@override | ||
List<String> call() { | ||
return _storage.getCities(); | ||
} | ||
} |
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,13 @@ | ||
import 'package:weathque/core/usecases/usecase.dart'; | ||
import 'package:weathque/features/app/domain/repository/storage_repository.dart'; | ||
|
||
class GetColorsUseCaseImplementation implements GetColorsUseCase{ | ||
final StorageRepository _storage; | ||
|
||
GetColorsUseCaseImplementation(this._storage); | ||
|
||
@override | ||
List<String> call() { | ||
return _storage.getColors(); | ||
} | ||
} |
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,13 @@ | ||
import 'package:weathque/core/usecases/usecase.dart'; | ||
import 'package:weathque/features/app/domain/repository/storage_repository.dart'; | ||
|
||
class SaveCityUseCaseImplementation implements SaveCityUseCase{ | ||
final StorageRepository _storage; | ||
|
||
SaveCityUseCaseImplementation(this._storage); | ||
|
||
@override | ||
Future<bool> call({required String cityName, required String colorValue}) { | ||
return _storage.saveCity(cityName, colorValue); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
lib/features/app/presentation/bloc/add_city/cities_changed_cubit.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,11 @@ | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:weathque/core/dependency_injection.dart'; | ||
import 'package:weathque/features/app/domain/usecases/get_cities.dart'; | ||
|
||
class CitiesChangedCubit extends Cubit<List<String>>{ | ||
CitiesChangedCubit() : super(locator<GetCitiesUseCaseImplementation>()()); | ||
|
||
void call(){ | ||
emit(locator<GetCitiesUseCaseImplementation>()()); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
lib/features/app/presentation/bloc/blocs_provider_builder.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,31 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:weathque/core/dependency_injection.dart'; | ||
import 'package:weathque/features/app/presentation/bloc/add_city/cities_changed_cubit.dart'; | ||
import 'package:weathque/features/app/presentation/bloc/get_current_weather/get_current_weather_bloc.dart'; | ||
import 'package:weathque/features/app/presentation/bloc/get_current_weather/get_current_weather_event.dart'; | ||
import 'package:weathque/features/app/presentation/bloc/get_weather_forecast/get_weather_forecast_bloc.dart'; | ||
import 'package:weathque/features/app/presentation/bloc/get_weather_forecast/get_weather_forecast_event.dart'; | ||
|
||
class BlocsProviderBuilder extends StatelessWidget { | ||
final Widget child; | ||
final BuildContext context; | ||
final List<BlocProvider> providers = [ | ||
BlocProvider<GetCurrentWeatherBloc>( | ||
create: (buildContext) => locator()..add(const GetCurrentWeather()) | ||
), | ||
BlocProvider<GetWeatherForecastBloc>( | ||
create: (buildContext) => locator()..add(const GetWeatherForecast()), | ||
), | ||
BlocProvider<CitiesChangedCubit>( | ||
create: (buildContext) => locator<CitiesChangedCubit>(), | ||
) | ||
]; | ||
|
||
BlocsProviderBuilder({super.key, required this.child, required this.context}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MultiBlocProvider(providers: providers, child: child); | ||
} | ||
} |
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.