Skip to content

Commit

Permalink
http_client_hoc081098, cancellation_token_hoc081098, Future to …
Browse files Browse the repository at this point in the history
…`Single` (#73)

* draft http_client_hoc081098

* further refactor

* further refactor

* further refactor

* further refactor

* further refactor

* rxdart_ext: ^0.2.6

* deps

* fix

* fix
  • Loading branch information
hoc081098 authored Nov 18, 2022
1 parent 27385fa commit 31eb33c
Show file tree
Hide file tree
Showing 27 changed files with 539 additions and 474 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/flutter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ jobs:
- name: Gen code
run: flutter packages pub run build_runner build --delete-conflicting-outputs

- name: Build APK
run: flutter build apk --no-shrink
- name: Build Debug APK
run: flutter build apk --debug --no-shrink

- name: Upload APK
if: ${{ matrix.flutter-channel == 'stable' }}
uses: actions/upload-artifact@v3
with:
name: app-${{ matrix.flutter-channel }}
path: build/app/outputs/apk/release/app-release.apk
path: build/app/outputs/apk/debug/app-debug.apk

3 changes: 2 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ linter:
- always_declare_return_types
- prefer_single_quotes
- unawaited_futures
- unsafe_html
- unsafe_html
- avoid_slow_async_io
6 changes: 6 additions & 0 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
PODS:
- CryptoSwift (1.2.0)
- cupertino_http (0.0.1):
- Flutter
- Flutter (1.0.0)
- image_picker_ios (0.0.1):
- Flutter
Expand All @@ -8,6 +10,7 @@ PODS:

DEPENDENCIES:
- CryptoSwift (~> 1.2.0)
- cupertino_http (from `.symlinks/plugins/cupertino_http/ios`)
- Flutter (from `Flutter`)
- image_picker_ios (from `.symlinks/plugins/image_picker_ios/ios`)
- shared_preferences_ios (from `.symlinks/plugins/shared_preferences_ios/ios`)
Expand All @@ -17,6 +20,8 @@ SPEC REPOS:
- CryptoSwift

EXTERNAL SOURCES:
cupertino_http:
:path: ".symlinks/plugins/cupertino_http/ios"
Flutter:
:path: Flutter
image_picker_ios:
Expand All @@ -26,6 +31,7 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
CryptoSwift: 40e374e45291d8dceedcb0d6184da94533eaabdf
cupertino_http: 5f8b1161107fe6c8d94a0c618735a033d93fa7db
Flutter: 50d75fe2f02b26cc09d224853bb45737f8b3214a
image_picker_ios: b786a5dcf033a8336a657191401bfdf12017dabb
shared_preferences_ios: 548a61f8053b9b8a49ac19c1ffbc8b92c50d68ad
Expand Down
2 changes: 1 addition & 1 deletion lib/data/exception/local_data_source_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ class LocalDataSourceException implements Exception {

@override
String toString() =>
'LocalDataSourceException{message=$message, error=$error}';
'LocalDataSourceException{message=$message, error=$error, stackTrace=$stackTrace}';
}
12 changes: 6 additions & 6 deletions lib/data/exception/remote_data_source_exception.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'dart:io';
class RemoteDataSourceException implements Exception {
final String message;
final Object error;
final StackTrace stackTrace;

class RemoteDataSourceException extends HttpException {
final int statusCode;

RemoteDataSourceException(this.statusCode, String message) : super(message);
const RemoteDataSourceException(this.message, this.error, this.stackTrace);

@override
String toString() =>
'RemoteDataSourceException{statusCode=$statusCode, message=$message}';
'RemoteDataSourceException{message=$message, error=$error, stackTrace=$stackTrace}';
}
7 changes: 4 additions & 3 deletions lib/data/local/local_data_source.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import 'package:node_auth/data/local/entities/user_and_token_entity.dart';
import 'package:node_auth/domain/models/app_error.dart';

abstract class LocalDataSource {
/// Returns a single-subscription stream that emits [UserAndTokenEntity] or null
Stream<UserAndTokenEntity?> get userAndToken$;

/// Returns a future that completes with a [UserAndTokenEntity] value or null
Future<UserAndTokenEntity?> get userAndToken;
Single<UserAndTokenEntity?> get userAndToken;

/// Save [userAndToken] into local storage.
/// Throws [LocalDataSourceException] if saving is failed
Future<void> saveUserAndToken(UserAndTokenEntity userAndToken);
Single<void> saveUserAndToken(UserAndTokenEntity userAndToken);

/// Remove user and token from local storage.
/// Throws [LocalDataSourceException] if removing is failed
Future<void> removeUserAndToken();
Single<void> removeUserAndToken();
}

abstract class Crypto {
Expand Down
33 changes: 19 additions & 14 deletions lib/data/local/shared_pref_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:node_auth/data/exception/local_data_source_exception.dart';
import 'package:node_auth/data/local/entities/user_and_token_entity.dart';
import 'package:node_auth/data/local/local_data_source.dart';
import 'package:rx_shared_preferences/rx_shared_preferences.dart';
import 'package:rxdart/rxdart.dart';
import 'package:rxdart_ext/rxdart_ext.dart';

class SharedPrefUtil implements LocalDataSource {
static const _kUserTokenKey = 'com.hoc.node_auth_flutter.user_and_token';
Expand All @@ -15,23 +15,28 @@ class SharedPrefUtil implements LocalDataSource {
const SharedPrefUtil(this._rxPrefs, this._crypto);

@override
Future<void> removeUserAndToken() =>
_rxPrefs.remove(_kUserTokenKey).onError<Object>((e, s) =>
throw LocalDataSourceException('Cannot delete user and token', e, s));
Single<void> removeUserAndToken() => Single.fromCallable(
() => _rxPrefs.remove(_kUserTokenKey).onError<Object>((e, s) =>
throw LocalDataSourceException(
'Cannot delete user and token', e, s)),
);

@override
Future<void> saveUserAndToken(UserAndTokenEntity userAndToken) {
return _rxPrefs
.write<UserAndTokenEntity>(_kUserTokenKey, userAndToken, _toString)
.onError<Object>((e, s) =>
throw LocalDataSourceException('Cannot save user and token', e, s));
}
Single<void> saveUserAndToken(UserAndTokenEntity userAndToken) =>
Single.fromCallable(
() => _rxPrefs
.write<UserAndTokenEntity>(_kUserTokenKey, userAndToken, _toString)
.onError<Object>((e, s) => throw LocalDataSourceException(
'Cannot save user and token', e, s)),
);

@override
Future<UserAndTokenEntity?> get userAndToken => _rxPrefs
.read<UserAndTokenEntity>(_kUserTokenKey, _toEntity)
.onError<Object>((e, s) =>
throw LocalDataSourceException('Cannot read user and token', e, s));
Single<UserAndTokenEntity?> get userAndToken => Single.fromCallable(
() => _rxPrefs
.read<UserAndTokenEntity>(_kUserTokenKey, _toEntity)
.onError<Object>((e, s) => throw LocalDataSourceException(
'Cannot read user and token', e, s)),
);

@override
Stream<UserAndTokenEntity?> get userAndToken$ => _rxPrefs
Expand Down
37 changes: 37 additions & 0 deletions lib/data/mappers.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
part of 'user_repository_imp.dart';

abstract class _Mappers {
///
/// Convert error to [Failure]
///
static AppError errorToAppError(Object e, StackTrace s) {
if (e is CancellationException) {
return const AppCancellationError();
}

if (e is RemoteDataSourceException) {
if (e.error is CancellationException) {
return const AppCancellationError();
}
return AppError(
message: e.message,
error: e,
stackTrace: s,
);
}

if (e is LocalDataSourceException) {
if (e.error is CancellationException) {
return const AppCancellationError();
}
return AppError(
message: e.message,
error: e,
stackTrace: s,
);
}

return AppError(
message: e.toString(),
error: e,
stackTrace: s,
);
}

/// Entity -> Domain
static AuthenticationState userAndTokenEntityToDomainAuthState(
UserAndTokenEntity? entity) {
Expand Down
Loading

0 comments on commit 31eb33c

Please sign in to comment.