From 325bae716a31d16412470404911dba435d4fa37a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lui=CC=81s=20Arteiro?= Date: Thu, 18 May 2023 11:45:04 +0100 Subject: [PATCH] feat: Adding comments. #96 --- lib/quotesy.dart | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/quotesy.dart b/lib/quotesy.dart index a4bb1be..69f1241 100644 --- a/lib/quotesy.dart +++ b/lib/quotesy.dart @@ -1,27 +1,32 @@ -/// Support for doing something awesome. -/// -/// More dartdocs go here. library quotesy; import 'dart:convert'; import 'dart:io'; import 'dart:math'; +/// Class holding a quote information class QuoteInfo { final String author; final String text; + final String? source; + final String? tags; - QuoteInfo(this.text, this.author); + QuoteInfo(this.text, this.author, {this.source, this.tags}); factory QuoteInfo.fromJson(Map json) { return QuoteInfo( json['text'], json['author'], + source: json['source'], + tags: json['tags'], ); } } +/// Static class that can be invoked to fetch quotes class Quotes { + + /// Loads the information from the `.json` file containing the quotes list. static Future> _quotesList() async { String jsonString = await File('quotes.json').readAsString(); List jsonList = json.decode(jsonString); @@ -29,10 +34,12 @@ class Quotes { return quotes; } + /// Returns a list of quotes static list() async { return await _quotesList(); } + /// Returns a random quote static Future random() async { final random = Random(); final list = await _quotesList(); @@ -40,11 +47,13 @@ class Quotes { return list[random.nextInt(list.length)]; } + /// Returns a list of quotes by author static Future> byAuthor(String author) async { final list = await _quotesList(); return list.where((quote) => quote.author == author).toList(); } + /// Returns a random quote by author static Future singleRandomByAuthor(String author) async { final list = await _quotesList(); final listByAuthor = list.where((quote) => quote.author == author).toList();