Skip to content

Commit 614108d

Browse files
committed
Add NoPatternMatchException and handler with url_launcher package
1 parent 63049a1 commit 614108d

File tree

6 files changed

+105
-56
lines changed

6 files changed

+105
-56
lines changed

lib/helpers/retriever.dart

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ Map<String, Map<String, RegExp>> lawRegexes = {
2222
class Retriever {
2323
static String lawTextString;
2424
static Map<String, String> lawContents = {};
25-
static String lawPattern;
25+
static String lawPattern = "";
2626

2727
static Future<Map<String, String>> retrieveLawText({String url}) async {
2828
await _getLawTextString(url);
29-
_selectLawPattern(); // TODO: if no pattern matches, url_launcher for Infoleg's site
29+
_selectLawPattern();
3030
_parseMostArticles();
3131
_parseFinalArticle();
3232
return lawContents;
@@ -39,10 +39,20 @@ class Retriever {
3939
}
4040

4141
static void _selectLawPattern() {
42-
if (lawTextString.contains(lawRegexes["law20305"]["initial"])) {
42+
bool isLaw20305 =
43+
lawTextString.contains(lawRegexes["law20305"]["initial"]) &&
44+
lawTextString.contains(lawRegexes["law20305"]["final"]);
45+
46+
bool isLaw11723 =
47+
lawTextString.contains(lawRegexes["law11723"]["initial"]) &&
48+
lawTextString.contains(lawRegexes["law11723"]["final"]);
49+
50+
if (isLaw20305) {
4351
lawPattern = "law20305";
44-
} else if (lawTextString.contains(lawRegexes["law11723"]["initial"])) {
52+
} else if (isLaw11723) {
4553
lawPattern = "law11723";
54+
} else if (lawPattern == "") {
55+
throw NoPatternMatchException();
4656
}
4757
}
4858

lib/screens/law_summary_screen.dart

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:flutter/material.dart';
22
import 'package:infobootleg/models/law_model.dart';
3+
import 'package:infobootleg/widgets/alert_box.dart';
34
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
45

56
import 'package:infobootleg/helpers/retriever.dart';
@@ -8,9 +9,14 @@ import 'package:infobootleg/widgets/law_title_card.dart';
89
import 'package:infobootleg/widgets/modif_relations_box.dart';
910
import 'package:infobootleg/models/search_state_model.dart';
1011
import 'package:infobootleg/helpers/hex_color.dart';
12+
import 'package:url_launcher/url_launcher.dart';
1113

1214
enum ModificationType { modifies, isModifiedBy }
1315

16+
class NoPatternMatchException implements Exception {
17+
NoPatternMatchException();
18+
}
19+
1420
class LawSummaryScreen extends StatelessWidget {
1521
LawSummaryScreen(this.searchState);
1622

@@ -105,19 +111,44 @@ class LawSummaryScreen extends StatelessWidget {
105111
);
106112
}
107113

114+
_launchURL(String url) async {
115+
if (await canLaunch(url)) {
116+
await launch(url);
117+
} else {
118+
throw 'Could not launch $url';
119+
}
120+
}
121+
122+
void _accessLaw(BuildContext context) async {
123+
searchState.toggleLoadingState();
124+
try {
125+
await searchState.updateLawContents();
126+
searchState.transitionToScreenVertically(Screen.text);
127+
searchState.toggleLoadingState();
128+
} on NoPatternMatchException {
129+
final bool answer = await AlertBox(
130+
title: "Formato desconocido",
131+
content:
132+
"La Ley ${searchState.activeLaw.number} tiene formato desconocido y la aplicación no puede procesarla. ¿Abrir esta ley en Infoleg en el navagador?",
133+
confirmActionText: "Sí",
134+
cancelActionText: "No",
135+
).show(context);
136+
if (answer) {
137+
_launchURL(searchState.activeLaw.link);
138+
}
139+
} finally {
140+
searchState.toggleLoadingState();
141+
}
142+
}
143+
108144
_buildLawAccessButton(BuildContext context, bool isLoading) {
109145
if (isLoading == true) {
110146
return CircularProgressIndicator();
111147
}
112148

113149
return RaisedButton(
150+
onPressed: () => _accessLaw(context),
114151
elevation: 10.0,
115-
onPressed: () async {
116-
searchState.toggleLoadingState();
117-
await searchState.updateLawContents();
118-
searchState.transitionToScreenVertically(Screen.text);
119-
searchState.toggleLoadingState();
120-
},
121152
autofocus: true,
122153
color: hexColor("2c7873"),
123154
shape: RoundedRectangleBorder(

lib/screens/law_text_screen.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,6 @@ class _LawTextScreenState extends State<LawTextScreen> {
9797
);
9898
}
9999

100-
bool _getStarredStatus(String articleNumber) {
101-
String dotlessLawNumber =
102-
widget.searchState.activeLaw.number.replaceAll(".", "");
103-
String cardName = dotlessLawNumber + "&" + articleNumber;
104-
if (userFavorites != null) {
105-
if (userFavorites.keys.toList().contains(cardName)) {
106-
return true;
107-
}
108-
}
109-
return false;
110-
}
111-
112100
Widget _buildListItem(
113101
{@required String articleNumber, @required BuildContext context}) {
114102
if (articleNumber == "0") {
@@ -138,6 +126,18 @@ class _LawTextScreenState extends State<LawTextScreen> {
138126
);
139127
}
140128

129+
bool _getStarredStatus(String articleNumber) {
130+
String dotlessLawNumber =
131+
widget.searchState.activeLaw.number.replaceAll(".", "");
132+
String cardName = dotlessLawNumber + "&" + articleNumber;
133+
if (userFavorites != null) {
134+
if (userFavorites.keys.toList().contains(cardName)) {
135+
return true;
136+
}
137+
}
138+
return false;
139+
}
140+
141141
void _onYesAtSave(Favorite favorite, BuildContext context) async {
142142
await widget.dbService.saveFavorite(favorite);
143143
_updateUserFavorites();

lib/services/database_service.dart

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -67,36 +67,3 @@ class DatabaseService {
6767
});
6868
}
6969
}
70-
71-
// https://firebase.google.com/docs/firestore/manage-data/add-data
72-
// https://firebase.google.com/docs/firestore/manage-data/delete-data
73-
74-
// @override
75-
// Future<void> createJob(Job job) async => _setData(
76-
// path: APIPath.job(userId, "job_abc"),
77-
// data: job.toMap(),
78-
// );
79-
80-
// single entry point for all writes to Firestore
81-
// Future<void> _setData({String path, Map<String, dynamic> data}) async {
82-
// final reference = Firestore.instance.document(path);
83-
// await reference.setData(data);
84-
// }
85-
86-
// Stream<List<Job>> jobsStream() => _collectionStream(
87-
// path: APIPath.jobs(userId),
88-
// builder: (data) => Job.fromMap(data),
89-
// );
90-
91-
// Stream<List<T>> _collectionStream<T>({
92-
// @required String path,
93-
// @required T builder(Map<String, dynamic> data),
94-
// }) {
95-
// final reference = Firestore.instance.collection(path);
96-
// final snapshots = reference.snapshots();
97-
98-
// return snapshots.map(
99-
// (snapshot) =>
100-
// snapshot.documents.map((snapshot) => builder(snapshot.data)).toList(),
101-
// );
102-
// }

pubspec.lock

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ packages:
109109
description: flutter
110110
source: sdk
111111
version: "0.0.0"
112+
flutter_web_plugins:
113+
dependency: transitive
114+
description: flutter
115+
source: sdk
116+
version: "0.0.0"
112117
flutter_widgets:
113118
dependency: "direct main"
114119
description:
@@ -200,6 +205,13 @@ packages:
200205
url: "https://pub.dartlang.org"
201206
source: hosted
202207
version: "2.4.0"
208+
plugin_platform_interface:
209+
dependency: transitive
210+
description:
211+
name: plugin_platform_interface
212+
url: "https://pub.dartlang.org"
213+
source: hosted
214+
version: "1.0.1"
203215
provider:
204216
dependency: "direct main"
205217
description:
@@ -268,6 +280,34 @@ packages:
268280
url: "https://pub.dartlang.org"
269281
source: hosted
270282
version: "1.1.6"
283+
url_launcher:
284+
dependency: "direct main"
285+
description:
286+
name: url_launcher
287+
url: "https://pub.dartlang.org"
288+
source: hosted
289+
version: "5.4.1"
290+
url_launcher_macos:
291+
dependency: transitive
292+
description:
293+
name: url_launcher_macos
294+
url: "https://pub.dartlang.org"
295+
source: hosted
296+
version: "0.0.1+2"
297+
url_launcher_platform_interface:
298+
dependency: transitive
299+
description:
300+
name: url_launcher_platform_interface
301+
url: "https://pub.dartlang.org"
302+
source: hosted
303+
version: "1.0.5"
304+
url_launcher_web:
305+
dependency: transitive
306+
description:
307+
name: url_launcher_web
308+
url: "https://pub.dartlang.org"
309+
source: hosted
310+
version: "0.1.0+2"
271311
vector_math:
272312
dependency: transitive
273313
description:
@@ -284,4 +324,4 @@ packages:
284324
version: "3.5.0"
285325
sdks:
286326
dart: ">=2.4.0 <3.0.0"
287-
flutter: ">=1.9.1 <2.0.0"
327+
flutter: ">=1.12.8 <2.0.0"

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ dependencies:
2020
http:
2121
html:
2222
flutter_widgets:
23+
url_launcher:
2324

2425
dev_dependencies:
2526
flutter_test:

0 commit comments

Comments
 (0)