theming with resource dictionary

This commit is contained in:
Kyle Spearrin 2019-04-22 11:32:17 -04:00
parent a6cd16cdb5
commit b48c231500
19 changed files with 302 additions and 18 deletions

View file

@ -27,6 +27,9 @@
<Compile Update="Pages\GeneratorPage.xaml.cs"> <Compile Update="Pages\GeneratorPage.xaml.cs">
<DependentUpon>GeneratorPage.xaml</DependentUpon> <DependentUpon>GeneratorPage.xaml</DependentUpon>
</Compile> </Compile>
<Compile Update="Pages\ViewPage.xaml.cs">
<DependentUpon>ViewPage.xaml</DependentUpon>
</Compile>
<Compile Update="Pages\SettingsPage.xaml.cs"> <Compile Update="Pages\SettingsPage.xaml.cs">
<DependentUpon>SettingsPage.xaml</DependentUpon> <DependentUpon>SettingsPage.xaml</DependentUpon>
</Compile> </Compile>
@ -54,6 +57,18 @@
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>
<AutoGen>True</AutoGen> <AutoGen>True</AutoGen>
</Compile> </Compile>
<Compile Update="Styles\Light.xaml.cs">
<DependentUpon>Light.xaml</DependentUpon>
</Compile>
<Compile Update="Styles\Dark.xaml.cs">
<DependentUpon>Dark.xaml</DependentUpon>
</Compile>
<Compile Update="Styles\iOS.xaml.cs">
<DependentUpon>iOS.xaml</DependentUpon>
</Compile>
<Compile Update="Styles\Android.xaml.cs">
<DependentUpon>Android.xaml</DependentUpon>
</Compile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -61,6 +76,9 @@
<LastGenOutput>AppResources.Designer.cs</LastGenOutput> <LastGenOutput>AppResources.Designer.cs</LastGenOutput>
<Generator>ResXFileCodeGenerator</Generator> <Generator>ResXFileCodeGenerator</Generator>
</EmbeddedResource> </EmbeddedResource>
<EmbeddedResource Update="Styles\Base.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup> </ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent"> <Target Name="PreBuild" BeforeTargets="PreBuildEvent">

View file

@ -29,7 +29,8 @@ namespace Bit.App
InitializeComponent(); InitializeComponent();
SetCulture(); SetCulture();
ThemeManager.SetTheme("light"); // ThemeManager.SetTheme("light");
ThemeManager.SetThemeStyle("light");
MainPage = new HomePage(); MainPage = new HomePage();
var mainPageTask = SetMainPageAsync(); var mainPageTask = SetMainPageAsync();

View file

@ -6,15 +6,16 @@
<ViewCell.View> <ViewCell.View>
<StackLayout x:Name="_layout" <StackLayout x:Name="_layout"
Padding="10" x:DataType="controls:CipherViewCellViewModel"
x:DataType="controls:CipherViewCellViewModel"> StyleClass="list-row">
<StackLayout.BindingContext> <StackLayout.BindingContext>
<controls:CipherViewCellViewModel /> <controls:CipherViewCellViewModel />
</StackLayout.BindingContext> </StackLayout.BindingContext>
<Label Text="{Binding Cipher.Name}" <Label Text="{Binding Cipher.Name, Mode=OneWay}"
LineBreakMode="NoWrap" StyleClass="list-title" />
FontSize="16" /> <Label Text="{Binding Cipher.SubTitle, Mode=OneWay}"
StyleClass="list-subtitle" />
</StackLayout> </StackLayout>
</ViewCell.View> </ViewCell.View>

View file

@ -21,14 +21,17 @@
<DataTemplate x:Key="folderTemplate" <DataTemplate x:Key="folderTemplate"
x:DataType="pages:GroupingsPageListItem"> x:DataType="pages:GroupingsPageListItem">
<ViewCell> <ViewCell>
<StackLayout StyleClass="list-cell"> <StackLayout Orientation="Horizontal"
StyleClass="list-row">
<controls:FaLabel Text="&#xf07c;" <controls:FaLabel Text="&#xf07c;"
HorizontalOptions="Start" HorizontalOptions="Start"
VerticalOptions="CenterAndExpand" /> VerticalOptions="CenterAndExpand"
<Label Text="{Binding Folder.Name}" StyleClass="list-icon" />
<Label Text="{Binding Folder.Name, Mode=OneWay}"
LineBreakMode="TailTruncation" LineBreakMode="TailTruncation"
HorizontalOptions="StartAndExpand" HorizontalOptions="StartAndExpand"
VerticalOptions="CenterAndExpand" /> VerticalOptions="CenterAndExpand"
StyleClass="list-title"/>
</StackLayout> </StackLayout>
</ViewCell> </ViewCell>
</DataTemplate> </DataTemplate>
@ -36,14 +39,17 @@
<DataTemplate x:Key="collectionTemplate" <DataTemplate x:Key="collectionTemplate"
x:DataType="pages:GroupingsPageListItem"> x:DataType="pages:GroupingsPageListItem">
<ViewCell> <ViewCell>
<StackLayout StyleClass="list-cell"> <StackLayout Orientation="Horizontal"
StyleClass="list-row">
<controls:FaLabel Text="&#xf1b2;" <controls:FaLabel Text="&#xf1b2;"
HorizontalOptions="Start" HorizontalOptions="Start"
VerticalOptions="CenterAndExpand" /> VerticalOptions="CenterAndExpand"
<Label Text="{Binding Collection.Name}" StyleClass="list-icon" />
<Label Text="{Binding Collection.Name, Mode=OneWay}"
LineBreakMode="TailTruncation" LineBreakMode="TailTruncation"
HorizontalOptions="StartAndExpand" HorizontalOptions="StartAndExpand"
VerticalOptions="CenterAndExpand" /> VerticalOptions="CenterAndExpand"
StyleClass="list-title"/>
</StackLayout> </StackLayout>
</ViewCell> </ViewCell>
</DataTemplate> </DataTemplate>
@ -65,11 +71,15 @@
IsRefreshing="{Binding Loading, Mode=OneWay}" IsRefreshing="{Binding Loading, Mode=OneWay}"
CachingStrategy="RecycleElement" CachingStrategy="RecycleElement"
ItemTemplate="{StaticResource listItemDataTemplateSelector}" ItemTemplate="{StaticResource listItemDataTemplateSelector}"
IsGroupingEnabled="True"> IsGroupingEnabled="True"
StyleClass="list">
<ListView.GroupHeaderTemplate> <ListView.GroupHeaderTemplate>
<DataTemplate x:DataType="pages:GroupingsPageListGroup"> <DataTemplate x:DataType="pages:GroupingsPageListGroup">
<ViewCell> <ViewCell>
<Label Text="{Binding Name}" /> <StackLayout StyleClass="list-row-header">
<Label Text="{Binding Name}"
StyleClass="list-header, list-header-themed, list-header-platform" />
</StackLayout>
</ViewCell> </ViewCell>
</DataTemplate> </DataTemplate>
</ListView.GroupHeaderTemplate> </ListView.GroupHeaderTemplate>

View file

@ -0,0 +1,31 @@
<?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="Bit.App.Pages.ViewPage"
xmlns:pages="clr-namespace:Bit.App.Pages"
xmlns:i18n="clr-namespace:Bit.App.Utilities"
xmlns:bv="clr-namespace:Bit.App.Controls.BoxedView"
x:DataType="pages:ViewPageViewModel"
Title="{Binding PageTitle}">
<ContentPage.BindingContext>
<pages:ViewPageViewModel />
</ContentPage.BindingContext>
<ContentPage.ToolbarItems>
<ToolbarItem Icon="cogs.png" Text="{i18n:Translate Edit}" />
</ContentPage.ToolbarItems>
<bv:BoxedView HasUnevenRows="True">
<bv:BoxedSection HeaderHeight="0">
<bv:EntryCell Title="{i18n:Translate EmailAddress}"
ValueText="{Binding Email}" />
<bv:EntryCell Title="{i18n:Translate MasterPassword}"
ValueText="{Binding MasterPassword}"
IsPassword="True"
Button1Icon="cogs"
Button1Command="{Binding ShowPasswordCommand}" />
</bv:BoxedSection>
</bv:BoxedView>
</ContentPage>

View file

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Bit.App.Pages
{
public partial class ViewPage : ContentPage
{
private ViewPageViewModel _vm;
public ViewPage()
{
InitializeComponent();
_vm = BindingContext as ViewPageViewModel;
_vm.Page = this;
}
}
}

View file

@ -0,0 +1,33 @@
using Bit.App.Abstractions;
using Bit.App.Resources;
using Bit.Core.Abstractions;
using Bit.Core.Exceptions;
using Bit.Core.Utilities;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
namespace Bit.App.Pages
{
public class ViewPageViewModel : BaseViewModel
{
private readonly IDeviceActionService _deviceActionService;
private readonly IAuthService _authService;
private readonly ISyncService _syncService;
public ViewPageViewModel()
{
_deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService");
_authService = ServiceContainer.Resolve<IAuthService>("authService");
_syncService = ServiceContainer.Resolve<ISyncService>("syncService");
PageTitle = AppResources.Bitwarden;
ShowPasswordCommand = new Command(() =>
Page.DisplayAlert("Button 1 Command", "Button 1 message", "Cancel"));
}
public ICommand ShowPasswordCommand { get; }
public string Email { get; set; }
public string MasterPassword { get; set; }
}
}

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Bit.App.Styles.Android">
<Style TargetType="Label"
Class="list-header-platform">
</Style>
</ResourceDictionary>

View file

@ -0,0 +1,12 @@
using Xamarin.Forms;
namespace Bit.App.Styles
{
public partial class Android : ResourceDictionary
{
public Android()
{
InitializeComponent();
}
}
}

41
src/App/Styles/Base.xaml Normal file
View file

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Bit.App.Styles.Base">
<Style TargetType="ListView"
Class="list">
<Setter Property="SeparatorColor"
Value="Transparent" />
</Style>
<Style TargetType="StackLayout"
Class="list-row-header">
<Setter Property="Padding"
Value="10" />
</Style>
<Style TargetType="Label"
Class="list-header">
<Setter Property="FontSize"
Value="Medium" />
</Style>
<Style TargetType="StackLayout"
Class="list-row">
<Setter Property="Padding"
Value="10" />
</Style>
<Style TargetType="Label"
Class="list-title">
<Setter Property="FontSize"
Value="Medium" />
</Style>
<Style TargetType="Label"
Class="list-subtitle">
<Setter Property="FontSize"
Value="Small" />
</Style>
<Style TargetType="Label"
Class="list-icon"
ApplyToDerivedTypes="True">
<Setter Property="FontSize"
Value="Medium" />
</Style>
</ResourceDictionary>

View file

@ -0,0 +1,12 @@
using Xamarin.Forms;
namespace Bit.App.Styles
{
public partial class Base : ResourceDictionary
{
public Base()
{
InitializeComponent();
}
}
}

10
src/App/Styles/Dark.xaml Normal file
View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Bit.App.Styles.Dark">
<Style TargetType="Label"
Class="list-header-themed">
<Setter Property="TextColor"
Value="Green" />
</Style>
</ResourceDictionary>

View file

@ -0,0 +1,12 @@
using Xamarin.Forms;
namespace Bit.App.Styles
{
public partial class Dark : ResourceDictionary
{
public Dark()
{
InitializeComponent();
}
}
}

10
src/App/Styles/Light.xaml Normal file
View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Bit.App.Styles.Light">
<Style TargetType="Label"
Class="list-header-themed">
<Setter Property="TextColor"
Value="Blue" />
</Style>
</ResourceDictionary>

View file

@ -0,0 +1,12 @@
using Xamarin.Forms;
namespace Bit.App.Styles
{
public partial class Light : ResourceDictionary
{
public Light()
{
InitializeComponent();
}
}
}

6
src/App/Styles/iOS.xaml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Bit.App.Styles.iOS">
</ResourceDictionary>

View file

@ -0,0 +1,12 @@
using Xamarin.Forms;
namespace Bit.App.Styles
{
public partial class iOS : ResourceDictionary
{
public iOS()
{
InitializeComponent();
}
}
}

View file

@ -1,4 +1,6 @@
using System.Reflection; using Bit.App.Styles;
using System;
using System.Reflection;
using Xamarin.Forms; using Xamarin.Forms;
using Xamarin.Forms.StyleSheets; using Xamarin.Forms.StyleSheets;
@ -21,5 +23,35 @@ namespace Bit.App.Utilities
Application.Current.Resources.Add(StyleSheet.FromAssemblyResource(assembly, Application.Current.Resources.Add(StyleSheet.FromAssemblyResource(assembly,
string.Format(themeFormat, "styles"))); string.Format(themeFormat, "styles")));
} }
public static void SetThemeStyle(string name)
{
// Reset styles
Application.Current.Resources.Clear();
Application.Current.Resources.MergedDictionaries.Clear();
// Base styles
Application.Current.Resources.MergedDictionaries.Add(new Base());
// Platform styles
if(Device.RuntimePlatform == Device.Android)
{
Application.Current.Resources.MergedDictionaries.Add(new Android());
}
else if(Device.RuntimePlatform == Device.iOS)
{
Application.Current.Resources.MergedDictionaries.Add(new iOS());
}
// Theme styles
if(name == "dark")
{
Application.Current.Resources.MergedDictionaries.Add(new Dark());
}
else
{
Application.Current.Resources.MergedDictionaries.Add(new Light());
}
}
} }
} }

View file

@ -45,7 +45,7 @@ namespace Bit.Core.Models.View
public DateTime RevisionDate { get; set; } public DateTime RevisionDate { get; set; }
public string Subtitle public string SubTitle
{ {
get get
{ {