From e81b20537f5c1f134294f254d9157ff2148d65d0 Mon Sep 17 00:00:00 2001 From: kshib Date: Sun, 31 Mar 2024 10:51:23 +0530 Subject: [PATCH] format --- lib/api/simplytranslate.dart | 13 +-- lib/extractor/trend/apnews.dart | 4 +- lib/extractor/trend/brave.dart | 4 +- lib/extractor/trend/none.dart | 3 +- lib/extractor/trend/yahoo.dart | 4 +- lib/model/country.dart | 2 +- lib/model/publisher.dart | 7 +- lib/model/trends.dart | 5 +- lib/model/user_subscription.dart | 4 +- lib/pages/full_article.dart | 16 +-- lib/pages/home.dart | 3 +- lib/pages/subscription.dart | 186 ++++++++++++++++++------------- lib/utils/network.dart | 4 +- lib/utils/store.dart | 7 +- lib/utils/string.dart | 1 - lib/utils/theme_provider.dart | 9 +- 16 files changed, 143 insertions(+), 129 deletions(-) diff --git a/lib/api/simplytranslate.dart b/lib/api/simplytranslate.dart index de6d6d7..1ce3ee4 100644 --- a/lib/api/simplytranslate.dart +++ b/lib/api/simplytranslate.dart @@ -139,10 +139,10 @@ class SimplyTranslate { }; Future translate(String inputText, String language) async { - if(!languages.containsKey(language)) { + if (!languages.containsKey(language)) { return inputText; } - String translatedText =""; + String translatedText = ""; String url = 'https://simplytranslate.org/?engine=google'; inputText = removeHtmlAttributes(inputText); List inputTextParts = splitString(inputText, 500); @@ -154,9 +154,7 @@ class SimplyTranslate { }; var response = await http.post(Uri.parse(url), body: payload); var document = parse(response.body); - translatedText += document - .getElementById('output') - ?.text ?? ""; + translatedText += document.getElementById('output')?.text ?? ""; } return translatedText; } @@ -177,12 +175,13 @@ class SimplyTranslate { return result; } + String removeHtmlAttributes(String htmlString) { RegExp exp = RegExp(r'<[^>]+>'); return htmlString.replaceAllMapped(exp, (match) { String tag = match.group(0)!; - return tag.replaceAll(RegExp(r'\s\S+?="[^"]*?"'), ''); // Removes attributes + return tag.replaceAll( + RegExp(r'\s\S+?="[^"]*?"'), ''); // Removes attributes }); } - } diff --git a/lib/extractor/trend/apnews.dart b/lib/extractor/trend/apnews.dart index 1b7a6d0..3285470 100644 --- a/lib/extractor/trend/apnews.dart +++ b/lib/extractor/trend/apnews.dart @@ -1,11 +1,9 @@ import 'package:raven/model/trends.dart'; class APNewsTrend extends Trend { - @override String get url => "https://apnews.com/"; @override String get locator => ".PageListTrending .PagePromoContentIcons-text"; - -} \ No newline at end of file +} diff --git a/lib/extractor/trend/brave.dart b/lib/extractor/trend/brave.dart index db65f38..c3ebcea 100644 --- a/lib/extractor/trend/brave.dart +++ b/lib/extractor/trend/brave.dart @@ -1,11 +1,9 @@ import 'package:raven/model/trends.dart'; class BraveTrend extends Trend { - @override String get url => "https://search.brave.com/search?q=news"; @override String get locator => "#news-topics a"; - -} \ No newline at end of file +} diff --git a/lib/extractor/trend/none.dart b/lib/extractor/trend/none.dart index 0cc18bc..c8aa1be 100644 --- a/lib/extractor/trend/none.dart +++ b/lib/extractor/trend/none.dart @@ -1,7 +1,6 @@ import 'package:raven/model/trends.dart'; class NoneTrend extends Trend { - @override String get url => ""; @@ -10,4 +9,4 @@ class NoneTrend extends Trend { @override Future> get topics async => []; -} \ No newline at end of file +} diff --git a/lib/extractor/trend/yahoo.dart b/lib/extractor/trend/yahoo.dart index 6dc9e16..4f54733 100644 --- a/lib/extractor/trend/yahoo.dart +++ b/lib/extractor/trend/yahoo.dart @@ -1,11 +1,9 @@ import 'package:raven/model/trends.dart'; class YahooTrend extends Trend { - @override String get url => "https://www.yahoo.com/"; @override String get locator => ".trendingNowTextList a span:last-child"; - -} \ No newline at end of file +} diff --git a/lib/model/country.dart b/lib/model/country.dart index 024a55d..d618cf2 100644 --- a/lib/model/country.dart +++ b/lib/model/country.dart @@ -587,4 +587,4 @@ Map countryFlags = { "Yemen": "πŸ‡ΎπŸ‡ͺ", "Zambia": "πŸ‡ΏπŸ‡²", "Zimbabwe": "πŸ‡ΏπŸ‡Ό", -}; \ No newline at end of file +}; diff --git a/lib/model/publisher.dart b/lib/model/publisher.dart index 666ed36..e70c657 100644 --- a/lib/model/publisher.dart +++ b/lib/model/publisher.dart @@ -49,9 +49,11 @@ abstract class Publisher { : categoryArticles(category: category, page: page); } - Future> categoryArticles({String category = "All", int page = 1}); + Future> categoryArticles( + {String category = "All", int page = 1}); - Future> searchedArticles({required String searchQuery, int page = 1}); + Future> searchedArticles( + {required String searchQuery, int page = 1}); Future article(NewsArticle newsArticle); @@ -62,5 +64,4 @@ abstract class Publisher { 'categories': categories, }; } - } diff --git a/lib/model/trends.dart b/lib/model/trends.dart index c88ae2e..7a67dba 100644 --- a/lib/model/trends.dart +++ b/lib/model/trends.dart @@ -1,4 +1,3 @@ - import 'dart:convert'; import 'package:html/dom.dart'; @@ -19,7 +18,6 @@ Map trends = { }; abstract class Trend { - String get url; String get locator; @@ -31,5 +29,4 @@ abstract class Trend { } return []; } - -} \ No newline at end of file +} diff --git a/lib/model/user_subscription.dart b/lib/model/user_subscription.dart index 33c1c40..0e3d73d 100644 --- a/lib/model/user_subscription.dart +++ b/lib/model/user_subscription.dart @@ -3,7 +3,7 @@ import 'package:hive/hive.dart'; part 'user_subscription.g.dart'; @HiveType(typeId: 0) -class UserSubscription extends HiveObject{ +class UserSubscription extends HiveObject { @HiveField(0) String publisher; @@ -27,4 +27,4 @@ class UserSubscription extends HiveObject{ @override int get hashCode => publisher.hashCode ^ category.hashCode; -} \ No newline at end of file +} diff --git a/lib/pages/full_article.dart b/lib/pages/full_article.dart index 01cc911..dc6099f 100644 --- a/lib/pages/full_article.dart +++ b/lib/pages/full_article.dart @@ -32,19 +32,20 @@ class _ArticlePageState extends State { TextStyle excerptStyle = TextStyle(fontSize: 16, fontWeight: FontWeight.bold); - Stream customArticle(NewsArticle newsArticle, BuildContext context) async* { + Stream customArticle( + NewsArticle newsArticle, BuildContext context) async* { NewsArticle cArticle = await newsArticle.publisher.article(newsArticle); if (Store.translate) { var translator = SimplyTranslate(); cArticle.title = - await translator.translate(cArticle.title, Store.languageSetting); + await translator.translate(cArticle.title, Store.languageSetting); yield cArticle; cArticle.content = - await translator.translate(cArticle.content, Store.languageSetting); + await translator.translate(cArticle.content, Store.languageSetting); yield cArticle; cArticle.excerpt = - await translator.translate(cArticle.excerpt, Store.languageSetting); + await translator.translate(cArticle.excerpt, Store.languageSetting); yield cArticle; } @@ -94,7 +95,9 @@ class _ArticlePageState extends State { padding: EdgeInsets.fromLTRB(8, 8, 8, 8), child: ListView( children: [ - snapshot.connectionState != ConnectionState.done ? LinearProgressIndicator():SizedBox.shrink(), + snapshot.connectionState != ConnectionState.done + ? LinearProgressIndicator() + : SizedBox.shrink(), textWidget("", snapshot.data!.title, titleStyle), textWidget( "Author", snapshot.data!.author, metadataStyle), @@ -125,8 +128,7 @@ class _ArticlePageState extends State { ), ), ); - } - else if (snapshot.hasError) { + } else if (snapshot.hasError) { String fallbackUrl = "${widget.article.publisher.homePage}${widget.article.url}"; return Scaffold( diff --git a/lib/pages/home.dart b/lib/pages/home.dart index 168bde1..edcac10 100644 --- a/lib/pages/home.dart +++ b/lib/pages/home.dart @@ -3,7 +3,6 @@ import 'package:raven/pages/feed.dart'; import 'package:raven/pages/settings.dart'; import 'package:raven/pages/subscription.dart'; - class MyHomePage extends StatefulWidget { const MyHomePage({super.key}); @@ -57,4 +56,4 @@ class _MyHomePageState extends State { void dispose() { super.dispose(); } -} \ No newline at end of file +} diff --git a/lib/pages/subscription.dart b/lib/pages/subscription.dart index 802366f..38ea347 100644 --- a/lib/pages/subscription.dart +++ b/lib/pages/subscription.dart @@ -13,7 +13,8 @@ class SubscriptionsPage extends StatefulWidget { State createState() => _SubscriptionsPageState(); } -class _SubscriptionsPageState extends State with AutomaticKeepAliveClientMixin { +class _SubscriptionsPageState extends State + with AutomaticKeepAliveClientMixin { List newsSources = publishers.keys.toList(); List filteredNewsSources = []; TextEditingController searchController = TextEditingController(); @@ -30,17 +31,19 @@ class _SubscriptionsPageState extends State with AutomaticKee super.build(context); return Scaffold( appBar: AppBar( - title: _isSearching ? TextField( - controller: searchController, - onChanged: (value) => searchSubscriptions(), - ) : Text('Subscriptions'), + title: _isSearching + ? TextField( + controller: searchController, + onChanged: (value) => searchSubscriptions(), + ) + : Text('Subscriptions'), actions: [ IconButton( - icon: Icon( _isSearching ? Icons.close: Icons.search), + icon: Icon(_isSearching ? Icons.close : Icons.search), onPressed: () { setState(() { _isSearching = !_isSearching; - if(!_isSearching) { + if (!_isSearching) { searchController.text = ""; searchSubscriptions(); } @@ -55,38 +58,40 @@ class _SubscriptionsPageState extends State with AutomaticKee spacing: 5.0, children: List.generate( Category.values.length + 1, - (int index) { - if (index==0) { - return ChoiceChip( - label: Text("all"), - selected: _value == index, - onSelected: (bool selected) { - setState(() { - _value = selected ? index : null; - filteredNewsSources = newsSources; - searchSubscriptions(); - }); - }, - ); - } else { - return ChoiceChip( - label: Text(Category.values[index-1].toString().split(".")[1]), - selected: _value == index, - onSelected: (bool selected) { - setState(() { - _value = selected ? index : null; - searchSubscriptions(); - filteredNewsSources = filteredNewsSources - .where((element) => publishers[element]?.mainCategory==Category.values[index-1]) - .toList(); - }); - }, - ); - } + (int index) { + if (index == 0) { + return ChoiceChip( + label: Text("all"), + selected: _value == index, + onSelected: (bool selected) { + setState(() { + _value = selected ? index : null; + filteredNewsSources = newsSources; + searchSubscriptions(); + }); + }, + ); + } else { + return ChoiceChip( + label: Text( + Category.values[index - 1].toString().split(".")[1]), + selected: _value == index, + onSelected: (bool selected) { + setState(() { + _value = selected ? index : null; + searchSubscriptions(); + filteredNewsSources = filteredNewsSources + .where((element) => + publishers[element]?.mainCategory == + Category.values[index - 1]) + .toList(); + }); + }, + ); + } }, ).toList(), ), - Expanded( child: ListView.builder( itemCount: filteredNewsSources.length, @@ -97,23 +102,25 @@ class _SubscriptionsPageState extends State with AutomaticKee title: Text(newsSource), leading: CachedNetworkImage( imageUrl: publishers[newsSource]!.iconUrl, - progressIndicatorBuilder: - (context, url, downloadProgress) { + progressIndicatorBuilder: (context, url, downloadProgress) { return CircularProgressIndicator( - value: downloadProgress.progress, + value: downloadProgress.progress, ); }, height: 24, width: 24, errorWidget: (context, url, error) => - const Icon(Icons.error), + const Icon(Icons.error), ), - trailing: categories.isEmpty?SizedBox.shrink():Icon(Icons.check_circle), + trailing: categories.isEmpty + ? SizedBox.shrink() + : Icon(Icons.check_circle), onTap: () { showDialog( context: context, builder: (context) { - return CategoryPopup(publishers, newsSource, callback: () { + return CategoryPopup(publishers, newsSource, + callback: () { setState(() { categories = getSelectedCategories(newsSource); }); @@ -132,17 +139,18 @@ class _SubscriptionsPageState extends State with AutomaticKee void searchSubscriptions() { setState(() { - filteredNewsSources = newsSources - .where((source) { - return source.toLowerCase().contains(searchController.text.toLowerCase()); + filteredNewsSources = newsSources.where((source) { + return source + .toLowerCase() + .contains(searchController.text.toLowerCase()); }).toList(); }); } String getSelectedCategories(String newsSource) { var categories = Store.selectedSubscriptions - .where((element) => element.publisher==newsSource) - .map((e) => e.category!="/"?e.category.split("/").last:e.category) + .where((element) => element.publisher == newsSource) + .map((e) => e.category != "/" ? e.category.split("/").last : e.category) .join(", "); return categories; } @@ -156,7 +164,8 @@ class CategoryPopup extends StatefulWidget { final String newsSource; final VoidCallback callback; - const CategoryPopup(this.publishers, this.newsSource, {super.key, required this.callback}); + const CategoryPopup(this.publishers, this.newsSource, + {super.key, required this.callback}); @override State createState() => _CategoryPopupState(); @@ -167,14 +176,18 @@ class _CategoryPopupState extends State { List customSubscriptions = []; // List String customCategory = ""; TextEditingController customCategoryController = TextEditingController(); + Future>? future; @override void initState() { setState(() { selectedSubscriptions = Store.selectedSubscriptions; - customSubscriptions = Store.customSubscriptions.where((element) => element.publisher==widget.newsSource).toList(); + customSubscriptions = Store.customSubscriptions + .where((element) => element.publisher == widget.newsSource) + .toList(); }); super.initState(); + future = widget.publishers[widget.newsSource]?.categories; } String convertString(String input) { @@ -195,7 +208,7 @@ class _CategoryPopupState extends State { Widget build(BuildContext context) { return Dialog( child: FutureBuilder( - future: widget.publishers[widget.newsSource]?.categories, + future: future, builder: (context, snapshot) { if (snapshot.hasData) { return Padding( @@ -207,7 +220,8 @@ class _CategoryPopupState extends State { padding: EdgeInsets.all(8.0), child: Text( "Categories", - style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500), + style: + TextStyle(fontSize: 18, fontWeight: FontWeight.w500), ), ), ListView.builder( @@ -222,12 +236,13 @@ class _CategoryPopupState extends State { // All checkbox return CheckboxListTile( title: Text(subCategoryKey), - value: selectedSubscriptions.contains(userSubscription), + value: + selectedSubscriptions.contains(userSubscription), onChanged: (value) { if (value!) { selectedSubscriptions.removeWhere((element) { return element.publisher == - userSubscription.publisher; + userSubscription.publisher; }); } updateList(value, userSubscription); @@ -245,7 +260,10 @@ class _CategoryPopupState extends State { title: Text(subCategoryKey), value: selectedSubscriptions.contains(userSubscription), onChanged: selectedSubscriptions - .where((element) => element.publisher == userSubscription.publisher && element.category == "/") + .where((element) => + element.publisher == + userSubscription.publisher && + element.category == "/") .isNotEmpty ? null : (value) { @@ -256,29 +274,38 @@ class _CategoryPopupState extends State { ), ListView.builder( shrinkWrap: true, - itemCount: customSubscriptions.where((element) => element.publisher==widget.newsSource).length, + itemCount: customSubscriptions + .where( + (element) => element.publisher == widget.newsSource) + .length, itemBuilder: (context, index) { return CheckboxListTile( - secondary: IconButton(icon: const Icon(Icons.delete_forever), onPressed: () { - var subscription = customSubscriptions[index]; - setState(() { - customSubscriptions.remove(subscription); - selectedSubscriptions.remove(subscription); - }); - var cs = Store.customSubscriptions; - cs.remove(subscription); - Store.customSubscriptions= cs; - var ss = Store.selectedSubscriptions; - ss.remove(subscription); - Store.selectedSubscriptions = ss; - }), - title: Text(convertString((customSubscriptions[index] as UserSubscription).category)), - value: selectedSubscriptions.contains(customSubscriptions[index]), + secondary: IconButton( + icon: const Icon(Icons.delete_forever), + onPressed: () { + var subscription = customSubscriptions[index]; + setState(() { + customSubscriptions.remove(subscription); + selectedSubscriptions.remove(subscription); + }); + var cs = Store.customSubscriptions; + cs.remove(subscription); + Store.customSubscriptions = cs; + var ss = Store.selectedSubscriptions; + ss.remove(subscription); + Store.selectedSubscriptions = ss; + }), + title: Text(convertString( + (customSubscriptions[index] as UserSubscription) + .category)), + value: selectedSubscriptions + .contains(customSubscriptions[index]), onChanged: (value) { updateList(value, customSubscriptions[index]); }, ); - },), + }, + ), Flex( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.center, @@ -288,8 +315,8 @@ class _CategoryPopupState extends State { flex: 3, child: TextField( controller: customCategoryController, - decoration: - const InputDecoration(hintText: "Custom category"), + decoration: const InputDecoration( + hintText: "Custom category"), onEditingComplete: () { setState(() { customCategory = customCategoryController.text; @@ -310,15 +337,18 @@ class _CategoryPopupState extends State { ? IconButton( onPressed: () { setState(() { - customSubscriptions.add(UserSubscription( + customSubscriptions + .add(UserSubscription( widget.newsSource, customCategory, )); }); - Store.customSubscriptions +=[UserSubscription( - widget.newsSource, - customCategory, - )]; + Store.customSubscriptions += [ + UserSubscription( + widget.newsSource, + customCategory, + ) + ]; }, icon: const Icon(Icons.save_alt)) : const Icon(Icons.cancel); diff --git a/lib/utils/network.dart b/lib/utils/network.dart index 777a438..b7d9336 100644 --- a/lib/utils/network.dart +++ b/lib/utils/network.dart @@ -2,8 +2,6 @@ import 'package:raven/utils/store.dart'; class Network { static bool shouldLoadImage(String url) { - return url.isNotEmpty && - url.startsWith("https") && - Store.loadImagesSetting; + return url.isNotEmpty && url.startsWith("https") && Store.loadImagesSetting; } } diff --git a/lib/utils/store.dart b/lib/utils/store.dart index e109708..4d624c9 100644 --- a/lib/utils/store.dart +++ b/lib/utils/store.dart @@ -4,7 +4,6 @@ import 'package:raven/model/user_subscription.dart'; import 'package:raven/utils/theme_provider.dart'; class Store { - static Map ladders = { "12ft": "https://12ft.io", "1ft": "https://1ft.io", @@ -18,7 +17,8 @@ class Store { } static List get selectedSubscriptions { - return List.from(subscriptions.get("selected", defaultValue: [])); + return List.from( + subscriptions.get("selected", defaultValue: [])); } static set selectedSubscriptions(List newSubscriptions) { @@ -104,5 +104,4 @@ class Store { static set translate(bool should) { settings.put("translate", should); } - -} \ No newline at end of file +} diff --git a/lib/utils/string.dart b/lib/utils/string.dart index 6e8c52a..38a1980 100644 --- a/lib/utils/string.dart +++ b/lib/utils/string.dart @@ -5,7 +5,6 @@ String getAsSearchQuery(String category) { return category; } - String createTag(String inputString) { RegExp specialChars = RegExp(r'[^\w\s]', multiLine: true); String tag = inputString.replaceAll(specialChars, '').toLowerCase(); diff --git a/lib/utils/theme_provider.dart b/lib/utils/theme_provider.dart index 52d922c..fb52d80 100644 --- a/lib/utils/theme_provider.dart +++ b/lib/utils/theme_provider.dart @@ -2,8 +2,7 @@ import 'package:flutter/material.dart'; import 'package:raven/utils/store.dart'; class ThemeProvider { - - static String defaultColor = 'Raven'; + static String defaultColor = 'Raven'; static Map colors = { 'Raven': Colors.deepPurple, @@ -13,12 +12,11 @@ class ThemeProvider { 'Orange': Colors.orange, }; - static ThemeData get(Color color, bool dark) { return ThemeData( colorScheme: ColorScheme.fromSeed( seedColor: color, - brightness: dark?Brightness.dark:Brightness.light, + brightness: dark ? Brightness.dark : Brightness.light, ), ); } @@ -29,5 +27,4 @@ class ThemeProvider { Store.darkThemeSetting, ); } - -} \ No newline at end of file +}