Skip to content

Commit

Permalink
WIP Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ashtanko committed Aug 27, 2023
1 parent 5ba24eb commit 6b8cd9a
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 17 deletions.
3 changes: 1 addition & 2 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@ linter:
- empty_statements
- hash_and_equals
- implementation_imports
- iterable_contains_unrelated_type
- collection_methods_unrelated_type
- library_names
- library_prefixes
- list_remove_unrelated_type
- lines_longer_than_80_chars
- no_adjacent_strings_in_list
- no_duplicate_case_values
Expand Down
8 changes: 7 additions & 1 deletion lib/bloc/theme/app_theme.dart
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
enum AppTheme { light, dark, yellow, system, experimental }
enum AppTheme {
light,
dark,
yellow,
system,
experimental,
}
8 changes: 8 additions & 0 deletions lib/config/build_type.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
/// Represents different build types for the application.
enum BuildType {
/// The debug build type is used during development for debugging purposes.
debug,

/// The staging build type is used for testing in a staging environment.
staging,

/// The QA build type is used for quality assurance testing.
qa,

/// The release build type is the final production build.
release,
}
41 changes: 30 additions & 11 deletions lib/config/environment.dart
Original file line number Diff line number Diff line change
@@ -1,43 +1,62 @@
import 'package:flutter/foundation.dart';
import 'package:flutter_bloc_app_template/config/build_type.dart';

/// Environment configuration.
/// A class for managing environment configurations based on different
/// build types.
class Environment<T> implements Listenable {
Environment._(this._currentBuildType, T config)
: _config = ValueNotifier<T>(config);
: _config = ValueNotifier<T>(config),
_listeners = [];

/// Provides instance [Environment].
/// Creates a new instance of [Environment].
factory Environment.instance() => _instance as Environment<T>;

/// The singleton instance of the [Environment].
static Environment<dynamic>? _instance;

final BuildType _currentBuildType;
final List<VoidCallback> _listeners;

/// Configuration.
T get config => _config.value;

set config(T c) => _config.value = c;
set config(T c) {
_config.value = c;
_notifyListeners(); // Notify listeners when the config changes
}

/// Is this application running in debug mode.
/// Checks if the current build type is debug.
bool get isDebug => _currentBuildType == BuildType.debug;

/// Is this application running in release mode.
/// Checks if the current build type is release.
bool get isRelease => _currentBuildType == BuildType.release;

/// App build type.
/// Returns the current build type.
BuildType get buildType => _currentBuildType;

ValueNotifier<T> _config;

@override
void addListener(VoidCallback listener) {
_config.addListener(listener);
_listeners.add(listener);
}

@override
void removeListener(VoidCallback listener) {
_config.removeListener(listener);
_listeners.remove(listener);
}

/// Notifies all registered listeners that the environment configuration
/// has changed.
void _notifyListeners() {
for (final listener in _listeners) {
listener();
}
}

/// Initializing the environment.
/// Initializes the singleton instance of [Environment].
///
/// This method should be called once at the app's initialization to set up
/// the environment configuration.
static void init<T>({
required BuildType buildType,
required T config,
Expand Down
2 changes: 1 addition & 1 deletion lib/repository/email_list_repository.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter_bloc_app_template/data/storage.dart';
import 'package:flutter_bloc_app_template/models/email.dart';

const _delay = Duration(milliseconds: 1000);
const _delay = Duration(milliseconds: 3000);

class EmailListRepository {
Future<List<Email>> loadData() {
Expand Down
4 changes: 2 additions & 2 deletions lib/view/email_list/email_list_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class EmailListScreen extends StatelessWidget {
onRefresh: () async {
await Future<void>.delayed(const Duration(seconds: 1));
},
child: SingleChildScrollView(
physics: const BouncingScrollPhysics(),
child: SizedBox(
height: MediaQuery.of(context).size.height,
child: EmailListView(),
),
),
Expand Down
1 change: 1 addition & 0 deletions lib/view/email_list/view/email_list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class EmailListView extends StatelessWidget {
var messages = state.messages;

return ListView.builder(
physics: const BouncingScrollPhysics(),
padding: EdgeInsets.zero,
shrinkWrap: true,
primary: false,
Expand Down
59 changes: 59 additions & 0 deletions test/config/environment_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import 'package:flutter_bloc_app_template/config/build_type.dart';
import 'package:flutter_bloc_app_template/config/environment.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('Environment Tests', () {
test('Initialization and Properties', () {
Environment.init(
buildType: BuildType.debug,
config: 'Debug Config',
);

final environment = Environment<String>.instance();

expect(environment.config, 'Debug Config');
expect(environment.isDebug, true);
expect(environment.isRelease, false);
expect(environment.buildType, BuildType.debug);
});

test('Config Update', () {
Environment.init(
buildType: BuildType.debug,
config: 'Initial Config',
);

final environment = Environment<String>.instance()
..config = 'Updated Config';

expect(environment.config, 'Updated Config');
});

test('Listener Notification', () {
Environment.init(
buildType: BuildType.debug,
config: 'Debug Config',
);

final environment = Environment<String>.instance();

var listenerCallCount = 0;
void listenerCallback() {
listenerCallCount++;
}

environment..addListener(listenerCallback)
..config = 'Updated Config';

// Ensure that the listener has been notified
expect(listenerCallCount, 1);

environment..removeListener(listenerCallback)
..config = 'Another Update';

// Listener should not be called after removal
expect(listenerCallCount, 1);
});
});
}

0 comments on commit 6b8cd9a

Please sign in to comment.