Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Settings panel #73

Merged
merged 38 commits into from
Nov 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
7742b98
New sliverbar asset & app icon
Nov 6, 2020
89994fd
Removed old file
Nov 9, 2020
71ad1ef
Added loading indicator helper
Nov 9, 2020
898efab
Added settings route, page, bloc
Nov 9, 2020
06fb34b
Handle empty settings & Acquisition board
Nov 10, 2020
cc47c91
Fixes to import structure
Nov 10, 2020
76a9ae9
Fixes to import structure part 2
Nov 10, 2020
d2e926b
Merge remote-tracking branch 'origin/master' into mobile/app-user-set…
Nov 10, 2020
62a0670
Minor changes to settings text
Nov 10, 2020
d8a45ad
Removed unnecessary imports after adding part of ''
Nov 10, 2020
b953667
Update mobile/lib/src/application/settings/settings_cubit.dart
mateobelanger Nov 10, 2020
c230e63
Update mobile/lib/src/application/settings/settings_cubit.dart
mateobelanger Nov 10, 2020
b652929
Update mobile/lib/src/application/settings/settings_cubit.dart
mateobelanger Nov 10, 2020
2eb2058
Update mobile/lib/src/application/settings/settings_cubit.dart
mateobelanger Nov 10, 2020
ee6c3b8
Added Sharedpreference property to cubit
Nov 10, 2020
85c9a7c
Merge remote-tracking branch 'origin/mobile/app-user-settings' into m…
Nov 10, 2020
40186af
Added genericity to Settings backbone
Nov 10, 2020
97094ca
Nit fix for formating
Nov 10, 2020
15ed171
Settings in Map<String, dynamic> and added Infrastructure layer
Nov 11, 2020
94437cc
Generic is the new fun
Nov 12, 2020
b24beef
Removed obsolete Acquisition board setting
Nov 12, 2020
a5bd02b
Added server url setting option
Nov 12, 2020
771d012
Merge remote-tracking branch 'origin/master' into mobile/app-user-set…
Nov 12, 2020
abf70be
Infrastructure getter gets only 1 setting at a time
Nov 13, 2020
35739d9
Renamed interface methods
Nov 13, 2020
8b200c7
more genericity
Nov 13, 2020
24c461d
Added URL server setting field, added infrastructure layer
Nov 21, 2020
2e75bb6
Renamed infrastructure getters & setters
Nov 21, 2020
3f665f9
Fixed rebase conflicts
Nov 21, 2020
d5c554a
Merge master into mobile/app-user-settings
Nov 21, 2020
6184dea
Added validation on Settings model
Nov 22, 2020
c3bd6d7
Library for infrastructure storage keys, Settings tile functions generic
Nov 22, 2020
2657711
added constants for max & min Age
Nov 22, 2020
e3e6cb8
Factory settings constructor with exceptions
Nov 22, 2020
ea3df86
Added validation on server address
Nov 22, 2020
45c94b1
Added validation and constants to age setting
Nov 22, 2020
445000e
Fixes
Nov 22, 2020
75bd04e
nit fix for launch config
Nov 22, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mobile/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "0.2.0",
"configurations": [
{
"name": "mobile",
"name": "Mobile debug",
"program": "lib/main.dart",
"request": "launch",
"type": "dart",
Expand Down
10 changes: 0 additions & 10 deletions mobile/lib/src/application/navdrawer/navdrawer_state.dart

This file was deleted.

56 changes: 56 additions & 0 deletions mobile/lib/src/application/settings/settings_cubit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:polydodo/src/domain/settings/i_settings_repository.dart';
import 'package:polydodo/src/domain/settings/settings.dart';

part 'settings_state.dart';

class SettingsCubit extends Cubit<SettingsState> {
final ISettingsRepository _repository;

SettingsCubit(this._repository) : super(SettingsLoadInProgress()) {
getSettings();
}

Future<void> getSettings() async {
await emit(SettingsLoadSuccess(await _repository.read()));
}

Future<void> setAge(int newAge) async {
if (state is SettingsLoadSuccess) {
emit(
SettingsLoadSuccess(
await _repository.store(
(state as SettingsLoadSuccess).settings.copyWith(age: newAge),
),
),
);
}
}

Future<void> setSex(Sex newSex) async {
if (state is SettingsLoadSuccess) {
mateobelanger marked this conversation as resolved.
Show resolved Hide resolved
emit(
SettingsLoadSuccess(
await _repository.store(
(state as SettingsLoadSuccess).settings.copyWith(sex: newSex),
),
),
);
}
}

Future<void> setServerAddress(String newServerAddress) async {
if (state is SettingsLoadSuccess) {
emit(
SettingsLoadSuccess(
await _repository.store(
(state as SettingsLoadSuccess)
.settings
.copyWith(serverAddress: newServerAddress),
),
),
);
}
}
}
24 changes: 24 additions & 0 deletions mobile/lib/src/application/settings/settings_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
part of 'settings_cubit.dart';

abstract class SettingsState extends Equatable {
const SettingsState();

@override
List<Object> get props => [];
}

class SettingsLoadInProgress extends SettingsState {}

class SettingsLoadSuccess extends SettingsState {
final Settings settings;

const SettingsLoadSuccess(this.settings);

@override
List<Object> get props => [settings];

@override
String toString() => 'SettingsLoadSuccess { settings: $settings }';
}

class SettingsLoadFailure extends SettingsState {}
11 changes: 11 additions & 0 deletions mobile/lib/src/common/constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
RegExp IP_ADDRESS_REGEX = RegExp(
r'^(?=\d+\.\d+\.\d+\.\d+$)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.?){4}$',
);

const DEFAULT_AGE = 30;
const DEFAULT_SERVER_ADDRESS = '192.168.1.1';

const MIN_AGE = 12;
const MAX_AGE = 125;
final MIN_BIRTH_DATE = DateTime.now().subtract(Duration(days: 365 * MAX_AGE));
final MAX_BIRTH_DATE = DateTime.now().subtract(Duration(days: 365 * MIN_AGE));
5 changes: 5 additions & 0 deletions mobile/lib/src/common/settings_keys.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
library settings_keys;

const String AGE = 'age';
const String SERVER_ADDRESS = 'aerverAdress';
const String SEX = 'serverAdress';
7 changes: 7 additions & 0 deletions mobile/lib/src/domain/settings/i_settings_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:polydodo/src/domain/settings/settings.dart';

abstract class ISettingsRepository {
Future<Settings> read();

Future<Settings> store(Settings newSettings);
}
50 changes: 50 additions & 0 deletions mobile/lib/src/domain/settings/settings.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'package:equatable/equatable.dart';
import 'package:polydodo/src/common/constants.dart';

part 'sex.dart';

class Settings extends Equatable {
final int age;
final String serverAddress;
final Sex sex;

factory Settings({int age, String serverAddress, Sex sex}) {
age = age ?? DEFAULT_AGE;
serverAddress = serverAddress ?? DEFAULT_SERVER_ADDRESS;
sex = sex ?? Sex.NotSet;
if (age < MIN_AGE || age > MAX_AGE) {
throw AgeNotInValidIntervalException(
"L'âge configuré doit être entre 12 et 125 ans.");
}

if (!IP_ADDRESS_REGEX.hasMatch(serverAddress)) {
throw InvalidIPAddressException(
"L'adresse du serveur configurée doit être une adresse de format IPv4.");
}

return Settings._internal(age, serverAddress, sex);
}

Settings._internal(this.age, this.serverAddress, this.sex);

@override
List<Object> get props => [age, serverAddress, sex];

Settings copyWith({int age, String serverAddress, Sex sex}) {
return Settings(
age: age ?? this.age,
serverAddress: serverAddress ?? this.serverAddress,
sex: sex ?? this.sex,
);
}
}

class InvalidIPAddressException implements Exception {
String cause;
InvalidIPAddressException(this.cause);
}

class AgeNotInValidIntervalException implements Exception {
String cause;
AgeNotInValidIntervalException(this.cause);
}
3 changes: 3 additions & 0 deletions mobile/lib/src/domain/settings/sex.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
part of 'settings.dart';

enum Sex { NotSet, Male, Female }
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:polydodo/src/common/settings_keys.dart' as settings_keys;
import 'package:polydodo/src/domain/settings/i_settings_repository.dart';
import 'package:polydodo/src/domain/settings/settings.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SettingsRepository extends ISettingsRepository {
SharedPreferences _prefs;

SettingsRepository();

@override
Future<Settings> read() async {
_prefs = (await SharedPreferences.getInstance());

return Settings(
age: _prefs.getInt(settings_keys.AGE),
serverAddress: _prefs.getString(settings_keys.SERVER_ADDRESS),
sex: Sex.values[(_prefs.getInt(settings_keys.SEX)) ?? Sex.NotSet.index],
);
}

@override
Future<Settings> store(Settings newSettings) async {
await _prefs.setInt(settings_keys.AGE, newSettings.age);
await _prefs.setString(
settings_keys.SERVER_ADDRESS, newSettings.serverAddress);
await _prefs.setInt(settings_keys.SEX, newSettings.sex.index);

return newSettings;
}
}
10 changes: 10 additions & 0 deletions mobile/lib/src/locator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:get_it/get_it.dart';
import 'package:polydodo/src/application/device/device_selector_cubit.dart';
import 'package:polydodo/src/application/eeg_data/data_cubit.dart';
import 'package:polydodo/src/application/settings/settings_cubit.dart';
import 'package:polydodo/src/application/sleep_sequence_history/sleep_sequence_history_cubit.dart';
import 'package:polydodo/src/application/sleep_sequence_stats/sleep_sequence_stats_cubit.dart';
import 'package:polydodo/src/domain/acquisition_device/device_locator_service.dart';
import 'package:polydodo/src/domain/acquisition_device/i_acquisition_device_repository.dart';
import 'package:polydodo/src/domain/eeg_data/i_eeg_data_repository.dart';
import 'package:polydodo/src/domain/settings/i_settings_repository.dart';
import 'package:polydodo/src/domain/sleep_sequence/i_sleep_sequence_repository.dart';
import 'package:polydodo/src/infrastructure/connection_repositories/serial_repository.dart';
import 'package:polydodo/src/infrastructure/connection_repositories/bluetooth_repository.dart';
import 'package:polydodo/src/infrastructure/connection_repositories/eeg_data_repository.dart';
import 'package:polydodo/src/infrastructure/sleep_history/sleep_sequence_repository.dart';
import 'package:polydodo/src/infrastructure/settings_repository/settings_repository.dart';

import 'infrastructure/settings_repository/settings_repository.dart';
mateobelanger marked this conversation as resolved.
Show resolved Hide resolved

/// Private GetIt instance as we want all DI to be performed here in this file
final _serviceLocator = GetIt.asNewInstance();
Expand All @@ -22,6 +27,7 @@ void registerServices() {
_serviceLocator.registerSingleton<IEEGDataRepository>(EEGDataRepository());
_serviceLocator
.registerSingleton<ISleepSequenceRepository>(SleepSequenceRepository());
_serviceLocator.registerSingleton<ISettingsRepository>(SettingsRepository());
}

/// This function creates all the BlocProviders used in this app
Expand All @@ -43,4 +49,8 @@ List<BlocProvider> createBlocProviders() => [
create: (context) => SleepSequenceHistoryCubit(
_serviceLocator.get<ISleepSequenceRepository>(),
BlocProvider.of<SleepSequenceStatsCubit>(context))),
BlocProvider<SettingsCubit>(
create: (context) =>
SettingsCubit(_serviceLocator.get<ISettingsRepository>()),
),
];
6 changes: 5 additions & 1 deletion mobile/lib/src/presentation/navigation/navdrawer_tabs.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
part of 'navdrawer_widget.dart';

enum NavdrawerTab {
BluetoothSelector,
Dashboard,
History,
RecordSleep,
Settings,
DeviceSelector,
History,
SleepSequenceStats
}
9 changes: 8 additions & 1 deletion mobile/lib/src/presentation/navigation/navdrawer_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:polydodo/src/presentation/navigation/routes/router.gr.dart';

import 'navdrawer_tabs.dart';
part 'navdrawer_tabs.dart';

class NavDrawer extends StatelessWidget {
static const name = 'appDrawerRoute';
Expand Down Expand Up @@ -48,6 +48,13 @@ class NavDrawer extends StatelessWidget {
tab: NavdrawerTab.History,
context: context,
),
_createDrawerItem(
icon: Icons.settings,
text: 'Settings',
route: Routes.settingsPage,
tab: NavdrawerTab.Settings,
context: context,
),
],
),
);
Expand Down
3 changes: 3 additions & 0 deletions mobile/lib/src/presentation/navigation/routes/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:polydodo/src/presentation/pages/device_selector/device_selector_
import 'package:polydodo/src/presentation/pages/record_sleep/record_sleep_guide_page.dart';
import 'package:polydodo/src/presentation/pages/record_sleep/record_sleep_recording_page.dart';
import 'package:polydodo/src/presentation/pages/record_sleep/record_sleep_validate_page.dart';
import 'package:polydodo/src/presentation/pages/settings/settings_page.dart';
import 'package:polydodo/src/presentation/pages/sleep_history_page/sleep_history_page.dart';
import 'package:polydodo/src/presentation/pages/sleep_sequence_stats_page/sleep_sequence_stats_page.dart';

Expand Down Expand Up @@ -33,5 +34,7 @@ import 'package:polydodo/src/presentation/pages/sleep_sequence_stats_page/sleep_
CustomRoute(
page: SleepSequenceStatsPage,
transitionsBuilder: TransitionsBuilders.fadeIn),
CustomRoute(
page: SettingsPage, transitionsBuilder: TransitionsBuilders.fadeIn),
])
class $Router {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flutter/material.dart';
import 'package:polydodo/src/presentation/navigation/navdrawer_tabs.dart';
import 'package:polydodo/src/presentation/navigation/navdrawer_widget.dart';
import 'package:polydodo/src/presentation/pages/dashboard/sliver_app_bar_title.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:polydodo/src/application/device/device_selector_cubit.dart';
import 'package:polydodo/src/application/device/device_selector_state.dart';
import 'package:polydodo/src/presentation/navigation/navdrawer_tabs.dart';
import 'package:polydodo/src/presentation/navigation/navdrawer_widget.dart';
import 'package:polydodo/src/presentation/navigation/routes/router.gr.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:polydodo/src/application/blocs.dart';
import 'package:polydodo/src/constants.dart';
import 'package:polydodo/src/presentation/navigation/navdrawer_tabs.dart';
import 'package:polydodo/src/presentation/navigation/navdrawer_widget.dart';
import 'package:polydodo/src/presentation/navigation/routes/router.gr.dart';
import 'package:url_launcher/url_launcher.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:polydodo/src/application/eeg_data/data_cubit.dart';
import 'package:polydodo/src/application/eeg_data/data_states.dart';
import 'package:polydodo/src/presentation/navigation/navdrawer_tabs.dart';
import 'package:polydodo/src/presentation/navigation/navdrawer_widget.dart';

class RecordSleepRecordingPage extends StatelessWidget {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:polydodo/src/application/eeg_data/data_cubit.dart';
import 'package:polydodo/src/application/eeg_data/data_states.dart';
import 'package:polydodo/src/presentation/navigation/navdrawer_tabs.dart';
import 'package:polydodo/src/presentation/navigation/navdrawer_widget.dart';
import 'package:polydodo/src/presentation/navigation/routes/router.gr.dart';
import 'package:polydodo/src/presentation/pages/record_sleep/signal_section.dart';
Expand Down
Loading