Skip to content

Commit

Permalink
Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
brainwo committed Sep 13, 2024
1 parent ac03ee7 commit 7e68899
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 99 deletions.
9 changes: 4 additions & 5 deletions lib/helper/command_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,9 @@ Future<void> playFromUrl(
final String url, {
final PlayMode mode = PlayMode.play,
}) async {
final prefs = await SharedPreferences.getInstance();
// final database = await HistoryDatabase.load();

await prefs.setStringList('history', [
...?prefs.getStringList('history'),
// TODO
]);
// TODO

final config = await UserConfig.load();

Expand Down Expand Up @@ -124,6 +121,8 @@ Future<void> playFromYoutubeVideo(
final bool fromHistory = false,
final PlayMode mode = PlayMode.play,
}) async {
// final database = await HistoryDatabase.load();

final prefs = await SharedPreferences.getInstance();

if (!fromHistory && (prefs.getBool('enable_history') ?? true)) {
Expand Down
140 changes: 80 additions & 60 deletions lib/model/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,63 +10,6 @@ enum ItemType {
channel,
}

class HistoryDatabase {
const HistoryDatabase({required this.history});

final List<HistoryModel> history;

static Future<HistoryDatabase> load({final int? limit}) async {
final file = File('${xdg.dataHome.path}/yatta/history.tsv');
return HistoryDatabase(
history: loadTsv(await file.readAsString())
.map((final e) => switch (e) {
{
'id': final String id,
'type': final String type,
'provider': final String provider,
'title': final String title,
'description': final String description,
'url': final String url,
'viewCount': final String viewCount,
'channelId': final String channelId,
'channelTitle': final String channelTitle,
'iconUrl': final String iconUrl,
'thumbnailUrl': final String thumbnailUrl,
'previewUrl': final String previewUrl,
'publishDate': final String publishDate,
'duration': final String duration,
'history': final String history,
'romanizedMetadata': final String romanizedMetadata,
} =>
HistoryModel(
id: id,
title: title,
description: description,
duration: duration,
romanizedMetadata: romanizedMetadata,
publishDate: publishDate,
type: switch (type) {
'video' => ItemType.video,
'playlist' => ItemType.playlist,
'channel' => ItemType.channel,
_ => ItemType.video,
},
history: history.split(','),
channelId: channelId,
iconUrl: iconUrl,
thumbnailUrl: thumbnailUrl,
previewUrl: previewUrl,
provider: provider,
channelTitle: channelTitle,
url: url,
viewCount: int.tryParse(viewCount),
),
_ => throw UnimplementedError(),
})
.toList());
}
}

class HistoryModel {
const HistoryModel({
required this.id,
Expand Down Expand Up @@ -104,13 +47,13 @@ class HistoryModel {
final String channelTitle;
final String channelId;

/// Cover image < 120px width
/// Cover image < 32px width
final String iconUrl;

/// Cover image < 32px width
/// Cover image < 48px width
final String thumbnailUrl;

/// Cover image < 48px width
/// Cover image < 120px width
final String previewUrl;

/// ISO 8601 format time
Expand All @@ -123,3 +66,80 @@ class HistoryModel {
/// Search friendly string
final String romanizedMetadata;
}

class HistoryDatabase {
const HistoryDatabase({required this.history});

final List<HistoryModel> history;

static Future<HistoryDatabase> load({final int? limit}) async {
final file = File('${xdg.dataHome.path}/yatta/history.tsv');

final historyList = <HistoryModel>[];

for (final (index, line)
in loadTsv(await file.readAsString()).reversed.indexed) {
// TODO: implement string line read limit
if (limit != null && index >= limit) break;

if (line
case {
'id': final String id,
'type': final String type,
'provider': final String provider,
'title': final String title,
'description': final String description,
'url': final String url,
'viewCount': final String viewCount,
'channelId': final String channelId,
'channelTitle': final String channelTitle,
'iconUrl': final String iconUrl,
'thumbnailUrl': final String thumbnailUrl,
'previewUrl': final String previewUrl,
'publishDate': final String publishDate,
'duration': final String duration,
'history': final String history,
'romanizedMetadata': final String romanizedMetadata,
}) {
historyList.add(
HistoryModel(
id: id,
title: title,
description: description,
duration: duration,
romanizedMetadata: romanizedMetadata,
publishDate: publishDate,
type: switch (type) {
'video' => ItemType.video,
'playlist' => ItemType.playlist,
'channel' => ItemType.channel,
_ => ItemType.video,
},
history: history.split(','),
channelId: channelId,
iconUrl: iconUrl,
thumbnailUrl: thumbnailUrl,
previewUrl: previewUrl,
provider: provider,
channelTitle: channelTitle,
url: url,
viewCount: int.tryParse(viewCount),
),
);
continue;
}

print('Unable to parse history, index $index, data $line');
}

return HistoryDatabase(history: historyList);
}

void append(final HistoryModel history) {
// TODO:
}

Future<void> write() async {
// TODO:
}
}
2 changes: 1 addition & 1 deletion lib/page/history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class _HistoryPageState extends State<HistoryPage> {
final database = await HistoryDatabase.load();

setState(() {
historyList = database.history;
historyList = database.history.reversed.toList();
filteredList = historyList;
});
}
Expand Down
57 changes: 24 additions & 33 deletions lib/page/home.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import 'package:autoscroll/autoscroll.dart';
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter/material.dart' show showLicensePage;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:youtube_api/youtube_api.dart';

import '../helper/time.dart';
import '../model/database.dart';
import '../widget/keyboard_navigation.dart';
import '../widget/list_items/list_item.dart';

Expand All @@ -22,7 +21,7 @@ class WelcomeMessage extends StatelessWidget {
padding: const EdgeInsets.symmetric(horizontal: 32),
children: [
const SizedBox(height: 8),
RecentHistory(),
const RecentHistory(),
const SizedBox(height: 24),
Wrap(
direction: isWide ? Axis.vertical : Axis.horizontal,
Expand Down Expand Up @@ -151,15 +150,11 @@ class WelcomeMessage extends StatelessWidget {
}

class RecentHistory extends StatelessWidget {
RecentHistory({
const RecentHistory({
super.key,
});

final fetchHistory = SharedPreferences.getInstance().then(
(final v) => v.getStringList('history')?.map(
(final e) => YoutubeVideo.fromString(e),
),
);
static final fetchHistory = HistoryDatabase.load(limit: 10);

@override
Widget build(final BuildContext context) {
Expand Down Expand Up @@ -194,11 +189,8 @@ class RecentHistory extends StatelessWidget {
AutoscrollSingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: snapshot.data!
.toList()
.reversed
.where((final e) => e.kind == 'video')
.take(10)
children: snapshot.data!.history
.where((final e) => e.type == ItemType.video)
.map(
(final data) =>
_HomeVideoThumbnail(youtubeVideo: data),
Expand All @@ -218,7 +210,7 @@ class _HomeVideoThumbnail extends StatelessWidget {
required this.youtubeVideo,
});

final YoutubeVideo youtubeVideo;
final HistoryModel youtubeVideo;

@override
Widget build(final BuildContext context) {
Expand All @@ -234,26 +226,25 @@ class _HomeVideoThumbnail extends StatelessWidget {
alignment: AlignmentDirectional.bottomEnd,
children: [
Image.network(
youtubeVideo.thumbnail.medium.url ?? '',
youtubeVideo.thumbnailUrl,
),
if (youtubeVideo.duration != null)
Padding(
padding: const EdgeInsets.all(4),
child: Container(
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(4),
),
padding: const EdgeInsets.symmetric(
horizontal: 4,
vertical: 2,
),
child: Text(
youtubeVideo.duration!,
style: const TextStyle(fontSize: 12),
),
Padding(
padding: const EdgeInsets.all(4),
child: Container(
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(4),
),
padding: const EdgeInsets.symmetric(
horizontal: 4,
vertical: 2,
),
child: Text(
youtubeVideo.duration,
style: const TextStyle(fontSize: 12),
),
),
),
],
),
const SizedBox(height: 10),
Expand All @@ -274,7 +265,7 @@ class _HomeVideoThumbnail extends StatelessWidget {
),
Text(
' • ${timeSince(
DateTime.parse(youtubeVideo.publishedAt!),
DateTime.parse(youtubeVideo.publishDate),
DateTime.now(),
)}',
style: const TextStyle(
Expand Down

0 comments on commit 7e68899

Please # to comment.