diff --git a/lib/application.dart b/lib/application.dart index 6caa8f3..729b3db 100644 --- a/lib/application.dart +++ b/lib/application.dart @@ -4,6 +4,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:light_sensor/light_sensor.dart'; +import 'package:lightmeter/data/caffeine_service.dart'; import 'package:lightmeter/data/haptics_service.dart'; import 'package:provider/provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -37,6 +38,7 @@ class Application extends StatelessWidget { providers: [ Provider.value(value: env.copyWith(hasLightSensor: snapshot.data![1] as bool)), Provider(create: (_) => UserPreferencesService(snapshot.data![0] as SharedPreferences)), + Provider(create: (_) => const CaffeineService()), Provider(create: (_) => const HapticsService()), Provider(create: (_) => PermissionsService()), Provider(create: (_) => const LightSensorService()), diff --git a/lib/data/caffeine_service.dart b/lib/data/caffeine_service.dart new file mode 100644 index 0000000..5d19b0f --- /dev/null +++ b/lib/data/caffeine_service.dart @@ -0,0 +1,15 @@ +import 'package:flutter/services.dart'; + +class CaffeineService { + static const _methodChannel = MethodChannel("com.vodemn.lightmeter/keepScreenOn"); + + const CaffeineService(); + + Future isKeepScreenOn() async { + return await _methodChannel.invokeMethod("isKeepScreenOn").then((value) => value!); + } + + Future keepScreenOn(bool keep) async { + await _methodChannel.invokeMethod("setKeepScreenOn", keep); + } +} diff --git a/lib/data/shared_prefs_service.dart b/lib/data/shared_prefs_service.dart index 6d39a13..ab57293 100644 --- a/lib/data/shared_prefs_service.dart +++ b/lib/data/shared_prefs_service.dart @@ -14,7 +14,9 @@ class UserPreferencesService { static const _cameraEvCalibrationKey = "cameraEvCalibration"; static const _lightSensorEvCalibrationKey = "lightSensorEvCalibration"; + static const _caffeineKey = "caffeine"; static const _hapticsKey = "haptics"; + static const _themeTypeKey = "themeType"; static const _primaryColorKey = "primaryColor"; static const _dynamicColorKey = "dynamicColor"; @@ -32,6 +34,9 @@ class UserPreferencesService { EvSourceType get evSourceType => EvSourceType.values[_sharedPreferences.getInt(_evSourceTypeKey) ?? 0]; set evSourceType(EvSourceType value) => _sharedPreferences.setInt(_evSourceTypeKey, value.index); + bool get caffeine => _sharedPreferences.getBool(_caffeineKey) ?? false; + set caffeine(bool value) => _sharedPreferences.setBool(_caffeineKey, value); + bool get haptics => _sharedPreferences.getBool(_hapticsKey) ?? false; set haptics(bool value) => _sharedPreferences.setBool(_hapticsKey, value); diff --git a/lib/interactors/metering_interactor.dart b/lib/interactors/metering_interactor.dart index bbac538..1948b97 100644 --- a/lib/interactors/metering_interactor.dart +++ b/lib/interactors/metering_interactor.dart @@ -1,6 +1,7 @@ import 'dart:io'; import 'package:app_settings/app_settings.dart'; +import 'package:lightmeter/data/caffeine_service.dart'; import 'package:lightmeter/data/haptics_service.dart'; import 'package:lightmeter/data/light_sensor_service.dart'; import 'package:lightmeter/data/permissions_service.dart'; @@ -9,16 +10,22 @@ import 'package:permission_handler/permission_handler.dart'; class MeteringInteractor { final UserPreferencesService _userPreferencesService; + final CaffeineService _caffeineService; final HapticsService _hapticsService; final PermissionsService _permissionsService; final LightSensorService _lightSensorService; - const MeteringInteractor( + MeteringInteractor( this._userPreferencesService, + this._caffeineService, this._hapticsService, this._permissionsService, this._lightSensorService, - ); + ) { + if (_userPreferencesService.caffeine) { + _caffeineService.keepScreenOn(true); + } + } double get cameraEvCalibration => _userPreferencesService.cameraEvCalibration; double get lightSensorEvCalibration => _userPreferencesService.lightSensorEvCalibration; diff --git a/lib/interactors/settings_interactor.dart b/lib/interactors/settings_interactor.dart index d19edfe..db99a7a 100644 --- a/lib/interactors/settings_interactor.dart +++ b/lib/interactors/settings_interactor.dart @@ -1,12 +1,15 @@ +import 'package:lightmeter/data/caffeine_service.dart'; import 'package:lightmeter/data/haptics_service.dart'; import 'package:lightmeter/data/shared_prefs_service.dart'; class SettingsInteractor { final UserPreferencesService _userPreferencesService; + final CaffeineService _caffeineService; final HapticsService _hapticsService; const SettingsInteractor( this._userPreferencesService, + this._caffeineService, this._hapticsService, ); @@ -14,9 +17,21 @@ class SettingsInteractor { void setCameraEvCalibration(double value) => _userPreferencesService.cameraEvCalibration = value; double get lightSensorEvCalibration => _userPreferencesService.lightSensorEvCalibration; - void setLightSensorEvCalibration(double value) => _userPreferencesService.lightSensorEvCalibration = value; + void setLightSensorEvCalibration(double value) => + _userPreferencesService.lightSensorEvCalibration = value; + + bool get isCaffeineEnabled => _userPreferencesService.caffeine; + Future enableCaffeine(bool enable) async { + await _caffeineService.keepScreenOn(enable).then((value) { + _userPreferencesService.caffeine = enable; + }); + } bool get isHapticsEnabled => _userPreferencesService.haptics; + void enableHaptics(bool enable) { + _userPreferencesService.haptics = enable; + quickVibration(); + } /// Executes vibration if haptics are enabled in settings void quickVibration() { @@ -27,6 +42,4 @@ class SettingsInteractor { void responseVibration() { if (_userPreferencesService.haptics) _hapticsService.responseVibration(); } - - void enableHaptics(bool enable) => _userPreferencesService.haptics = enable; } diff --git a/lib/l10n/intl_en.arb b/lib/l10n/intl_en.arb index c8ae2cb..6d5dffc 100644 --- a/lib/l10n/intl_en.arb +++ b/lib/l10n/intl_en.arb @@ -34,6 +34,7 @@ "camera": "Camera", "lightSensor": "Light sensor", "general": "General", + "keepScreenOn": "Keep screen on", "haptics": "Haptics", "theme": "Theme", "chooseTheme": "Choose theme", diff --git a/lib/screens/metering/flow_metering.dart b/lib/screens/metering/flow_metering.dart index 162800f..7dbb05c 100644 --- a/lib/screens/metering/flow_metering.dart +++ b/lib/screens/metering/flow_metering.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:lightmeter/data/caffeine_service.dart'; import 'package:lightmeter/data/haptics_service.dart'; import 'package:lightmeter/data/light_sensor_service.dart'; import 'package:lightmeter/data/models/photography_values/photography_value.dart'; @@ -25,6 +26,7 @@ class _MeteringFlowState extends State { return Provider( create: (context) => MeteringInteractor( context.read(), + context.read(), context.read(), context.read(), context.read(), diff --git a/lib/screens/settings/components/caffeine/bloc_list_tile_caffeine.dart b/lib/screens/settings/components/caffeine/bloc_list_tile_caffeine.dart new file mode 100644 index 0000000..3a48e8d --- /dev/null +++ b/lib/screens/settings/components/caffeine/bloc_list_tile_caffeine.dart @@ -0,0 +1,15 @@ +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:lightmeter/interactors/settings_interactor.dart'; + +class CaffeineListTileBloc extends Cubit { + final SettingsInteractor _settingsInteractor; + + CaffeineListTileBloc( + this._settingsInteractor, + ) : super(_settingsInteractor.isCaffeineEnabled); + + void onCaffeineChanged(bool value) { + _settingsInteractor.enableCaffeine(value); + emit(value); + } +} diff --git a/lib/screens/settings/components/caffeine/provider_list_tile_caffeine.dart b/lib/screens/settings/components/caffeine/provider_list_tile_caffeine.dart new file mode 100644 index 0000000..479b1e7 --- /dev/null +++ b/lib/screens/settings/components/caffeine/provider_list_tile_caffeine.dart @@ -0,0 +1,18 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:lightmeter/interactors/settings_interactor.dart'; + +import 'bloc_list_tile_caffeine.dart'; +import 'widget_list_tile_caffeine.dart'; + +class CaffeineListTileProvider extends StatelessWidget { + const CaffeineListTileProvider({super.key}); + + @override + Widget build(BuildContext context) { + return BlocProvider( + create: (context) => CaffeineListTileBloc(context.read()), + child: const CaffeineListTile(), + ); + } +} diff --git a/lib/screens/settings/components/caffeine/widget_list_tile_caffeine.dart b/lib/screens/settings/components/caffeine/widget_list_tile_caffeine.dart new file mode 100644 index 0000000..dfd7bbe --- /dev/null +++ b/lib/screens/settings/components/caffeine/widget_list_tile_caffeine.dart @@ -0,0 +1,21 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:lightmeter/generated/l10n.dart'; + +import 'bloc_list_tile_caffeine.dart'; + +class CaffeineListTile extends StatelessWidget { + const CaffeineListTile({super.key}); + + @override + Widget build(BuildContext context) { + return BlocBuilder( + builder: (context, state) => SwitchListTile( + secondary: const Icon(Icons.screen_lock_portrait), + title: Text(S.of(context).keepScreenOn), + value: state, + onChanged: context.read().onCaffeineChanged, + ), + ); + } +} diff --git a/lib/screens/settings/flow_settings.dart b/lib/screens/settings/flow_settings.dart index 9dcb803..9e757b4 100644 --- a/lib/screens/settings/flow_settings.dart +++ b/lib/screens/settings/flow_settings.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:lightmeter/data/caffeine_service.dart'; import 'package:lightmeter/data/haptics_service.dart'; import 'package:lightmeter/data/shared_prefs_service.dart'; import 'package:lightmeter/interactors/settings_interactor.dart'; @@ -13,6 +14,7 @@ class SettingsFlow extends StatelessWidget { return Provider( create: (context) => SettingsInteractor( context.read(), + context.read(), context.read(), ), child: const SettingsScreen(), diff --git a/lib/screens/settings/screen_settings.dart b/lib/screens/settings/screen_settings.dart index 32714a5..02af05e 100644 --- a/lib/screens/settings/screen_settings.dart +++ b/lib/screens/settings/screen_settings.dart @@ -4,6 +4,7 @@ import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/res/dimens.dart'; import 'package:provider/provider.dart'; +import 'components/caffeine/provider_list_tile_caffeine.dart'; import 'components/calibration/widget_list_tile_calibration.dart'; import 'components/haptics/provider_list_tile_haptics.dart'; import 'components/primary_color/widget_list_tile_primary_color.dart'; @@ -61,6 +62,7 @@ class SettingsScreen extends StatelessWidget { SettingsSection( title: S.of(context).general, children: const [ + CaffeineListTileProvider(), HapticsListTileProvider(), ], ),