Created
January 4, 2020 20:29
-
-
Save Minenash/577d01f1b111fc9f16279a01fce7467c to your computer and use it in GitHub Desktop.
database.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:moor_flutter/moor_flutter.dart'; | |
import 'package:xml/xml.dart'; | |
import 'package:simple_pod/data_structures/rss/helpers.dart'; | |
part 'database.g.dart'; | |
AppDatabase Database = AppDatabase(); | |
class PodcastFeeds extends Table { | |
IntColumn get id => integer().autoIncrement()(); | |
TextColumn get source => text()(); | |
TextColumn get title => text().nullable()(); | |
TextColumn get link => text().nullable()(); | |
TextColumn get description => text().nullable()(); | |
TextColumn get language => text().nullable()(); | |
TextColumn get copyright => text().nullable()(); | |
TextColumn get pubDate => text().nullable()(); | |
// itunes | |
TextColumn get author => text().nullable()(); | |
TextColumn get artwork => text().nullable()(); | |
TextColumn get keywords => text().nullable()(); | |
TextColumn get categories => text().nullable()(); | |
BoolColumn get explicit => boolean().nullable()(); | |
BoolColumn get block => boolean().nullable()(); | |
// app only elements | |
IntColumn get episodes => integer()(); | |
IntColumn get downloaded => integer()(); | |
IntColumn get listened => integer()(); | |
IntColumn get ignored => integer()(); | |
static PodcastFeed parseFeed(String source, String feed) { | |
XmlDocument document = parse(feed); | |
XmlElement c = document?.findAllElements("channel")?.first; | |
if (c == null) return null; | |
return PodcastFeed( | |
source: source, | |
title: findString(c, "title"), | |
link: findString(c, "link"), | |
description: findString(c, "description"), | |
language: findString(c, "language"), | |
copyright: findString(c, "copyright"), | |
author: findString(c, "itunes:author"), | |
artwork: find(c, "itunes:image")?.getAttribute("href"), | |
keywords: findString(c, "itunes:keywords"), | |
explicit: findBool(c, "itunes:explicit"), | |
block: findBool(c, "itunes:block"), | |
pubDate: findString(c, "pubDate"), | |
episodes: findAll(c, "item")?.length, | |
listened: 0, | |
downloaded: 0, | |
ignored: 0, | |
); | |
} | |
} | |
@UseMoor(tables: [PodcastFeeds]) | |
class AppDatabase extends _$AppDatabase { | |
AppDatabase() : super(FlutterQueryExecutor.inDatabaseFolder( | |
path: 'db.sqlite', logStatements: true)); | |
@override | |
int get schemaVersion => 2; | |
Future<List<PodcastFeed>> getPodcastFeeds() => select(podcastFeeds).get(); | |
Future insertPodcastFeed(PodcastFeed feed) => into(podcastFeeds).insert(feed); | |
Future clearAllPodcastFeeds() => delete(podcastFeeds).go(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment