Skip to content

Commit

Permalink
Optimized first load of the app.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Holovsky committed May 14, 2017
1 parent 2580a38 commit 814e0a0
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 58 deletions.
2 changes: 1 addition & 1 deletion ParkAndRidePrague.Core/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="10.0.2" targetFramework="portable46-net451+win81" />
<package id="System.Net.Http" version="4.3.1" targetFramework="portable46-net451+win81" />
<package id="System.Net.Http" version="4.3.2" targetFramework="portable46-net451+win81" />
</packages>
2 changes: 1 addition & 1 deletion ParkAndRidePrague.UWP/Package.appxmanifest
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" IgnorableNamespaces="uap mp">
<Identity Name="18928JakubHolovsky.PraguePR" Publisher="CN=C19F1B7B-E11D-48DB-941A-BB32098921C0" Version="1.1.2.0" />
<Identity Name="18928JakubHolovsky.PraguePR" Publisher="CN=C19F1B7B-E11D-48DB-941A-BB32098921C0" Version="1.1.3.0" />
<mp:PhoneIdentity PhoneProductId="f70bb7c2-c108-4f1e-a8dd-c8348b51ddc7" PhonePublisherId="00000000-0000-0000-0000-000000000000" />
<Properties>
<DisplayName>Prague P+R</DisplayName>
Expand Down
Binary file modified ParkAndRidePrague.UWP/_language-cs.appx
Binary file not shown.
2 changes: 1 addition & 1 deletion ParkAndRidePrague.UWP/_pkginfo.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
E:\Dev\JakubHolovskyGit\ParkAndRidePrague\ParkAndRidePrague.UWP\bin\ARM\Release\Upload\ParkAndRidePrague.UWP_1.1.2.0\ParkAndRidePrague.UWP_1.1.2.0_x86_x64_arm.appxbundle
E:\Dev\JakubHolovskyGit\ParkAndRidePrague\ParkAndRidePrague.UWP\bin\ARM\Release\Upload\ParkAndRidePrague.UWP_1.1.3.0\ParkAndRidePrague.UWP_1.1.3.0_x86_x64_arm.appxbundle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.parkandrideprague.android" android:installLocation="auto" android:versionCode="2" android:versionName="1.0">
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.parkandrideprague.android" android:installLocation="auto" android:versionCode="3" android:versionName="1.0">
<uses-sdk android:minSdkVersion="16" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Expand Down
9 changes: 9 additions & 0 deletions ParkAndRidePrague/ParkAndRidePrague/AppResources.Designer.cs

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

3 changes: 3 additions & 0 deletions ParkAndRidePrague/ParkAndRidePrague/AppResources.cs.resx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@
<data name="sinceLastUpdate" xml:space="preserve">
<value>Od poslední aktualizace</value>
</data>
<data name="sortingByLocation" xml:space="preserve">
<value>řadím dle polohy</value>
</data>
<data name="takenPlaces" xml:space="preserve">
<value>Obsazená místa</value>
</data>
Expand Down
3 changes: 3 additions & 0 deletions ParkAndRidePrague/ParkAndRidePrague/AppResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@
<data name="sinceLastUpdate" xml:space="preserve">
<value>Since Last Update</value>
</data>
<data name="sortingByLocation" xml:space="preserve">
<value>sorting by location</value>
</data>
<data name="takenPlaces" xml:space="preserve">
<value>Taken Places</value>
</data>
Expand Down
136 changes: 82 additions & 54 deletions ParkAndRidePrague/ParkAndRidePrague/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -73,76 +74,103 @@ private async void ListViewParkingsOnRefreshing(object sender, EventArgs eventAr

private async Task RefreshParkings(bool displayLoading)
{
UpdateStatus(AppResources.updating);
if (displayLoading)
UpdateLoading(true);
UpdateStatus(AppResources.updating);
if (displayLoading)
UpdateLoading(true);

var hasInternetAccess = await NetworkHelper.HasInternetAccess();
if (!hasInternetAccess)
{
UpdateStatus(AppResources.noNetwork);
var hasInternetAccess = await NetworkHelper.HasInternetAccess();
if (!hasInternetAccess)
{
UpdateStatus(AppResources.noNetwork);

if (displayLoading)
UpdateLoading(false);
if (displayLoading)
UpdateLoading(false);

return;
}
return;
}

var apiResult = await parkingApi.GetParkings();
var apiResult = await parkingApi.GetParkings();

if (apiResult.Error)
{
UpdateStatus(AppResources.cannotUpdateParkings);
if (apiResult.Error)
{
UpdateStatus(AppResources.cannotUpdateParkings);

if (displayLoading)
UpdateLoading(false);
if (displayLoading)
UpdateLoading(false);

return;
}
return;
}

foreach (var observableParking in parkings)
{
var parking = apiResult.Result.SingleOrDefault(p => p.Id == observableParking.Id);
if (parking != null)
{
if (parking.LastUpdateDate == observableParking.LastUpdateDate)
parking.PreviousFreePlacesCount = observableParking.PreviousFreePlacesCount;
else
parking.PreviousFreePlacesCount = observableParking.FreePlacesCount;
}
}
((App)Application.Current).InvokeParkingsRefreshed(apiResult);
var refreshedParkings = apiResult.Result;

try
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled)
{
var position = await CrossGeolocator.Current.GetPositionAsync();
refreshedParkings =
refreshedParkings.OrderBy(p => p.GetDistance(position.Latitude, position.Longitude))
.ToList();
}
}
catch (Exception exception)
{
logger.Log(exception);
}

foreach (var observableParking in parkings)
{
var parking = apiResult.Result.SingleOrDefault(p => p.Id == observableParking.Id);
if (parking != null)
{
if (parking.LastUpdateDate == observableParking.LastUpdateDate)
parking.PreviousFreePlacesCount = observableParking.PreviousFreePlacesCount;
else
parking.PreviousFreePlacesCount = observableParking.FreePlacesCount;
}
}
((App)Application.Current).InvokeParkingsRefreshed(apiResult);
var refreshedParkings = apiResult.Result;

if (parkings.Count == 0)
{
// If we came to the app for the first time, show parkings then do sorting.
RefreshParkings(refreshedParkings);
refreshedParkings = await SortParkings(refreshedParkings, true);
RefreshParkings(refreshedParkings);
}
else
{
refreshedParkings = await SortParkings(refreshedParkings, false);
RefreshParkings(refreshedParkings);
}

if (displayLoading)
UpdateLoading(false);
UpdateStatus($"{AppResources.updatedAt.ToLower()} {apiResult.UpdatedAt:HH:mm:ss}");
}

private void RefreshParkings(List<IParking> refreshedParkings)
{
parkings.Clear();
foreach (var parking in refreshedParkings)
{
parkings.Add(parking);
}
}

private async Task<List<IParking>> SortParkings(List<IParking> refreshedParkings, bool displayMessage)
{
try
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled)
{
if (displayMessage)
UpdateStatus(AppResources.sortingByLocation);
var position = await CrossGeolocator.Current.GetPositionAsync();
refreshedParkings =
refreshedParkings.OrderBy(p => p.GetDistance(position.Latitude, position.Longitude))
.ToList();
}
else
{
refreshedParkings = refreshedParkings.OrderBy(p => p.Name).ToList();
}
}
catch (Exception exception)
{
logger.Log(exception);
}

if (displayLoading)
UpdateLoading(false);
UpdateStatus($"{AppResources.updatedAt.ToLower()} {apiResult.UpdatedAt.ToString("HH:mm:ss")}");
return refreshedParkings;
}

private void UpdateLoading(bool showLoading)
private void UpdateLoading(bool showLoading)
{
Device.BeginInvokeOnMainThread(() =>
{
Expand Down

0 comments on commit 814e0a0

Please sign in to comment.