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 custom API key settings section (#195)
* Add API Key Settings Section * Implement Reset API Key in clima_data * Fixed an embarrasing mistake * Remove unrelated changes * Add `ApiKeyModel` * [data] use snake case for api key prefs key It's more consistent with other prefs keys in Clima. * Delete loading_screen.dart * Fix some errors * Fix some more errors * Some refactoring * Only use double quotes when needed * Fix formatting issues * Fix formatting issues * Add `ApiKeyStateNotifier` class, and use it in `ApiKeyDialog` * Show error when API key is invalid * Use correct response code for invalid API key error * Add dynamic subtitle for the API key settings tile * Show snackbar when API key was updated successfully * Set larger `errorMaxLines` for API key text field * Update packages/clima_ui/lib/screens/settings_screen.dart Co-authored-by: Mohammed Anas <[email protected]> * Add comment suggestions * Update packages/clima_ui/lib/screens/weather_screen.dart Co-authored-by: Mohammed Anas <[email protected]> * Refactor showFailureSnackBar to use the showSnackBar function * Fixed an error where errorMaxLines property was placed outside the InputDecoration widget * Remove unused final * Organize imports * Add clarification and TODO comment * Clarify info about shared API key * Re-add empty line Why was it removed anyway? Co-authored-by: mhmdanas <[email protected]>
- Loading branch information
1 parent
a652d03
commit 8a1d94b
Showing
13 changed files
with
415 additions
and
46 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
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:equatable/equatable.dart'; | ||
|
||
const _defaultApiKey = '0cca00b6155fcac417cc140a5deba9a4'; | ||
|
||
class ApiKeyModel extends Equatable { | ||
// There's an underscore here because `default` can't be an identifier in | ||
// Dart. | ||
const ApiKeyModel.default_() | ||
: apiKey = _defaultApiKey, | ||
isCustom = false; | ||
|
||
const ApiKeyModel.custom(this.apiKey) : isCustom = true; | ||
|
||
factory ApiKeyModel.parse(String? string) { | ||
if (string == null) return const ApiKeyModel.default_(); | ||
|
||
return ApiKeyModel.custom(string); | ||
} | ||
|
||
final String apiKey; | ||
|
||
final bool isCustom; | ||
|
||
@override | ||
List<Object?> get props => [apiKey, isCustom]; | ||
} |
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
78 changes: 78 additions & 0 deletions
78
packages/clima_ui/lib/state_notifiers/api_key_state_notifier.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,78 @@ | ||
/* | ||
* 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/failure.dart'; | ||
import 'package:clima_data/models/api_key_model.dart'; | ||
import 'package:clima_data/repos/api_key_repo.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:meta/meta.dart'; | ||
import 'package:riverpod/riverpod.dart'; | ||
|
||
@sealed | ||
@immutable | ||
abstract class ApiKeyState extends Equatable { | ||
const ApiKeyState(); | ||
|
||
ApiKeyModel? get apiKey => null; | ||
|
||
@override | ||
List<Object?> get props => const []; | ||
} | ||
|
||
class Empty extends ApiKeyState { | ||
const Empty(); | ||
} | ||
|
||
class Loading extends ApiKeyState { | ||
const Loading(); | ||
} | ||
|
||
class Loaded extends ApiKeyState { | ||
const Loaded(this.apiKey); | ||
|
||
@override | ||
final ApiKeyModel apiKey; | ||
|
||
@override | ||
List<Object?> get props => [apiKey]; | ||
} | ||
|
||
class Error extends ApiKeyState { | ||
const Error(this.failure, {this.apiKey}); | ||
|
||
final Failure failure; | ||
|
||
@override | ||
final ApiKeyModel? apiKey; | ||
|
||
@override | ||
List<Object?> get props => [failure, apiKey]; | ||
} | ||
|
||
class ApiKeyStateNotifier extends StateNotifier<ApiKeyState> { | ||
ApiKeyStateNotifier(this._apiKeyRepo) : super(const Empty()); | ||
|
||
final ApiKeyRepo _apiKeyRepo; | ||
|
||
Future<void> loadApiKey() async { | ||
state = const Loading(); | ||
final data = await _apiKeyRepo.getApiKey(); | ||
state = data.fold((failure) => Error(failure), (city) => Loaded(city)); | ||
} | ||
|
||
Future<void> setApiKey(ApiKeyModel apiKey) async { | ||
(await _apiKeyRepo.setApiKey(apiKey)).fold((failure) { | ||
state = Error(failure, apiKey: state.apiKey); | ||
}, (_) { | ||
state = Loaded(apiKey); | ||
}); | ||
} | ||
} | ||
|
||
final apiKeyStateNotifierProvider = | ||
StateNotifierProvider<ApiKeyStateNotifier, ApiKeyState>( | ||
(ref) => ApiKeyStateNotifier(ref.watch(apiKeyRepoProvider)), | ||
); |
Oops, something went wrong.