Skip to content

Commit

Permalink
refactor(diec_repository): ♻️ extract repository
Browse files Browse the repository at this point in the history
  • Loading branch information
albertms10 committed Feb 4, 2024
1 parent 8707377 commit 61abd65
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 50 deletions.
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'src/settings/settings_service.dart';
void main() async {
// Set up the SettingsController, which will glue user settings to multiple
// Flutter Widgets.
final settingsController = SettingsController(SettingsService());
final settingsController = SettingsController(const SettingsService());

// Load the user's preferred theme while the splash screen is displayed.
// This prevents a sudden theme change when the app is first displayed.
Expand Down
26 changes: 3 additions & 23 deletions lib/src/model/autocomplete_entries.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;

import '../repositories/diec_repository.dart';
import 'autocomplete_entry.dart';
import 'search_condition.dart';

Expand All @@ -16,24 +12,8 @@ extension AutocompleteEntries on List<AutocompleteEntry> {
String query, {
SearchCondition searchCondition = SearchCondition.defaultCondition,
}) async {
final response = await http.post(
Uri.https(
'dlc.iec.cat',
'/Results/CompleteEntradaText',
{
'EntradaText': query,
'OperEntrada': '${searchCondition.index}',
},
),
headers: const {'Content-Type': 'application/json'},
);

if (response.statusCode != 200) {
debugPrint("Can't find definition");
return null;
}

final responseBody = json.decode(response.body) as Map<String, dynamic>;
final responseBody = await const DIECRepository()
.autocompleteEntries(query, searchCondition: searchCondition);

return fromJson(responseBody['entrades'] as List<dynamic>);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/model/autocomplete_entry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'definition_entry_sense.dart';
import 'word.dart';

@immutable
class AutocompleteEntry {
final class AutocompleteEntry {
final String id;
final String word;

Expand Down
2 changes: 1 addition & 1 deletion lib/src/model/bookmark_collection.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';

/// A collection of bookmarks.
class BookmarkCollection with ChangeNotifier {
final class BookmarkCollection with ChangeNotifier {
final Map<String, bool> _bookmarks;

/// Creates a new [BookmarkCollection] from [_bookmarks].
Expand Down
2 changes: 1 addition & 1 deletion lib/src/model/definition_entry_sense.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'gender.dart';
import 'scope.dart';

@immutable
class DefinitionEntrySense {
final class DefinitionEntrySense {
final int? number;
final int? subNumber;
final Gender? gender;
Expand Down
23 changes: 3 additions & 20 deletions lib/src/model/definition_entry_senses.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
// ignore_for_file: lines_longer_than_80_chars

import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:html/dom.dart';
import 'package:html/parser.dart' as html show parse;
import 'package:http/http.dart' as http;

import '../repositories/diec_repository.dart';
import 'definition_entry_sense.dart';

extension DefinitionEntrySenses on List<DefinitionEntrySense> {
Expand All @@ -19,22 +16,8 @@ extension DefinitionEntrySenses on List<DefinitionEntrySense> {
];

static Future<List<DefinitionEntrySense>?> fetch(String id) async {
final response = await http.post(
Uri.https('dlc.iec.cat', '/Results/Accepcio', {'id': id}),
headers: const {'Content-Type': 'application/json'},
);

if (response.statusCode != 200) {
debugPrint("Can't find definition");
return null;
}

final responseBody = json.decode(response.body) as Map<String, dynamic>;
return _parseHtml(responseBody['content'] as String);
}

static List<DefinitionEntrySense> _parseHtml(String content) {
final document = html.parse(content);
final responseBody = await const DIECRepository().definitionEntries(id);
final document = html.parse(responseBody['content']);
final body = document.getElementsByTagName('body').first;

final definitions = <List<Element>>[];
Expand Down
2 changes: 1 addition & 1 deletion lib/src/model/word_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:flutter/material.dart';
import 'word.dart';

/// The word cache.
class WordCache with ChangeNotifier {
final class WordCache with ChangeNotifier {
final Map<String, Word> _words;

/// Creates a new [WordCache] from [_words].
Expand Down
49 changes: 49 additions & 0 deletions lib/src/repositories/diec_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'dart:convert' show json;

import 'package:flutter/foundation.dart' show debugPrint, immutable;
import 'package:http/http.dart' as http;

import '../model/search_condition.dart';

@immutable
final class DIECRepository {
final String authority;

const DIECRepository({this.authority = 'dlc.iec.cat'});

Future<Map<String, dynamic>> autocompleteEntries(
String query, {
required SearchCondition searchCondition,
}) async {
final response = await http.post(
Uri.https(
authority,
'/Results/CompleteEntradaText',
{
'EntradaText': query,
'OperEntrada': '${searchCondition.index}',
},
),
headers: const {'Content-Type': 'application/json'},
);

return _parseResponse(response);
}

Future<Map<String, dynamic>> definitionEntries(String id) async {
final response = await http.post(
Uri.https(authority, '/Results/Accepcio', {'id': id}),
headers: const {'Content-Type': 'application/json'},
);

return _parseResponse(response);
}

Map<String, dynamic> _parseResponse(http.Response response) {
if (response.statusCode != 200) {
debugPrint("Can't find definition");
}

return json.decode(response.body) as Map<String, dynamic>;
}
}
2 changes: 1 addition & 1 deletion lib/src/settings/settings_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'settings_service.dart';
///
/// Controllers glue Data Services to Flutter Widgets. The SettingsController
/// uses the SettingsService to store and retrieve user settings.
class SettingsController with ChangeNotifier {
final class SettingsController with ChangeNotifier {
SettingsController(this._settingsService);

// Make SettingsService a private variable so it is not used directly.
Expand Down
5 changes: 4 additions & 1 deletion lib/src/settings/settings_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import 'package:flutter/material.dart';
/// By default, this class does not persist user settings. If you'd like to
/// persist the user settings locally, use the shared_preferences package. If
/// you'd like to store settings on a web server, use the http package.
class SettingsService {
@immutable
final class SettingsService {
const SettingsService();

/// Loads the User's preferred ThemeMode from local or remote storage.
Future<ThemeMode> themeMode() async => ThemeMode.system;

Expand Down

0 comments on commit 61abd65

Please sign in to comment.