Skip to content

Commit

Permalink
✨ Also write offline listens to Hive box
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxr1998 committed Dec 6, 2023
1 parent 1071e87 commit 0b2518c
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 20 deletions.
2 changes: 2 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ Future<void> setupHive() async {
Hive.registerAdapter(DownloadedImageAdapter());
Hive.registerAdapter(ThemeModeAdapter());
Hive.registerAdapter(LocaleAdapter());
Hive.registerAdapter(OfflineListenAdapter());
await Future.wait([
Hive.openBox<DownloadedParent>("DownloadedParents"),
Hive.openBox<DownloadedSong>("DownloadedItems"),
Expand All @@ -186,6 +187,7 @@ Future<void> setupHive() async {
Hive.openBox<String>("DownloadedImageIds"),
Hive.openBox<ThemeMode>("ThemeMode"),
Hive.openBox<Locale?>(LocaleHelper.boxName),
Hive.openBox<OfflineListen>("OfflineListens")
]);

// If the settings box is empty, we add an initial settings value here.
Expand Down
40 changes: 38 additions & 2 deletions lib/models/finamp_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:hive/hive.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:uuid/uuid.dart';
import 'package:path/path.dart' as path_helper;
import 'package:uuid/uuid.dart';

import '../services/finamp_settings_helper.dart';
import 'jellyfin_models.dart';
import '../services/get_internal_song_dir.dart';
import 'jellyfin_models.dart';

part 'finamp_models.g.dart';

Expand Down Expand Up @@ -571,3 +571,39 @@ class DownloadedImage {
downloadLocationId: downloadLocationId,
);
}

@HiveType(typeId: 43)
class OfflineListen {
OfflineListen({
required this.timestamp,
required this.userId,
required this.itemId,
required this.name,
this.artist,
this.album,
this.trackMbid,
});

/// The stop timestamp of the listen, measured in seconds since the epoch.
@HiveField(0)
int timestamp;

@HiveField(1)
String userId;

@HiveField(2)
String itemId;

@HiveField(3)
String name;

@HiveField(4)
String? artist;

@HiveField(5)
String? album;

// The MusicBrainz ID of the track, if available.
@HiveField(6)
String? trackMbid;
}
52 changes: 52 additions & 0 deletions lib/models/finamp_models.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 20 additions & 18 deletions lib/services/offline_listen_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import 'dart:convert';
import 'dart:io';

import 'package:audio_service/audio_service.dart';
import 'package:finamp/models/finamp_models.dart';
import 'package:finamp/services/finamp_user_helper.dart';
import 'package:get_it/get_it.dart';
import 'package:hive/hive.dart';
import 'package:logging/logging.dart';
import 'package:path_provider/path_provider.dart';

Expand Down Expand Up @@ -33,39 +35,39 @@ class OfflineListenLogHelper {
Future<void> logOfflineListen(MediaItem item) async {
final itemJson = item.extras!["itemJson"];

await _logOfflineListen(
final offlineListen = OfflineListen(
timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000,
userId: _finampUserHelper.currentUserId!,
itemId: itemJson["Id"],
name: itemJson["Name"],
artist: itemJson["AlbumArtist"],
album: itemJson["Album"],
trackMbid: itemJson["ProviderIds"]?["MusicBrainzTrack"],
userId: _finampUserHelper.currentUserId,
);

await _logOfflineListen(offlineListen);
}

/// Logs a listen to a file.
///
/// This is used when the user is offline or submitting live playback events fails.
/// The [timestamp] provided to this function should be in seconds
/// and marks the time the track was stopped.
Future<void> _logOfflineListen({
required int timestamp,
required String itemId,
required String name,
String? artist,
String? album,
String? trackMbid,
String? userId,
}) async {
Future<void> _logOfflineListen(OfflineListen listen) async {
Hive.box<OfflineListen>("OfflineListens").add(listen);

_exportOfflineListenToFile(listen);
}

Future<void> _exportOfflineListenToFile(OfflineListen listen) async {
final data = {
'timestamp': timestamp,
'item_id': itemId,
'title': name,
'artist': artist,
'album': album,
'track_mbid': trackMbid,
'user_id': userId,
'timestamp': listen.timestamp,
'item_id': listen.itemId,
'title': listen.name,
'artist': listen.artist,
'album': listen.album,
'track_mbid': listen.trackMbid,
'user_id': listen.userId,
};
final content = json.encode(data) + Platform.lineTerminator;

Expand Down

0 comments on commit 0b2518c

Please sign in to comment.