web feed intermediate

This commit is contained in:
Fabian Freund
2025-02-28 14:31:07 +01:00
parent 3c4d479bd9
commit 2a2311253b
51 changed files with 5503 additions and 0 deletions
@@ -0,0 +1,21 @@
import 'dart:convert';
import 'package:drift/drift.dart';
import 'package:lensai/features/web_feed/data/models/feed_author.dart';
class FeedAuthorsConverter extends TypeConverter<List<FeedAuthor>, String> {
const FeedAuthorsConverter();
@override
List<FeedAuthor> fromSql(String fromDb) {
final authors = jsonDecode(fromDb) as List<dynamic>;
return authors
.map((author) => FeedAuthor.fromJson(author as Map<String, dynamic>))
.toList();
}
@override
String toSql(List<FeedAuthor> value) {
return jsonEncode(value.map((author) => author.toJson()).toList());
}
}
@@ -0,0 +1,24 @@
import 'dart:convert';
import 'package:drift/drift.dart';
import 'package:lensai/features/web_feed/data/models/feed_category.dart';
class FeedCategoriesConverter
extends TypeConverter<List<FeedCategory>, String> {
const FeedCategoriesConverter();
@override
List<FeedCategory> fromSql(String fromDb) {
final categories = jsonDecode(fromDb) as List<dynamic>;
return categories
.map(
(category) => FeedCategory.fromJson(category as Map<String, dynamic>),
)
.toList();
}
@override
String toSql(List<FeedCategory> value) {
return jsonEncode(value.map((category) => category.toJson()).toList());
}
}
@@ -0,0 +1,16 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:lensai/features/web_feed/data/database/database.dart';
class FeedDataConverter extends JsonConverter<FeedData, Map<String, dynamic>> {
const FeedDataConverter();
@override
FeedData fromJson(Map<String, dynamic> json) {
return FeedData.fromJson(json);
}
@override
Map<String, dynamic> toJson(FeedData object) {
return object.toJson();
}
}
@@ -0,0 +1,21 @@
import 'dart:convert';
import 'package:drift/drift.dart';
import 'package:lensai/features/web_feed/data/models/feed_link.dart';
class FeedLinksConverter extends TypeConverter<List<FeedLink>, String> {
const FeedLinksConverter();
@override
List<FeedLink> fromSql(String fromDb) {
final links = jsonDecode(fromDb) as List<dynamic>;
return links
.map((link) => FeedLink.fromJson(link as Map<String, dynamic>))
.toList();
}
@override
String toSql(List<FeedLink> value) {
return jsonEncode(value.map((link) => link.toJson()).toList());
}
}
@@ -0,0 +1,119 @@
import 'package:drift/drift.dart';
import 'package:lensai/features/web_feed/data/database/database.dart';
import 'package:lensai/features/web_feed/data/models/feed_article.dart';
import 'package:lensai/features/web_feed/data/models/feed_article_query_result.dart';
part 'article.g.dart';
@DriftAccessor()
class ArticleDao extends DatabaseAccessor<FeedDatabase> with _$ArticleDaoMixin {
ArticleDao(super.attachedDatabase);
Selectable<FeedArticle> getFeedArticles(Uri? url) {
final select = db.article.select();
if (url != null) {
select.where((article) => article.feedId.equalsValue(url));
}
return select..orderBy([
(row) => OrderingTerm(
expression: coalesce([row.updated, row.created]),
mode: OrderingMode.desc,
),
]);
}
SingleOrNullSelectable<FeedArticle> getArticleById(String articleId) {
return db.article.select()..where((row) => row.id.equals(articleId));
}
Future<void> upsertArticles(List<FeedArticle> articles) {
return db.transaction(() async {
await Future.wait(
articles
.map(
(article) => db.article.insertOne(
article,
onConflict: DoUpdate(
(old) {
return ArticleCompanion(
authors: Value(article.authors),
contentMarkdown: Value(article.contentMarkdown),
contentPlain: Value(article.contentPlain),
links: Value(article.links),
summaryMarkdown: Value(article.summaryMarkdown),
summaryPlain: Value(article.summaryPlain),
tags: Value(article.tags),
title: Value(article.title),
updated: Value(article.updated),
);
},
where:
(old) =>
old.updated.isNotNull() &
old.updated.isSmallerThanValue(
article.updated ?? DateTime(0),
),
),
),
)
.toList(),
);
});
}
Future<int> updateArticleRead(String articleId, DateTime? read) {
final statement =
db.article.update()..where((article) => article.id.equals(articleId));
return statement.write(ArticleCompanion(lastRead: Value(read)));
}
Selectable<(String, int)> getUnreadArticleCount() {
final count = countAll();
final countByFeed =
db.article.selectOnly()
..addColumns([db.article.feedId, count])
..where(
db.article.lastRead.isNull() |
(db.article.updated.isNotNull() &
db.article.lastRead.isSmallerThan(db.article.lastRead)),
)
..groupBy([db.article.feedId]);
return countByFeed.map(
(result) => (result.read(db.article.feedId)!, result.read(count)!),
);
}
Selectable<FeedArticleQueryResult> queryArticles({
required String matchPrefix,
required String matchSuffix,
required String ellipsis,
required int snippetLength,
required String searchString,
required Uri? feedId,
}) {
final ftsQuery = db.buildFtsQuery(searchString);
if (ftsQuery.isNotEmpty) {
return db.queryArticlesFullContent(
feedId: feedId?.toString(),
query: ftsQuery,
snippetLength: snippetLength,
beforeMatch: matchPrefix,
afterMatch: matchSuffix,
ellipsis: ellipsis,
);
} else {
return db.queryArticlesBasic(
feedId: feedId?.toString(),
query: db.buildLikeQuery(searchString),
beforeMatch: matchPrefix,
afterMatch: matchSuffix,
);
}
}
}
@@ -0,0 +1,6 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'article.dart';
// ignore_for_file: type=lint
mixin _$ArticleDaoMixin on DatabaseAccessor<FeedDatabase> {}
@@ -0,0 +1,38 @@
import 'package:drift/drift.dart';
import 'package:lensai/features/web_feed/data/database/database.dart';
part 'feed.g.dart';
@DriftAccessor()
class FeedDao extends DatabaseAccessor<FeedDatabase> with _$FeedDaoMixin {
FeedDao(super.attachedDatabase);
Selectable<FeedData> getFeeds() {
return db.feed.select();
}
Future<int> updateFeedFetched(Uri url, DateTime fetched) {
final statement =
db.feed.update()..where((feed) => feed.url.equalsValue(url));
return statement.write(FeedCompanion(lastFetched: Value(fetched)));
}
Future<int> deleteFeed(Uri url) {
return db.feed.deleteWhere((feed) => feed.url.equals(url.toString()));
}
Future<int> upsertFeed(FeedData feedData) {
return db.feed.insertOne(
feedData,
onConflict: DoUpdate((old) {
return FeedCompanion(
authors: Value(feedData.authors),
title: Value(feedData.title),
description: Value(feedData.description),
tags: Value(feedData.tags),
);
}),
);
}
}
@@ -0,0 +1,6 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'feed.dart';
// ignore_for_file: type=lint
mixin _$FeedDaoMixin on DatabaseAccessor<FeedDatabase> {}
@@ -0,0 +1,36 @@
import 'package:drift/drift.dart';
import 'package:lensai/data/database/converters/uri.dart';
import 'package:lensai/features/search/domain/fts_tokenizer.dart';
import 'package:lensai/features/web_feed/data/database/converters/feed_authors.dart';
import 'package:lensai/features/web_feed/data/database/converters/feed_categories.dart';
import 'package:lensai/features/web_feed/data/database/converters/feed_links.dart';
import 'package:lensai/features/web_feed/data/database/daos/article.dart';
import 'package:lensai/features/web_feed/data/database/daos/feed.dart';
import 'package:lensai/features/web_feed/data/models/feed_article.dart';
import 'package:lensai/features/web_feed/data/models/feed_article_query_result.dart';
import 'package:lensai/features/web_feed/data/models/feed_author.dart';
import 'package:lensai/features/web_feed/data/models/feed_category.dart';
import 'package:lensai/features/web_feed/data/models/feed_link.dart';
part 'database.g.dart';
@DriftDatabase(include: {'database.drift'}, daos: [ArticleDao, FeedDao])
class FeedDatabase extends _$FeedDatabase with TrigramQueryBuilderMixin {
@override
final int schemaVersion = 1;
@override
final int ftsTokenLimit = 10;
@override
final int ftsMinTokenLength = 3;
@override
MigrationStrategy get migration => MigrationStrategy(
beforeOpen: (details) async {
await customStatement('PRAGMA foreign_keys = ON;');
await optimizeFtsIndex();
},
);
FeedDatabase(super.e);
}
@@ -0,0 +1,116 @@
import 'package:lensai/data/database/converters/uri.dart';
import 'package:lensai/features/web_feed/data/models/feed_article_query_result.dart';
import 'package:lensai/features/web_feed/data/database/converters/feed_authors.dart';
import 'package:lensai/features/web_feed/data/database/converters/feed_categories.dart';
import 'package:lensai/features/web_feed/data/database/converters/feed_links.dart';
import 'package:lensai/features/web_feed/data/models/feed_article.dart';
CREATE TABLE feed (
url TEXT PRIMARY KEY NOT NULL MAPPED BY `const UriConverter()`,
title TEXT,
description TEXT,
authors TEXT MAPPED BY `const FeedAuthorsConverter()`,
tags TEXT MAPPED BY `const FeedCategoriesConverter()`,
last_fetched DATETIME
);
CREATE TABLE article (
id TEXT PRIMARY KEY NOT NULL,
feed_id TEXT NOT NULL MAPPED BY `const UriConverter()` REFERENCES feed ("url") ON DELETE CASCADE,
fetched DATETIME NOT NULL,
created DATETIME,
updated DATETIME,
last_read DATETIME,
title TEXT,
authors TEXT MAPPED BY `const FeedAuthorsConverter()`,
tags TEXT MAPPED BY `const FeedCategoriesConverter()`,
links TEXT MAPPED BY `const FeedLinksConverter()`,
summaryMarkdown TEXT,
summaryPlain TEXT,
contentMarkdown TEXT,
contentPlain TEXT
) WITH FeedArticle;
CREATE INDEX article_feed_id ON article (feed_id);
CREATE VIRTUAL TABLE article_fts
USING fts5(
title,
summaryPlain,
contentPlain,
content=article,
tokenize="trigram"
);
-- Triggers to keep the FTS index up to date.
CREATE TRIGGER article_after_insert AFTER INSERT ON article BEGIN
INSERT INTO
article_fts(rowid, title, summaryPlain, contentPlain)
VALUES (new.rowid, new.title, new.summaryPlain, new.contentPlain);
END;
CREATE TRIGGER article_after_delete AFTER DELETE ON article BEGIN
INSERT INTO
article_fts(article_fts, rowid, title, summaryPlain, contentPlain)
VALUES('delete', old.rowid, old.title, old.summaryPlain, old.contentPlain);
END;
CREATE TRIGGER article_after_update AFTER UPDATE ON article BEGIN
INSERT INTO
article_fts(article_fts, rowid, title, summaryPlain, contentPlain)
VALUES('delete', old.rowid, old.title, old.summaryPlain, old.contentPlain);
INSERT INTO
article_fts(rowid, title, summaryPlain, contentPlain)
VALUES (new.rowid, new.title, new.summaryPlain, new.contentPlain);
END;
optimizeFtsIndex:
INSERT INTO article_fts(article_fts) VALUES ('optimize');
queryArticlesBasic(:feed_id AS TEXT OR NULL) WITH FeedArticleQueryResult:
WITH weights AS (
SELECT
-- Customize these weights (higher = more important)
1.0 as title_weight -- Title matches are most important
)
SELECT
a.*,
highlight(article_fts, 0, :beforeMatch, :afterMatch) AS title,
(
bm25(article_fts, weights.title_weight)
) AS weighted_rank
FROM article_fts(:query) fts
INNER JOIN
article a ON a.rowid = fts.rowid
CROSS JOIN weights
WHERE
fts.title LIKE :query AND
:feed_id IS NULL OR a.feed_id = :feed_id
ORDER BY
weighted_rank ASC,
a.created DESC NULLS LAST;
queryArticlesFullContent(:feed_id AS TEXT OR NULL) WITH FeedArticleQueryResult:
WITH weights AS (
SELECT
-- Customize these weights (higher = more important)
5.0 as title_weight, -- Title matches are most important
2.0 as summary_weight, -- Summary matches are quite important
1.0 as content_weight -- Content matches are basic
)
SELECT
a.*,
highlight(article_fts, 0, :beforeMatch, :afterMatch) AS title,
snippet(article_fts, 1, :beforeMatch, :afterMatch, :ellipsis, :snippetLength) AS summary,
snippet(article_fts, 2, :beforeMatch, :afterMatch, :ellipsis, :snippetLength) AS content,
(
bm25(article_fts, weights.title_weight, weights.summary_weight,
weights.content_weight)
) AS weighted_rank
FROM article_fts(:query) fts
INNER JOIN
article a ON a.rowid = fts.rowid
CROSS JOIN weights
WHERE
:feed_id IS NULL OR a.feed_id = :feed_id
ORDER BY
weighted_rank ASC,
a.created DESC NULLS LAST;
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,116 @@
import 'package:drift/drift.dart';
import 'package:fast_equatable/fast_equatable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:lensai/features/web_feed/data/database/database.dart';
import 'package:lensai/features/web_feed/data/models/feed_author.dart';
import 'package:lensai/features/web_feed/data/models/feed_category.dart';
import 'package:lensai/features/web_feed/data/models/feed_link.dart';
part 'feed_article.g.dart';
@JsonSerializable()
class FeedArticle with FastEquatable implements Insertable<FeedArticle> {
final String id;
final Uri feedId;
final DateTime fetched;
final DateTime? created;
final DateTime? updated;
final DateTime? lastRead;
final String? title;
final List<FeedAuthor>? authors;
final List<FeedCategory>? tags;
final List<FeedLink>? links;
final String? summaryMarkdown;
final String? summaryPlain;
final String? contentMarkdown;
final String? contentPlain;
FeedArticle({
required this.id,
required this.feedId,
required this.fetched,
this.created,
this.updated,
this.lastRead,
this.title,
this.authors,
this.tags,
this.links,
this.summaryMarkdown,
this.summaryPlain,
this.contentMarkdown,
this.contentPlain,
});
factory FeedArticle.fromJson(Map<String, dynamic> json) =>
_$FeedArticleFromJson(json);
Map<String, dynamic> toJson() => _$FeedArticleToJson(this);
@override
Map<String, Expression> toColumns(bool nullToAbsent) {
final map = <String, Expression>{};
map['id'] = Variable<String>(id);
{
map['feed_id'] = Variable<String>(Article.$converterfeedId.toSql(feedId));
}
map['fetched'] = Variable<DateTime>(fetched);
if (!nullToAbsent || created != null) {
map['created'] = Variable<DateTime>(created);
}
if (!nullToAbsent || updated != null) {
map['updated'] = Variable<DateTime>(updated);
}
if (!nullToAbsent || lastRead != null) {
map['last_read'] = Variable<DateTime>(lastRead);
}
if (!nullToAbsent || title != null) {
map['title'] = Variable<String>(title);
}
if (!nullToAbsent || authors != null) {
map['authors'] = Variable<String>(
Article.$converterauthorsn.toSql(authors),
);
}
if (!nullToAbsent || tags != null) {
map['tags'] = Variable<String>(Article.$convertertagsn.toSql(tags));
}
if (!nullToAbsent || links != null) {
map['links'] = Variable<String>(Article.$converterlinksn.toSql(links));
}
if (!nullToAbsent || summaryMarkdown != null) {
map['summaryMarkdown'] = Variable<String>(summaryMarkdown);
}
if (!nullToAbsent || summaryPlain != null) {
map['summaryPlain'] = Variable<String>(summaryPlain);
}
if (!nullToAbsent || contentMarkdown != null) {
map['contentMarkdown'] = Variable<String>(contentMarkdown);
}
if (!nullToAbsent || contentPlain != null) {
map['contentPlain'] = Variable<String>(contentPlain);
}
return map;
}
@override
bool get cacheHash => true;
@override
List<Object?> get hashParameters => [
id,
feedId,
fetched,
created,
updated,
lastRead,
title,
authors,
tags,
links,
summaryMarkdown,
summaryPlain,
contentMarkdown,
contentPlain,
];
}
@@ -0,0 +1,60 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'feed_article.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
FeedArticle _$FeedArticleFromJson(Map<String, dynamic> json) => FeedArticle(
id: json['id'] as String,
feedId: Uri.parse(json['feedId'] as String),
fetched: DateTime.parse(json['fetched'] as String),
created:
json['created'] == null
? null
: DateTime.parse(json['created'] as String),
updated:
json['updated'] == null
? null
: DateTime.parse(json['updated'] as String),
lastRead:
json['lastRead'] == null
? null
: DateTime.parse(json['lastRead'] as String),
title: json['title'] as String?,
authors:
(json['authors'] as List<dynamic>?)
?.map((e) => FeedAuthor.fromJson(e as Map<String, dynamic>))
.toList(),
tags:
(json['tags'] as List<dynamic>?)
?.map((e) => FeedCategory.fromJson(e as Map<String, dynamic>))
.toList(),
links:
(json['links'] as List<dynamic>?)
?.map((e) => FeedLink.fromJson(e as Map<String, dynamic>))
.toList(),
summaryMarkdown: json['summaryMarkdown'] as String?,
summaryPlain: json['summaryPlain'] as String?,
contentMarkdown: json['contentMarkdown'] as String?,
contentPlain: json['contentPlain'] as String?,
);
Map<String, dynamic> _$FeedArticleToJson(FeedArticle instance) =>
<String, dynamic>{
'id': instance.id,
'feedId': instance.feedId.toString(),
'fetched': instance.fetched.toIso8601String(),
'created': instance.created?.toIso8601String(),
'updated': instance.updated?.toIso8601String(),
'lastRead': instance.lastRead?.toIso8601String(),
'title': instance.title,
'authors': instance.authors?.map((e) => e.toJson()).toList(),
'tags': instance.tags?.map((e) => e.toJson()).toList(),
'links': instance.links?.map((e) => e.toJson()).toList(),
'summaryMarkdown': instance.summaryMarkdown,
'summaryPlain': instance.summaryPlain,
'contentMarkdown': instance.contentMarkdown,
'contentPlain': instance.contentPlain,
};
@@ -0,0 +1,29 @@
import 'package:lensai/features/web_feed/data/models/feed_article.dart';
class FeedArticleQueryResult extends FeedArticle {
final double weightedRank;
FeedArticleQueryResult({
required super.id,
required super.feedId,
required super.fetched,
required this.weightedRank,
super.created,
super.updated,
super.lastRead,
super.title,
super.authors,
super.tags,
super.links,
super.summaryMarkdown,
super.summaryPlain,
super.contentMarkdown,
super.contentPlain,
});
@override
bool get cacheHash => true;
@override
List<Object?> get hashParameters => [...super.hashParameters, weightedRank];
}
@@ -0,0 +1,23 @@
import 'package:fast_equatable/fast_equatable.dart';
import 'package:json_annotation/json_annotation.dart';
part 'feed_author.g.dart';
@JsonSerializable()
class FeedAuthor with FastEquatable {
final String? name;
final String? email;
FeedAuthor({this.name, this.email});
factory FeedAuthor.fromJson(Map<String, dynamic> json) =>
_$FeedAuthorFromJson(json);
Map<String, dynamic> toJson() => _$FeedAuthorToJson(this);
@override
bool get cacheHash => true;
@override
List<Object?> get hashParameters => [name, email];
}
@@ -0,0 +1,13 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'feed_author.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
FeedAuthor _$FeedAuthorFromJson(Map<String, dynamic> json) =>
FeedAuthor(name: json['name'] as String?, email: json['email'] as String?);
Map<String, dynamic> _$FeedAuthorToJson(FeedAuthor instance) =>
<String, dynamic>{'name': instance.name, 'email': instance.email};
@@ -0,0 +1,23 @@
import 'package:fast_equatable/fast_equatable.dart';
import 'package:json_annotation/json_annotation.dart';
part 'feed_category.g.dart';
@JsonSerializable()
class FeedCategory with FastEquatable {
final String id;
final String? title;
FeedCategory({required this.id, this.title});
factory FeedCategory.fromJson(Map<String, dynamic> json) =>
_$FeedCategoryFromJson(json);
Map<String, dynamic> toJson() => _$FeedCategoryToJson(this);
@override
bool get cacheHash => true;
@override
List<Object?> get hashParameters => [id, title];
}
@@ -0,0 +1,13 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'feed_category.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
FeedCategory _$FeedCategoryFromJson(Map<String, dynamic> json) =>
FeedCategory(id: json['id'] as String, title: json['title'] as String?);
Map<String, dynamic> _$FeedCategoryToJson(FeedCategory instance) =>
<String, dynamic>{'id': instance.id, 'title': instance.title};
@@ -0,0 +1,19 @@
import 'package:copy_with_extension/copy_with_extension.dart';
import 'package:fast_equatable/fast_equatable.dart';
part 'feed_filter.g.dart';
@CopyWith()
class FeedFilter with FastEquatable {
final Uri? feedId;
final String? query;
final Set<String>? tags;
FeedFilter({this.feedId, this.query, this.tags});
@override
bool get cacheHash => true;
@override
List<Object?> get hashParameters => [feedId, query, tags];
}
@@ -0,0 +1,76 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'feed_filter.dart';
// **************************************************************************
// CopyWithGenerator
// **************************************************************************
abstract class _$FeedFilterCWProxy {
FeedFilter feedId(Uri? feedId);
FeedFilter query(String? query);
FeedFilter tags(Set<String>? tags);
/// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `FeedFilter(...).copyWith.fieldName(...)` to override fields one at a time with nullification support.
///
/// Usage
/// ```dart
/// FeedFilter(...).copyWith(id: 12, name: "My name")
/// ````
FeedFilter call({Uri? feedId, String? query, Set<String>? tags});
}
/// Proxy class for `copyWith` functionality. This is a callable class and can be used as follows: `instanceOfFeedFilter.copyWith(...)`. Additionally contains functions for specific fields e.g. `instanceOfFeedFilter.copyWith.fieldName(...)`
class _$FeedFilterCWProxyImpl implements _$FeedFilterCWProxy {
const _$FeedFilterCWProxyImpl(this._value);
final FeedFilter _value;
@override
FeedFilter feedId(Uri? feedId) => this(feedId: feedId);
@override
FeedFilter query(String? query) => this(query: query);
@override
FeedFilter tags(Set<String>? tags) => this(tags: tags);
@override
/// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `FeedFilter(...).copyWith.fieldName(...)` to override fields one at a time with nullification support.
///
/// Usage
/// ```dart
/// FeedFilter(...).copyWith(id: 12, name: "My name")
/// ````
FeedFilter call({
Object? feedId = const $CopyWithPlaceholder(),
Object? query = const $CopyWithPlaceholder(),
Object? tags = const $CopyWithPlaceholder(),
}) {
return FeedFilter(
feedId:
feedId == const $CopyWithPlaceholder()
? _value.feedId
// ignore: cast_nullable_to_non_nullable
: feedId as Uri?,
query:
query == const $CopyWithPlaceholder()
? _value.query
// ignore: cast_nullable_to_non_nullable
: query as String?,
tags:
tags == const $CopyWithPlaceholder()
? _value.tags
// ignore: cast_nullable_to_non_nullable
: tags as Set<String>?,
);
}
}
extension $FeedFilterCopyWith on FeedFilter {
/// Returns a callable class that can be used as follows: `instanceOfFeedFilter.copyWith(...)` or like so:`instanceOfFeedFilter.copyWith.fieldName(...)`.
// ignore: library_private_types_in_public_api
_$FeedFilterCWProxy get copyWith => _$FeedFilterCWProxyImpl(this);
}
@@ -0,0 +1,41 @@
import 'package:fast_equatable/fast_equatable.dart';
import 'package:json_annotation/json_annotation.dart';
part 'feed_link.g.dart';
enum FeedLinkRelation {
///an alternate representation of the entry or feed, for example a permalink to the html version of the entry, or the front page of the weblog.
alternate,
///a related resource which is potentially large in size and might require special handling, for example an audio or video recording.
enclosure,
///an document related to the entry or feed.
related,
///the feed itself.
self,
///the source of the information provided in the entry.
via,
}
@JsonSerializable()
class FeedLink with FastEquatable {
final Uri uri;
final FeedLinkRelation? relation;
final String? title;
FeedLink({required this.uri, this.relation, this.title});
factory FeedLink.fromJson(Map<String, dynamic> json) =>
_$FeedLinkFromJson(json);
Map<String, dynamic> toJson() => _$FeedLinkToJson(this);
@override
bool get cacheHash => true;
@override
List<Object?> get hashParameters => [uri, relation, title];
}
@@ -0,0 +1,27 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'feed_link.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
FeedLink _$FeedLinkFromJson(Map<String, dynamic> json) => FeedLink(
uri: Uri.parse(json['uri'] as String),
relation: $enumDecodeNullable(_$FeedLinkRelationEnumMap, json['relation']),
title: json['title'] as String?,
);
Map<String, dynamic> _$FeedLinkToJson(FeedLink instance) => <String, dynamic>{
'uri': instance.uri.toString(),
'relation': _$FeedLinkRelationEnumMap[instance.relation],
'title': instance.title,
};
const _$FeedLinkRelationEnumMap = {
FeedLinkRelation.alternate: 'alternate',
FeedLinkRelation.enclosure: 'enclosure',
FeedLinkRelation.related: 'related',
FeedLinkRelation.self: 'self',
FeedLinkRelation.via: 'via',
};
@@ -0,0 +1,20 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:lensai/features/web_feed/data/database/converters/feed_data.dart';
import 'package:lensai/features/web_feed/data/database/database.dart';
import 'package:lensai/features/web_feed/data/models/feed_article.dart';
part 'feed_parse_result.g.dart';
@JsonSerializable()
class FeedParseResult {
@FeedDataConverter()
final FeedData feedData;
final List<FeedArticle> articleData;
FeedParseResult({required this.feedData, required this.articleData});
factory FeedParseResult.fromJson(Map<String, dynamic> json) =>
_$FeedParseResultFromJson(json);
Map<String, dynamic> toJson() => _$FeedParseResultToJson(this);
}
@@ -0,0 +1,24 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'feed_parse_result.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
FeedParseResult _$FeedParseResultFromJson(Map<String, dynamic> json) =>
FeedParseResult(
feedData: const FeedDataConverter().fromJson(
json['feedData'] as Map<String, dynamic>,
),
articleData:
(json['articleData'] as List<dynamic>)
.map((e) => FeedArticle.fromJson(e as Map<String, dynamic>))
.toList(),
);
Map<String, dynamic> _$FeedParseResultToJson(FeedParseResult instance) =>
<String, dynamic>{
'feedData': const FeedDataConverter().toJson(instance.feedData),
'articleData': instance.articleData.map((e) => e.toJson()).toList(),
};
@@ -0,0 +1,46 @@
import 'dart:async';
import 'dart:io';
import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:lensai/features/web_feed/data/database/database.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart' as path_provider;
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:sqlite3/sqlite3.dart';
import 'package:sqlite3_flutter_libs/sqlite3_flutter_libs.dart';
part 'providers.g.dart';
@Riverpod(keepAlive: true)
FeedDatabase feedDatabase(Ref ref) {
final db = FeedDatabase(
LazyDatabase(() async {
// put the database file, called db.sqlite here, into the documents folder
// for your app.
final dbFolder = await path_provider.getApplicationDocumentsDirectory();
final file = File(p.join(dbFolder.path, 'feed.db'));
// Also work around limitations on old Android versions
if (Platform.isAndroid) {
await applyWorkaroundToOpenSqlite3OnOldAndroidVersions();
}
// Make sqlite3 pick a more suitable location for temporary files - the
// one from the system may be inaccessible due to sandboxing.
final cachebase = (await path_provider.getTemporaryDirectory()).path;
// We can't access /tmp on Android, which sqlite3 would try by default.
// Explicitly tell it about the correct temporary directory.
sqlite3.tempDirectory = cachebase;
return NativeDatabase.createInBackground(file);
}),
);
ref.onDispose(() {
unawaited(db.close());
});
return db;
}
@@ -0,0 +1,26 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'providers.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
String _$feedDatabaseHash() => r'a4a1ffa36b73c2aecbe17bf096e30fa9b9ad23c9';
/// See also [feedDatabase].
@ProviderFor(feedDatabase)
final feedDatabaseProvider = Provider<FeedDatabase>.internal(
feedDatabase,
name: r'feedDatabaseProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product') ? null : _$feedDatabaseHash,
dependencies: null,
allTransitiveDependencies: null,
);
@Deprecated('Will be removed in 3.0. Use Ref instead')
// ignore: unused_element
typedef FeedDatabaseRef = ProviderRef<FeedDatabase>;
// ignore_for_file: type=lint
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member, deprecated_member_use_from_same_package
@@ -0,0 +1,38 @@
import 'package:lensai/features/web_feed/data/database/database.dart';
import 'package:lensai/features/web_feed/data/models/feed_filter.dart';
import 'package:lensai/features/web_feed/data/models/feed_article.dart';
import 'package:lensai/features/web_feed/domain/repositories/feed_repository.dart';
import 'package:riverpod/riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'providers.g.dart';
@Riverpod()
Stream<List<FeedData>> feedList(Ref ref) {
final repository = ref.watch(feedRepositoryProvider.notifier);
return repository.watchFeeds();
}
@Riverpod()
Stream<List<FeedArticle>> feedArticleList(Ref ref, FeedFilter filter) {
final repository = ref.watch(feedRepositoryProvider.notifier);
return repository.watchFeedArticles(filter);
}
@Riverpod()
Stream<FeedArticle?> feedArticle(Ref ref, String articleId) {
final repository = ref.watch(feedRepositoryProvider.notifier);
return repository.watchArticle(articleId);
}
@Riverpod()
Raw<Stream<Map<String, int>>> _unreadArticleCount(Ref ref) {
final repository = ref.watch(feedRepositoryProvider.notifier);
return repository.watchUnreadFeedArticleCount();
}
@Riverpod()
Stream<int?> unreadFeedArticleCount(Ref ref, Uri feedId) {
final stream = ref.watch(_unreadArticleCountProvider);
return stream.map((counts) => counts[feedId.toString()]);
}
@@ -0,0 +1,433 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'providers.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
String _$feedListHash() => r'0076186437354768c39fb1d7c8bcfcf7b94c7dd1';
/// See also [feedList].
@ProviderFor(feedList)
final feedListProvider = AutoDisposeStreamProvider<List<FeedData>>.internal(
feedList,
name: r'feedListProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product') ? null : _$feedListHash,
dependencies: null,
allTransitiveDependencies: null,
);
@Deprecated('Will be removed in 3.0. Use Ref instead')
// ignore: unused_element
typedef FeedListRef = AutoDisposeStreamProviderRef<List<FeedData>>;
String _$feedArticleListHash() => r'64e834a1b69d913f1c860b38956b69af9e89a833';
/// Copied from Dart SDK
class _SystemHash {
_SystemHash._();
static int combine(int hash, int value) {
// ignore: parameter_assignments
hash = 0x1fffffff & (hash + value);
// ignore: parameter_assignments
hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
return hash ^ (hash >> 6);
}
static int finish(int hash) {
// ignore: parameter_assignments
hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3));
// ignore: parameter_assignments
hash = hash ^ (hash >> 11);
return 0x1fffffff & (hash + ((0x00003fff & hash) << 15));
}
}
/// See also [feedArticleList].
@ProviderFor(feedArticleList)
const feedArticleListProvider = FeedArticleListFamily();
/// See also [feedArticleList].
class FeedArticleListFamily extends Family<AsyncValue<List<FeedArticle>>> {
/// See also [feedArticleList].
const FeedArticleListFamily();
/// See also [feedArticleList].
FeedArticleListProvider call(FeedFilter filter) {
return FeedArticleListProvider(filter);
}
@override
FeedArticleListProvider getProviderOverride(
covariant FeedArticleListProvider provider,
) {
return call(provider.filter);
}
static const Iterable<ProviderOrFamily>? _dependencies = null;
@override
Iterable<ProviderOrFamily>? get dependencies => _dependencies;
static const Iterable<ProviderOrFamily>? _allTransitiveDependencies = null;
@override
Iterable<ProviderOrFamily>? get allTransitiveDependencies =>
_allTransitiveDependencies;
@override
String? get name => r'feedArticleListProvider';
}
/// See also [feedArticleList].
class FeedArticleListProvider
extends AutoDisposeStreamProvider<List<FeedArticle>> {
/// See also [feedArticleList].
FeedArticleListProvider(FeedFilter filter)
: this._internal(
(ref) => feedArticleList(ref as FeedArticleListRef, filter),
from: feedArticleListProvider,
name: r'feedArticleListProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product')
? null
: _$feedArticleListHash,
dependencies: FeedArticleListFamily._dependencies,
allTransitiveDependencies:
FeedArticleListFamily._allTransitiveDependencies,
filter: filter,
);
FeedArticleListProvider._internal(
super._createNotifier, {
required super.name,
required super.dependencies,
required super.allTransitiveDependencies,
required super.debugGetCreateSourceHash,
required super.from,
required this.filter,
}) : super.internal();
final FeedFilter filter;
@override
Override overrideWith(
Stream<List<FeedArticle>> Function(FeedArticleListRef provider) create,
) {
return ProviderOverride(
origin: this,
override: FeedArticleListProvider._internal(
(ref) => create(ref as FeedArticleListRef),
from: from,
name: null,
dependencies: null,
allTransitiveDependencies: null,
debugGetCreateSourceHash: null,
filter: filter,
),
);
}
@override
AutoDisposeStreamProviderElement<List<FeedArticle>> createElement() {
return _FeedArticleListProviderElement(this);
}
@override
bool operator ==(Object other) {
return other is FeedArticleListProvider && other.filter == filter;
}
@override
int get hashCode {
var hash = _SystemHash.combine(0, runtimeType.hashCode);
hash = _SystemHash.combine(hash, filter.hashCode);
return _SystemHash.finish(hash);
}
}
@Deprecated('Will be removed in 3.0. Use Ref instead')
// ignore: unused_element
mixin FeedArticleListRef on AutoDisposeStreamProviderRef<List<FeedArticle>> {
/// The parameter `filter` of this provider.
FeedFilter get filter;
}
class _FeedArticleListProviderElement
extends AutoDisposeStreamProviderElement<List<FeedArticle>>
with FeedArticleListRef {
_FeedArticleListProviderElement(super.provider);
@override
FeedFilter get filter => (origin as FeedArticleListProvider).filter;
}
String _$feedArticleHash() => r'b1670f2ce11636f42fc0595befe42ada19319880';
/// See also [feedArticle].
@ProviderFor(feedArticle)
const feedArticleProvider = FeedArticleFamily();
/// See also [feedArticle].
class FeedArticleFamily extends Family<AsyncValue<FeedArticle?>> {
/// See also [feedArticle].
const FeedArticleFamily();
/// See also [feedArticle].
FeedArticleProvider call(String articleId) {
return FeedArticleProvider(articleId);
}
@override
FeedArticleProvider getProviderOverride(
covariant FeedArticleProvider provider,
) {
return call(provider.articleId);
}
static const Iterable<ProviderOrFamily>? _dependencies = null;
@override
Iterable<ProviderOrFamily>? get dependencies => _dependencies;
static const Iterable<ProviderOrFamily>? _allTransitiveDependencies = null;
@override
Iterable<ProviderOrFamily>? get allTransitiveDependencies =>
_allTransitiveDependencies;
@override
String? get name => r'feedArticleProvider';
}
/// See also [feedArticle].
class FeedArticleProvider extends AutoDisposeStreamProvider<FeedArticle?> {
/// See also [feedArticle].
FeedArticleProvider(String articleId)
: this._internal(
(ref) => feedArticle(ref as FeedArticleRef, articleId),
from: feedArticleProvider,
name: r'feedArticleProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product')
? null
: _$feedArticleHash,
dependencies: FeedArticleFamily._dependencies,
allTransitiveDependencies: FeedArticleFamily._allTransitiveDependencies,
articleId: articleId,
);
FeedArticleProvider._internal(
super._createNotifier, {
required super.name,
required super.dependencies,
required super.allTransitiveDependencies,
required super.debugGetCreateSourceHash,
required super.from,
required this.articleId,
}) : super.internal();
final String articleId;
@override
Override overrideWith(
Stream<FeedArticle?> Function(FeedArticleRef provider) create,
) {
return ProviderOverride(
origin: this,
override: FeedArticleProvider._internal(
(ref) => create(ref as FeedArticleRef),
from: from,
name: null,
dependencies: null,
allTransitiveDependencies: null,
debugGetCreateSourceHash: null,
articleId: articleId,
),
);
}
@override
AutoDisposeStreamProviderElement<FeedArticle?> createElement() {
return _FeedArticleProviderElement(this);
}
@override
bool operator ==(Object other) {
return other is FeedArticleProvider && other.articleId == articleId;
}
@override
int get hashCode {
var hash = _SystemHash.combine(0, runtimeType.hashCode);
hash = _SystemHash.combine(hash, articleId.hashCode);
return _SystemHash.finish(hash);
}
}
@Deprecated('Will be removed in 3.0. Use Ref instead')
// ignore: unused_element
mixin FeedArticleRef on AutoDisposeStreamProviderRef<FeedArticle?> {
/// The parameter `articleId` of this provider.
String get articleId;
}
class _FeedArticleProviderElement
extends AutoDisposeStreamProviderElement<FeedArticle?>
with FeedArticleRef {
_FeedArticleProviderElement(super.provider);
@override
String get articleId => (origin as FeedArticleProvider).articleId;
}
String _$unreadArticleCountHash() =>
r'6fb96215fb3b7739a6cbd594a0358415ced04fca';
/// See also [_unreadArticleCount].
@ProviderFor(_unreadArticleCount)
final _unreadArticleCountProvider =
AutoDisposeProvider<Raw<Stream<Map<String, int>>>>.internal(
_unreadArticleCount,
name: r'_unreadArticleCountProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product')
? null
: _$unreadArticleCountHash,
dependencies: null,
allTransitiveDependencies: null,
);
@Deprecated('Will be removed in 3.0. Use Ref instead')
// ignore: unused_element
typedef _UnreadArticleCountRef =
AutoDisposeProviderRef<Raw<Stream<Map<String, int>>>>;
String _$unreadFeedArticleCountHash() =>
r'f8573674477f0d9813b42c82d75e8196c1c1445b';
/// See also [unreadFeedArticleCount].
@ProviderFor(unreadFeedArticleCount)
const unreadFeedArticleCountProvider = UnreadFeedArticleCountFamily();
/// See also [unreadFeedArticleCount].
class UnreadFeedArticleCountFamily extends Family<AsyncValue<int?>> {
/// See also [unreadFeedArticleCount].
const UnreadFeedArticleCountFamily();
/// See also [unreadFeedArticleCount].
UnreadFeedArticleCountProvider call(Uri feedId) {
return UnreadFeedArticleCountProvider(feedId);
}
@override
UnreadFeedArticleCountProvider getProviderOverride(
covariant UnreadFeedArticleCountProvider provider,
) {
return call(provider.feedId);
}
static const Iterable<ProviderOrFamily>? _dependencies = null;
@override
Iterable<ProviderOrFamily>? get dependencies => _dependencies;
static const Iterable<ProviderOrFamily>? _allTransitiveDependencies = null;
@override
Iterable<ProviderOrFamily>? get allTransitiveDependencies =>
_allTransitiveDependencies;
@override
String? get name => r'unreadFeedArticleCountProvider';
}
/// See also [unreadFeedArticleCount].
class UnreadFeedArticleCountProvider extends AutoDisposeStreamProvider<int?> {
/// See also [unreadFeedArticleCount].
UnreadFeedArticleCountProvider(Uri feedId)
: this._internal(
(ref) =>
unreadFeedArticleCount(ref as UnreadFeedArticleCountRef, feedId),
from: unreadFeedArticleCountProvider,
name: r'unreadFeedArticleCountProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product')
? null
: _$unreadFeedArticleCountHash,
dependencies: UnreadFeedArticleCountFamily._dependencies,
allTransitiveDependencies:
UnreadFeedArticleCountFamily._allTransitiveDependencies,
feedId: feedId,
);
UnreadFeedArticleCountProvider._internal(
super._createNotifier, {
required super.name,
required super.dependencies,
required super.allTransitiveDependencies,
required super.debugGetCreateSourceHash,
required super.from,
required this.feedId,
}) : super.internal();
final Uri feedId;
@override
Override overrideWith(
Stream<int?> Function(UnreadFeedArticleCountRef provider) create,
) {
return ProviderOverride(
origin: this,
override: UnreadFeedArticleCountProvider._internal(
(ref) => create(ref as UnreadFeedArticleCountRef),
from: from,
name: null,
dependencies: null,
allTransitiveDependencies: null,
debugGetCreateSourceHash: null,
feedId: feedId,
),
);
}
@override
AutoDisposeStreamProviderElement<int?> createElement() {
return _UnreadFeedArticleCountProviderElement(this);
}
@override
bool operator ==(Object other) {
return other is UnreadFeedArticleCountProvider && other.feedId == feedId;
}
@override
int get hashCode {
var hash = _SystemHash.combine(0, runtimeType.hashCode);
hash = _SystemHash.combine(hash, feedId.hashCode);
return _SystemHash.finish(hash);
}
}
@Deprecated('Will be removed in 3.0. Use Ref instead')
// ignore: unused_element
mixin UnreadFeedArticleCountRef on AutoDisposeStreamProviderRef<int?> {
/// The parameter `feedId` of this provider.
Uri get feedId;
}
class _UnreadFeedArticleCountProviderElement
extends AutoDisposeStreamProviderElement<int?>
with UnreadFeedArticleCountRef {
_UnreadFeedArticleCountProviderElement(super.provider);
@override
Uri get feedId => (origin as UnreadFeedArticleCountProvider).feedId;
}
// ignore_for_file: type=lint
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member, deprecated_member_use_from_same_package
@@ -0,0 +1,26 @@
import 'package:lensai/extensions/nullable.dart';
import 'package:lensai/features/web_feed/data/models/feed_filter.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'article_filter.g.dart';
@Riverpod(keepAlive: true)
class ArticleFilter extends _$ArticleFilter {
void addTag(String tagId) {
final tags = {...?state.tags, tagId};
state = state.copyWith.tags(tags);
}
void removeTag(String tagId) {
if (state.tags.isNotEmpty) {
final tags = {...state.tags!}..remove(tagId);
state = state.copyWith.tags(tags);
}
}
@override
FeedFilter build() {
return FeedFilter();
}
}
@@ -0,0 +1,27 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'article_filter.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
String _$articleFilterHash() => r'dfc997af8a33cbcb995288ef633ff321e95bae8d';
/// See also [ArticleFilter].
@ProviderFor(ArticleFilter)
final articleFilterProvider =
NotifierProvider<ArticleFilter, FeedFilter>.internal(
ArticleFilter.new,
name: r'articleFilterProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product')
? null
: _$articleFilterHash,
dependencies: null,
allTransitiveDependencies: null,
);
typedef _$ArticleFilter = Notifier<FeedFilter>;
// ignore_for_file: type=lint
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member, deprecated_member_use_from_same_package
@@ -0,0 +1,117 @@
import 'package:lensai/extensions/nullable.dart';
import 'package:lensai/features/web_feed/data/database/database.dart';
import 'package:lensai/features/web_feed/data/models/feed_filter.dart';
import 'package:lensai/features/web_feed/data/models/feed_article.dart';
import 'package:lensai/features/web_feed/data/providers.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'feed_repository.g.dart';
@Riverpod(keepAlive: true)
class FeedRepository extends _$FeedRepository {
Future<List<FeedData>> getAllFeeds() {
return ref.read(feedDatabaseProvider).feedDao.getFeeds().get();
}
Future<void> touchFeedFetched(Uri url) {
return ref
.read(feedDatabaseProvider)
.feedDao
.updateFeedFetched(url, DateTime.now());
}
Future<void> upsertFeed(FeedData feedData) {
return ref.read(feedDatabaseProvider).feedDao.upsertFeed(feedData);
}
Future<void> upsertArticles(List<FeedArticle> articles) {
return ref.read(feedDatabaseProvider).articleDao.upsertArticles(articles);
}
Future<int> deleteFeed(Uri url) {
return ref.read(feedDatabaseProvider).feedDao.deleteFeed(url);
}
Future<void> touchArticleRead(String articleId) {
return ref
.read(feedDatabaseProvider)
.articleDao
.updateArticleRead(articleId, DateTime.now());
}
Future<void> unsetArticleRead(String articleId) {
return ref
.read(feedDatabaseProvider)
.articleDao
.updateArticleRead(articleId, null);
}
Stream<List<FeedData>> watchFeeds() {
return ref.read(feedDatabaseProvider).feedDao.getFeeds().watch();
}
Stream<List<FeedArticle>> watchFeedArticles(
FeedFilter filter, {
int snippetLength = 120,
String matchPrefix = '***',
String matchSuffix = '***',
String ellipsis = '',
}) {
final stream =
filter.query.isNotEmpty
? ref
.read(feedDatabaseProvider)
.articleDao
.queryArticles(
matchPrefix: matchPrefix,
matchSuffix: matchSuffix,
ellipsis: ellipsis,
snippetLength: snippetLength,
searchString: filter.query!,
feedId: filter.feedId,
)
.watch()
: ref
.read(feedDatabaseProvider)
.articleDao
.getFeedArticles(filter.feedId)
.watch();
if (filter.tags.isNotEmpty) {
return stream.map(
(articles) =>
articles
.where(
(article) =>
article.tags?.toSet().containsAll(filter.tags!) ?? false,
)
.toList(),
);
} else {
return stream;
}
}
Stream<FeedArticle?> watchArticle(String articleId) {
return ref
.read(feedDatabaseProvider)
.articleDao
.getArticleById(articleId)
.watchSingleOrNull();
}
Stream<Map<String, int>> watchUnreadFeedArticleCount() {
return ref
.read(feedDatabaseProvider)
.articleDao
.getUnreadArticleCount()
.watch()
.map(
(results) =>
Map.fromEntries(results.map((e) => MapEntry(e.$1, e.$2))),
);
}
@override
void build() {}
}
@@ -0,0 +1,26 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'feed_repository.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
String _$feedRepositoryHash() => r'6a050933a6a4eafbe76a66262e1b64bd838a1078';
/// See also [FeedRepository].
@ProviderFor(FeedRepository)
final feedRepositoryProvider = NotifierProvider<FeedRepository, void>.internal(
FeedRepository.new,
name: r'feedRepositoryProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product')
? null
: _$feedRepositoryHash,
dependencies: null,
allTransitiveDependencies: null,
);
typedef _$FeedRepository = Notifier<void>;
// ignore_for_file: type=lint
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member, deprecated_member_use_from_same_package
@@ -0,0 +1,45 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:http/http.dart' as http;
import 'package:lensai/features/web_feed/data/models/feed_parse_result.dart';
import 'package:lensai/features/web_feed/utils/feed_parser.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'feed_reader.g.dart';
@Riverpod(keepAlive: true)
class FeedReader extends _$FeedReader {
Future<FeedParseResult> parseFeed(Uri url) async {
final rootIsolateToken = ServicesBinding.rootIsolateToken!;
final result = await compute((args) async {
// Initialize BackgroundIsolateBinaryMessenger with the token
BackgroundIsolateBinaryMessenger.ensureInitialized(
args['token']! as RootIsolateToken,
);
final client = http.Client();
try {
final url = Uri.parse(args['url']! as String);
final response = await client
.get(url)
.timeout(const Duration(seconds: 30));
final parser = FeedParser.parse(url: url, xmlString: response.body);
final result = FeedParseResult(
feedData: parser.readGeneralData(),
articleData: await parser.readArticles(),
);
return result.toJson();
} finally {
client.close();
}
}, {'token': rootIsolateToken, 'url': url.toString()});
return FeedParseResult.fromJson(result);
}
@override
void build() {}
}
@@ -0,0 +1,24 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'feed_reader.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
String _$feedReaderHash() => r'67b09485e40c522108709ca32b9c624b4db2446b';
/// See also [FeedReader].
@ProviderFor(FeedReader)
final feedReaderProvider = NotifierProvider<FeedReader, void>.internal(
FeedReader.new,
name: r'feedReaderProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product') ? null : _$feedReaderHash,
dependencies: null,
allTransitiveDependencies: null,
);
typedef _$FeedReader = Notifier<void>;
// ignore_for_file: type=lint
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member, deprecated_member_use_from_same_package
@@ -0,0 +1,57 @@
import 'package:collection/collection.dart';
import 'package:lensai/extensions/nullable.dart';
import 'package:lensai/features/web_feed/data/models/feed_author.dart';
import 'package:lensai/features/web_feed/data/models/feed_category.dart';
import 'package:lensai/features/web_feed/data/models/feed_link.dart';
import 'package:rss_dart/dart_rss.dart';
extension ParseAtomLink on List<AtomLink> {
List<FeedLink> toFeedLinks() {
return map((link) {
final uri = link.href.mapNotNull(Uri.tryParse);
if (uri == null) {
return null;
}
final relation = link.rel.mapNotNull(
(rel) => FeedLinkRelation.values.firstWhereOrNull((e) => e.name == rel),
);
return FeedLink(
uri: uri,
relation: relation,
title: link.title.whenNotEmpty,
);
}).nonNulls.toList();
}
}
extension ParseAtomCategory on List<AtomCategory> {
List<FeedCategory> toFeedCategories() {
return where((category) => category.term.isNotEmpty)
.map(
(category) => FeedCategory(
id: category.term!,
title: category.label.whenNotEmpty,
),
)
.toList();
}
}
extension ParseAtomPerson on List<AtomPerson> {
List<FeedAuthor> toFeedAuthors() {
return where(
(author) =>
author.name.whenNotEmpty != null ||
author.email.whenNotEmpty != null,
)
.map(
(author) => FeedAuthor(
name: author.name.whenNotEmpty,
email: author.email.whenNotEmpty,
),
)
.toList();
}
}
@@ -0,0 +1,17 @@
import 'package:collection/collection.dart';
import 'package:lensai/extensions/nullable.dart';
import 'package:lensai/features/web_feed/data/models/feed_article.dart';
import 'package:lensai/features/web_feed/data/models/feed_link.dart';
extension FeedArticleX on FeedArticle {
String get displayTitle =>
title ??
links
?.firstWhereOrNull(
(link) => link.relation == FeedLinkRelation.alternate,
)
.mapNotNull(
(link) => link.title.whenNotEmpty ?? link.uri.toString(),
) ??
'Unnamed Article';
}
@@ -0,0 +1,24 @@
import 'package:lensai/extensions/nullable.dart';
import 'package:lensai/features/web_feed/data/models/feed_category.dart';
import 'package:rss_dart/dart_rss.dart';
import 'package:rss_dart/domain/dublin_core/dublin_core.dart';
extension ParseDublinCategory on DublinCore {
List<FeedCategory> toFeedCategories() {
return subjects
.where((subject) => subject.isNotEmpty)
.map((subject) => FeedCategory(id: subject))
.toList();
}
}
extension ParseRssCategory on List<RssCategory> {
List<FeedCategory> toFeedCategories() {
return where((category) => category.value.isNotEmpty)
.map(
(category) =>
FeedCategory(id: '${category.domain} ${category.value}'.trim()),
)
.toList();
}
}
@@ -0,0 +1,50 @@
import 'package:rss_dart/dart_rss.dart';
extension WebFeedJson on WebFeed {
Map<String, dynamic> toJson() {
return {
'title': title,
'description': description,
'links': links.where((link) => link != null).toList(),
'items': items.map((item) => item.toJson()).toList(),
};
}
static WebFeed fromJson(Map<String, dynamic> json) {
return WebFeed(
title: json['title'] as String,
description: json['description'] as String,
links: (json['links'] as List).cast<String?>(),
items:
(json['items'] as List)
.map(
(item) =>
WebFeedItemJson.fromJson(item as Map<String, dynamic>),
)
.toList(),
);
}
}
extension WebFeedItemJson on WebFeedItem {
Map<String, dynamic> toJson() {
return {
'title': title,
'body': body,
'links': links.where((link) => link != null).toList(),
'updated': updated?.toIso8601String(),
};
}
static WebFeedItem fromJson(Map<String, dynamic> json) {
return WebFeedItem(
title: json['title'] as String,
body: json['body'] as String,
links: (json['links'] as List).cast<String?>(),
updated:
json['updated'] != null
? DateTime.parse(json['updated'] as String)
: null,
);
}
}
@@ -0,0 +1,50 @@
import 'package:lensai/features/web_feed/domain/repositories/feed_repository.dart';
import 'package:lensai/features/web_feed/domain/services/feed_reader.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'fetch_articles.g.dart';
@Riverpod()
class FetchArticlesController extends _$FetchArticlesController {
Future<void> fetchAllArticles() async {
state = const AsyncLoading();
state = await AsyncValue.guard(() async {
final feeds =
await ref.read(feedRepositoryProvider.notifier).getAllFeeds();
await Future.wait(
feeds.map((feed) async {
final result = await ref
.read(feedReaderProvider.notifier)
.parseFeed(feed.url);
await ref
.read(feedRepositoryProvider.notifier)
.upsertArticles(result.articleData);
await ref
.read(feedRepositoryProvider.notifier)
.touchFeedFetched(feed.url);
}).toList(),
);
});
}
Future<void> fetchFeedArticles(Uri uri) async {
state = const AsyncLoading();
state = await AsyncValue.guard(() async {
final result = await ref.read(feedReaderProvider.notifier).parseFeed(uri);
await ref
.read(feedRepositoryProvider.notifier)
.upsertArticles(result.articleData);
await ref.read(feedRepositoryProvider.notifier).touchFeedFetched(uri);
});
}
@override
AsyncValue<void> build() {
return const AsyncData(null);
}
}
@@ -0,0 +1,30 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'fetch_articles.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
String _$fetchArticlesControllerHash() =>
r'63da7f6ba9eab28aebff4c60d98e2f2a404c5138';
/// See also [FetchArticlesController].
@ProviderFor(FetchArticlesController)
final fetchArticlesControllerProvider = AutoDisposeNotifierProvider<
FetchArticlesController,
AsyncValue<void>
>.internal(
FetchArticlesController.new,
name: r'fetchArticlesControllerProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product')
? null
: _$fetchArticlesControllerHash,
dependencies: null,
allTransitiveDependencies: null,
);
typedef _$FetchArticlesController = AutoDisposeNotifier<AsyncValue<void>>;
// ignore_for_file: type=lint
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member, deprecated_member_use_from_same_package
@@ -0,0 +1,261 @@
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:go_router/go_router.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:lensai/core/providers/format.dart';
import 'package:lensai/core/routing/routes.dart';
import 'package:lensai/extensions/nullable.dart';
import 'package:lensai/features/geckoview/domain/repositories/tab.dart';
import 'package:lensai/features/web_feed/data/models/feed_link.dart';
import 'package:lensai/features/web_feed/domain/providers.dart';
import 'package:lensai/features/web_feed/extensions/feed_article.dart';
import 'package:lensai/features/web_feed/presentation/widgets/authors_horizontal_list.dart';
import 'package:lensai/features/web_feed/presentation/widgets/tags_horizontal_list.dart';
import 'package:lensai/presentation/widgets/failure_widget.dart';
import 'package:lensai/utils/markdown/image_extractor.dart';
import 'package:lensai/utils/ui_helper.dart';
enum _Pages { summary, content }
class FeedArticleScreen extends HookConsumerWidget {
final String articleId;
const FeedArticleScreen({super.key, required this.articleId});
@override
Widget build(BuildContext context, WidgetRef ref) {
final articleAsync = ref.watch(feedArticleProvider(articleId));
return Scaffold(
body: articleAsync.when(
data: (article) {
if (article == null) {
return SizedBox.shrink();
}
return HookBuilder(
builder: (context) {
final hasArticleCreated = article.created != null;
final hasArticleUpdated =
article.updated != null && article.updated != article.created;
final hasAuthors = article.authors.isNotEmpty;
final hasTags = article.tags.isNotEmpty;
final tabs = useMemoized(
() => [
if (article.summaryMarkdown.isNotEmpty) _Pages.summary,
if (article.contentMarkdown.isNotEmpty) _Pages.content,
],
);
final tabController = useTabController(
initialLength: tabs.length,
);
final articleLink = useMemoized(
() => article.links?.firstWhereOrNull(
(link) => link.relation == FeedLinkRelation.alternate,
),
);
final articleImages = useMemoized(
() => (article.contentMarkdown ?? article.summaryMarkdown)
.mapNotNull(extractImagesFromMarkdown),
);
final bottomHeight = useMemoized(() {
var height = 0.0;
if (hasArticleCreated) {
height += 20;
}
if (hasArticleUpdated) {
height += 20;
}
if (hasAuthors) {
height += 56;
}
if (hasTags) {
height += 56;
}
return height;
});
return NestedScrollView(
headerSliverBuilder:
(context, innerBoxIsScrolled) => [
SliverAppBar.large(
pinned: false,
flexibleSpace: FlexibleSpaceBar(
centerTitle: false,
titlePadding: EdgeInsetsDirectional.only(
start: 72,
end: 72,
bottom: bottomHeight + 16,
),
title: Text(
article.displayTitle,
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
background: articleImages?.firstOrNull.mapNotNull(
(img) => Image.network(
img.toString(),
fit: BoxFit.cover,
color: Theme.of(
context,
).colorScheme.surface.withAlpha(200),
colorBlendMode: BlendMode.darken,
),
),
),
bottom: PreferredSize(
preferredSize: Size(double.infinity, bottomHeight),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16.0,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Divider(),
Text(
'Published: ${hasArticleCreated ? ref.read(formatProvider.notifier).fullDateTimeWithTimezone(article.created!) : 'N/A'}',
style: Theme.of(context).textTheme.bodySmall
?.copyWith(fontStyle: FontStyle.italic),
),
if (hasArticleUpdated)
Text(
'Updated: ${ref.read(formatProvider.notifier).fullDateTimeWithTimezone(article.updated!)}',
style: Theme.of(context).textTheme.bodySmall
?.copyWith(fontStyle: FontStyle.italic),
),
if (hasAuthors)
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Row(
children: [
const Text('Authors:'),
const SizedBox(width: 8),
Expanded(
child: AuthorsHorizontalList(
authors: article.authors!,
),
),
],
),
),
if (hasTags)
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Row(
children: [
const Text('Tags:'),
const SizedBox(width: 8),
Expanded(
child: TagsHorizontalList(
tags: article.tags!,
),
),
],
),
),
],
),
),
),
actions: [
if (articleLink != null)
IconButton(
onPressed: () async {
await ref
.read(tabRepositoryProvider.notifier)
.addTab(url: articleLink.uri);
if (context.mounted) {
context.go(BrowserRoute().location);
}
},
icon: const Icon(Icons.open_in_browser),
),
],
),
// SliverToBoxAdapter(
// child: Padding(
// padding: const EdgeInsets.all(8.0),
// child: TabBar(
// controller: tabController,
// tabs: [
// ...tabs.map(
// (tab) => switch (tab) {
// _Pages.summary => const Tab(text: 'Summary'),
// _Pages.content => const Tab(text: 'Article'),
// },
// ),
// ],
// ),
// ),
// ),
],
body: TabBarView(
controller: tabController,
children: [
...tabs.map(
(tab) => Markdown(
selectable: true,
onTapLink: (text, href, title) async {
if (href.mapNotNull(Uri.tryParse)
case final Uri url) {
await ref
.read(tabRepositoryProvider.notifier)
.addTab(url: url);
if (context.mounted) {
showTabOpenedMessage(
context,
tabName: title.whenNotEmpty,
onShow: () {
context.go(BrowserRoute().location);
},
);
}
}
},
styleSheet: MarkdownStyleSheet.fromTheme(
Theme.of(context),
).copyWith(
blockquoteDecoration: BoxDecoration(
color:
Theme.of(
context,
).colorScheme.secondaryContainer,
borderRadius: BorderRadius.circular(2.0),
),
),
data: switch (tab) {
_Pages.summary => article.summaryMarkdown!,
_Pages.content => article.contentMarkdown!,
},
),
),
],
),
);
},
);
},
error:
(error, stackTrace) => Center(
child: FailureWidget(
title: 'Failed reading article',
exception: error,
),
),
loading: () => const Center(child: CircularProgressIndicator()),
),
);
}
}
@@ -0,0 +1,103 @@
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:lensai/domain/entities/equatable_iterable.dart';
import 'package:lensai/extensions/nullable.dart';
import 'package:lensai/features/web_feed/data/models/feed_filter.dart';
import 'package:lensai/features/web_feed/domain/providers.dart';
import 'package:lensai/features/web_feed/domain/providers/article_filter.dart';
import 'package:lensai/features/web_feed/presentation/controllers/fetch_articles.dart';
import 'package:lensai/features/web_feed/presentation/widgets/feed_article_card.dart';
import 'package:lensai/presentation/widgets/failure_widget.dart';
class FeedArticleListScreen extends HookConsumerWidget {
final Uri? feedId;
const FeedArticleListScreen({required this.feedId});
@override
Widget build(BuildContext context, WidgetRef ref) {
final articlesAsync = ref.watch(
// ignore: provider_parameters
feedArticleListProvider(FeedFilter(feedId: feedId)),
);
return Scaffold(
body: NestedScrollView(
floatHeaderSlivers: true,
headerSliverBuilder: (context, innerBoxIsScrolled) {
return [
Consumer(
builder: (context, ref, child) {
final tags = ref.watch(articleFilterProvider);
return SliverAppBar(floating: true, title: Text('Articles'));
},
),
];
},
body: articlesAsync.when(
data: (articles) {
return RefreshIndicator(
onRefresh: () async {
if (feedId != null) {
await ref
.read(fetchArticlesControllerProvider.notifier)
.fetchFeedArticles(feedId!);
} else {
await ref
.read(fetchArticlesControllerProvider.notifier)
.fetchAllArticles();
}
},
child: ListView.builder(
itemCount: articles.length,
itemBuilder: (context, i) {
final article = articles[i];
return Consumer(
key: ValueKey(article.id),
builder: (context, ref, child) {
final tags = ref.watch(
articleFilterProvider.select(
(value) => EquatableCollection(
value.tags ?? const {},
immutable: false,
),
),
);
return FeedArticleCard(
selectedTags: tags.collection,
onTagSelected: (tagId, value) {
if (value) {
ref
.read(articleFilterProvider.notifier)
.addTag(tagId);
} else {
ref
.read(articleFilterProvider.notifier)
.removeTag(tagId);
}
},
article: article,
);
},
);
},
),
);
},
error:
(error, stackTrace) => Center(
child: FailureWidget(
title: 'Failed to load Articles',
exception: error,
),
),
loading: () => const SizedBox.shrink(),
),
),
);
}
}
@@ -0,0 +1,204 @@
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:go_router/go_router.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:lensai/extensions/nullable.dart';
import 'package:lensai/extensions/uri.dart';
import 'package:lensai/features/web_feed/data/database/database.dart';
import 'package:lensai/features/web_feed/data/models/feed_category.dart';
import 'package:lensai/features/web_feed/domain/repositories/feed_repository.dart';
import 'package:lensai/features/web_feed/presentation/widgets/tag_field.dart';
import 'package:lensai/presentation/widgets/url_icon.dart';
enum _DialogMode { create, edit }
class FeedEditScreen extends HookConsumerWidget {
final _DialogMode _mode;
final FeedData initialFeed;
const FeedEditScreen._({required _DialogMode mode, required this.initialFeed})
: _mode = mode;
factory FeedEditScreen.create({required FeedData initialFeed}) {
return FeedEditScreen._(mode: _DialogMode.create, initialFeed: initialFeed);
}
factory FeedEditScreen.edit({required FeedData initialFeed}) {
return FeedEditScreen._(mode: _DialogMode.edit, initialFeed: initialFeed);
}
@override
Widget build(BuildContext context, WidgetRef ref) {
final formKey = useMemoized(() => GlobalKey<FormState>());
final initialTags = useMemoized(
() => initialFeed.tags?.map((tag) => tag.id).toSet(),
);
final tags = useRef(initialTags ?? {});
final titleTextController = useTextEditingController(
text: initialFeed.title ?? initialFeed.url.host,
);
final descriptionTextController = useTextEditingController(
text: initialFeed.description,
);
final urlTextController = useTextEditingController(
text: initialFeed.url.toString(),
);
return Scaffold(
appBar: AppBar(
title: Text(switch (_mode) {
_DialogMode.create => 'New Feed',
_DialogMode.edit => 'Edit Feed',
}),
actions: [
IconButton(
onPressed: () async {
if (formKey.currentState?.validate() ?? false) {
final feedData = FeedData(
url: Uri.parse(urlTextController.text),
authors: initialFeed.authors,
description: descriptionTextController.text.whenNotEmpty,
tags: tags.value.map((tag) => FeedCategory(id: tag)).toList(),
title: titleTextController.text.whenNotEmpty,
);
await ref
.read(feedRepositoryProvider.notifier)
.upsertFeed(feedData);
if (context.mounted) {
context.pop();
}
}
},
icon: const Icon(Icons.check),
),
],
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Column(
children: [
Expanded(
child: Form(
key: formKey,
child: ListView(
children: [
TextFormField(
decoration: InputDecoration(
prefixIcon: Padding(
padding: const EdgeInsets.all(10.0),
child: UrlIcon(
initialFeed.url.base,
iconSize: 24.0,
),
),
label: const Text('Title'),
),
controller: titleTextController,
),
TextFormField(
decoration: const InputDecoration(
label: Text('Description'),
),
minLines: 1,
maxLines: 3,
controller: descriptionTextController,
),
const SizedBox(height: 16),
TagField(
initialTags: tags.value,
onTagsUpdate: (newTags) {
tags.value = newTags;
},
),
const SizedBox(height: 16),
TextFormField(
decoration: const InputDecoration(
label: Text('Address'),
),
keyboardType: TextInputType.url,
controller: urlTextController,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) {
if (value.isEmpty) {
return 'Address must be provided';
}
if (Uri.tryParse(value!) case final Uri url) {
if (url.isScheme('https') ||
url.isScheme('http') &&
url.authority.isNotEmpty) {
return null;
}
}
return 'Inavlid URL';
},
),
],
),
),
),
if (_mode == _DialogMode.edit)
SizedBox(
width: double.infinity,
child: OutlinedButton.icon(
style: OutlinedButton.styleFrom(
side: BorderSide(
color: Theme.of(context).colorScheme.error,
),
foregroundColor: Theme.of(context).colorScheme.error,
iconColor: Theme.of(context).colorScheme.error,
),
label: const Text('Delete'),
icon: const Icon(Icons.delete),
onPressed: () async {
final result = await showDialog<bool?>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Delete Feed'),
content: const Text(
'Are you sure you want to delete this feed and delete all related articles?',
),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.pop(context, false);
},
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
Navigator.pop(context, true);
},
child: const Text('Delete'),
),
],
);
},
);
if (result == true) {
await ref
.read(feedRepositoryProvider.notifier)
.deleteFeed(initialFeed.url);
if (context.mounted) {
context.pop();
}
}
},
),
),
],
),
),
),
);
}
}
@@ -0,0 +1,44 @@
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:lensai/features/web_feed/domain/providers.dart';
import 'package:lensai/features/web_feed/presentation/controllers/fetch_articles.dart';
import 'package:lensai/features/web_feed/presentation/widgets/feed_card.dart';
import 'package:lensai/presentation/widgets/failure_widget.dart';
class FeedListScreen extends HookConsumerWidget {
const FeedListScreen();
@override
Widget build(BuildContext context, WidgetRef ref) {
final feeds = ref.watch(feedListProvider);
return Scaffold(
appBar: AppBar(title: const Text('Feeds')),
body: feeds.when(
data: (feeds) {
return RefreshIndicator(
onRefresh: () async {
await ref
.read(fetchArticlesControllerProvider.notifier)
.fetchAllArticles();
},
child: ListView.builder(
itemCount: feeds.length,
itemBuilder: (context, i) {
return FeedCard(feed: feeds[i]);
},
),
);
},
error:
(error, stackTrace) => Center(
child: FailureWidget(
title: 'Failed to load Feeds',
exception: error,
),
),
loading: () => const SizedBox.shrink(),
),
);
}
}
@@ -0,0 +1,41 @@
import 'package:fading_scroll/fading_scroll.dart';
import 'package:flutter/material.dart';
import 'package:lensai/extensions/nullable.dart';
import 'package:lensai/features/web_feed/data/models/feed_author.dart';
class AuthorsHorizontalList extends StatelessWidget {
late final List<Widget> _authors;
AuthorsHorizontalList({required List<FeedAuthor> authors}) {
_authors =
authors
.map(
(author) => Chip(
label: Text(
'${author.name ?? ''} ${author.email.mapNotNull((email) => '($email)') ?? ''}'
.trim(),
),
),
)
.toList();
}
@override
Widget build(BuildContext context) {
return SizedBox(
height: 48,
child: FadingScroll(
fadingSize: 15,
builder: (context, controller) {
return ListView.builder(
itemCount: _authors.length,
controller: controller,
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) => _authors[index],
);
},
),
);
}
}
@@ -0,0 +1,122 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:lensai/core/routing/routes.dart';
import 'package:lensai/extensions/nullable.dart';
import 'package:lensai/extensions/uri.dart';
import 'package:lensai/features/web_feed/data/models/feed_article.dart';
import 'package:lensai/features/web_feed/domain/repositories/feed_repository.dart';
import 'package:lensai/features/web_feed/extensions/feed_article.dart';
import 'package:lensai/features/web_feed/presentation/widgets/authors_horizontal_list.dart';
import 'package:lensai/features/web_feed/presentation/widgets/tags_horizontal_list.dart';
import 'package:lensai/presentation/widgets/url_icon.dart';
import 'package:timeago/timeago.dart' as timeago;
class FeedArticleCard extends HookConsumerWidget {
final FeedArticle article;
final Set<String> selectedTags;
final void Function(String tagId, bool value)? onTagSelected;
const FeedArticleCard({
super.key,
required this.article,
this.onTagSelected,
this.selectedTags = const {},
});
@override
Widget build(BuildContext context, WidgetRef ref) {
final theme = Theme.of(context);
return Card(
clipBehavior: Clip.antiAlias,
child: InkWell(
onTap: () async {
await ref
.read(feedRepositoryProvider.notifier)
.touchArticleRead(article.id);
if (context.mounted) {
await context.push(
FeedArticleRoute(articleId: article.id).location,
extra: article,
);
}
},
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
UrlIcon(article.feedId.base, iconSize: 34.0),
const SizedBox(width: 12.0),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
article.displayTitle,
style: theme.textTheme.titleMedium,
),
if (article.summaryPlain != null)
Text(
article.summaryPlain!,
style: theme.textTheme.bodySmall,
maxLines: 3,
overflow: TextOverflow.ellipsis,
),
],
),
),
if (article.lastRead != null)
IconButton(
onPressed: () async {
await ref
.read(feedRepositoryProvider.notifier)
.unsetArticleRead(article.id);
},
icon: const Icon(Icons.visibility),
),
],
),
if (article.authors.isNotEmpty || article.tags.isNotEmpty) ...[
const SizedBox(height: 8),
if (article.authors.isNotEmpty)
AuthorsHorizontalList(authors: article.authors!),
if (article.tags.isNotEmpty)
TagsHorizontalList(
tags: article.tags!,
selectedTags: selectedTags,
onTagSelected: onTagSelected,
),
const Divider(),
],
Row(
children: [
Text(
'Published: ${(article.created != null) ? timeago.format(article.created!) : 'N/A'}',
style: Theme.of(context).textTheme.bodySmall?.copyWith(
fontStyle: FontStyle.italic,
),
),
if (article.updated != null &&
article.updated != article.created)
Text(
'Updated: ${timeago.format(article.updated!)}',
style: Theme.of(context).textTheme.bodySmall?.copyWith(
fontStyle: FontStyle.italic,
),
),
],
),
],
),
),
),
);
}
}
@@ -0,0 +1,132 @@
import 'package:flutter/material.dart';
import 'package:flutter_material_design_icons/flutter_material_design_icons.dart';
import 'package:go_router/go_router.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:lensai/core/routing/routes.dart';
import 'package:lensai/extensions/nullable.dart';
import 'package:lensai/extensions/uri.dart';
import 'package:lensai/features/web_feed/data/database/database.dart';
import 'package:lensai/features/web_feed/domain/providers.dart';
import 'package:lensai/features/web_feed/presentation/widgets/authors_horizontal_list.dart';
import 'package:lensai/features/web_feed/presentation/widgets/tags_horizontal_list.dart';
import 'package:lensai/presentation/widgets/rounded_text.dart';
import 'package:lensai/presentation/widgets/url_icon.dart';
import 'package:timeago/timeago.dart' as timeago;
class FeedCard extends HookConsumerWidget {
final FeedData feed;
const FeedCard({super.key, required this.feed});
@override
Widget build(BuildContext context, WidgetRef ref) {
final theme = Theme.of(context);
return Card(
clipBehavior: Clip.antiAlias,
child: InkWell(
onTap: () async {
await context.push(FeedArticleListRoute(feedId: feed.url).location);
},
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
UrlIcon(feed.url.base, iconSize: 34.0),
const SizedBox(width: 12.0),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
feed.title ?? feed.url.host,
style: theme.textTheme.titleMedium,
),
if (feed.description != null)
Text(
feed.description!,
style: theme.textTheme.bodySmall,
maxLines: 3,
overflow: TextOverflow.ellipsis,
),
],
),
),
],
),
if (feed.authors.isNotEmpty || feed.tags.isNotEmpty) ...[
if (feed.authors.isNotEmpty)
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: AuthorsHorizontalList(authors: feed.authors!),
),
if (feed.tags.isNotEmpty)
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: TagsHorizontalList(tags: feed.tags!),
),
const Divider(),
],
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Last fetched: ${(feed.lastFetched != null) ? timeago.format(feed.lastFetched!) : 'N/A'}',
style: Theme.of(context).textTheme.bodySmall?.copyWith(
fontStyle: FontStyle.italic,
),
),
Consumer(
builder: (context, ref, child) {
final countAsync = ref.watch(
unreadFeedArticleCountProvider(feed.url),
);
return countAsync.when(
data: (count) {
if (count == null) {
return const SizedBox();
}
return RoundedBackground(
child: Row(
children: [
Icon(
MdiIcons.newspaperVariantMultipleOutline,
size: 18,
color:
Theme.of(context).colorScheme.onPrimary,
),
const SizedBox(width: 4),
Text(
count.toString(),
textAlign: TextAlign.center,
style: TextStyle(
color:
Theme.of(context).colorScheme.onPrimary,
),
),
],
),
);
},
error: (error, stackTrace) => const Text('N/A'),
loading: () => const SizedBox.shrink(),
);
},
),
],
),
],
),
),
),
);
}
}
@@ -0,0 +1,89 @@
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:lensai/presentation/hooks/listenable_callback.dart';
final _tagSplitPatter = RegExp(r'[,\s]+');
class TagField extends HookWidget {
final Set<String> initialTags;
final void Function(Set<String> tags) onTagsUpdate;
const TagField({
super.key,
required this.initialTags,
required this.onTagsUpdate,
});
@override
Widget build(BuildContext context) {
final textController = useTextEditingController();
final tags = useState(initialTags);
useListenableCallback(tags, () {
onTagsUpdate(tags.value);
});
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Tags', style: Theme.of(context).textTheme.labelMedium),
const SizedBox(height: 4),
Wrap(
spacing: 8.0,
children:
tags.value
.map(
(tag) => InputChip(
label: Text(tag),
onDeleted: () {
tags.value = {...tags.value}..remove(tag);
},
),
)
.toList(),
),
TextField(
controller: textController,
decoration: const InputDecoration(
label: Text('Add'),
floatingLabelBehavior: FloatingLabelBehavior.always,
hintText: 'tag1, tag2, ...',
),
onChanged: (String value) {
if (value.isNotEmpty) {
final values = value.split(_tagSplitPatter);
if (values.length > 1) {
final trimmed = values
.map((str) => str.trim())
.where((str) => str.isNotEmpty);
if (trimmed.isNotEmpty) {
textController.clear();
tags.value = {...tags.value, ...trimmed};
}
}
}
},
onSubmitted: (value) {
if (value.isNotEmpty) {
final values =
value
.split(_tagSplitPatter)
.map((str) => str.trim())
.where((str) => str.isNotEmpty)
.toList();
if (values.isNotEmpty) {
textController.clear();
tags.value = {...tags.value, ...values};
}
}
},
),
],
);
}
}
@@ -0,0 +1,54 @@
import 'package:fading_scroll/fading_scroll.dart';
import 'package:flutter/material.dart';
import 'package:lensai/extensions/nullable.dart';
import 'package:lensai/features/web_feed/data/models/feed_category.dart';
class TagsHorizontalList extends StatelessWidget {
late final List<Widget> _tags;
TagsHorizontalList({
required List<FeedCategory> tags,
Set<String> selectedTags = const {},
void Function(String tagId, bool value)? onTagSelected,
}) {
_tags =
tags
.map(
(tag) => Padding(
padding: const EdgeInsets.only(right: 8.0),
child: FilterChip(
label: Text(
'${tag.id} ${tag.title.mapNotNull((title) => '($title)') ?? ''}'
.trim(),
),
selected: selectedTags.contains(tag.id),
onSelected: onTagSelected.mapNotNull(
(onTagSelected) => (value) {
onTagSelected(tag.id, value);
},
),
),
),
)
.toList();
}
@override
Widget build(BuildContext context) {
return SizedBox(
height: 48,
child: FadingScroll(
fadingSize: 15,
builder: (context, controller) {
return ListView.builder(
itemCount: _tags.length,
controller: controller,
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) => _tags[index],
);
},
),
);
}
}
@@ -0,0 +1,95 @@
import 'package:html/dom.dart';
class FeedFinder {
final Uri url;
final Document document;
late final String _base;
FeedFinder({required this.url, required this.document}) {
final uri = url.removeFragment();
_base = '${uri.scheme}://${uri.authority}';
}
// Future<Set<String>> _verifyCandidates(Set<String> candidates) async {
// final results = <String>{};
// final client = http.Client();
// try {
// for (final candidate in candidates) {
// try {
// await client.get(Uri.parse(candidate));
// } catch (e) {
// continue;
// }
// results.add(candidate);
// }
// } finally {
// client.close();
// }
// return results;
// }
void _parseBody(Set<String> candidates) {
for (final a in document.querySelectorAll('a')) {
var href = a.attributes['href'];
if (href != null) {
if (href.contains('rss') ||
href.contains('xml') ||
href.contains('feed')) {
// Fix relative URLs
href = href.startsWith('/') ? _base + href : href;
href = href.endsWith('/') ? href.substring(0, href.length - 2) : href;
// Fix naked URLs
href = !href.startsWith('http') ? '$_base/$href' : href;
candidates.add(href);
}
}
}
}
void _parseHead(Set<String> candidates) {
for (final link in document.querySelectorAll("link[rel='alternate']")) {
final type = link.attributes['type'];
if (type != null) {
if (type.contains('rss') || type.contains('xml')) {
var href = link.attributes['href'];
if (href != null) {
// Fix relative URLs
href = href.startsWith('/') ? _base + href : href;
candidates.add(href);
}
}
}
}
}
Future<Set<String>> parse({
bool parseHead = true,
bool parseBody = true,
// bool verifyCandidates = true,
}) async {
final candidates = <String>{};
// Look for feed candidates in head
if (parseHead) {
_parseHead(candidates);
}
// Look for feed candidates in body
if (parseBody) {
_parseBody(candidates);
}
// Verify candidates
// if (verifyCandidates) {
// return _verifyCandidates(candidates);
// }
return candidates;
}
}
@@ -0,0 +1,221 @@
import 'package:collection/collection.dart';
import 'package:flutter_mozilla_components/flutter_mozilla_components.dart';
import 'package:lensai/extensions/nullable.dart';
import 'package:lensai/features/web_feed/data/database/database.dart';
import 'package:lensai/features/web_feed/data/models/feed_article.dart';
import 'package:lensai/features/web_feed/data/models/feed_author.dart';
import 'package:lensai/features/web_feed/data/models/feed_link.dart';
import 'package:lensai/features/web_feed/extensions/atom.dart';
import 'package:lensai/features/web_feed/extensions/rss.dart';
import 'package:rss_dart/dart_rss.dart';
import 'package:rss_dart/domain/rss1_feed.dart';
class FeedParser {
final Uri url;
late Object _feed;
FeedParser._(this.url, this._feed);
factory FeedParser.parse({required Uri url, required String xmlString}) {
final rssVersion = WebFeed.detectRssVersion(xmlString);
final feed = switch (rssVersion) {
RssVersion.rss1 => Rss1Feed.parse(xmlString),
RssVersion.rss2 => RssFeed.parse(xmlString),
RssVersion.atom => AtomFeed.parse(xmlString),
RssVersion.unknown =>
throw Error.safeToString(
'Invalid XML String? We cannot detect RSS/Atom version.',
),
};
return FeedParser._(url, feed);
}
FeedData readGeneralData() {
switch (_feed) {
case final Rss1Feed feed:
return FeedData(
url: url,
title: feed.title.whenNotEmpty ?? feed.dc?.title,
description: feed.description.whenNotEmpty ?? feed.dc?.description,
authors: feed.dc?.creator.whenNotEmpty.mapNotNull(
(creator) => [FeedAuthor(name: creator)],
),
tags: feed.dc?.toFeedCategories(),
);
case final RssFeed feed:
final categories = feed.categories.toFeedCategories();
return FeedData(
url: url,
title: feed.title.whenNotEmpty ?? feed.dc?.title,
description: feed.description.whenNotEmpty ?? feed.dc?.description,
authors: (feed.author.whenNotEmpty ?? feed.dc?.creator.whenNotEmpty)
.mapNotNull((creator) => [FeedAuthor(name: creator)]),
tags:
categories.isNotEmpty ? categories : feed.dc?.toFeedCategories(),
);
case final AtomFeed feed:
final authors = feed.authors.toFeedAuthors();
final tags = feed.categories.toFeedCategories();
return FeedData(
url: url,
title: feed.title.whenNotEmpty,
description: feed.subtitle.whenNotEmpty,
authors: authors.isNotEmpty ? authors : null,
tags: tags,
);
default:
throw Exception();
}
}
Future<List<FeedArticle>> readArticles() async {
final fetchDate = DateTime.now();
switch (_feed) {
case final Rss1Feed feed:
final processedContents = await GeckoTurndownService().turndownHtml(
feed.items.map((item) => item.content?.value ?? '').toList(),
);
final processedSummaries = await GeckoTurndownService().turndownHtml(
feed.items
.map(
(item) =>
item.description.whenNotEmpty ?? item.dc?.description ?? '',
)
.toList(),
);
return feed.items.mapIndexed((i, item) {
final title = item.title.whenNotEmpty ?? item.dc?.title.whenNotEmpty;
final itemId = item.dc?.identifier.whenNotEmpty ?? title;
final uniqueId =
'${item.link.whenNotEmpty ?? feed.link.whenNotEmpty ?? url}#$itemId';
final date = SafeParseDateTime.safeParse(item.dc?.date);
final link = item.link.mapNotNull(Uri.tryParse);
return FeedArticle(
id: uniqueId,
feedId: url,
title: title,
fetched: fetchDate,
created: date,
authors: item.dc?.creator.whenNotEmpty.mapNotNull(
(creator) => [FeedAuthor(name: creator)],
),
summaryPlain: processedSummaries[i].plain.whenNotEmpty,
summaryMarkdown: processedSummaries[i].markdown.whenNotEmpty,
links: link.mapNotNull((link) => [FeedLink(uri: link)]),
tags: item.dc?.toFeedCategories(),
contentPlain: processedContents[i].plain.trim().whenNotEmpty,
contentMarkdown: processedContents[i].markdown?.trim().whenNotEmpty,
);
}).toList();
case final RssFeed feed:
final processedContents = await GeckoTurndownService().turndownHtml(
feed.items.map((item) => item.content?.value ?? '').toList(),
);
final processedSummaries = await GeckoTurndownService().turndownHtml(
feed.items
.map(
(item) =>
item.description.whenNotEmpty ?? item.dc?.description ?? '',
)
.toList(),
);
return feed.items.mapIndexed((i, item) {
final title = item.title.whenNotEmpty ?? item.dc?.title.whenNotEmpty;
final itemId =
item.guid.whenNotEmpty ??
item.dc?.identifier.whenNotEmpty ??
title;
final uniqueId =
'${item.link.whenNotEmpty ?? feed.link.whenNotEmpty ?? url}#$itemId';
final date = SafeParseDateTime.safeParse(
item.pubDate.whenNotEmpty ?? item.dc?.date,
);
final link = item.link.mapNotNull(Uri.tryParse);
final author =
item.author.whenNotEmpty ?? item.dc?.creator.whenNotEmpty;
final categories = item.categories.toFeedCategories();
return FeedArticle(
id: uniqueId,
feedId: url,
title: title,
fetched: fetchDate,
created: date,
authors: author.mapNotNull(
(creator) => [FeedAuthor(name: creator)],
),
summaryPlain: processedSummaries[i].plain.whenNotEmpty,
summaryMarkdown: processedSummaries[i].markdown.whenNotEmpty,
links: link.mapNotNull((link) => [FeedLink(uri: link)]),
tags:
categories.isNotEmpty
? categories
: item.dc?.toFeedCategories(),
contentPlain: processedContents[i].plain.trim().whenNotEmpty,
contentMarkdown: processedContents[i].markdown?.trim().whenNotEmpty,
);
}).toList();
case final AtomFeed feed:
final processedContents = await GeckoTurndownService().turndownHtml(
feed.items.map((item) => item.content ?? '').toList(),
);
final processedSummaries = await GeckoTurndownService().turndownHtml(
feed.items.map((item) => item.summary ?? '').toList(),
);
final feedLink = feed.links.toFeedLinks().firstWhereOrNull(
(link) => link.relation == FeedLinkRelation.self,
);
return feed.items.mapIndexed((i, item) {
final authors = item.authors.toFeedAuthors();
final tags = item.categories.toFeedCategories();
final itemLinks = item.links.toFeedLinks();
final articleLink = itemLinks.firstWhereOrNull(
(link) => link.relation == FeedLinkRelation.alternate,
);
final itemId = item.id.whenNotEmpty ?? item.title;
final uniqueId =
'${articleLink?.uri ?? feedLink?.uri ?? url}#$itemId';
final published = SafeParseDateTime.safeParse(item.published);
final updated = SafeParseDateTime.safeParse(item.updated);
return FeedArticle(
id: uniqueId,
feedId: url,
title: item.title.whenNotEmpty,
summaryPlain: processedSummaries[i].plain.whenNotEmpty,
summaryMarkdown: processedSummaries[i].markdown.whenNotEmpty,
links: itemLinks,
authors: authors.isNotEmpty ? authors : null,
tags: tags,
fetched: fetchDate,
created: published,
updated: updated,
contentPlain: processedContents[i].plain.trim().whenNotEmpty,
contentMarkdown: processedContents[i].markdown?.trim().whenNotEmpty,
);
}).toList();
default:
throw Exception();
}
}
}