Change Settings and About pages

Added Media tab in About
Language and notifications settings
This commit is contained in:
2019-03-16 22:11:47 +01:00
parent 9af2bf392d
commit 7e7109d609
22 changed files with 981 additions and 394 deletions

View File

@@ -6,6 +6,7 @@ using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Acr.UserDialogs;
namespace PlantBox.Client.Droid
{
@@ -20,6 +21,9 @@ namespace PlantBox.Client.Droid
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
// Acr
UserDialogs.Init(this);
}
}
}

View File

@@ -53,6 +53,9 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Acr.UserDialogs">
<Version>7.0.4</Version>
</PackageReference>
<PackageReference Include="Xamarin.Forms" Version="3.4.0.1008975" />
<PackageReference Include="Xamarin.Android.Support.Design" Version="27.0.2.1" />
<PackageReference Include="Xamarin.Android.Support.v7.AppCompat" Version="27.0.2.1" />

File diff suppressed because it is too large Load Diff

View File

@@ -189,6 +189,9 @@
</Page>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Acr.UserDialogs">
<Version>7.0.4</Version>
</PackageReference>
<PackageReference Include="Xamarin.Forms" Version="3.4.0.1008975" />
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform" Version="6.1.5" />
</ItemGroup>

View File

@@ -148,6 +148,9 @@
<Reference Include="Xamarin.iOS" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Acr.UserDialogs">
<Version>7.0.4</Version>
</PackageReference>
<PackageReference Include="Xamarin.Forms" Version="3.4.0.1008975" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.iOS.CSharp.targets" />

View File

@@ -1,8 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewmodels="clr-namespace:PlantBox.Client.ViewModels"
x:Class="PlantBox.Client.App">
<Application.Resources>
<viewmodels:SettingsViewModel x:Key="Settings" />
<Style TargetType="ListView" >
<Setter Property="Margin" Value="{OnPlatform UWP='10, 2'}" />
</Style>
<Style TargetType="TableView" >
<Setter Property="Margin" Value="{OnPlatform UWP='10, 0'}" />
</Style>
</Application.Resources>
</Application>

View File

@@ -1,4 +1,6 @@
using PlantBox.Client.Forms;
using PlantBox.Client.Resources;
using PlantBox.Client.ViewModels;
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
@@ -8,10 +10,20 @@ namespace PlantBox.Client
{
public partial class App : Application
{
public static SettingsViewModel Settings { get; private set; }
public App()
{
InitializeComponent();
// Load settings
Settings = (SettingsViewModel)Resources["Settings"];
if (Settings.Language != null)
{
Locale.Culture = Settings.Language;
}
MainPage = new MainPage();
}
@@ -23,6 +35,7 @@ namespace PlantBox.Client
protected override void OnSleep()
{
// Handle when your app sleeps
Settings.Save();
}
protected override void OnResume()

View File

@@ -0,0 +1,29 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace PlantBox.Client.Converters
{
class JsonCultureInfoConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(CultureInfo) ? true : false;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var c = new CultureInfo((string)reader.Value);
return c;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var culture = (CultureInfo)value;
writer.WriteValue(culture.Name);
}
}
}

View File

@@ -22,7 +22,7 @@ namespace PlantBox.Client.Extensions
public object ProvideValue(IServiceProvider serviceProvider)
{
return _resourceManager.GetString(Name) ?? $"#{Name}";
return _resourceManager.GetString(Name, Locale.Culture) ?? $"#{Name}";
}
}
}

View File

@@ -3,12 +3,9 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:extensions="clr-namespace:PlantBox.Client.Extensions"
xmlns:models="clr-namespace:PlantBox.Client.Models"
x:Class="PlantBox.Client.Forms.AboutPage"
Padding="{OnPlatform UWP=10}">
x:Class="PlantBox.Client.Forms.AboutPage">
<ContentPage.Content>
<ListView x:Name="listView"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
SelectionMode="None"
IsGroupingEnabled="True"
GroupDisplayBinding="{Binding Title}"

View File

@@ -28,7 +28,13 @@ namespace PlantBox.Client.Forms
},
new AboutPageItemGroup(Locale.Libraries)
{
new AboutPageItem("Microcharts", "https://github.com/aloisdeniel/Microcharts", true)
new AboutPageItem("Microcharts", "https://github.com/aloisdeniel/Microcharts", true),
new AboutPageItem("Newtonsoft.Json", "https://github.com/JamesNK/Newtonsoft.Json", true)
},
new AboutPageItemGroup(Locale.Media)
{
new AboutPageItem("Android Material Icons", "https://material.io/tools/icons/", true),
new AboutPageItem("Android Asset Studio", "https://romannurik.github.io/AndroidAssetStudio/", true)
}
};
}

View File

@@ -16,17 +16,17 @@
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal"
HorizontalOptions="FillAndExpand"
VerticalOptions="CenterAndExpand"
Margin="15, 0, 0, 0">
HorizontalOptions="FillAndExpand"
VerticalOptions="CenterAndExpand"
Margin="15, 0, 0, 0">
<Image HeightRequest="32"
WidthRequest="32"
Source="{Binding Image}" />
WidthRequest="32"
Source="{Binding Image}" />
<Label HorizontalOptions="FillAndExpand"
VerticalOptions="CenterAndExpand"
Margin="5, 0, 0, 0"
Style="{DynamicResource SubtitleStyle}"
Text="{Binding Title}" />
VerticalOptions="CenterAndExpand"
Margin="5, 0, 0, 0"
Style="{DynamicResource SubtitleStyle}"
Text="{Binding Title}" />
</StackLayout>
</ViewCell>
</DataTemplate>

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="PlantBox.Client.Forms.Settings.LanguageSelectorPage">
<ContentPage.Content>
<ListView x:Name="listView"
SelectionMode="None"
ItemTapped="ListView_ItemTapped">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding DisplayName}"
Detail="{Binding Name}"
TextColor="Accent" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
</ContentPage>

View File

@@ -0,0 +1,46 @@
using Acr.UserDialogs;
using PlantBox.Client.Resources;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace PlantBox.Client.Forms.Settings
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LanguageSelectorPage : ContentPage
{
private CultureInfo[] _supportedCultures = new CultureInfo[]
{
new CultureInfo("en-US"),
new CultureInfo("fr-FR")
};
public LanguageSelectorPage()
{
InitializeComponent();
listView.ItemsSource = _supportedCultures;
}
private async void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
{
if (e.Item is CultureInfo culture)
{
App.Settings.Language = culture;
Locale.Culture = culture;
await App.Settings.SaveAsync();
(Application.Current as App).MainPage = new MainPage();
//await DisplayAlert(Locale.LanguageChangedTitle, Locale.LanguageChangedMessage, "Ok");
//await Navigation.PopAsync();
}
}
}
}

View File

@@ -1,12 +1,29 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="PlantBox.Client.Forms.Settings.SettingsPage">
xmlns:extensions="clr-namespace:PlantBox.Client.Extensions"
x:Class="PlantBox.Client.Forms.Settings.SettingsPage"
BindingContext="{StaticResource Settings}">
<ContentPage.Resources>
<Style TargetType="TextCell">
<Setter x:Name="TextCellStyle" Property="TextColor" Value="Green" />
</Style>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout>
<Label Text="Welcome to Xamarin.Forms!"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
<TableView x:Name="table" Intent="Settings">
<TableRoot>
<TableSection Title="{extensions:Locale General}">
<TextCell Text="{extensions:Locale LanguageSetting}"
Detail="{extensions:Locale LanguageSettingDetail}"
Tapped="Language_Tapped" />
</TableSection>
<TableSection Title="{extensions:Locale Notifications}">
<SwitchCell Text="{extensions:Locale NotificationsDisabled}"
On="{Binding NotificationsDisabled}" />
<SwitchCell Text="{extensions:Locale NotificationsMuted}"
On="{Binding NotificationsMuted}" />
</TableSection>
</TableRoot>
</TableView>
</ContentPage.Content>
</ContentPage>

View File

@@ -1,5 +1,7 @@
using System;
using PlantBox.Client.Resources;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -12,9 +14,38 @@ namespace PlantBox.Client.Forms.Settings
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SettingsPage : ContentPage
{
public SettingsPage ()
public SettingsPage()
{
InitializeComponent ();
InitializeComponent();
// Save on close
Disappearing += async (s, e) => await App.Settings.SaveAsync();
// Can't apply style to cells, so...
ApplyStyle();
}
}
private void ApplyStyle()
{
foreach (var cell in table.Root.SelectMany(x => x))
{
if (cell is TextCell textCell)
{
if (Device.RuntimePlatform == Device.UWP)
{
textCell.TextColor = Color.Accent;
}
}
else if (cell is SwitchCell switchCell)
{
switchCell.Tapped += (s, e) => (s as SwitchCell).On = !(s as SwitchCell).On;
}
}
}
private async void Language_Tapped(object sender, EventArgs e)
{
await Navigation.PushAsync(new LanguageSelectorPage());
}
}
}

View File

@@ -0,0 +1,18 @@
using Newtonsoft.Json;
using PlantBox.Client.Converters;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace PlantBox.Client.Models
{
class Settings
{
[JsonConverter(typeof(JsonCultureInfoConverter))]
public CultureInfo Language { get; set; }
public bool NotificationsDisabled { get; set; } = false;
public bool NotificationsMuted { get; set; } = false;
}
}

View File

@@ -27,6 +27,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Acr.UserDialogs" Version="7.0.4" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="Xamarin.Forms" Version="3.4.0.1008975" />
</ItemGroup>
@@ -47,6 +49,9 @@
<EmbeddedResource Update="Forms\MenuPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Forms\Settings\LanguageSelectorPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Forms\Settings\SettingsPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
@@ -57,10 +62,9 @@
</ItemGroup>
<ItemGroup>
<Folder Include="ViewModels\" />
</ItemGroup>
<ItemGroup>
<Compile Update="Forms\Settings\LanguageSelectorPage.xaml.cs">
<DependentUpon>LanguageSelectorPage.xaml</DependentUpon>
</Compile>
<Compile Update="Resources\Locale.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>

View File

@@ -78,6 +78,15 @@ namespace PlantBox.Client.Resources {
}
}
/// <summary>
/// Recherche une chaîne localisée semblable à General.
/// </summary>
internal static string General {
get {
return ResourceManager.GetString("General", resourceCulture);
}
}
/// <summary>
/// Recherche une chaîne localisée semblable à Home.
/// </summary>
@@ -96,6 +105,42 @@ namespace PlantBox.Client.Resources {
}
}
/// <summary>
/// Recherche une chaîne localisée semblable à Changes will take effect at next start.
/// </summary>
internal static string LanguageChangedMessage {
get {
return ResourceManager.GetString("LanguageChangedMessage", resourceCulture);
}
}
/// <summary>
/// Recherche une chaîne localisée semblable à Language changed.
/// </summary>
internal static string LanguageChangedTitle {
get {
return ResourceManager.GetString("LanguageChangedTitle", resourceCulture);
}
}
/// <summary>
/// Recherche une chaîne localisée semblable à Language.
/// </summary>
internal static string LanguageSetting {
get {
return ResourceManager.GetString("LanguageSetting", resourceCulture);
}
}
/// <summary>
/// Recherche une chaîne localisée semblable à Change the language.
/// </summary>
internal static string LanguageSettingDetail {
get {
return ResourceManager.GetString("LanguageSettingDetail", resourceCulture);
}
}
/// <summary>
/// Recherche une chaîne localisée semblable à Libraries.
/// </summary>
@@ -105,6 +150,42 @@ namespace PlantBox.Client.Resources {
}
}
/// <summary>
/// Recherche une chaîne localisée semblable à Media.
/// </summary>
internal static string Media {
get {
return ResourceManager.GetString("Media", resourceCulture);
}
}
/// <summary>
/// Recherche une chaîne localisée semblable à Notifications.
/// </summary>
internal static string Notifications {
get {
return ResourceManager.GetString("Notifications", resourceCulture);
}
}
/// <summary>
/// Recherche une chaîne localisée semblable à Desactivate notifications.
/// </summary>
internal static string NotificationsDisabled {
get {
return ResourceManager.GetString("NotificationsDisabled", resourceCulture);
}
}
/// <summary>
/// Recherche une chaîne localisée semblable à Mute notifications.
/// </summary>
internal static string NotificationsMuted {
get {
return ResourceManager.GetString("NotificationsMuted", resourceCulture);
}
}
/// <summary>
/// Recherche une chaîne localisée semblable à Settings.
/// </summary>

View File

@@ -123,17 +123,44 @@
<data name="Author" xml:space="preserve">
<value>Auteur</value>
</data>
<data name="General" xml:space="preserve">
<value>Général</value>
</data>
<data name="HomePageTitle" xml:space="preserve">
<value>Accueil</value>
</data>
<data name="Informations" xml:space="preserve">
<value>Informations</value>
</data>
<data name="LanguageChangedMessage" xml:space="preserve">
<value>Les changements prendront effet au prochain lancement</value>
</data>
<data name="LanguageChangedTitle" xml:space="preserve">
<value>Langue modifiée</value>
</data>
<data name="LanguageSetting" xml:space="preserve">
<value>Langue</value>
</data>
<data name="LanguageSettingDetail" xml:space="preserve">
<value>Modifie la langue</value>
</data>
<data name="Libraries" xml:space="preserve">
<value>Bibliothèques</value>
</data>
<data name="Media" xml:space="preserve">
<value>Media</value>
</data>
<data name="Notifications" xml:space="preserve">
<value>Notifications</value>
</data>
<data name="NotificationsDisabled" xml:space="preserve">
<value>Désactiver les notifications</value>
</data>
<data name="NotificationsMuted" xml:space="preserve">
<value>Rendre muet les notifications</value>
</data>
<data name="Settings" xml:space="preserve">
<value>Options</value>
<value>Préférences</value>
</data>
<data name="Version" xml:space="preserve">
<value>Version</value>

View File

@@ -123,15 +123,42 @@
<data name="Author" xml:space="preserve">
<value>Author</value>
</data>
<data name="General" xml:space="preserve">
<value>General</value>
</data>
<data name="HomePageTitle" xml:space="preserve">
<value>Home</value>
</data>
<data name="Informations" xml:space="preserve">
<value>Informations</value>
</data>
<data name="LanguageChangedMessage" xml:space="preserve">
<value>Changes will take effect at next start</value>
</data>
<data name="LanguageChangedTitle" xml:space="preserve">
<value>Language changed</value>
</data>
<data name="LanguageSetting" xml:space="preserve">
<value>Language</value>
</data>
<data name="LanguageSettingDetail" xml:space="preserve">
<value>Change the language</value>
</data>
<data name="Libraries" xml:space="preserve">
<value>Libraries</value>
</data>
<data name="Media" xml:space="preserve">
<value>Media</value>
</data>
<data name="Notifications" xml:space="preserve">
<value>Notifications</value>
</data>
<data name="NotificationsDisabled" xml:space="preserve">
<value>Desactivate notifications</value>
</data>
<data name="NotificationsMuted" xml:space="preserve">
<value>Mute notifications</value>
</data>
<data name="Settings" xml:space="preserve">
<value>Settings</value>
</data>

View File

@@ -0,0 +1,94 @@
using Newtonsoft.Json;
using PlantBox.Client.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace PlantBox.Client.ViewModels
{
public class SettingsViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public const string FileName = "Settings.json";
public string FilePath => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), FileName);
private Settings _settings;
// Settings
public CultureInfo Language
{
get => _settings.Language;
set
{
if (value != _settings.Language)
{
_settings.Language = value;
OnPropertyChanged();
}
}
}
public bool NotificationsDisabled
{
get => _settings.NotificationsDisabled;
set
{
if (value != _settings.NotificationsDisabled)
{
_settings.NotificationsDisabled = value;
OnPropertyChanged();
}
}
}
public bool NotificationsMuted
{
get => _settings.NotificationsMuted;
set
{
if (value != _settings.NotificationsMuted)
{
_settings.NotificationsMuted = value;
OnPropertyChanged();
}
}
}
public SettingsViewModel()
{
if (File.Exists(FilePath))
{
_settings = JsonConvert.DeserializeObject<Settings>(File.ReadAllText(FilePath));
}
else
{
_settings = new Settings();
}
}
public void Save()
{
using (var file = new StreamWriter(FilePath, false))
{
file.WriteLine(JsonConvert.SerializeObject(_settings));
}
}
public async Task SaveAsync()
{
using (var file = new StreamWriter(FilePath, false))
{
await file.WriteLineAsync(JsonConvert.SerializeObject(_settings));
}
}
public void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}