Example #1
0
        /// <summary>
        /// First thing called. Entry point of some sort. Calls other methods to get Tinder data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public async void StartInitialize(object sender, EventArgs e)
        {
            ConnectionStatus = Resources.tinder_auth_connecting;

            // Only do full authorization if it's the first time or data is not saved yet
            if (string.IsNullOrEmpty(UserName))
            {
                if (await Authenticate(FbId, FbToken))
                {
                    UserName = User.ToString();

                    string lat = "0";
                    string lon = "0";

                    if (User.Pos != null)
                    {
                        lat = User.Pos.Latitude.Replace('.', ',');
                        lon = User.Pos.Longtitude.Replace('.', ',');
                    }

                    SerializationHelper.CreateUser(FbId, FbToken, UserName, User.LatestUpdateDate,
                                                   lat, lon);
                    SerializationHelper.UpdateTinderToken(Auth.Token);


                    // Gets matches
                    await GetMatches();

                    // Gets recs
                    await GetRecs();

                    int time = MatchList.Count * 3 / 60;
                    // Dont ask just save nobody cares what user wants
                    MessageBox.Show($"All your matches will be saved. It may take up to {time} minutes "
                                    + "for the download to complete. I recommend not to cancel this action.",
                                    "Downloading data", MessageBoxButton.OK, MessageBoxImage.Information);

                    Messenger.Default.Send(new SerializationPacket(MatchList, RecList, User),
                                           MessengerToken.ShowSerializationDialog);

                    Settings.Default["FirstStart"] = false;
                    Settings.Default.Save();

                    StartUpdatingMatches();
                    StartUpdatingRecs();
                    ConnectionStatus = Resources.tinder_auth_okay;
                }
            }
            // It's not the first time launching application
            else
            {
                SerializationHelper.CurrentUser = UserName;


                // FIXME hangs application for a second
                // Deserializes matches and recs first
                MatchList = SerializationHelper.DeserializeMatchList();


                MatchListSetup();
                FilterVM.UpdateStatusBar();

                RecList = SerializationHelper.DeserializeRecList();

                if (await Authenticate(FbId, FbToken))
                {
                    string lat = "0";
                    string lon = "0";

                    if (User.Pos != null)
                    {
                        lat = User.Pos.Latitude.Replace('.', ',');
                        lon = User.Pos.Longtitude.Replace('.', ',');
                        SerializationHelper.UpdateUserPosition(lat, lon);
                    }


                    SerializationHelper.UpdateTinderToken(Auth.Token);

                    // Updates last five matches
                    Parallel.ForEach(MatchList.OrderByDescending(x => x.LastActivityDate).Take(5), async x =>
                    {
                        try
                        {
                            var updatedMatch = await TinderHelper.GetFullMatchData(x.Person.Id);
                            SerializationHelper.UpdateMatchModel(x, updatedMatch);
                            new Task(() => SerializationHelper.SerializeMatch(x)).Start();
                        }
                        catch (TinderRequestException ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    });

                    FilterVM.SortMatchList();

                    UpdateMatches(this, null);
                    UpdateRecs(this, null);

                    ConnectionStatus = Resources.tinder_auth_okay;

                    // Starts automatic updates
                    StartUpdatingMatches();
                    StartUpdatingRecs();
                }
                else
                {
                    ConnectionStatus = Resources.tinder_auth_error;
                }
            }

            // Now we check if there's a new version available
            var restClient = new RestClient("https://api.github.com/");
            var request    = new RestRequest("repos/dainius14/twinder/releases/latest", Method.GET);
            var response   = await restClient.ExecuteTaskAsync(request);

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var json = JsonConvert.DeserializeObject <dynamic>(response.Content);
                if (!("v" + Assembly.GetExecutingAssembly().GetName().Version.ToString()).StartsWith((string)json.tag_name))
                {
                    Messenger.Default.Send((string)json.html_url, MessengerToken.ShowUpdateAvailableDialog);
                }
            }
        }