Skip to content

Commit

Permalink
Merge pull request #27 from selmaohneh/release-notification
Browse files Browse the repository at this point in the history
Implemented first version of release notification. #20
  • Loading branch information
selmaohneh authored Apr 23, 2019
2 parents bc3b2e9 + 48995d8 commit d94bd53
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 64 deletions.
11 changes: 3 additions & 8 deletions DdfGuide.Android/AudioDramaListAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,9 @@ public override View GetView(int position, View convertView, ViewGroup parent)
var favoriteView = view.FindViewById<CheckBox>(Resource.Id.listcheckboxfavorite);
favoriteView.Checked = audioDrama.AudioDramaUserData.IsFavorite;

if (_releaseDateService.IsReleased(audioDrama.AudioDramaDto))
{
view.SetBackgroundColor(Color.White);
}
else
{
view.SetBackgroundColor(Color.LightGray);
}
view.SetBackgroundColor(_releaseDateService.IsReleased(audioDrama.AudioDramaDto)
? Color.White
: Color.LightGray);

return view;
}
Expand Down
4 changes: 4 additions & 0 deletions DdfGuide.Android/AudioDramaListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public override View OnCreateView(LayoutInflater inflater, ViewGroup container,
private void SetupFloatingRandomButton()
{
var fab = _view.FindViewById<FloatingActionButton>(Resource.Id.floatingActionButtonRandom);

// todo: #26
fab.SetBackgroundColor(Resources.GetColor(Resource.Color.ddfBlue));

fab.Click += (sender, args) => { RandomClicked?.Invoke(this, EventArgs.Empty); };
Expand All @@ -77,6 +79,8 @@ private void SetupToolbar()
{
_toolbar = _view.FindViewById<Toolbar>(Resource.Id.toolbar);
_toolbar.SetTitle(Resource.String.app_name);

// todo: #26
_toolbar.SetTitleTextColor(Resources.GetColor(Resource.Color.ddfWhite));

SetupMenuItems();
Expand Down
3 changes: 3 additions & 0 deletions DdfGuide.Android/DdfGuide.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@
<PackageReference Include="Newtonsoft.Json">
<Version>12.0.1</Version>
</PackageReference>
<PackageReference Include="Nito.AsyncEx">
<Version>3.0.1</Version>
</PackageReference>
<PackageReference Include="Xam.Plugins.Settings">
<Version>3.1.1</Version>
</PackageReference>
Expand Down
7 changes: 4 additions & 3 deletions DdfGuide.Android/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

namespace DdfGuide.Android
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true,ScreenOrientation = ScreenOrientation.Portrait, ConfigurationChanges = ConfigChanges.Orientation)]
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true,
ScreenOrientation = ScreenOrientation.Portrait, ConfigurationChanges = ConfigChanges.Orientation)]
public class MainActivity : Activity, IRootView
{
private Core.DdfGuide _ddfGuide;
Expand Down Expand Up @@ -49,7 +50,7 @@ private void ScheduleReleaseNotificationJob()
{
var jobBuilder = this.CreateJobBuilderUsingJobId<ReleaseNotificationJob>(1);
var jobInfo = jobBuilder
.SetPeriodic(TimeSpan.FromHours(12).Milliseconds, TimeSpan.FromHours(6).Milliseconds)
.SetPeriodic(TimeSpan.FromHours(6).Milliseconds, TimeSpan.FromHours(1).Milliseconds)
.SetPersisted(true)
.Build();

Expand All @@ -61,7 +62,7 @@ public void Show(IView view)
{
if (!(view is Fragment fragment))
{
throw new Exception("View needs to be of type Android.App.Fragment");
throw new Exception("View needs to be of type Android.App.Fragment");
}

var transaction = FragmentManager.BeginTransaction();
Expand Down
99 changes: 53 additions & 46 deletions DdfGuide.Android/ReleaseNotificationJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Android.Support.V4.App;
using DdfGuide.Core;
using FFImageLoading;
using Nito.AsyncEx;

namespace DdfGuide.Android
{
Expand All @@ -17,73 +18,79 @@ public class ReleaseNotificationJob : JobService
{
public override bool OnStartJob(JobParameters @params)
{
Task.Run(async () =>
{
var source = new DtoCache();
var dtos = source.Load()?.ToList();
Task.Run(() => ShowNotificationsIfApplicable(@params));

if (dtos == null)
{
JobFinished(@params, false);
return;
}
return true;
}

var lastNoficationCache = new LastNotificationCache();
var lastNofication = lastNoficationCache.Load();
private void ShowNotificationsIfApplicable(JobParameters @params)
{
var source = new DtoCache();
var dtos = source.Load()?.ToList();

if (lastNofication.Date == DateTime.Today)
{
JobFinished(@params, false);
return;
}
if (dtos == null)
{
JobFinished(@params, false);
return;
}

var lastNotificationCache = new LastNotificationCache();
var lastNotification = lastNotificationCache.Load();

if (lastNotification.Date == DateTime.Today)
{
JobFinished(@params, false);
return;
}

var releaseDateService = new ReleaseDateService();
var releases = releaseDateService.GetTodaysReleasesFrom(dtos).ToList();
var releaseDateService = new ReleaseDateService();
var releases = releaseDateService.GetTodaysReleasesFrom(dtos).ToList();

CreateNotificationChannel();
var notificationManager = NotificationManagerCompat.From(this);
if (!releases.Any())
{
JobFinished(@params, false);
return;
}

for (var i = 0; i < releases.Count; i++)
{
var dto = releases[i];
CreateNotificationChannel();
var notificationManager = NotificationManagerCompat.From(this);

var cover = await DownloadCover(dto);
for (var i = 0; i < releases.Count; i++)
{
var dto = releases[i];

var builder = new NotificationCompat.Builder(this, "channelid")
.SetOnlyAlertOnce(true)
.SetAutoCancel(true)
.SetContentTitle($"Neue {dto.Interpreter}-Folge!")
.SetSmallIcon(Resource.Mipmap.ic_stat_notification)
.SetLargeIcon(cover)
.SetContentText(dto.Title);
var cover = AsyncContext.Run(() => DownloadCover(dto));

notificationManager.Notify(i, builder.Build());
var builder = new NotificationCompat.Builder(this, "channelid")
.SetAutoCancel(true)
.SetContentTitle($"Neue {dto.Interpreter}-Folge!")
.SetSmallIcon(Resource.Mipmap.ic_stat_notification)
.SetLargeIcon(cover)
.SetContentText(dto.Title);

lastNoficationCache.Save(DateTime.Now);
}
notificationManager.Notify(i, builder.Build());

JobFinished(@params, false);
});
lastNotificationCache.Save(DateTime.Now);
}

return true;
JobFinished(@params, false);
}

private async Task<Bitmap> DownloadCover(AudioDramaDto dto)
{
Bitmap cover = null;
if (string.IsNullOrWhiteSpace(dto.CoverUrl))
{
return BitmapFactory.DecodeResource(Resources, Resource.Drawable.ic_launcher);
}

try
{
cover = (await ImageService.Instance.LoadUrl(dto.CoverUrl).AsBitmapDrawableAsync()).Bitmap;
return (await ImageService.Instance.LoadUrl(dto.CoverUrl).AsBitmapDrawableAsync()).Bitmap;
}
finally
catch (Exception)
{
if (cover == null)
{
cover = BitmapFactory.DecodeResource(Resources, Resource.Drawable.ic_launcher);
}
return BitmapFactory.DecodeResource(Resources, Resource.Drawable.ic_launcher);
}

return cover;
}

public override bool OnStopJob(JobParameters @params)
Expand Down
1 change: 0 additions & 1 deletion DdfGuide.Parser/HoerspielDeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using DdfGuide.Core;
using HtmlAgilityPack;

Expand Down
1 change: 0 additions & 1 deletion DdfGuide.Test/AudioDramaExplorerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using DdfGuide.Core.Sorting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Moq.AutoMock;

namespace DdfGuide.Test
{
Expand Down
3 changes: 1 addition & 2 deletions DdfGuide.Test/DtoFileTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using DdfGuide.Core;
Expand Down
1 change: 0 additions & 1 deletion DdfGuide.Test/HoerspielDeParserTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
Expand Down
2 changes: 0 additions & 2 deletions DdfGuide.Test/ReleaseDateServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using DdfGuide.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace DdfGuide.Test
{
Expand Down

0 comments on commit d94bd53

Please sign in to comment.