From 119dfc3ac70db7536e86191eb3c89ffa1fd4f576 Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Thu, 20 Sep 2012 11:25:22 -0400 Subject: Adding the UI to the same repo. Made some default theme progress --- MediaBrowser.UI/App.xaml.cs | 213 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 213 insertions(+) create mode 100644 MediaBrowser.UI/App.xaml.cs (limited to 'MediaBrowser.UI/App.xaml.cs') diff --git a/MediaBrowser.UI/App.xaml.cs b/MediaBrowser.UI/App.xaml.cs new file mode 100644 index 000000000..6f2afa91c --- /dev/null +++ b/MediaBrowser.UI/App.xaml.cs @@ -0,0 +1,213 @@ +using MediaBrowser.Common.Kernel; +using MediaBrowser.Common.Plugins; +using MediaBrowser.Common.UI; +using MediaBrowser.Model.Configuration; +using MediaBrowser.Model.DTO; +using MediaBrowser.Model.Weather; +using MediaBrowser.UI.Controller; +using System; +using System.ComponentModel; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Media.Imaging; + +namespace MediaBrowser.UI +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : BaseApplication, IApplication + { + private Timer ClockTimer { get; set; } + private Timer ServerConfigurationTimer { get; set; } + + public static App Instance + { + get + { + return Application.Current as App; + } + } + + public DtoUser CurrentUser + { + get + { + return UIKernel.Instance.CurrentUser; + } + set + { + UIKernel.Instance.CurrentUser = value; + OnPropertyChanged("CurrentUser"); + } + } + + public ServerConfiguration ServerConfiguration + { + get + { + return UIKernel.Instance.ServerConfiguration; + } + set + { + UIKernel.Instance.ServerConfiguration = value; + OnPropertyChanged("ServerConfiguration"); + } + } + + private DateTime _currentTime = DateTime.Now; + public DateTime CurrentTime + { + get + { + return _currentTime; + } + private set + { + _currentTime = value; + OnPropertyChanged("CurrentTime"); + } + } + + private WeatherInfo _currentWeather; + public WeatherInfo CurrentWeather + { + get + { + return _currentWeather; + } + private set + { + _currentWeather = value; + OnPropertyChanged("CurrentWeather"); + } + } + + private BaseTheme _currentTheme; + public BaseTheme CurrentTheme + { + get + { + return _currentTheme; + } + private set + { + _currentTheme = value; + OnPropertyChanged("CurrentTheme"); + } + } + + [STAThread] + public static void Main() + { + RunApplication("MediaBrowserUI"); + } + + #region BaseApplication Overrides + protected override IKernel InstantiateKernel() + { + return new UIKernel(); + } + + protected override Window InstantiateMainWindow() + { + return new MainWindow(); + } + + protected override void OnKernelLoaded() + { + base.OnKernelLoaded(); + + PropertyChanged += AppPropertyChanged; + + // Update every 10 seconds + ClockTimer = new Timer(ClockTimerCallback, null, 0, 10000); + + // Update every 30 minutes + ServerConfigurationTimer = new Timer(ServerConfigurationTimerCallback, null, 0, 1800000); + + CurrentTheme = UIKernel.Instance.Plugins.OfType().First(); + + foreach (var resource in CurrentTheme.GlobalResources) + { + Resources.MergedDictionaries.Add(resource); + } + } + #endregion + + async void AppPropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (e.PropertyName.Equals("ServerConfiguration")) + { + if (string.IsNullOrEmpty(ServerConfiguration.WeatherZipCode)) + { + CurrentWeather = null; + } + else + { + CurrentWeather = await UIKernel.Instance.ApiClient.GetWeatherInfoAsync(ServerConfiguration.WeatherZipCode); + } + } + } + + private void ClockTimerCallback(object stateInfo) + { + CurrentTime = DateTime.Now; + } + + private async void ServerConfigurationTimerCallback(object stateInfo) + { + ServerConfiguration = await UIKernel.Instance.ApiClient.GetServerConfigurationAsync(); + } + + public async Task GetImage(string url) + { + var image = new Image(); + + image.Source = await GetBitmapImage(url); + + return image; + } + + public async Task GetBitmapImage(string url) + { + Stream stream = await UIKernel.Instance.ApiClient.GetImageStreamAsync(url); + + BitmapImage bitmap = new BitmapImage(); + + bitmap.CacheOption = BitmapCacheOption.Default; + + bitmap.BeginInit(); + bitmap.StreamSource = stream; + bitmap.EndInit(); + + return bitmap; + } + + public async Task LogoutUser() + { + CurrentUser = null; + + if (ServerConfiguration.EnableUserProfiles) + { + Navigate(CurrentTheme.LoginPageUri); + } + else + { + DtoUser defaultUser = await UIKernel.Instance.ApiClient.GetDefaultUserAsync(); + CurrentUser = defaultUser; + + Navigate(new Uri("/Pages/HomePage.xaml", UriKind.Relative)); + } + } + + public void Navigate(Uri uri) + { + (MainWindow as MainWindow).Navigate(uri); + } + } +} -- cgit v1.2.3