Skip to content
This repository has been archived by the owner on Sep 14, 2018. It is now read-only.

Commit

Permalink
Local caching
Browse files Browse the repository at this point in the history
Credit to @samsp
Trying to merge and refactor :)
  • Loading branch information
bartlannoeye committed Mar 7, 2016
1 parent baacb7d commit 78afa2f
Show file tree
Hide file tree
Showing 26 changed files with 803 additions and 139 deletions.
17 changes: 17 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Auto detect text files and perform LF normalization
* text=auto

# Custom for Visual Studio
*.cs diff=csharp

# Standard to msysgit
*.doc diff=astextplain
*.DOC diff=astextplain
*.docx diff=astextplain
*.DOCX diff=astextplain
*.dot diff=astextplain
*.DOT diff=astextplain
*.pdf diff=astextplain
*.PDF diff=astextplain
*.rtf diff=astextplain
*.RTF diff=astextplain
1 change: 1 addition & 0 deletions src/Kliva/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("AsyncUsage", "AsyncFixer002:Long running / blocking operations under an async method", Justification = "Newtonsoft async method obsolete", Scope = "member", Target = "~M:Kliva.Services.SettingsService.GetStoredStravaAccessToken~System.Threading.Tasks.Task{System.String}")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("AsyncUsage", "AsyncFixer002:Long running / blocking operations under an async method", Justification = "Newtonsoft async method obsolete", Scope = "member", Target = "~M:Kliva.Services.SettingsService.SetDistanceUnitType(Kliva.Models.DistanceUnitType)~System.Threading.Tasks.Task")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("AsyncUsage", "AsyncFixer002:Long running / blocking operations under an async method", Justification = "<Pending>", Scope = "member", Target = "~M:Kliva.Services.SettingsService.LoadSettings(System.Boolean)~System.Threading.Tasks.Task")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("AsyncUsage", "AsyncFixer002:Long running / blocking operations under an async method", Justification = "<Pending>", Scope = "member", Target = "~M:Kliva.Services.StravaService.GetActivitySummaryRelationsAsync(System.Collections.Generic.IEnumerable{Kliva.Models.ActivitySummary})~System.Threading.Tasks.Task")]

9 changes: 8 additions & 1 deletion src/Kliva/Kliva.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@
<Compile Include="Messages\PivotMessage.cs" />
<Compile Include="Models\AccessToken.cs" />
<Compile Include="Models\Activity.cs" />
<Compile Include="Models\ActivityIncrementalCollection.cs" />
<Compile Include="Models\Converters\AthleteJsonConverter.cs" />
<Compile Include="Models\Incremental\ActivityIncrementalCollection.cs" />
<Compile Include="Models\ActivityMeta.cs" />
<Compile Include="Models\ActivitySummary.cs" />
<Compile Include="Models\AppVersion.cs" />
Expand All @@ -154,6 +155,10 @@
<Compile Include="Models\Constants.cs" />
<Compile Include="Models\Enums.cs" />
<Compile Include="Models\IncrementalLoadingBase.cs" />
<Compile Include="Models\Incremental\CachedKeyedIncrementalLoadingBase.cs" />
<Compile Include="Models\Incremental\FriendActivityIncrementalCollection.cs" />
<Compile Include="Models\Incremental\IKey.cs" />
<Compile Include="Models\Incremental\MyActivityIncrementalCollection.cs" />
<Compile Include="Models\Limit.cs" />
<Compile Include="Models\Limits.cs" />
<Compile Include="Models\Map.cs" />
Expand All @@ -166,6 +171,7 @@
<Compile Include="Models\StravaIdentityConstants.cs" />
<Compile Include="Models\Usage.cs" />
<Compile Include="Services\ApplicationInfoService.cs" />
<Compile Include="Services\LocalCacheService.cs" />
<Compile Include="Services\Endpoints.cs" />
<Compile Include="Services\Framework\AsyncResponseReceivedEventArgs.cs" />
<Compile Include="Services\Interfaces\IApplicationInfoService.cs" />
Expand All @@ -175,6 +181,7 @@
<Compile Include="Services\Interfaces\IStravaClubService.cs" />
<Compile Include="Services\Interfaces\IStravaSegmentService.cs" />
<Compile Include="Services\Interfaces\IStravaService.cs" />
<Compile Include="Services\Performance\ETWLogging.cs" />
<Compile Include="Services\StravaActivityService.cs" />
<Compile Include="Services\SettingsService.cs" />
<Compile Include="Services\StravaAthleteService.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/Kliva/Kliva.csproj.DotSettings
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=models_005Cincremental/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=services_005Cframework/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
47 changes: 0 additions & 47 deletions src/Kliva/Models/ActivityIncrementalCollection.cs

This file was deleted.

34 changes: 26 additions & 8 deletions src/Kliva/Models/ActivitySummary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Kliva.Services.Interfaces;
using Microsoft.Practices.ServiceLocation;

namespace Kliva.Models
{
Expand Down Expand Up @@ -341,17 +343,26 @@ public double? EndLongitude
[JsonProperty("map")]
public Map Map { get; set; }

private AthleteMeta _AthleteMeta;
/// <summary>
/// Meta object of the athlete of this activity.
/// </summary>
[JsonProperty("athlete")]
public AthleteMeta AthleteMeta { get; set; }
public AthleteMeta AthleteMeta
{
get { return _AthleteMeta; }
set
{
_AthleteMeta = value;
ConsolidateWithCache(value);
}
}
}

/// <summary>
/// Separated added fields from original response class!
/// </summary>
public partial class ActivitySummary
public partial class ActivitySummary : IKey
{
private AthleteSummary _athlete;
public AthleteSummary Athlete
Expand All @@ -374,8 +385,8 @@ public List<Photo> AllPhotos
set { Set(() => AllPhotos, ref _allPhotos, value); }
}

private List<Athlete> _kudos;
public List<Athlete> Kudos
private List<AthleteSummary> _kudos;
public List<AthleteSummary> Kudos
{
get { return _kudos; }
set { Set(() => Kudos, ref _kudos, value); }
Expand Down Expand Up @@ -407,24 +418,20 @@ public string TypeImage
case ActivityType.EBikeRide:
case ActivityType.VirtualRide:
_typeImage = "";
//_typeImage = "M5,20.5A3.5,3.5 0 0,1 1.5,17A3.5,3.5 0 0,1 5,13.5A3.5,3.5 0 0,1 8.5,17A3.5,3.5 0 0,1 5,20.5M5,12A5,5 0 0,0 0,17A5,5 0 0,0 5,22A5,5 0 0,0 10,17A5,5 0 0,0 5,12M14.8,10H19V8.2H15.8L13.86,4.93C13.57,4.43 13,4.1 12.4,4.1C11.93,4.1 11.5,4.29 11.2,4.6L7.5,8.29C7.19,8.6 7,9 7,9.5C7,10.13 7.33,10.66 7.85,10.97L11.2,13V18H13V11.5L10.75,9.85L13.07,7.5M19,20.5A3.5,3.5 0 0,1 15.5,17A3.5,3.5 0 0,1 19,13.5A3.5,3.5 0 0,1 22.5,17A3.5,3.5 0 0,1 19,20.5M19,12A5,5 0 0,0 14,17A5,5 0 0,0 19,22A5,5 0 0,0 24,17A5,5 0 0,0 19,12M16,4.8C17,4.8 17.8,4 17.8,3C17.8,2 17,1.2 16,1.2C15,1.2 14.2,2 14.2,3C14.2,4 15,4.8 16,4.8Z";
break;
case ActivityType.Walk:
_typeImage = "";
break;
case ActivityType.Run:
//_typeImage = "M17.12,10L16.04,8.18L15.31,11.05L17.8,15.59V22H16V17L13.67,13.89L12.07,18.4L7.25,20.5L6.2,19L10.39,16.53L12.91,6.67L10.8,7.33V11H9V5.8L14.42,4.11L14.92,4.03C15.54,4.03 16.08,4.37 16.38,4.87L18.38,8.2H22V10H17.12M17,3.8C16,3.8 15.2,3 15.2,2C15.2,1 16,0.2 17,0.2C18,0.2 18.8,1 18.8,2C18.8,3 18,3.8 17,3.8M7,9V11H4A1,1 0 0,1 3,10A1,1 0 0,1 4,9H7M9.25,13L8.75,15H5A1,1 0 0,1 4,14A1,1 0 0,1 5,13H9.25M7,5V7H3A1,1 0 0,1 2,6A1,1 0 0,1 3,5H7Z";
_typeImage = "";
break;
case ActivityType.AlpineSki:
case ActivityType.BackcountrySki:
case ActivityType.NordicSki:
case ActivityType.Snowboard:
//_typeImage = "M6,14A1,1 0 0,1 7,15A1,1 0 0,1 6,16A5,5 0 0,1 1,11A5,5 0 0,1 6,6C7,3.65 9.3,2 12,2C15.43,2 18.24,4.66 18.5,8.03L19,8A4,4 0 0,1 23,12A4,4 0 0,1 19,16H18A1,1 0 0,1 17,15A1,1 0 0,1 18,14H19A2,2 0 0,0 21,12A2,2 0 0,0 19,10H17V9A5,5 0 0,0 12,4C9.5,4 7.45,5.82 7.06,8.19C6.73,8.07 6.37,8 6,8A3,3 0 0,0 3,11A3,3 0 0,0 6,14M7.88,18.07L10.07,17.5L8.46,15.88C8.07,15.5 8.07,14.86 8.46,14.46C8.85,14.07 9.5,14.07 9.88,14.46L11.5,16.07L12.07,13.88C12.21,13.34 12.76,13.03 13.29,13.17C13.83,13.31 14.14,13.86 14,14.4L13.41,16.59L15.6,16C16.14,15.86 16.69,16.17 16.83,16.71C16.97,17.24 16.66,17.79 16.12,17.93L13.93,18.5L15.54,20.12C15.93,20.5 15.93,21.15 15.54,21.54C15.15,21.93 14.5,21.93 14.12,21.54L12.5,19.93L11.93,22.12C11.79,22.66 11.24,22.97 10.71,22.83C10.17,22.69 9.86,22.14 10,21.6L10.59,19.41L8.4,20C7.86,20.14 7.31,19.83 7.17,19.29C7.03,18.76 7.34,18.21 7.88,18.07Z";
_typeImage = "";
break;
default:
//_typeImage = "M14,6L10.25,11L13.1,14.8L11.5,16C9.81,13.75 7,10 7,10L1,18H23L14,6Z";
_typeImage = "";
break;
}
Expand Down Expand Up @@ -540,5 +547,16 @@ public string ElevationGainFormatted
/// AthleteCount = Athlete + other
/// </summary>
public int OtherAthleteCount => AthleteCount -1;

string IKey.Key => Id.ToString();

private void ConsolidateWithCache(AthleteMeta meta)
{
// TODO refactor servicelocator out of model
IStravaService stravaService = ServiceLocator.Current.GetInstance<IStravaService>();

AthleteSummary summary = stravaService.ConsolidateWithCache(meta);
Athlete = summary;
}
}
}
4 changes: 3 additions & 1 deletion src/Kliva/Models/AthleteMeta.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Newtonsoft.Json;
using Kliva.Models.Converters;
using Newtonsoft.Json;

namespace Kliva.Models
{
/// <summary>
/// This class represents an athlete.
/// </summary>
[JsonConverter(typeof(AthleteJsonConverter))]
public class AthleteMeta : BaseClass
{
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Kliva/Models/Comment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class Comment
/// The athlete who wrote the comment.
/// </summary>
[JsonProperty("athlete")]
public Athlete Athlete { get; set; }
public AthleteSummary Athlete { get; set; }

/// <summary>
/// The time when the comment was crated.
Expand Down
52 changes: 52 additions & 0 deletions src/Kliva/Models/Converters/AthleteJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Kliva.Models.Converters
{
/// <summary>
/// This will look at the fields of the athlete and will create an object based on those fields
/// </summary>
public class AthleteJsonConverter : JsonConverter
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
AthleteMeta athleteMeta;
// Load JObject from stream
JObject jObject = JObject.Load(reader);

if (FieldExists("athlete_type", jObject))
{
athleteMeta = new Athlete();
}
else if (FieldExists("firstname", jObject))
{
athleteMeta = new AthleteSummary();
}
else
{
athleteMeta = new AthleteMeta();
}

// Populate the object properties
serializer.Populate(jObject.CreateReader(), athleteMeta);

return athleteMeta;
}

private bool FieldExists(string fieldName, JObject jObject)
{
return jObject[fieldName] != null;
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value);
}

public override bool CanConvert(Type objectType)
{
return (objectType == typeof(AthleteMeta));
}
}
}
3 changes: 2 additions & 1 deletion src/Kliva/Models/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ public enum ActivityFeedFilter
{
All,
My,
Followers
Followers,
Friends // Following
}

public enum AppTarget
Expand Down
31 changes: 31 additions & 0 deletions src/Kliva/Models/Incremental/ActivityIncrementalCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Kliva.Services.Interfaces;
using Kliva.Helpers;

namespace Kliva.Models
{
public abstract class ActivityIncrementalCollection : CachedKeyedIncrementalLoadingBase
{
private readonly IStravaService _stravaService;

protected ActivityIncrementalCollection(IStravaService stravaService, ActivityFeedFilter cachename)
: base(cachename)
{
_stravaService = stravaService;
}

protected override async Task<List<object>> HydrateItems(string data)
{
var results = await _stravaService.HydrateActivityData(data);
//foreach( var item in from r in results where r.PhotoCount > 0 select r) // TODO review

This comment has been minimized.

Copy link
@samsp

samsp Mar 8, 2016

Can be removed, I was playing with photos, to see which activities had them, but ran into the same issue with photos not being accessible anyway.

// {

// }
return results.Cast<object>().ToList();
}
}
}
Loading

1 comment on commit 78afa2f

@samsp
Copy link

@samsp samsp commented on 78afa2f Mar 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm not too familiar with the IoC pattern you are using for services, but this seems like a more consistent version of what I was trying to achieve.

Please sign in to comment.