From 13258c6bc66cb3edc1f681289b327de0ab64a552 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Wed, 5 Oct 2022 16:57:23 +0200 Subject: [PATCH] neon: Fix article body fixing --- packages/neon/lib/src/apps/news/app.dart | 2 - .../src/apps/news/widgets/articles_view.dart | 112 ++++++++---------- 2 files changed, 48 insertions(+), 66 deletions(-) diff --git a/packages/neon/lib/src/apps/news/app.dart b/packages/neon/lib/src/apps/news/app.dart index 6c7fd187..73bfc393 100644 --- a/packages/neon/lib/src/apps/news/app.dart +++ b/packages/neon/lib/src/apps/news/app.dart @@ -12,9 +12,7 @@ import 'package:material_design_icons_flutter/material_design_icons_flutter.dart import 'package:neon/l10n/localizations.dart'; import 'package:neon/src/apps/news/blocs/articles.dart'; import 'package:neon/src/apps/news/blocs/news.dart'; -import 'package:neon/src/blocs/accounts.dart'; import 'package:neon/src/blocs/apps.dart'; -import 'package:neon/src/models/account.dart'; import 'package:neon/src/neon.dart'; import 'package:nextcloud/nextcloud.dart'; import 'package:provider/provider.dart'; diff --git a/packages/neon/lib/src/apps/news/widgets/articles_view.dart b/packages/neon/lib/src/apps/news/widgets/articles_view.dart index 81b30700..c138206c 100644 --- a/packages/neon/lib/src/apps/news/widgets/articles_view.dart +++ b/packages/neon/lib/src/apps/news/widgets/articles_view.dart @@ -132,23 +132,8 @@ class _NewsArticlesViewState extends State { final NewsArticlesBloc bloc, final NewsArticle article, final NewsFeed feed, - ) { - final clientID = RxBlocProvider.of(context).activeAccount.value!.client.id; - - return ResultStreamBuilder( - stream: Provider.of(context).wrapString( - clientID, - 'news-articles-body-${article.id}', - () async => _fixArticleBody(article.body!), - preferCache: true, - ), - builder: ( - final context, - final bodyData, - final bodyError, - final bodyLoading, - ) => - ListTile( + ) => + ListTile( title: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ @@ -225,56 +210,53 @@ class _NewsArticlesViewState extends State { bloc.markArticleAsUnread(article); } }, - onTap: bodyData != null - ? () async { - final viewType = bloc.newsBloc.options.articleViewTypeOption.value; - if (viewType == ArticleViewType.direct) { - await Navigator.of(context).push( - MaterialPageRoute( - builder: (final context) => NewsArticlePage( - bloc: bloc, - article: article, - useWebView: false, - bodyData: bodyData, - ), - ), - ); - } else if (Provider.of(context, listen: false).canUseWebView && - viewType == ArticleViewType.internalBrowser) { - await Navigator.of(context).push( - MaterialPageRoute( - builder: (final context) => NewsArticlePage( - bloc: bloc, - article: article, - useWebView: true, - ), - ), - ); - } else { - if (article.unread!) { - bloc.markArticleAsRead(article); - } - await launchUrlString( - article.url!, - mode: LaunchMode.externalApplication, - ); - } - } - : null, - ), - ); - } - - String _fixArticleBody(final String b) => _fixTree(html_parser.parse(b).documentElement!).outerHtml; + onTap: () async { + final viewType = bloc.newsBloc.options.articleViewTypeOption.value; + String? bodyData; + try { + bodyData = _fixArticleBody(article.body!); + } catch (e, s) { + debugPrint(e.toString()); + debugPrint(s.toString()); + } - html_dom.Element _fixTree(final html_dom.Element element) { - _fixElement(element); - element.children.forEach(_fixTree); + if (viewType == ArticleViewType.direct && bodyData != null) { + await Navigator.of(context).push( + MaterialPageRoute( + builder: (final context) => NewsArticlePage( + bloc: bloc, + article: article, + useWebView: false, + bodyData: bodyData, + ), + ), + ); + } else if (Provider.of(context, listen: false).canUseWebView && + viewType == ArticleViewType.internalBrowser) { + await Navigator.of(context).push( + MaterialPageRoute( + builder: (final context) => NewsArticlePage( + bloc: bloc, + article: article, + useWebView: true, + ), + ), + ); + } else { + if (article.unread!) { + bloc.markArticleAsRead(article); + } + await launchUrlString( + article.url!, + mode: LaunchMode.externalApplication, + ); + } + }, + ); - return element; - } + String _fixArticleBody(final String b) => _fixArticleBodyElement(html_parser.parse(b).documentElement!).outerHtml; - html_dom.Element _fixElement(final html_dom.Element element) { + html_dom.Element _fixArticleBodyElement(final html_dom.Element element) { for (final attributeName in ['src', 'href']) { final attributeValue = element.attributes[attributeName]; if (attributeValue != null && attributeValue.startsWith('//')) { @@ -282,6 +264,8 @@ class _NewsArticlesViewState extends State { } } + element.children.forEach(_fixArticleBodyElement); + return element; } }