Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
128 changes: 128 additions & 0 deletions lib/features/preferences/jump_threshold_modal.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import 'package:doomscroll_stop/providers/app_jump_threshold_provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

const _minMinutes = 1;
const _maxMinutes = 30;

class JumpThresholdModal extends ConsumerStatefulWidget {
const JumpThresholdModal({super.key});

static Future<void> show(BuildContext context) {
return showModalBottomSheet(
context: context,
isScrollControlled: true,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(24)),
),
builder: (_) => const JumpThresholdModal(),
);
}

@override
ConsumerState<JumpThresholdModal> createState() => _JumpThresholdModalState();
}

class _JumpThresholdModalState extends ConsumerState<JumpThresholdModal> {
late int _minutes;
bool _initialized = false;

@override
Widget build(BuildContext context) {
final thresholdAsync = ref.watch(appJumpThresholdProvider);

return thresholdAsync.when(
loading: () => const Padding(
padding: EdgeInsets.all(48),
child: Center(child: CircularProgressIndicator()),
),
error: (e, _) => Padding(
padding: const EdgeInsets.all(24),
child: Text('Error: $e'),
),
data: (thresholdMs) {
if (!_initialized) {
_minutes = (thresholdMs / 60000).round().clamp(_minMinutes, _maxMinutes);
_initialized = true;
}

return Padding(
padding: EdgeInsets.only(
left: 24,
right: 24,
top: 24,
bottom: MediaQuery.of(context).viewInsets.bottom + 24,
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'App Switch Threshold',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(height: 4),
Text(
'Minimum time between app switches before it counts as doomscrolling.',
style: TextStyle(
color: Theme.of(context).colorScheme.onSurface.withValues(alpha: 0.6),
fontSize: 13,
),
),
const SizedBox(height: 32),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton.filled(
icon: const Icon(Icons.remove),
onPressed: _minutes > _minMinutes
? () => setState(() => _minutes--)
: null,
),
const SizedBox(width: 24),
Text(
'$_minutes min',
style: const TextStyle(
fontSize: 28,
fontWeight: FontWeight.bold,
),
),
const SizedBox(width: 24),
IconButton.filled(
icon: const Icon(Icons.add),
onPressed: _minutes < _maxMinutes
? () => setState(() => _minutes++)
: null,
),
],
),
const SizedBox(height: 32),
Row(
children: [
Expanded(
child: OutlinedButton(
onPressed: () => Navigator.pop(context),
child: const Text('CANCEL'),
),
),
const SizedBox(width: 12),
Expanded(
child: FilledButton(
onPressed: () async {
await ref
.read(appJumpThresholdProvider.notifier)
.setThreshold(_minutes * 60000);
if (context.mounted) Navigator.pop(context);
},
child: const Text('SAVE'),
),
),
],
),
],
),
);
},
);
}
}
6 changes: 6 additions & 0 deletions lib/features/preferences/preferences_page.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:doomscroll_stop/features/preferences/jump_threshold_modal.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:doomscroll_stop/features/preferences/app_selection_sheet.dart';
Expand Down Expand Up @@ -33,6 +34,11 @@ class _PreferencesPageState extends ConsumerState<PreferencesPage> {
appBar: AppBar(
title: const Text('Tracked Apps'),
actions: [
IconButton(
icon: const Icon(Icons.tune),
tooltip: 'Switch threshold',
onPressed: () => JumpThresholdModal.show(context),
),
IconButton(
icon: const Icon(Icons.add),
onPressed: () {
Expand Down
29 changes: 29 additions & 0 deletions lib/providers/app_jump_threshold_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:doomscroll_stop/providers/app_preferences_provider.dart';
import 'package:doomscroll_stop/providers/doomscroll_background_service_provider.dart';
import 'package:doomscroll_stop/services/db_service/local_storage_service_interface.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:get_it/get_it.dart';

final appJumpThresholdProvider =
AsyncNotifierProvider<AppJumpThresholdNotifier, int>(
AppJumpThresholdNotifier.new,
);

class AppJumpThresholdNotifier extends AsyncNotifier<int> {
@override
Future<int> build() async {
return GetIt.I.get<LocalStorageInterface>().getJumpThresholdMs();
}

Future<void> setThreshold(int ms) async {
await GetIt.I.get<LocalStorageInterface>().saveJumpThresholdMs(ms);
state = AsyncValue.data(ms);

final prefs = ref.read(appPreferencesProvider).value;
if (prefs != null && prefs.isNotEmpty) {
final service = ref.read(doomscrollBackgroundServiceProvider.notifier);
await service.stop();
await service.start(prefs, ms);
}
}
}
5 changes: 3 additions & 2 deletions lib/providers/app_preferences_provider.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'package:doomscroll_stop/providers/app_jump_threshold_provider.dart';
import 'package:doomscroll_stop/repositories/preferences_repository.dart';
import 'package:doomscroll_stop/services/db_service/local_storage_service_interface.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:doomscroll_stop/providers/doomscroll_background_service_provider.dart';
import 'package:get_it/get_it.dart';

const maxMinutes = 300;
const defaultAppJumpThresholdMs = 30000;

class AppPreferencesNotifier extends AsyncNotifier<Map<String, int>> {
@override
Expand Down Expand Up @@ -39,7 +39,8 @@ class AppPreferencesNotifier extends AsyncNotifier<Map<String, int>> {
await serviceNotifier.stop();

if (currentValue.isNotEmpty) {
await serviceNotifier.start(currentValue, defaultAppJumpThresholdMs);
final thresholdMs = await ref.read(appJumpThresholdProvider.future);
await serviceNotifier.start(currentValue, thresholdMs);
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions lib/services/db_service/local_storage_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'dart:convert';
import 'package:doomscroll_stop/services/db_service/local_storage_service_interface.dart';
import 'package:shared_preferences/shared_preferences.dart';

const _defaultJumpThresholdMs = 60000;

class LocalStorageService implements LocalStorageInterface {
final SharedPreferences _sharedPreferences;
LocalStorageService(this._sharedPreferences);
Expand All @@ -18,4 +20,14 @@ class LocalStorageService implements LocalStorageInterface {
if (r == null) return {};
return (jsonDecode(r) as Map<String, dynamic>).cast<String, int>();
}

@override
Future<void> saveJumpThresholdMs(int ms) async {
await _sharedPreferences.setInt('jumpThresholdMs', ms);
}

@override
Future<int> getJumpThresholdMs() async {
return _sharedPreferences.getInt('jumpThresholdMs') ?? _defaultJumpThresholdMs;
}
}
2 changes: 2 additions & 0 deletions lib/services/db_service/local_storage_service_interface.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
abstract interface class LocalStorageInterface {
Future<void> savePreferences(Map<String, int> appLimits);
Future<Map<String, int>> getPreferences();
Future<void> saveJumpThresholdMs(int ms);
Future<int> getJumpThresholdMs();
}
6 changes: 4 additions & 2 deletions test/providers/app_preferences_provider_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:doomscroll_stop/providers/app_jump_threshold_provider.dart';
import 'package:doomscroll_stop/providers/app_preferences_provider.dart';
import 'package:doomscroll_stop/providers/doomscroll_background_service_provider.dart';
import 'package:doomscroll_stop/repositories/preferences_repository.dart';
Expand Down Expand Up @@ -148,6 +149,7 @@ void main() {
() => mockRepo.getPreferences(),
).thenAnswer((_) async => {'com.app.a': 60});
when(() => mockStorage.savePreferences(any())).thenAnswer((_) async {});
when(() => mockStorage.getJumpThresholdMs()).thenAnswer((_) async => 60000);
when(
() => mockService.isServiceRunning(),
).thenAnswer((_) async => false);
Expand All @@ -163,8 +165,8 @@ void main() {
addTearDown(container.dispose);

await container.read(appPreferencesProvider.future);
// Ensure background service provider is initialized too
await container.read(doomscrollBackgroundServiceProvider.future);
await container.read(appJumpThresholdProvider.future);

await container.read(appPreferencesProvider.notifier).saveAndApply();

Expand All @@ -173,7 +175,7 @@ void main() {
verify(
() => mockService.startDetectionService(
appTimeLimits: {'com.app.a': 60},
appJumpThresholdMs: defaultAppJumpThresholdMs,
appJumpThresholdMs: 60000,
),
).called(1);
},
Expand Down