示例#1
0
        public async void GetEvents(bool refresh = false)
        {
            if (PastEventsPage.Instance != null)
            {
                PastEventsPage.Instance.GetEvents("");
            }

            ErrorScreen.IsVisible = false;

            // PopToRootAsync não funciona como esperado no Windows
            if (Device.OS == TargetPlatform.Windows)
            {
                while (Navigation.NavigationStack.Count > 1)
                {
                    await Navigation.PopAsync();
                }
            }
            else
            {
                if (Navigation.NavigationStack.Count > 1)
                {
                    await Navigation.PopToRootAsync();
                }
            }

            ListEvents.IsVisible = false;
            Loading.IsVisible    = true;

            listEvents = new ObservableCollection <Event>();

            string jsonString = "";

            try
            {
                // Pelo que parece o Windows Phone armazena uma solicitação http em cache e não atualiza tão facilmente, por isso precisa mudar alguma query
                if (!Other.Other.GetSetting("refresh_token").Equals(""))
                {
                    var client = new HttpClient();
                    client.MaxResponseContentBufferSize = 256000;
                    HttpResponseMessage response = await client.PostAsync(Other.Other.GetEventsUrl() + "&time=" + time++, Other.Other.GetRefreshToken(true));

                    if (response.IsSuccessStatusCode)
                    {
                        jsonString = await response.Content.ReadAsStringAsync();
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
                else
                {
                    jsonString = await new HttpClient().GetStringAsync(new Uri(Other.Other.GetEventsUrl() + "&time=" + time++));
                }
            }
            catch
            {
                if (refresh)
                {
                    var alert = await DisplayAlert("Verifique sua conexão de internet", "Deseja tentar novamente?", "Sim", "Não");

                    if (alert)
                    {
                        GetEvents(true);
                        ErrorScreen.IsVisible = false;
                    }
                    else
                    {
                        ListEvents.IsVisible = true;
                        Loading.IsVisible    = false;
                    }
                }
                else
                {
                    ErrorMessage.Text     = "Verifique sua conexão de internet";
                    ErrorScreen.IsVisible = true;
                    Loading.IsVisible     = false;
                }
                return;
            }

            JObject root = null;

            try
            {
                root       = JObject.Parse(jsonString);
                listEvents = JsonConvert.DeserializeObject <ObservableCollection <Event> >(root["events"].ToString());
            }
            catch
            {
                ErrorMessage.Text     = "Houve um erro ao receber os eventos";
                ErrorScreen.IsVisible = true;
                Loading.IsVisible     = false;
                return;
            }

            HeaderImage.Source = ImageSource.FromUri(new Uri(root["header"].ToString()));

            DependencyService.Get <IDependencies>().SendOneSignalTag("app_version", DependencyService.Get <IDependencies>().GetAppVersion());

            LinksPage linksPage = LinksPage.Instance;

            if ((int)root["member"]["id"] != 0)
            {
                LinksPage.Instance.member = JsonConvert.DeserializeObject <Person>(root["member"].ToString());
                Other.Other.AddSetting("member_profile", root["member"].ToString());

                linksPage.profileImage.Source = ImageSource.FromUri(new Uri(linksPage.member.Photo));
                linksPage.profileName.Text    = linksPage.member.Name;
                linksPage.profileIntro.Text   = linksPage.member.Intro;

                if ((bool)root["member"]["is_admin"])
                {
                    if (!notificationAdded)
                    {
                        linksPage.listLinks.Insert(0, new Link()
                        {
                            Title = "Enviar notificação", Icon = Device.OnPlatform("SendNotification.png", "ic_send.png", "Assets/Images/SendNotification.png"), Value = "send_notification"
                        });
                        linksPage.listLinks.Insert(1, new Link()
                        {
                            Title = "Gerenciar sorteios", Icon = Device.OnPlatform("Raffle.png", "ic_raffle.png", "Assets/Images/Raffle.png"), Value = "raffle_manager"
                        });
                        notificationAdded = true;
                    }
                }

                DependencyService.Get <IDependencies>().SendOneSignalTag("member_id", linksPage.member.Id.ToString());
            }
            else
            {
                if (!Other.Other.GetSetting("refresh_token").Equals(""))
                {
                    Other.Other.AddSetting("refresh_token", "");
                    Other.Other.AddSetting("member_profile", "");
                    Other.Other.AddSetting("qr_code", "");
                    linksPage.member = null;

                    string image = "";

                    image = Device.OnPlatform("Icon-72.png", "ic_launcher.png", "Assets/Square71x71Logo.scale-140.png");

                    if (Device.Idiom == TargetIdiom.Desktop)
                    {
                        image = "Assets/Square71x71Logo.scale-150.png";
                    }
                    else if ((Device.Idiom == TargetIdiom.Tablet && Device.OS == TargetPlatform.Windows))
                    {
                        image = "Assets/Square70x70Logo.scale-100.png";
                    }

                    linksPage.profileImage.Source = ImageSource.FromFile(image);
                    linksPage.profileName.Text    = "Fazer login";
                    linksPage.profileIntro.Text   = "";
                }
            }

            // Notei que a primeira vez que abre no iOS ocorre um bug no tamanho da lista, e corrige se atualizar, como não tenho muito tempo para mexer em um Mac/simulador de iOS então vou deixar assim mesmo
            if (Device.OS == TargetPlatform.iOS)
            {
                if (Other.Other.GetSetting("isfirst").Equals(""))
                {
                    GetEvents();
                    Other.Other.AddSetting("isfirst", "n");
                }
            }

            if (listEvents.Count > 0)
            {
                if (imageWidth > 0)
                {
                    for (int i = 0; i < listEvents.Count; i++)
                    {
                        listEvents[i].HeightRequest = Other.Other.GetHeightImage(imageWidth - 20, listEvents[i].Image_width, listEvents[i].Image_height);
                    }
                }

                HeaderImage.SizeChanged += (s, e) =>
                {
                    ForceLayout();

                    if (imageWidth == 0)
                    {
                        imageWidth = HeaderImage.Width;
                    }

                    HeaderImage.HeightRequest = Other.Other.GetHeightImage(imageWidth, (double)root["header_width"], (double)root["header_height"]);

                    if (imageWidth > 0)
                    {
                        for (int i = 0; i < listEvents.Count; i++)
                        {
                            listEvents[i].HeightRequest = Other.Other.GetHeightImage(imageWidth - 20, listEvents[i].Image_width, listEvents[i].Image_height);
                        }
                    }

                    ListEvents.ItemsSource = listEvents;
                    ForceLayout();
                };

                ListEvents.ItemsSource = listEvents;

                if ((Device.Idiom == TargetIdiom.Desktop || Device.Idiom == TargetIdiom.Tablet || Device.Idiom == TargetIdiom.Desktop) && !refresh)
                {
                    _event = listEvents[0];

                    if (!tabletMenu)
                    {
                        ToolbarItems.Remove(more);

                        ToolbarItems.Add(new ToolbarItem("RSVP", Other.Other.GetImage("Rsvp"), async() =>
                        {
                            if (Other.Other.GetSetting("refresh_token").Length > 0)
                            {
                                if (_event.Rsvpable)
                                {
                                    await Navigation.PushAsync(new RSVPPage(_event));
                                }
                                else
                                {
                                    await DisplayAlert("", "Não é possível fazer RSVP nesse evento", "OK");
                                }
                            }
                            else
                            {
                                bool alert = await DisplayAlert("RSVP", "Você precisa fazer login para fazer RSVP, deseja fazer login agora?", "Sim", "Não");
                                if (alert)
                                {
                                    await Navigation.PushAsync(new WebViewPage(Other.Other.GetLoginUrl(), true, _event.Id)
                                    {
                                        Title = "Login"
                                    });
                                }
                            }
                        }));

                        ToolbarItems.Add(new ToolbarItem(_event.Who, Other.Other.GetImage("People"), () =>
                        {
                            Navigation.PushAsync(new TabbedPeoplePage(_event));
                        }));

                        ToolbarItems.Add(new ToolbarItem("Compartilhar", Other.Other.GetImage("Share"), () =>
                        {
                            Other.Other.ShareLink(_event.Name, _event.Link);
                        }));

                        more.Command = new Command(async() =>
                        {
                            string action = await DisplayActionSheet("Menu", "Cancelar", null, "Atualizar", "Fazer check-in", "Abrir meetup", "Abrir no navegador");

                            if (action != null)
                            {
                                if (action.Equals("Atualizar"))
                                {
                                    GetEvents(true);
                                }
                                else if (action.Equals("Fazer check-in"))
                                {
                                    OpenCheckin();
                                }
                                else if (action.Equals("Abrir meetup"))
                                {
                                    Other.Other.OpenSite("meetup.com/" + AppResources.MeetupId, this);
                                }
                                else if (action.Equals("Abrir no navegador"))
                                {
                                    Other.Other.OpenSite(_event.Link, this);
                                }
                            }
                        });

                        ToolbarItems.Add(more);

                        tabletMenu = true;
                    }
                }

                if (openEvent != 0)
                {
                    foreach (Event e in listEvents)
                    {
                        if (e.Id == openEvent)
                        {
                            ListEvents.SelectedItem = e;
                        }
                    }
                    openEvent = 0;
                }
                else if (Device.Idiom == TargetIdiom.Desktop || Device.Idiom == TargetIdiom.Tablet)
                {
                    _event = listEvents[0];
                    TabletPost();
                }

                ListEvents.IsVisible = true;
            }
            else
            {
                ErrorMessage.Text     = "Não há eventos futuros";
                ErrorScreen.IsVisible = true;
            }

            Loading.IsVisible = false;

            PastEventsPage.Instance.GetEvents(jsonString);

            SuggestLogin();
        }
示例#2
0
        public LinksPage()
        {
            InitializeComponent();

            instance = this;

            profileImage = ProfileImage;
            profileName  = ProfileName;
            profileIntro = ProfileIntro;

            if (Other.Other.GetSetting("member_profile").Length > 0)
            {
                member = JsonConvert.DeserializeObject <Person>(Other.Other.GetSetting("member_profile"));

                if (!member.Name.Equals(""))
                {
                    ProfileImage.Source = ImageSource.FromUri(new Uri(member.Photo.ToString()));
                    ProfileName.Text    = member.Name.ToString();
                    ProfileIntro.Text   = member.Intro.ToString();
                }
            }
            else
            {
                string image = "";

                image = Device.OnPlatform("Icon-72.png", "ic_launcher.png", "Assets/Square71x71Logo.scale-140.png");

                if (Device.Idiom == TargetIdiom.Desktop)
                {
                    image = "Assets/Square71x71Logo.scale-150.png";
                }
                else if ((Device.Idiom == TargetIdiom.Tablet && Device.OS == TargetPlatform.Windows))
                {
                    image = "Assets/Square70x70Logo.scale-100.png";
                }

                ProfileImage.Source = ImageSource.FromFile(image);
                ProfileName.Text    = "Fazer login";
            }

            listLinks.Add(new Link()
            {
                Title = "Site", Icon = Other.Other.GetImage("Site"), Value = AppResources.SiteUrl
            });
            listLinks.Add(new Link()
            {
                Title = "Meetups antigos", Icon = Other.Other.GetImage("YouTube"), Value = AppResources.OldMeetupsUrl
            });
            listLinks.Add(new Link()
            {
                Title = "Facebook", Icon = Other.Other.GetImage("Facebook"), Value = AppResources.FacebookUrl
            });
            listLinks.Add(new Link()
            {
                Title = "Google+", Icon = Other.Other.GetImage("Google"), Value = AppResources.GooglePlusUrl
            });
            listLinks.Add(new Link()
            {
                Title = "Instagram", Icon = Other.Other.GetImage("Instagram"), Value = AppResources.InstagramUrl
            });
            listLinks.Add(new Link()
            {
                Title = "Twitter", Icon = Other.Other.GetImage("Twitter"), Value = AppResources.TwitterUrl
            });
            listLinks.Add(new Link()
            {
                Title = "YouTube", Icon = Other.Other.GetImage("YouTube"), Value = AppResources.YouTubeUrl
            });
            listLinks.Add(new Link()
            {
                Title = "Contato", Icon = Other.Other.GetImage("Mail"), Value = AppResources.ContactMail
            });

            ListLinks.ItemsSource = listLinks;

            var login = new TapGestureRecognizer();

            login.Tapped += (s, e) => {
                if (member == null)
                {
                    Navigation.PushAsync(new WebViewPage(Other.Other.GetLoginUrl(), true)
                    {
                        Title = "Login"
                    });
                }
                else
                {
                    Other.Other.OpenSite("meetup.com/" + AppResources.MeetupId + "/member/" + member.Id, this, true, member.Name);
                }
            };
            ProfileGrid.GestureRecognizers.Add(login);
        }