This repository has been archived by the owner on Aug 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for imperial units (#242)
Co-authored-by: mhmdanas <triallax@tutanota.com>
- Loading branch information
1 parent
f3139c6
commit a652d03
Showing
22 changed files
with
772 additions
and
54 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
packages/clima_data/lib/data_sources/unit_system_local_data_source.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,38 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import 'package:clima_core/either.dart'; | ||
import 'package:clima_core/failure.dart'; | ||
import 'package:clima_data/models/unit_system_model.dart'; | ||
import 'package:clima_data/providers.dart'; | ||
import 'package:riverpod/riverpod.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
const _prefsKey = 'unit_system'; | ||
|
||
class UnitSystemLocalDataSource { | ||
UnitSystemLocalDataSource(this._prefs); | ||
|
||
final SharedPreferences _prefs; | ||
|
||
Future<Either<Failure, UnitSystemModel?>> getUnitSystem() async { | ||
final string = _prefs.getString(_prefsKey); | ||
|
||
if (string == null) return const Right(null); | ||
|
||
return Right(UnitSystemModel.parse(string)); | ||
} | ||
|
||
Future<Either<Failure, void>> setUnitSystem(UnitSystemModel model) async { | ||
await _prefs.setString(_prefsKey, model.toString()); | ||
|
||
return const Right(null); | ||
} | ||
} | ||
|
||
final unitSystemLocalDataSourceProvider = Provider( | ||
(ref) => UnitSystemLocalDataSource(ref.watch(sharedPreferencesProvider)), | ||
); |
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,41 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import 'package:clima_domain/entities/unit_system.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
|
||
class UnitSystemModel extends Equatable { | ||
const UnitSystemModel(this.unitSystem); | ||
|
||
final UnitSystem unitSystem; | ||
|
||
factory UnitSystemModel.parse(String string) { | ||
switch (string) { | ||
case 'metric': | ||
return const UnitSystemModel(UnitSystem.metric); | ||
|
||
case 'imperial': | ||
return const UnitSystemModel(UnitSystem.imperial); | ||
|
||
default: | ||
throw ArgumentError(); | ||
} | ||
} | ||
|
||
@override | ||
String toString() { | ||
switch (unitSystem) { | ||
case UnitSystem.metric: | ||
return 'metric'; | ||
|
||
case UnitSystem.imperial: | ||
return 'imperial'; | ||
} | ||
} | ||
|
||
@override | ||
List<Object?> get props => [unitSystem]; | ||
} |
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,32 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import 'package:clima_core/either.dart'; | ||
import 'package:clima_core/failure.dart'; | ||
import 'package:clima_data/data_sources/unit_system_local_data_source.dart'; | ||
import 'package:clima_data/models/unit_system_model.dart'; | ||
import 'package:clima_domain/entities/unit_system.dart'; | ||
import 'package:clima_domain/repos/unit_system_repo.dart'; | ||
import 'package:riverpod/riverpod.dart'; | ||
|
||
class UnitSystemRepoImpl implements UnitSystemRepo { | ||
final UnitSystemLocalDataSource _localDataSource; | ||
|
||
UnitSystemRepoImpl(this._localDataSource); | ||
|
||
@override | ||
Future<Either<Failure, UnitSystem>> getUnitSystem() async => | ||
(await _localDataSource.getUnitSystem()) | ||
.map((model) => model?.unitSystem ?? UnitSystem.metric); | ||
|
||
@override | ||
Future<Either<Failure, void>> setUnitSystem(UnitSystem unitSystem) => | ||
_localDataSource.setUnitSystem(UnitSystemModel(unitSystem)); | ||
} | ||
|
||
final unitSystemRepoImplProvider = Provider( | ||
(ref) => UnitSystemRepoImpl(ref.watch(unitSystemLocalDataSourceProvider)), | ||
); |
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
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
Oops, something went wrong.