Skip to content

Commit

Permalink
Comprehensive refactor & Simplify (#278)
Browse files Browse the repository at this point in the history
* Big refactoring

- Combined theme and locale scopes into settings scope
- Refactored rest client a bit
- Removed [DependenciesMutable]

* Small style changes

* Add codegen dependencies

* Update Flutter SDK version and package
dependencies

* Format

* Update tests for Rest Client
  • Loading branch information
hawkkiller authored Nov 21, 2023
1 parent 545e38d commit 15ea040
Show file tree
Hide file tree
Showing 35 changed files with 757 additions and 797 deletions.
2 changes: 1 addition & 1 deletion .fvm/fvm_config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"flutterSdkVersion": "3.13.0",
"flutterSdkVersion": "3.16.0",
"flavors": {}
}
6 changes: 5 additions & 1 deletion .github/workflows/code-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ jobs:
- name: 📚 Git Checkout
uses: actions/checkout@v3

- name: Get Flutter Version From FVM config
id: get_flutter_version
run: echo "::set-output name=version::$(cat .fvm/fvm_config.json | jq -r '.flutterSdkVersion')"

- name: 🐦 Setup Flutter
uses: subosito/[email protected]
with:
flutter-version: 3.13.0
flutter-version: ${{ steps.get_flutter_version.outputs.version }}
channel: stable
cache: true
cache-key: flutter-:os:-:channel:-:version:-:arch:-:hash:-${{ hashFiles('**/pubspec.lock') }}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/core/bloc/app_bloc_observer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AppBlocObserver extends BlocObserver {
..writeln('Bloc: ${bloc.runtimeType} | ${transition.event.runtimeType}')
..write('Transition: ${transition.currentState.runtimeType}')
..writeln(' -> ${transition.nextState.runtimeType}')
..writeln('New State: ${transition.nextState.toString().limit(100)}');
..writeln('New State: ${transition.nextState.toString().limit(200)}');
logger.info(buffer.toString());
super.onTransition(bloc, transition);
}
Expand All @@ -25,7 +25,7 @@ class AppBlocObserver extends BlocObserver {
void onEvent(Bloc<Object?, Object?> bloc, Object? event) {
final buffer = StringBuffer()
..writeln('Bloc: ${bloc.runtimeType} | ${event.runtimeType}')
..writeln('Event: ${event.toString().limit(100)}');
..writeln('Event: ${event.toString().limit(200)}');
logger.info(buffer.toString());
super.onEvent(bloc, event);
}
Expand Down
12 changes: 12 additions & 0 deletions lib/src/core/localization/localization.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:sizzle_starter/src/core/localization/generated/l10n.dart';
Expand Down Expand Up @@ -36,6 +37,17 @@ final class Localization extends GeneratedLocalization {
/// Locale which is currently used.
final Locale locale;

/// Computes the default locale.
///
/// This is the locale that is used when no locale is specified.
static Locale computeDefaultLocale() {
final locale = PlatformDispatcher.instance.locale;

if (const AppLocalizationDelegate().isSupported(locale)) return locale;

return const Locale('en');
}

/// Obtain [Localization] instance from [BuildContext].
static Localization of(BuildContext context) =>
Localizations.of<Localization>(context, Localization) ??
Expand Down
10 changes: 5 additions & 5 deletions lib/src/core/rest_client/src/exception/network_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import 'package:meta/meta.dart';
/// Base class for all network exceptions
/// {@endtemplate}
@immutable
abstract class NetworkException implements Exception {}
abstract class ClientException implements Exception {}

/// {@template rest_client_exception}
/// If something went wrong on the client side
/// {@endtemplate}
base class RestClientException implements NetworkException {
base class RestClientException implements ClientException {
/// {@macro rest_client_exception}
const RestClientException({this.message, this.statusCode});

Expand Down Expand Up @@ -48,7 +48,7 @@ final class ConnectionException extends RestClientException {
const ConnectionException({super.message, super.statusCode});

@override
String toString() => 'NoInternetException('
String toString() => 'ConnectionException('
'message: $message,'
'statusCode: $statusCode'
')';
Expand All @@ -57,7 +57,7 @@ final class ConnectionException extends RestClientException {
/// {@template internal_server_exception}
/// If something went wrong on the server side
/// {@endtemplate}
base class InternalServerException implements NetworkException {
base class InternalServerException implements ClientException {
/// {@macro internal_server_exception}
const InternalServerException({this.message, this.statusCode});

Expand All @@ -68,7 +68,7 @@ base class InternalServerException implements NetworkException {
final int? statusCode;

@override
String toString() => 'InternalServerErrorException('
String toString() => 'InternalServerException('
'message: $message,'
'statusCode: $statusCode'
')';
Expand Down
9 changes: 5 additions & 4 deletions lib/src/core/rest_client/src/rest_client_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ abstract base class RestClientBase implements RestClient {
/// The base url for the client
final Uri baseUri;

static final _jsonUTF8 = json.fuse(utf8);

/// Encodes [body] to JSON and then to UTF8
@protected
@visibleForTesting
List<int> encodeBody(Map<String, Object?> body) {
try {
return json.fuse(utf8).encode(body);
return _jsonUTF8.encode(body);
} on Object catch (e, stackTrace) {
Error.throwWithStackTrace(
RestClientException(message: 'Error occured during encoding $e'),
Expand Down Expand Up @@ -64,8 +66,7 @@ abstract base class RestClientBase implements RestClient {
} else if (body is Map<String, Object?>) {
result = body;
} else if (body is List<int>) {
// if length is biggger than 25kb then use isolate
if (body.length > 25 * 1024) {
if (body.length > 10000) {
result = await Isolate.run(
() => json.decode(utf8.decode(body)) as Map<String, Object?>,
);
Expand All @@ -91,7 +92,7 @@ abstract base class RestClientBase implements RestClient {
}

return null;
} on NetworkException {
} on ClientException {
rethrow;
} on Object catch (e, stackTrace) {
Error.throwWithStackTrace(
Expand Down
4 changes: 2 additions & 2 deletions lib/src/core/rest_client/src/rest_client_dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:meta/meta.dart';
import 'package:sizzle_starter/src/core/rest_client/rest_client.dart';

/// {@template rest_client_dio}
/// Rest client that uses [Dio] to send requests
/// Rest client that uses [Dio] as HTTP library.
/// {@endtemplate}
final class RestClientDio extends RestClientBase {
/// {@macro rest_client_dio}
Expand Down Expand Up @@ -37,7 +37,7 @@ final class RestClientDio extends RestClientBase {
);

return decodeResponse(response.data, statusCode: response.statusCode);
} on NetworkException {
} on ClientException {
rethrow;
} on DioException catch (e) {
if (e.type == DioExceptionType.connectionError ||
Expand Down
15 changes: 15 additions & 0 deletions lib/src/core/utils/extensions/context_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,19 @@ extension ContextExtension on BuildContext {
'a $T of the exact type',
'out_of_scope',
));

/// Maybe inherit specific aspect from [InheritedModel].
T? maybeInheritFrom<A extends Object, T extends InheritedModel<A>>(
A? aspect,
) =>
InheritedModel.inheritFrom<T>(this, aspect: aspect);

/// Inherit specific aspect from [InheritedModel].
T inheritFrom<A extends Object, T extends InheritedModel<A>>({A? aspect}) =>
InheritedModel.inheritFrom<T>(this, aspect: aspect) ??
(throw ArgumentError(
'Out of scope, not found inherited model '
'a $T of the exact type',
'out_of_scope',
));
}
11 changes: 8 additions & 3 deletions lib/src/core/utils/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart' as logging;

/// Logger instance
/// Logger instance declared as global variable
///
/// Usage:
/// ```dart
/// logger.info('Hello World!');
/// ```
final Logger logger = LoggerLogging();

/// Typedef for the log formatter
Expand Down Expand Up @@ -201,11 +206,11 @@ String _formatLoggerMessage({
}
buffer.write(log.message);
if (log.error != null) {
buffer.write(' | ');
buffer.writeln();
buffer.write(log.error);
}
if (log.stackTrace != null) {
buffer.write(' | ');
buffer.writeln();
buffer.writeln(log.stackTrace);
}

Expand Down
2 changes: 0 additions & 2 deletions lib/src/core/utils/pattern_match.dart

This file was deleted.

19 changes: 10 additions & 9 deletions lib/src/core/utils/preferences_dao.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ abstract base class PreferencesDao {
final SharedPreferences _sharedPreferences;

/// {@macro preferences_dao}
PreferencesDao(this._sharedPreferences);
const PreferencesDao({required SharedPreferences sharedPreferences})
: _sharedPreferences = sharedPreferences;

/// Obtain [bool] entry from the preferences.
PreferencesEntry<bool> boolEntry(String key) => _PreferencesEntry<bool>(
Expand Down Expand Up @@ -48,12 +49,12 @@ abstract base class PreferencesDao {
/// This is used to get and set values in the preferences.
/// {@endtemplate}
abstract base class PreferencesEntry<T extends Object> {
/// The key of the entry in the preferences.
String get key;

/// {@macro preferences_entry}
const PreferencesEntry();

/// The key of the entry in the preferences.
String get key;

/// Obtain the value of the entry from the preferences.
T? read();

Expand All @@ -69,13 +70,16 @@ abstract base class PreferencesEntry<T extends Object> {
}

final class _PreferencesEntry<T extends Object> extends PreferencesEntry<T> {
final SharedPreferences _sharedPreferences;

_PreferencesEntry({
required SharedPreferences sharedPreferences,
required this.key,
}) : _sharedPreferences = sharedPreferences;

final SharedPreferences _sharedPreferences;

@override
final String key;

@override
T? read() {
final value = _sharedPreferences.get(key);
Expand All @@ -89,9 +93,6 @@ final class _PreferencesEntry<T extends Object> extends PreferencesEntry<T> {
);
}

@override
final String key;

@override
Future<void> set(T value) => switch (value) {
final int value => _sharedPreferences.setInt(key, value),
Expand Down
51 changes: 0 additions & 51 deletions lib/src/feature/app/data/locale_datasource.dart

This file was deleted.

28 changes: 0 additions & 28 deletions lib/src/feature/app/data/locale_repository.dart

This file was deleted.

Loading

0 comments on commit 15ea040

Please sign in to comment.