Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using Xamarin.Forms;
- using ImageCircle.Forms.Plugin.Abstractions;
- using FormsApp.Controllers;
- using FormsApp.Model;
- using FormsApp.Views.Setup;
- using PanicModels;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- using Connectivity.Plugin;
- using FormsApp.Views.Setup.Tariff;
- using Newtonsoft.Json;
- using Geolocator.Plugin;
- using XLabs.Forms.Services;
- using XLabs.Ioc;
- using XLabs.Platform.Device;
- namespace FormsApp.Views
- {
- public class MasterPage : MasterDetailPage
- {
- private ListItemModel selected = null;
- private Label header;
- private RelativeLayout profileLayout;
- private CircleImage profileImage;
- private Image profileImageBackground;
- public PanicPage detailPage;
- private ContentPage masterPage;
- private ListView listView;
- private ProfilePage profilePage;
- private TariffPage tariffPage;
- private TermsPage termsPage;
- private InstructionsPage instructionsPage;
- private HelpPage helpPage;
- private ActivityIndicator busyIndicator;
- public MasterPage(Profile profile)
- {
- var layout = new StackLayout
- {
- VerticalOptions = LayoutOptions.FillAndExpand,
- HorizontalOptions = LayoutOptions.FillAndExpand,
- BackgroundColor = Color.FromRgb(242, 247, 252)
- };
- busyIndicator = new ActivityIndicator();
- busyIndicator.VerticalOptions = LayoutOptions.Center;
- busyIndicator.HorizontalOptions = LayoutOptions.Center;
- layout.Children.Add(busyIndicator);
- this.Master = new ContentPage
- {
- Title = "Profile", // Title required!
- BackgroundColor = Color.FromRgb(242, 247, 252),
- Content = new StackLayout
- {
- BackgroundColor = Color.FromRgb(242, 247, 252),
- VerticalOptions = LayoutOptions.CenterAndExpand,
- HorizontalOptions = LayoutOptions.CenterAndExpand,
- Children =
- {
- layout
- }
- }
- };
- this.Detail = new ContentPage { BackgroundColor = Color.FromRgb(242, 247, 252), Content = layout };
- LayoutHelper.Device = Resolver.Resolve<IDevice>();
- LayoutHelper.FontManager = Resolver.Resolve<IFontManager>();
- Load (profile);
- busyIndicator.IsRunning = true;
- busyIndicator.IsEnabled = true;
- }
- private async Task Load(Profile profile)
- {
- #region tab menu
- header = new Label
- {
- Text =
- !ProfileController.Instance.Logined
- ? "Вы не авторизованы"
- : (profile.profileModel.Name + "\n" + profile.profileModel.Phonenumber),
- Font = Font.SystemFontOfSize(LayoutHelper.GetHeaderFontSize(), FontAttributes.None),
- HorizontalOptions = LayoutOptions.Center,
- TextColor = Color.White,
- XAlign = TextAlignment.Center,
- YAlign = TextAlignment.Center,
- BackgroundColor = Color.Transparent,
- HeightRequest = LayoutHelper.GetHeaderHeight()
- };
- profileImage = new CircleImage
- {
- HeightRequest = LayoutHelper.GetProfileImageSize(),
- WidthRequest = LayoutHelper.GetProfileImageSize(),
- HorizontalOptions = LayoutOptions.Fill,
- Aspect = Aspect.AspectFill,
- Source = ImageSource.FromFile("photoIcon.png"),
- BorderThickness = 2,
- BorderColor = Color.White,
- VerticalOptions = LayoutOptions.Center
- };
- var profileButton = new Button
- {
- HeightRequest = LayoutHelper.GetProfileImageSize(),
- WidthRequest = LayoutHelper.GetProfileImageSize(),
- HorizontalOptions = LayoutOptions.Fill,
- VerticalOptions = LayoutOptions.Center,
- BackgroundColor = Color.Transparent,
- };
- var profileButtonLayout = new RelativeLayout {HeightRequest = LayoutHelper.GetImageSideViewHeight()};
- profileButtonLayout.BackgroundColor = Color.Transparent;
- profileButtonLayout.Children.Add(profileImage,
- Constraint.Constant(0),
- Constraint.Constant(0));
- profileButtonLayout.Children.Add(profileButton,
- Constraint.Constant(0),
- Constraint.Constant(0));
- profileLayout = new RelativeLayout {BackgroundColor = Color.FromRgb(149, 114, 236)};
- profileLayout.HeightRequest = LayoutHelper.GetImageSideViewHeight();
- profileImageBackground = new Image()
- {
- Source = "defaultBack.png"
- };
- profileLayout.Children.Add(profileImageBackground,
- Constraint.Constant(0),
- Constraint.Constant(0),
- Constraint.RelativeToParent((parent) => { return parent.Width; }));
- busyIndicator = null;
- busyIndicator = new ActivityIndicator();
- profileLayout.Children.Add(profileButtonLayout,
- Constraint.RelativeToParent(
- (parent) => { return (parent.Width - LayoutHelper.GetProfileImageSize())/2; }),
- Constraint.RelativeToParent(
- (parent) => { return (parent.Height - LayoutHelper.GetProfileImageSize())/2; }),
- Constraint.Constant(LayoutHelper.GetProfileImageSize()),
- Constraint.Constant(LayoutHelper.GetProfileImageSize()));
- profileLayout.Children.Add(busyIndicator,
- Constraint.RelativeToParent(
- (parent) => { return (parent.Width - LayoutHelper.GetProfileImageSize()) / 2; }),
- Constraint.RelativeToParent(
- (parent) => { return (parent.Height - LayoutHelper.GetProfileImageSize()) / 2; }),
- Constraint.Constant(LayoutHelper.GetProfileImageSize()),
- Constraint.Constant(LayoutHelper.GetProfileImageSize()));
- busyIndicator.IsRunning = false;
- busyIndicator.IsVisible = false;
- profileLayout.Children.Add(header,
- Constraint.Constant(0),
- Constraint.RelativeToView(profileLayout, (layout, view) => { return view.HeightRequest - header.HeightRequest - 5; }),
- Constraint.RelativeToParent((parent) => { return parent.Width; }));
- #endregion
- // Assemble an array of NamedColor objects.
- #region menu buttons
- ListItemModel[] menuButtons =
- {
- new ListItemModel(@"AlarmMenu32.png", "Тревога"),
- new ListItemModel(@"Wallet32.png", "Тарифы"),
- new ListItemModel(@"UseInfo32.png", "Условия пользования"),
- new ListItemModel(@"InfoFilled32.png", "Инструкции"),
- new ListItemModel(@"Help32.png", "Помощь"),
- new ListItemModel(@"Exit25.png", "Выход")
- };
- // Create ListView for the master page.
- listView = new ListView
- {
- ItemsSource = menuButtons,
- ItemTemplate = new DataTemplate(typeof (MenuButtonView))
- };
- listView.RowHeight = LayoutHelper.GetMenuRowHeight();
- listView.BackgroundColor = Color.FromRgb(242, 247, 252);
- listView.SeparatorVisibility = SeparatorVisibility.None;
- listView.SeparatorColor = Color.Transparent;
- // Create the master page with the ListView.
- #endregion
- #region events
- masterPage = new ContentPage
- {
- Title = "Profile", // Title required!
- BackgroundColor = Color.FromRgb(242, 247, 252),
- Content = new StackLayout
- {
- VerticalOptions = LayoutOptions.Start,
- Children =
- {
- profileLayout,
- listView
- }
- }
- };
- ProfileController.Instance.OnProfileUpdated += ProfileController_OnProfileUpdated;
- ProfileController.Instance.OnImageReceived += Profile_ImageUpdate;
- // Create the detail page using NamedColorPage
- detailPage = new PanicPage(this);
- profileButton.Clicked += async (sender, args) =>
- {
- try
- {
- if (selected != null)
- {
- selected.SetColors(false);
- }
- if (busyIndicator == null)
- {
- busyIndicator = new ActivityIndicator();
- profileLayout.Children.Add(busyIndicator,
- Constraint.RelativeToParent(
- (parent) => { return (parent.Width - LayoutHelper.GetProfileImageSize()) / 2; }),
- Constraint.RelativeToParent(
- (parent) => { return (parent.Height - LayoutHelper.GetProfileImageSize()) / 2; }),
- Constraint.Constant(LayoutHelper.GetProfileImageSize()),
- Constraint.Constant(LayoutHelper.GetProfileImageSize()));
- }
- if (ProfileController.Instance.Logined && !busyIndicator.IsRunning)
- {
- busyIndicator.IsRunning = true;
- busyIndicator.IsVisible = true;
- if (profilePage == null)
- {
- var lprofile = await ProfileController.Instance.GetProfile();
- // Set the BindingContext of the detail page.
- profilePage = new ProfilePage(lprofile);
- await Task.Delay(500);
- await profilePage.Load();
- }
- profilePage.UpdateImage();
- this.Detail = profilePage;
- // Show the detail page.
- this.IsPresented = true;
- await Task.Delay(500);
- busyIndicator.IsRunning = false;
- busyIndicator.IsVisible = false;
- }
- else if (!ProfileController.Instance.Logined)
- {
- Navigation.PushAsync(new LoginPage(detailPage.EnableService));
- }
- }
- catch(Exception ex)
- {
- string msg = ex.ToString();
- }
- };
- // For Android & Windows Phone, provide a way to get back to the master page.я
- if (Device.OS != TargetPlatform.iOS)
- {
- TapGestureRecognizer tap = new TapGestureRecognizer();
- tap.Tapped += (sender, args) =>
- {
- this.IsPresented = true;
- };
- detailPage.Content.GestureRecognizers.Add(tap);
- }
- // Define a selected handler for the ListView.
- listView.ItemSelected += (sender, args) =>
- {
- try
- {
- if (!this.IsPresented)
- return;
- this.IsPresented = false;
- if (selected != null)
- {
- selected.SetColors(false);
- }
- // Select new
- selected = (listView.SelectedItem as ListItemModel);
- selected.SetColors(true);
- if (selected.Name == "Тревога")
- {
- this.Detail = detailPage;
- }
- if (selected.Name == "Тарифы")
- {
- if (tariffPage == null)
- tariffPage = new TariffPage(this);
- tariffPage.Load();
- this.Detail = tariffPage;
- }
- if (selected.Name == "Условия пользования")
- {
- if (termsPage == null) termsPage = new TermsPage(false);
- termsPage.Load();
- this.Detail = termsPage;
- }
- if (selected.Name == "Инструкции")
- {
- if (instructionsPage == null) instructionsPage = new InstructionsPage();
- instructionsPage.Load();
- this.Detail = instructionsPage;
- }
- if (selected.Name == "Помощь")
- {
- if (helpPage == null) helpPage = new HelpPage();
- this.Detail = helpPage;
- }
- if (selected.Name == "Выход")
- {
- Logout();
- }
- this.Detail.BindingContext = args.SelectedItem;
- // Show the detail page.
- this.IsPresented = true;
- }
- catch (NullReferenceException exs)
- {
- string msg = exs.ToString();
- }
- catch (Exception ex)
- {
- string msg = ex.ToString();
- }
- };
- // Initialize the ListView selection.
- listView.SelectedItem = menuButtons[0];
- #endregion
- finished = false;
- Task.Factory.StartNew(async() =>
- {
- if (CrossConnectivity.Current.IsConnected && !ProfileController.Instance.Logined)
- {
- ProfileController.Instance.CheckSessionKey();
- if (ProfileController.Instance.Logined)
- {
- await ProfileController.Instance.GetProfile();
- if (ProfileController.Instance.IsActivated())
- detailPage.EnableService(null, EventArgs.Empty);
- else
- FinishLoadPage();
- }
- else
- {
- FinishLoadPage();
- }
- }
- else
- {
- FinishLoadPage();
- }
- });
- /*
- CrossGeolocator.Current.DesiredAccuracy = 50;
- var position = await CrossGeolocator.Current.GetPositionAsync(timeout: 10000);
- PanicController.OnGeoUpdated(position.Latitude, position.Longitude);*/
- }
- private static bool finished = false;
- public void FinishLoadPage()
- {
- Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
- {
- if (!finished)
- {
- this.Master = masterPage;
- this.Detail = detailPage;
- if (ProfileController.Instance.Logined)
- Profile_ImageUpdate(null, ProfileController.Instance.ProfileImage);
- }
- finished = true;
- });
- }
- private void ProfileController_OnProfileUpdated(Profile profile)
- {
- Xamarin.Forms.Device.BeginInvokeOnMainThread(async () =>
- {
- if (ProfileController.Instance.Logined && profile != null)
- {
- header.Text = (profile.profileModel.Name + "\n" + profile.profileModel.Phonenumber);
- header.HeightRequest = LayoutHelper.GetHeaderHeight()*2;
- if (profile.profileModel.PaymentStatus == "[PUSH]")
- {
- var result = await DisplayAlert("ВНИМАНИЕ!",
- "На ваше счету недостаточно средств для вызова следующей тревоги, Ваш аккаунт будет деактивирован через час!",
- "Оплатить", "ОК");
- if (result)
- {
- if (tariffPage == null)
- tariffPage = new TariffPage(this);
- tariffPage.Load();
- this.Detail = tariffPage;
- }
- }
- /* if (ProfileController.ProfileImage == null) return;
- profileImage.Source = null;
- profileImage.Source = ImageSource.FromStream(() => {return new MemoryStream(ProfileController.ProfileImage);});
- profileImageBackground.Source = null;
- profileImageBackground.Source = ImageSource.FromStream(() => {return new MemoryStream(ProfileController.ProfileImage);});
- profileLayout.HeightRequest = profileImageBackground.Width * 0.565656;*/
- }
- });
- }
- private void Profile_ImageUpdate(object o, ImageModel image)
- {
- if (finished)
- {
- Xamarin.Forms.Device.BeginInvokeOnMainThread(async () =>
- {
- if (ProfileController.Instance.Logined && image != null)
- {
- profileImage.Source = null;
- profileImage.Source = ImageSource.FromStream(() => { return new MemoryStream(image.data); });
- profileImageBackground.Source = null;
- profileImageBackground.Source = ImageSource.FromStream(() => { return new MemoryStream(image.data); });
- profileLayout.HeightRequest = profileImageBackground.Width * 0.565656;
- if (!image.Horizontal) {
- profileImage.Rotation = 90;
- profileImageBackground.Rotation = 90;
- }
- else
- {
- profileImage.Rotation = 0;
- profileImageBackground.Rotation = 0;
- }
- }
- });
- }
- }
- public void LoginPage()
- {
- if (selected != null)
- {
- selected.SetColors(false);
- }
- // Select new
- selected = (listView.ItemsSource as IEnumerable<ListItemModel>).First();
- selected.SetColors(true);
- listView.SelectedItem = selected;
- this.Detail = detailPage;
- Navigation.PushAsync(new LoginPage(detailPage.EnableService));
- }
- public void Logout()
- {
- if (ProfileController.Instance.Logined)
- {
- detailPage.DisableView();
- ProfileController.Instance.SessionToken = JsonConvert.SerializeObject(new LoginToken());
- ProfileController.Instance.Logout();
- ProfileController.Instance.Logined = false;
- profileImageBackground.Source = null;
- profileImageBackground.Source = ImageSource.FromFile("defaultBack.png");
- profileImage.Source = null;
- profileImage.Source = ImageSource.FromFile("photoIcon.png");
- header.Text = "Вы не авторизованы";
- this.Detail = detailPage;
- }
- }
- public static async Task ConnectionLoop()
- {
- while (!CrossConnectivity.Current.IsConnected)
- {
- await Task.Delay(1500);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement