//Methods
        private async void getChatroom()
        {
            try
            {
                if (VindRitChatVM.ID == null)
                {
                    string json = await Localdata.read("chatroom.json");

                    var definition = new { ID = 0, UserID = 0 };
                    var data       = JsonConvert.DeserializeAnonymousType(json, definition);
                    if (data.ID == -1)
                    {
                        //geen chatroom
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
                        await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
                        {
                            Type type = null;
                            if (MainViewVM.USER.IsBob == true)
                            {
                                type = typeof(VindRitBob);
                            }
                            else
                            {
                                type = typeof(VindRit);
                            }
                            Frame rootFrame = MainViewVM.MainFrame as Frame;
                            rootFrame.Navigate(type);
                        });

#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
                    }
                    else
                    {
                        VindRitChatVM.ID    = data.ID;
                        MainViewVM.ChatRoom = new ChatRoom()
                        {
                            ID = VindRitChatVM.ID.Value
                        };


                        GetChatComments();
                    }
                }
                else
                {
                    MainViewVM.ChatRoom = new ChatRoom()
                    {
                        ID = VindRitChatVM.ID.Value
                    };


                    GetChatComments();
                }
            }
            catch (Exception ex)
            {
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
                {
                    Type type = null;
                    if (MainViewVM.USER.IsBob == true)
                    {
                        type = typeof(VindRitBob);
                    }
                    else
                    {
                        type = typeof(VindRit);
                    }
                    Frame rootFrame = MainViewVM.MainFrame as Frame;
                    rootFrame.Navigate(type);
                });

#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
            }
        }
Exemple #2
0
        //login, zelfde als bij loginVM
        private async Task <Boolean> Login_task(string email, string pass)
        {
            this.Error       = "";
            this.Loading     = true;
            this.EnableLogin = false;
            RaiseAll();
            Tests();

            if (this.Online == true)
            {
                Response res = await LoginRepository.Login(email, pass);

                if (res.Success == true)
                {
                    try
                    {
                        User user = await UserRepository.GetUser();

                        MainViewVM.USER = user;

                        string jsonUser = await Localdata.read("user.json");

                        var definitionMail = new { Email = "", Password = "" };
                        if (jsonUser != null)
                        {
                            var dataUser = JsonConvert.DeserializeAnonymousType(jsonUser, definitionMail);
                            if (user.ID != MainViewVM.USER.ID)
                            {
                                Clear();
                            }
                        }


                        var json = JsonConvert.SerializeObject(new { Email = email, Password = pass });
                        await Localdata.save("user.json", json);


                        string jsonChat = await Localdata.read("chatroom.json");

                        string jsonTrip = await Localdata.read("trip.json");

                        var definition = new { ID = 0, UserID = 0 };

                        if (jsonChat != null && jsonTrip != null)
                        {
                            var dataChat = JsonConvert.DeserializeAnonymousType(jsonChat, definition);
                            var dataTrip = JsonConvert.DeserializeObject <Trip>(jsonTrip);
                            if (dataChat == null | dataTrip == null)
                            {
                                Clear();
                            }
                            else if (MainViewVM.USER.IsBob == false && dataChat.UserID != MainViewVM.USER.ID)
                            {
                                Clear();
                            }
                            else if (MainViewVM.USER.IsBob == false && dataTrip.Users_ID != MainViewVM.USER.ID)
                            {
                                Clear();
                            }
                            else if (MainViewVM.USER.IsBob == true && dataTrip.Bobs_ID != MainViewVM.USER.Bobs_ID)
                            {
                                Clear();
                            }
                            else
                            {
                                // Clear();
                                MainViewVM.CurrentTrip = await TripRepository.GetCurrentTrip();

                                if (MainViewVM.CurrentTrip != null)
                                {
                                    VindRitChatVM.ID = dataChat.ID;
                                    if (dataTrip.ID != null && MainViewVM.CurrentTrip.ID == dataTrip.ID)
                                    {
                                        Messenger.Default.Send <NavigateTo>(new NavigateTo()
                                        {
                                            Name = "trip_location:reload"
                                        });
                                    }
                                }
                                else
                                {
                                    Clear();
                                }
                            }
                        }
                        MainViewVM.socket = IO.Socket(URL.SOCKET);
                        MainViewVM.socket.Connect();

                        Messenger.Default.Send <GoToPage>(new GoToPage()
                        {
                            Name = "MainView"
                        });
                    }
                    catch (Exception ex)
                    {
                        var error = ex.Message;
                    }


                    this.Loading     = false;
                    this.EnableLogin = true;
                    RaiseAll();
                }
                else
                {
                    this.Loading     = false;
                    this.EnableLogin = true;
                    RaiseAll();

                    switch (res.Error)
                    {
                    case "Invalid Login":
                        this.Error = "Gegeven email en wachtwoord komen niet overeen of bestaan niet.";
                        break;

                    case "Server Offline":
                        this.Error = "De server is offline";
                        task       = Login_task(NewRegister.Email, NewRegister.Password);
                        break;

                    case "Connectie error":
                        this.Error = "Connectie error";
                        task       = Login_task(NewRegister.Email, NewRegister.Password);
                        break;

                    default:
                        this.Error = "Connectie error";

                        break;
                    }

                    RaiseAll();
                }

                return(res.Success);
            }
            else
            {
                this.Loading     = false;
                this.EnableLogin = true;
                RaiseAll();

                return(false);
            }
        }