Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
♻️ Move trusted URLs storage to user preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
Komposten committed Aug 25, 2019
1 parent 69b3e76 commit 3c728d3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 52 deletions.
10 changes: 5 additions & 5 deletions lib/pages/home/bottom_sheets/confirm_open_url.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:Okuna/provider.dart';
import 'package:Okuna/services/localization.dart';
import 'package:Okuna/services/theme.dart';
import 'package:Okuna/services/theme_value_parser.dart';
import 'package:Okuna/services/url_launcher.dart';
import 'package:Okuna/services/user_preferences.dart';
import 'package:Okuna/widgets/buttons/button.dart';
import 'package:Okuna/widgets/fields/checkbox_field.dart';
import 'package:Okuna/widgets/theming/primary_color_container.dart';
Expand All @@ -22,7 +22,7 @@ class OBConfirmOpenUrlBottomSheet extends StatefulWidget {
}

class OBConfirmOpenUrlBottomSheetState extends State<OBConfirmOpenUrlBottomSheet> {
UrlLauncherService _urlLauncherService;
UserPreferencesService _preferencesService;
LocalizationService _localizationService;
ThemeService _themeService;
ThemeValueParserService _themeValueParserService;
Expand All @@ -45,7 +45,7 @@ class OBConfirmOpenUrlBottomSheetState extends State<OBConfirmOpenUrlBottomSheet
Widget build(BuildContext context) {
if (_needsBootstrap) {
var openbookProvider = OpenbookProvider.of(context);
_urlLauncherService = openbookProvider.urlLauncherService;
_preferencesService = openbookProvider.userPreferencesService;
_localizationService = openbookProvider.localizationService;
_themeService = openbookProvider.themeService;
_themeValueParserService = openbookProvider.themeValueParserService;
Expand Down Expand Up @@ -136,14 +136,14 @@ class OBConfirmOpenUrlBottomSheetState extends State<OBConfirmOpenUrlBottomSheet
void _toggleDontAskForHost() {
setState(() {
_askForHost = !_askForHost;
_urlLauncherService.storeAskToConfirmOpen(_askForHost, host: widget._uri.host);
_preferencesService.setAskToConfirmOpenUrl(_askForHost, host: widget._uri.host);
});
}

void _toggleDontAsk() {
setState(() {
_ask = !_ask;
_urlLauncherService.storeAskToConfirmOpen(_ask);
_preferencesService.setAskToConfirmOpenUrl(_ask);
});
}

Expand Down
1 change: 0 additions & 1 deletion lib/provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ class OpenbookProviderState extends State<OpenbookProvider> {
documentsService.setHttpService(httpService);
moderationApiService.setStringTemplateService(stringTemplateService);
moderationApiService.setHttpieService(httpService);
urlLauncherService.setStorageService(storageService);
}

void initAsyncState() async {
Expand Down
45 changes: 0 additions & 45 deletions lib/services/url_launcher.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import 'package:Okuna/services/storage.dart';
import 'package:url_launcher/url_launcher.dart';

class UrlLauncherService {
static const keyAskToConfirmOpen = 'askToConfirmOpen';
static const keyAskToConfirmExceptions = 'askToConfirmExceptions';

OBStorage _storage;

Future launchUrl(String url) async {
bool canOpenUrl = await canLaunchUrl(url);

Expand All @@ -20,45 +14,6 @@ class UrlLauncherService {
Future<bool> canLaunchUrl(String url){
return canLaunch(url);
}

void setStorageService(StorageService storageService) {
_storage = storageService.getSystemPreferencesStorage(namespace: 'url');
}

void storeAskToConfirmOpen(bool ask, {String host}) async {
if (host == null) {
_storage?.set(keyAskToConfirmOpen, ask.toString());
} else {
String exceptions = await _storage?.get(keyAskToConfirmExceptions) ?? '';

var hasException = exceptions.contains(";$host");

if (!hasException && !ask) {
exceptions += ";$host";
_storage?.set(keyAskToConfirmExceptions, exceptions);
} else if (hasException && ask){
exceptions.replaceAll(";$host", "");
_storage?.set(keyAskToConfirmExceptions, exceptions);
}
}
}

Future<bool> getAskToConfirmOpen({String host}) async {
String ask = await _storage?.get(keyAskToConfirmOpen);
bool shouldAsk = true;

if (ask != null && ask.toLowerCase() == "false") {
shouldAsk = false;
} else if (host != null) {
String exceptions = await _storage?.get(keyAskToConfirmExceptions);

if (exceptions != null && exceptions.contains(host)) {
shouldAsk = false;
}
}

return shouldAsk;
}
}

class UrlLauncherUnsupportedUrlException implements Exception {
Expand Down
42 changes: 42 additions & 0 deletions lib/services/user_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import 'package:Okuna/models/post_comment.dart';
import 'package:Okuna/services/storage.dart';

class UserPreferencesService {
static const keyAskToConfirmOpen = 'askToConfirmOpen';
static const keyAskToConfirmExceptions = 'askToConfirmExceptions';

OBStorage _storage;
static const postCommentsSortTypeStorageKey = 'postCommentsSortType';
Future _getPostCommentsSortTypeCache;
Expand Down Expand Up @@ -37,6 +40,45 @@ class UserPreferencesService {
return PostCommentsSortType.asc;
}

Future setAskToConfirmOpenUrl(bool ask, {String host}) async {
Future status;

if (host == null) {
status = _storage?.set(keyAskToConfirmOpen, ask.toString());
} else {
String exceptions = await _storage?.get(keyAskToConfirmExceptions) ?? '';

var hasException = exceptions.contains(";$host");

if (!hasException && !ask) {
exceptions += ";$host";
status = _storage?.set(keyAskToConfirmExceptions, exceptions);
} else if (hasException && ask){
exceptions.replaceAll(";$host", "");
status = _storage?.set(keyAskToConfirmExceptions, exceptions);
}
}

return status;
}

Future<bool> getAskToConfirmOpenUrl({String host}) async {
String ask = await _storage?.get(keyAskToConfirmOpen);
bool shouldAsk = true;

if (ask != null && ask.toLowerCase() == "false") {
shouldAsk = false;
} else if (host != null) {
String exceptions = await _storage?.get(keyAskToConfirmExceptions);

if (exceptions != null && exceptions.contains(host)) {
shouldAsk = false;
}
}

return shouldAsk;
}

Future clear() {
return _storage.clear();
}
Expand Down
5 changes: 4 additions & 1 deletion lib/widgets/theming/actionable_smart_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:Okuna/services/navigation_service.dart';
import 'package:Okuna/services/toast.dart';
import 'package:Okuna/services/url_launcher.dart';
import 'package:Okuna/services/user.dart';
import 'package:Okuna/services/user_preferences.dart';
import 'package:Okuna/widgets/theming/smart_text.dart';
export 'package:Okuna/widgets/theming/smart_text.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -40,6 +41,7 @@ class OBActionableTextState extends State<OBActionableSmartText> {
NavigationService _navigationService;
UserService _userService;
UrlLauncherService _urlLauncherService;
UserPreferencesService _preferencesService;
ToastService _toastService;
BottomSheetService _bottomSheetService;

Expand All @@ -64,6 +66,7 @@ class OBActionableTextState extends State<OBActionableSmartText> {
_navigationService = openbookProvider.navigationService;
_userService = openbookProvider.userService;
_urlLauncherService = openbookProvider.urlLauncherService;
_preferencesService = openbookProvider.userPreferencesService;
_toastService = openbookProvider.toastService;
_bottomSheetService = openbookProvider.bottomSheetService;
_needsBootstrap = false;
Expand Down Expand Up @@ -117,7 +120,7 @@ class OBActionableTextState extends State<OBActionableSmartText> {
var result = true;
var uri = Uri.parse(link);

if (await _urlLauncherService.getAskToConfirmOpen(host: uri.host)) {
if (await _preferencesService.getAskToConfirmOpenUrl(host: uri.host)) {
result = await _bottomSheetService.showConfirmOpenUrl(
link: link, context: context);
}
Expand Down

0 comments on commit 3c728d3

Please sign in to comment.