Skip to content

Commit

Permalink
♻️ Add user ID to offline listens log
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxr1998 committed Dec 5, 2023
1 parent 3301c65 commit 3bd08b1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
5 changes: 4 additions & 1 deletion lib/services/finamp_user_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class FinampUserHelper {
/// Checks if there are any saved users.
bool get isUsersEmpty => _finampUserBox.isEmpty;

/// Loads the id from CurrentUserId. Returns null if no id is stored.
String? get currentUserId => _currentUserIdBox.get("CurrentUserId");

/// Loads the FinampUser with the id from CurrentUserId. Returns null if no
/// user exists.
FinampUser? get currentUser =>
Expand Down Expand Up @@ -61,4 +64,4 @@ class FinampUserHelper {

_finampUserBox.delete(id);
}
}
}
33 changes: 20 additions & 13 deletions lib/services/offline_listen_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import 'dart:convert';
import 'dart:io';

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

/// Logs offline listens or failed submissions to a file.
class OfflineListenLogHelper {
final _logger = Logger("OfflineListenLogHelper");
final _finampUserHelper = GetIt.instance<FinampUserHelper>();

Future<Directory> get _logDirectory async {
if (!Platform.isAndroid) {
Expand All @@ -25,32 +28,36 @@ class OfflineListenLogHelper {
}

Future<void> logOfflineListen(MediaItem item) async {
final timestamp = DateTime.now().millisecondsSinceEpoch ~/ 1000;
final itemJson = item.extras!["itemJson"];
final id = itemJson["Id"];
final name = itemJson["Name"];
final albumArtist = itemJson["AlbumArtist"];
final album = itemJson["Album"];
final trackMbid = itemJson["ProviderIds"]?["MusicBrainzTrack"];

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

Future<void> _logOfflineListen(
int timestamp,
String id,
String name,
Future<void> _logOfflineListen({
required int timestamp,
required String itemId,
required String name,
String? artist,
String? album,
String? trackMbid,
) async {
String? userId,
}) async {
final data = {
'timestamp': timestamp,
'id': id,
'item_id': itemId,
'title': name,
'artist': artist,
'album': album,
'track_mbid': trackMbid,
'user_id': userId,
};
final content = json.encode(data) + Platform.lineTerminator;

Expand Down

0 comments on commit 3bd08b1

Please sign in to comment.