diff --git a/lib/core/prefs/general_prefs.dart b/lib/core/prefs/general_prefs.dart index aec3bf2a..47b54783 100644 --- a/lib/core/prefs/general_prefs.dart +++ b/lib/core/prefs/general_prefs.dart @@ -31,3 +31,20 @@ class DebugModeNotifier extends _$DebugModeNotifier { return _pref.update(value); } } + +@riverpod +class MarkNewProfileActive extends _$MarkNewProfileActive { + late final _pref = Pref( + ref.watch(sharedPreferencesProvider), + "mark_new_profile_active", + true, + ); + + @override + bool build() => _pref.getValue(); + + Future update(bool value) { + state = value; + return _pref.update(value); + } +} diff --git a/lib/features/common/profile_tile.dart b/lib/features/common/profile_tile.dart index 8f1edef5..18621e0a 100644 --- a/lib/features/common/profile_tile.dart +++ b/lib/features/common/profile_tile.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:gap/gap.dart'; +import 'package:go_router/go_router.dart'; import 'package:hiddify/core/core_providers.dart'; import 'package:hiddify/core/prefs/prefs.dart'; import 'package:hiddify/core/router/routes/routes.dart'; @@ -32,6 +33,9 @@ class ProfileTile extends HookConsumerWidget { initialOnFailure: (err) { CustomToast.error(t.printError(err)).show(context); }, + initialOnSuccess: () { + if (context.mounted) context.pop(); + }, ); final subInfo = profile.subInfo; diff --git a/lib/features/profiles/notifier/profiles_notifier.dart b/lib/features/profiles/notifier/profiles_notifier.dart index 4db60c13..faa61089 100644 --- a/lib/features/profiles/notifier/profiles_notifier.dart +++ b/lib/features/profiles/notifier/profiles_notifier.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'package:fpdart/fpdart.dart'; +import 'package:hiddify/core/prefs/prefs.dart'; import 'package:hiddify/data/data_providers.dart'; import 'package:hiddify/domain/enums.dart'; import 'package:hiddify/domain/profiles/profiles.dart'; @@ -40,9 +41,9 @@ class ProfilesNotifier extends _$ProfilesNotifier with AppLogger { ProfilesRepository get _profilesRepo => ref.read(profilesRepositoryProvider); - Future selectActiveProfile(String id) async { + Future selectActiveProfile(String id) async { loggy.debug('changing active profile to: [$id]'); - await _profilesRepo.setAsActive(id).mapLeft((f) { + return _profilesRepo.setAsActive(id).getOrElse((f) { loggy.warning('failed to set [$id] as active profile, $f'); throw f; }).run(); @@ -50,10 +51,12 @@ class ProfilesNotifier extends _$ProfilesNotifier with AppLogger { Future addProfile(String url) async { final activeProfile = await ref.read(activeProfileProvider.future); + final markAsActive = + activeProfile == null || ref.read(markNewProfileActiveProvider); loggy.debug("adding profile, url: [$url]"); return ref .read(profilesRepositoryProvider) - .addByUrl(url, markAsActive: activeProfile == null) + .addByUrl(url, markAsActive: markAsActive) .getOrElse((l) { loggy.warning("failed to add profile: $l"); throw l;