Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Badge adjustments #609

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions BMM.Core/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ public override void Initialize()
Mvx.IoCProvider.RegisterType<ILanguagesLogger, LanguagesLogger>();
Mvx.IoCProvider.ConstructAndRegisterSingleton<IGlobalMediaDownloader, GlobalMediaDownloader>();

Mvx.IoCProvider.LazyConstructAndRegisterSingleton<IBadgeService, BadgeService>();

Mvx.IoCProvider.LazyConstructAndRegisterSingleton<IPodcastOfflineManager, PodcastOfflineManager>();

Mvx.IoCProvider.ConstructAndRegisterSingleton<IReceive<PodcastNotification>, PodcastNotificationReceiver>();
Expand All @@ -244,9 +246,7 @@ public override void Initialize()

Mvx.IoCProvider.RegisterType<IDocumentFilter, NullFilter>();
Mvx.IoCProvider.RegisterType<IDownloadedTracksOnlyFilter, DownloadedTracksOnlyFilter>();

Mvx.IoCProvider.LazyConstructAndRegisterSingleton<IBadgeService, BadgeService>();


Mvx.IoCProvider.RegisterType<IPlayerErrorHandler, PlayerErrorHandler>();
Mvx.IoCProvider.LazyConstructAndRegisterSingleton<IPlayerAnalytics, PlayerAnalytics>();
Mvx.IoCProvider.LazyConstructAndRegisterSingleton<IPlaybackHistoryService, PlaybackHistoryService>();
Expand Down
24 changes: 8 additions & 16 deletions BMM.Core/Implementations/Badge/BadgeService.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
using BMM.Api.Implementation.Models;
using BMM.Core.Implementations.Connection;
using BMM.Core.Implementations.FirebaseRemoteConfig;
using BMM.Core.Implementations.Notifications;
using BMM.Core.Implementations.Storage;
using BMM.Core.Messages;
using MvvmCross.Plugin.Messenger;

namespace BMM.Core.Implementations.Badge;

public class BadgeService : IBadgeService
{
private readonly ISettingsStorage _settingsStorage;
private readonly IFirebaseRemoteConfig _firebaseRemoteConfig;
private readonly INotificationDisplayer _notificationDisplayer;
private bool _isBadgeSet;

public BadgeService(
ISettingsStorage settingsStorage,
IFirebaseRemoteConfig firebaseRemoteConfig,
INotificationDisplayer notificationDisplayer)
public BadgeService(ISettingsStorage settingsStorage)
{
_settingsStorage = settingsStorage;
_firebaseRemoteConfig = firebaseRemoteConfig;
_notificationDisplayer = notificationDisplayer;
}

public bool IsBadgeSet
Expand All @@ -35,13 +23,17 @@ private set
}
}

public async Task Set()
public async Task<bool> SetIfPossible()
{
if (!await _settingsStorage.GetBibleStudyBadgeEnabled())
return false;

AppSettings.IsBadgeSet = IsBadgeSet = true;
AppSettings.BadgeSetAt = DateTime.UtcNow;
return true;
}

public async Task Remove()
public void Remove()
{
AppSettings.IsBadgeSet = IsBadgeSet = false;
AppSettings.BadgeSetAt = DateTime.MinValue;
Expand All @@ -52,7 +44,7 @@ public async Task VerifyBadge()
IsBadgeSet = AppSettings.IsBadgeSet;

if (AppSettings.BadgeSetAt.AddDays(1) < DateTime.UtcNow)
await Remove();
Remove();
}

public event EventHandler BadgeChanged;
Expand Down
4 changes: 2 additions & 2 deletions BMM.Core/Implementations/Badge/Interfaces/IBadgeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace BMM.Core.Implementations.Badge;
public interface IBadgeService
{
bool IsBadgeSet { get; }
Task Set();
Task Remove();
Task<bool> SetIfPossible();
void Remove();
Task VerifyBadge();
event EventHandler BadgeChanged;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public AkavacheBlobSettingsStorage(IFirebaseRemoteConfig config)
public async Task<bool> GetAutoplayEnabled() => AppSettings.AutoplayEnabled ?? _config.AutoplayEnabledDefaultSetting;

public async Task<bool> GetStreakHidden() => AppSettings.StreakHidden;
public async Task<bool> GetBibleStudyBadgeEnabled() => AppSettings.BibleStudyBadgeEnabled;
public async Task<bool> GetBibleStudyBadgeEnabled() => _config.IsBadgesFeatureEnabled && AppSettings.BibleStudyBadgeEnabled;

public async Task<bool> GetBibleStudyOnHomeEnabled() => AppSettings.BibleStudyOnHomeEnabled;

public async Task<bool> GetMobileNetworkDownloadAllowed() => AppSettings.MobileDownloadEnabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using BMM.Core.Implementations.Connection;
using BMM.Core.Implementations.FileStorage;
using BMM.Core.Implementations.FirebaseRemoteConfig;
using BMM.Core.Implementations.PlayObserver.Streak;
using BMM.Core.Implementations.UI;
using BMM.Core.Models.POs.Tiles;
using BMM.Core.Models.POs.Tiles.Interfaces;
Expand All @@ -26,11 +27,10 @@ public class TilePOFactory : ITilePOFactory
private readonly IUriOpener _uriOpener;
private readonly IDeepLinkHandler _deepLinkHandler;
private readonly IBadgeService _badgeService;
private readonly IStreakObserver _streakObserver;
private readonly IFirebaseRemoteConfig _firebaseRemoteConfig;
private readonly ISettingsStorage _settingsStorage;

public TilePOFactory(
ITileClickedAction tileClickedAction,
public TilePOFactory(ITileClickedAction tileClickedAction,
IContinuePlayingAction continuePlayingAction,
IShuffleButtonClickedAction shuffleButtonClickedAction,
IShowTrackInfoAction showTrackInfoAction,
Expand All @@ -39,8 +39,8 @@ public TilePOFactory(
IUriOpener uriOpener,
IDeepLinkHandler deepLinkHandler,
IBadgeService badgeService,
IFirebaseRemoteConfig firebaseRemoteConfig,
ISettingsStorage settingsStorage)
IStreakObserver streakObserver,
IFirebaseRemoteConfig firebaseRemoteConfig)
{
_tileClickedAction = tileClickedAction;
_continuePlayingAction = continuePlayingAction;
Expand All @@ -51,8 +51,8 @@ public TilePOFactory(
_uriOpener = uriOpener;
_deepLinkHandler = deepLinkHandler;
_badgeService = badgeService;
_streakObserver = streakObserver;
_firebaseRemoteConfig = firebaseRemoteConfig;
_settingsStorage = settingsStorage;
}

public ITilePO Create(IMvxAsyncCommand<Document> optionsClickedCommand, Document tile)
Expand All @@ -70,7 +70,7 @@ public ITilePO Create(IMvxAsyncCommand<Document> optionsClickedCommand, Document
_mediaPlayer,
_storageManager,
_badgeService,
_settingsStorage,
_streakObserver,
_firebaseRemoteConfig,
continueListeningTile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ private class Variables
public const string CurrentPodcastId = "current_podcast_id";
public const string AutoSubscribePodcasts = "auto_subscribe_podcasts";
public const string EnableReadingTranscriptions = "enable_reading_transcriptions";

public const string IsBadgesFeatureEnabled = "is_badges_feature_enabled";
}

public static readonly Dictionary<string, string> Defaults = new()
Expand Down Expand Up @@ -87,7 +89,8 @@ private class Variables
{Variables.ShowBlueDotForSongs, false.ToString()},
{Variables.CurrentPodcastId, PodcastsConstants.FraKårePodcastId.ToString()},
{Variables.AutoSubscribePodcasts, $"{PodcastsConstants.FraKårePodcastId},{PodcastsConstants.RomanPodcastId}"},
{Variables.EnableReadingTranscriptions, false.ToString()}
{Variables.EnableReadingTranscriptions, false.ToString()},
{Variables.IsBadgesFeatureEnabled, true.ToString()},
};

public FirebaseRemoteConfig(IPlatformSpecificRemoteConfig platformSpecificRemoteConfig, SemanticVersionParser semanticVersionParser)
Expand All @@ -109,6 +112,7 @@ public async Task UpdateValuesFromFirebaseRemoteConfig()
public string UserVoiceLink => _platformSpecificRemoteConfig.GetStringValue(Variables.UserVoiceLink);
public string PrivacyPolicyLink => _platformSpecificRemoteConfig.GetStringValue(Variables.PrivacyPolicyLink);
public string RomansQuestionsUrl => _platformSpecificRemoteConfig.GetStringValue(Variables.RomansQuestionsUrl);
public bool IsBadgesFeatureEnabled => _platformSpecificRemoteConfig.GetBoolValue(Variables.IsBadgesFeatureEnabled);
public string DeleteAccountLink => _platformSpecificRemoteConfig.GetStringValue(Variables.DeleteAccountLink);

public string SongTreasuresSongLink => _platformSpecificRemoteConfig.GetStringValue(Variables.SongTreasuresSongLink);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ public interface IFirebaseRemoteConfig
bool IsReadingTranscriptionsEnabled { get; }
string PrivacyPolicyLink { get; }
string RomansQuestionsUrl { get; }
bool IsBadgesFeatureEnabled { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
using BMM.Core.Extensions;
using BMM.Core.Helpers;
using BMM.Core.Implementations.Analytics;
using BMM.Core.Implementations.Badge;
using BMM.Core.Implementations.Downloading;
using BMM.Core.Implementations.Downloading.DownloadQueue;
using BMM.Core.Implementations.Downloading.FileDownloader;
using BMM.Core.Implementations.Exceptions;
using BMM.Core.Implementations.FirebaseRemoteConfig;
using BMM.Core.Implementations.Notifications.Data;
using BMM.Core.Implementations.Storage;
using BMM.Core.ViewModels;
using BMM.Core.ViewModels.Parameters;
using MvvmCross.Localization;
Expand All @@ -27,6 +30,8 @@ public class PodcastNotificationReceiver : IReceive<PodcastNotification>, IDispo
private readonly IMvxMessenger _messenger;
private readonly IMvxNavigationService _navigationService;
private readonly IUiDependentExecutor _executor;
private readonly IBadgeService _badgeService;
private readonly IFirebaseRemoteConfig _firebaseRemoteConfig;

private MvxSubscriptionToken _completedToken;

Expand All @@ -40,14 +45,18 @@ public PodcastNotificationReceiver(
IExceptionHandler exceptionHandler,
IDownloadQueue downloadQueue,
IMvxNavigationService navigationService,
IUiDependentExecutor executor)
IUiDependentExecutor executor,
IBadgeService badgeService,
IFirebaseRemoteConfig firebaseRemoteConfig)
{
_mediaDownloader = mediaDownloader;
_analytics = analytics;
_exceptionHandler = exceptionHandler;
_downloadQueue = downloadQueue;
_navigationService = navigationService;
_executor = executor;
_badgeService = badgeService;
_firebaseRemoteConfig = firebaseRemoteConfig;
_messenger = messenger;

_tcs = new TaskCompletionSource<bool>();
Expand Down Expand Up @@ -82,18 +91,25 @@ public void OnNotificationReceived(PodcastNotification podcastNotification)

LogPodcastEvent("Podcast notification received", podcastNotification);
_podcastNotificationOfDownloadFile = podcastNotification;

_completedToken = _messenger.Subscribe<QueueFinishedMessage>(QueueFinished);

PodcastLoggingExtensions.PodcastTrackIdToDownload = podcastNotification.TrackIds.First();
await _mediaDownloader.SynchronizeOfflineTracks();
await AllDownloadsCompleted(podcastNotification);
PodcastLoggingExtensions.PodcastTrackIdToDownload = null;

ShowBadgeIfNeeded(podcastNotification.PodcastId);
LogPodcastEvent("Podcast notification offline tracks synchronized", podcastNotification);
});
}

private void ShowBadgeIfNeeded(int podcastId)
{
if (podcastId == _firebaseRemoteConfig.CurrentPodcastId)
_badgeService.SetIfPossible();
}

private async Task AllDownloadsCompleted(PodcastNotification podcastNotification)
{
if (_downloadQueue.RemainingDownloadsCount > 0)
Expand Down
30 changes: 16 additions & 14 deletions BMM.Core/Implementations/PlayObserver/Streak/StreakObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace BMM.Core.Implementations.PlayObserver.Streak
public interface IStreakObserver
{
Task UpdateStreakIfLocalVersionIsNewer(List<Document> documents);
ListeningStreak LatestStreak { get; }
}

/// <summary>
Expand All @@ -32,7 +33,6 @@ public class StreakObserver : IStreakObserver
private readonly IAnalytics _analytics;
private readonly IPlayStatistics _playStatistics;
private readonly IBadgeService _badgeService;
private ListeningStreak _latestStreak;

private readonly MvxSubscriptionToken _trackCompletedToken;
private readonly MvxSubscriptionToken _trackChangedToken;
Expand Down Expand Up @@ -64,14 +64,16 @@ public StreakObserver(IMvxMessenger messenger,
_trackCompletedToken = messenger.Subscribe<StreakTrackCompletedMessage>(TrackCompleted);
_trackChangedToken = messenger.Subscribe<CurrentTrackChangedMessage>(TrackChanged);
}

public ListeningStreak LatestStreak { get; private set; }

private void TrackChanged(CurrentTrackChangedMessage message)
{
if (_latestStreak == null
if (LatestStreak == null
|| message.CurrentTrack == null
|| message.CurrentTrack.Id != _latestStreak.TodaysFraKaareTrackId
|| DateTime.UtcNow >= _latestStreak.EligibleUntil.ToUniversalTime()
|| _latestStreak.IsTodayAlreadyListened())
|| message.CurrentTrack.Id != LatestStreak.TodaysFraKaareTrackId
|| DateTime.UtcNow >= LatestStreak.EligibleUntil.ToUniversalTime()
|| LatestStreak.IsTodayAlreadyListened())
{
DisposeTimer();
return;
Expand Down Expand Up @@ -122,10 +124,10 @@ private void CheckIfListenedEnoughYet(object sender, ElapsedEventArgs e)

private async Task<bool> UpdateStreakIfListened(ITrackModel track, Func<PlayMeasurements> measurementsFactory)
{
if (_latestStreak != null
&& track?.Id == _latestStreak.TodaysFraKaareTrackId
&& !_latestStreak.IsTodayAlreadyListened()
&& DateTime.UtcNow < _latestStreak.EligibleUntil.ToUniversalTime())
if (LatestStreak != null
&& track?.Id == LatestStreak.TodaysFraKaareTrackId
&& !LatestStreak.IsTodayAlreadyListened()
&& DateTime.UtcNow < LatestStreak.EligibleUntil.ToUniversalTime())
{
var measurements = measurementsFactory.Invoke();
if (measurements == null)
Expand All @@ -137,7 +139,7 @@ private async Task<bool> UpdateStreakIfListened(ITrackModel track, Func<PlayMeas
{
await _playStatistics.PostStreakPoints(track, measurements);

_latestStreak.MarkTodayAsListened();
LatestStreak.MarkTodayAsListened();
_analytics.LogEvent("mark today as listened",
new Dictionary<string, object>
{
Expand All @@ -147,9 +149,9 @@ private async Task<bool> UpdateStreakIfListened(ITrackModel track, Func<PlayMeas
{"playbackStarted", measurements.TimestampStart},
{"playbackEnded", measurements.TimestampEnd}
});
_messenger.Publish(new ListeningStreakChangedMessage(this) {ListeningStreak = _latestStreak});
await Store(_latestStreak);
await _badgeService.Remove();
_messenger.Publish(new ListeningStreakChangedMessage(this) {ListeningStreak = LatestStreak});
await Store(LatestStreak);
_badgeService.Remove();
return true;
}
}
Expand Down Expand Up @@ -194,7 +196,7 @@ public async Task UpdateStreakIfLocalVersionIsNewer(List<Document> documents)
}
}

_latestStreak = streakFromServer;
LatestStreak = streakFromServer;
await Store(null);
}
}
Expand Down
Loading