Skip to content

Commit

Permalink
fix url validation
Browse files Browse the repository at this point in the history
  • Loading branch information
wuuzw committed Feb 9, 2021
1 parent 93620f7 commit 4a446f6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
3 changes: 2 additions & 1 deletion lib/app/modules/item/item_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter_modular/flutter_modular.dart';
import 'package:mobx/mobx.dart';

import '../../shared/models/item.dart';
import '../../shared/util/extensions.dart';
import 'repository/item_repository.dart';

part 'item_controller.g.dart';
Expand All @@ -27,7 +28,7 @@ abstract class _ItemControllerBase with Store {
@observable
bool isLoadingWebContent = false;

bool get hasWebContent => Uri.parse(itemFuture.value.url).isAbsolute;
bool get hasWebContent => !itemFuture.value.domain.isNullOrEmpty();

@action
void switchView() {
Expand Down
5 changes: 2 additions & 3 deletions lib/app/service/share_service.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import 'package:flutter_modular/flutter_modular.dart';
import 'package:share/share.dart';

import '../shared/util/url_util.dart';
import '../shared/util/extensions.dart';

part 'share_service.g.dart';

@Injectable()
class ShareService extends Disposable {
void share({String title, String url}) {
var parsedUrl = UrlUtil.parseUrl(url);
Share.share('$title\n$parsedUrl');
Share.share('$title\n${url.toUrl()}');
}

//dispose will be called automatically
Expand Down
5 changes: 2 additions & 3 deletions lib/app/service/url_launch_service.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import 'package:flutter_modular/flutter_modular.dart';
import 'package:url_launcher/url_launcher.dart';

import '../shared/util/url_util.dart';
import '../shared/util/extensions.dart';

part 'url_launch_service.g.dart';

@Injectable()
class UrlLaunchService extends Disposable {
Future<void> launchUrl(String url) async {
var parseUrl = UrlUtil.parseUrl(url);
await launch(parseUrl);
await launch(url.toUrl());
}

//dispose will be called automatically
Expand Down
10 changes: 10 additions & 0 deletions lib/app/shared/util/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,14 @@ extension StringExtension on String {
return feedType.name == toLowerCase();
});
}

bool isNullOrEmpty() {
return this == null || isEmpty;
}

String toUrl() {
var urlPattern = r'(https?|http)://([-A-Z0-9.]+)(/[-A-Z0-9+&@#/%=~_|!:,.;]*)?(\?[A-Z0-9+&@#/%=~_|!:‌​,.;]*)?';
var match = RegExp(urlPattern, caseSensitive: false).firstMatch(this);
return match != null ? this : 'https://news.ycombinator.com/$this';
}
}
5 changes: 0 additions & 5 deletions lib/app/shared/util/url_util.dart

This file was deleted.

0 comments on commit 4a446f6

Please sign in to comment.