Skip to content

Commit

Permalink
Merge pull request #6 from domagojmedo/master
Browse files Browse the repository at this point in the history
Small improvements
  • Loading branch information
gabrielfreire committed May 5, 2020
2 parents a689afb + 79fe25a commit 4364baa
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 104 deletions.
7 changes: 2 additions & 5 deletions IMSCovidTracker.Android/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;


using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Runtime;

namespace IMSCovidTracker.Droid
{
Expand Down
Binary file modified IMSCovidTracker.Android/Resources/drawable/confirmed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 8 additions & 12 deletions IMSCovidTracker/Components/CovidInformationCard.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


using Xamarin.Forms;
using Xamarin.Forms.Xaml;

Expand All @@ -12,24 +7,25 @@ namespace IMSCovidTracker.Components
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CovidInformationCard : ContentView
{
public static readonly BindableProperty CountryNameProperty = BindableProperty.Create(nameof(CountryName), typeof(string), typeof(string), default(string));

public static readonly BindableProperty CountryNameProperty = BindableProperty.Create(nameof(CountryName), typeof(string), typeof(CovidInformationCard), default(string));
public string CountryName { get => (string)GetValue(CountryNameProperty); set => SetValue(CountryNameProperty, value); }

public static readonly BindableProperty CountryFlagProperty = BindableProperty.Create(nameof(CountryFlag), typeof(string), typeof(string), default(string));
public static readonly BindableProperty CountryFlagProperty = BindableProperty.Create(nameof(CountryFlag), typeof(string), typeof(CovidInformationCard), default(string));
public string CountryFlag { get => (string)GetValue(CountryFlagProperty); set => SetValue(CountryFlagProperty, value); }


public static readonly BindableProperty ConfirmedProperty = BindableProperty.Create(nameof(Confirmed), typeof(int), typeof(int), default(int));
public static readonly BindableProperty ConfirmedProperty = BindableProperty.Create(nameof(Confirmed), typeof(int), typeof(CovidInformationCard), default(int));
public int Confirmed { get => (int)GetValue(ConfirmedProperty); set => SetValue(ConfirmedProperty, value); }


public static readonly BindableProperty DeathsProperty = BindableProperty.Create(nameof(Deaths), typeof(int), typeof(int), default(int));
public static readonly BindableProperty DeathsProperty = BindableProperty.Create(nameof(Deaths), typeof(int), typeof(CovidInformationCard), default(int));
public int Deaths { get => (int)GetValue(DeathsProperty); set => SetValue(DeathsProperty, value); }

public static readonly BindableProperty RecoveredProperty = BindableProperty.Create(nameof(Recovered), typeof(int), typeof(int), default(int));
public static readonly BindableProperty RecoveredProperty = BindableProperty.Create(nameof(Recovered), typeof(int), typeof(CovidInformationCard), default(int));
public int Recovered { get => (int)GetValue(RecoveredProperty); set => SetValue(RecoveredProperty, value); }

public static readonly BindableProperty SmallProperty = BindableProperty.Create(nameof(Small), typeof(bool), typeof(bool), false);
public static readonly BindableProperty SmallProperty = BindableProperty.Create(nameof(Small), typeof(bool), typeof(CovidInformationCard), false);
public bool Small { get => (bool)GetValue(SmallProperty); set => SetValue(SmallProperty, value); }

public CovidInformationCard()
Expand Down
28 changes: 10 additions & 18 deletions IMSCovidTracker/ViewModels/HomeViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using IMSCovidTracker.Models;
using IMSCovidTracker.Views;
using System;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using IMSCovidTracker.Models;
using IMSCovidTracker.Views;
using Xamarin.Essentials;
using Xamarin.Forms;

Expand All @@ -17,17 +17,14 @@ public class HomeViewModel : BaseViewModel
#region Private members
private CovidLocation _totalCases;
private CovidLocation _resultCountry;
private ObservableCollection<CovidLocation> _countryWidgets = new ObservableCollection<CovidLocation>();
private string _searchQuery;
private bool _searchSuccess = false;
private int _widgetsCount => CountryWidgets.Count;
#endregion

#region Public members
public event EventHandler<int> WidgetCollectionChanged;

public bool SearchSuccess { get => _searchSuccess; set => RaiseIfPropertyChanged(ref _searchSuccess, value); }
public ObservableCollection<CovidLocation> CountryWidgets { get => _countryWidgets; set => RaiseIfPropertyChanged(ref _countryWidgets, value); }
public ObservableCollection<CovidLocation> CountryWidgets { get; } = new ObservableCollection<CovidLocation>();
public string SearchQuery { get => _searchQuery; set => RaiseIfPropertyChanged(ref _searchQuery, value); }
public CovidLocation TotalCases { get => _totalCases; set => RaiseIfPropertyChanged(ref _totalCases, value); }

Expand Down Expand Up @@ -56,7 +53,7 @@ private async Task RefreshData()
await LoadCovidData(true);
}

public async Task LoadCovidData(bool isRefresh=false)
public async Task LoadCovidData(bool isRefresh = false)
{
try
{
Expand Down Expand Up @@ -98,7 +95,7 @@ public async Task LoadCovidData(bool isRefresh=false)

public async Task LoadDefaultWidgets()
{
ObservableCollection<CovidLocation> _storedWidgets = new ObservableCollection< CovidLocation>();
ObservableCollection<CovidLocation> _storedWidgets = new ObservableCollection<CovidLocation>();
IEnumerable<string> _storedCountries = new List<string>();
try
{
Expand All @@ -111,7 +108,7 @@ public async Task LoadDefaultWidgets()
}

_storedCountries = await App.StorageService.GetWidgetPreferences();

if (_storedCountries != null)
{
foreach (var country in _storedCountries)
Expand Down Expand Up @@ -148,16 +145,13 @@ public async Task LoadDefaultWidgets()
_storedWidgets.Add(App.CovidService.Find("Italy"));
_storedWidgets.Add(App.CovidService.Find("Romania"));
_storedWidgets.Add(App.CovidService.Find("united states"));
CountryWidgets = _storedWidgets;
}
else
foreach (var item in _storedWidgets)
{
// config was found, we add it
CountryWidgets = _storedWidgets;
CountryWidgets.Add(item);
}
WidgetCollectionChanged?.Invoke(this, 0);
IsBusy = false;
});
}
Expand Down Expand Up @@ -198,7 +192,6 @@ private void AddCountryWidget(SearchModalViewModel sender, string countryName)
CountryWidgets.Add(_country);
await App.StorageService.StoreWidgetPreferences(CountryWidgets.Select(c => c.Country).ToList());
WidgetCollectionChanged?.Invoke(this, 200);
});
}

Expand All @@ -212,7 +205,6 @@ private void DeleteWidget(CovidLocation cLoc)

_ = App.StorageService.StoreWidgetPreferences(CountryWidgets.Select(c => c.Country).ToList());

WidgetCollectionChanged?.Invoke(this, 395);

}
catch (Exception ex)
Expand Down
43 changes: 26 additions & 17 deletions IMSCovidTracker/Views/HomePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
<converters:BoolInvertConverter x:Key="BoolInvert"/>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.BindingContext>
<vm:HomeViewModel />
</ContentPage.BindingContext>
<ContentPage.Content>
<AbsoluteLayout x:Name="homeLayout">

<ScrollView Padding="2" IsVisible="{Binding Source={x:Reference HomePageView}, Path=BindingContext.IsBusy, Converter={StaticResource BoolInvert}}"
<ScrollView Padding="2" IsVisible="{Binding IsBusy, Converter={StaticResource BoolInvert}}"
AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All">
<Frame Padding="5" BackgroundColor="#ddd" HasShadow="{OnPlatform iOS=false, Default=true}">
<Grid >
Expand All @@ -38,62 +41,69 @@
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Source={x:Reference HomePageView}, Path=BindingContext.RefreshDataCommand}"/>
<TapGestureRecognizer Command="{Binding RefreshDataCommand}"/>
</Grid.GestureRecognizers>
<Image Grid.Column="1" Source="refresh.png" Aspect="AspectFit"/>
</Grid>

<components:LargeCovidInformationCard
Grid.Row="1" CountryName="Total Cases"
Country="{Binding Source={x:Reference HomePageView}, Path=BindingContext.TotalCases}"/>
Country="{Binding TotalCases}"/>

<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="30"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" FontSize="14" FontAttributes="Bold" TextColor="Gray"
Text="{Binding Source={x:Reference HomePageView}, Path=BindingContext.CountryWidgets.Count, StringFormat='{0} of 6 widgets'}"/>
Text="{Binding CountryWidgets.Count, StringFormat='{0} of 6 widgets'}"/>
<Image Grid.Column="1" Source="plus.png" Aspect="AspectFit" BackgroundColor="Transparent">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Source={x:Reference HomePageView}, Path=BindingContext.AddWidgetCommand}"/>
<TapGestureRecognizer Command="{Binding AddWidgetCommand}"/>
</Image.GestureRecognizers>
</Image>
</Grid>

<CollectionView Grid.Row="3" BackgroundColor="Transparent" x:Name="WidgetCollection" x:FieldModifier="public" VerticalScrollBarVisibility="Never"
IsVisible="{Binding Source={x:Reference HomePageView}, Path=BindingContext.IsBusy, Converter={StaticResource BoolInvert}}"
ItemsSource="{Binding Source={x:Reference HomePageView}, Path=BindingContext.CountryWidgets}">
<CollectionView Grid.Row="3"
BackgroundColor="Transparent"
x:Name="WidgetCollection"
x:FieldModifier="public"
VerticalScrollBarVisibility="Never"
IsVisible="{Binding IsBusy, Converter={StaticResource BoolInvert}}"
ItemsSource="{Binding CountryWidgets}"
ItemSizingStrategy="MeasureFirstItem">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical" HorizontalItemSpacing="10" VerticalItemSpacing="10" Span="2"/>
<GridItemsLayout Orientation="Vertical" Span="2"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<SwipeView BackgroundColor="Transparent">
<ContentView>
<SwipeView Margin="5">
<SwipeView.LeftItems>
<SwipeItems Mode="Execute">
<SwipeItem Text="Delete"
Command="{Binding Source={x:Reference HomePageView}, Path=BindingContext.DeleteWidgetCommand}"
Command="{Binding Source={x:Reference WidgetCollection}, Path=BindingContext.DeleteWidgetCommand}"
CommandParameter="{Binding .}"
BackgroundColor="Red"/>
</SwipeItems>
</SwipeView.LeftItems>
<SwipeView.RightItems>
<SwipeItems Mode="Execute">
<SwipeItem Text="View"
Command="{Binding Source={x:Reference HomePageView}, Path=BindingContext.ViewWidgetCommand}"
Command="{Binding Source={x:Reference WidgetCollection}, Path=BindingContext.ViewWidgetCommand}"
CommandParameter="{Binding .}"
BackgroundColor="Green"/>
</SwipeItems>
</SwipeView.RightItems>
<components:CovidInformationCard MinimumHeightRequest="100"
<components:CovidInformationCard
CountryName="{Binding Country}"
Small="True"
CountryFlag="{Binding FlagImageUrl}"
Confirmed="{Binding Confirmed}"
Deaths="{Binding Deaths}"
Recovered="{Binding Recovered}"/>
</SwipeView>
</ContentView>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
Expand All @@ -103,12 +113,11 @@

<ActivityIndicator
AbsoluteLayout.LayoutBounds=".5, .5, 1, 1" AbsoluteLayout.LayoutFlags="All"
IsRunning="{Binding Source={x:Reference HomePageView}, Path=BindingContext.IsBusy}"
IsVisible="{Binding Source={x:Reference HomePageView}, Path=BindingContext.IsBusy}"
IsRunning="{Binding IsBusy}"
IsVisible="{Binding IsBusy}"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"
Color="Black"/>

Color="Black"/>

</AbsoluteLayout>
</ContentPage.Content>
Expand Down
53 changes: 1 addition & 52 deletions IMSCovidTracker/Views/HomePage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
using IMSCovidTracker.ViewModels;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

Expand All @@ -14,67 +7,23 @@ namespace IMSCovidTracker.Views
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class HomePage : ContentPage
{
private HomeViewModel _viewModel;

public HomePage()
{
InitializeComponent();

BindingContext = _viewModel = new HomeViewModel();

// where to display tutorial
navbarComponent.AbsoluteLayoutElement = homeLayout;

_viewModel.WidgetCollectionChanged += _viewModel_WidgetCollectionChanged;

}

private void _viewModel_WidgetCollectionChanged(object sender, int delay)
{
_ = ForceCollectionLayout(delay);
}

protected override void OnAppearing()
{
base.OnAppearing();

// CollectionView in iOS is a bit buggy but forcing layout after rendering seems to solve some issues
//if (Device.RuntimePlatform == Device.iOS)
ForceLayout();

// show only in first launch
if (VersionTracking.IsFirstLaunchEver)
{
_ = navbarComponent.ShowTutorial();
}
}

/// <summary>
/// <i>CollectionLayout is a bit buggy when adding/removing items so we must force some kind of re-layout</i> <br/>
/// Force layout for collectionview
/// </summary>
/// <returns></returns>
private async Task ForceCollectionLayout(int delay = 400)
{


await Device.InvokeOnMainThreadAsync(async () =>
{
WidgetCollection.HeightRequest = 3 * 155;
if (Device.RuntimePlatform == Device.iOS)
{
ForceLayout();
return;
}
await Task.Delay(delay);
WidgetCollection.ItemsSource = null;
WidgetCollection.ItemsSource = _viewModel.CountryWidgets;
});

}
}
}

0 comments on commit 4364baa

Please sign in to comment.