Esempio n. 1
0
    public static void addPair(string title, WeekTimetable week, Action <Pair> yesAction, string okButtonTitle = "ДОБАВИТЬ", string cancelButtonTitle = "ОТМЕНА")
    {
        hideAll();

        current.overlay.gameObject.SetActive(true);
        current.editPairDialog.gameObject.SetActive(true);

        current.editPairDialog.wrongInput.text = "заполните все поля со *";


        current.editPairDialog.selectedDayOfWeek = DayOfWeek.Sunday;
        current.editPairDialog.selectionBox.Setup();


        Loom.QueueOnMainThread(() => {
            current.editPairDialog.title.text = title;

            current.editPairDialog.startTime.text = "";
            current.editPairDialog.startTime.GetComponent <InputFieldConfig>().Refresh();


            current.editPairDialog.endTime.text = "";
            current.editPairDialog.endTime.GetComponent <InputFieldConfig>().Refresh();

            current.editPairDialog.name.text = "";
            current.editPairDialog.name.GetComponent <InputFieldConfig>().Refresh();

            current.editPairDialog.location.text = "";
            current.editPairDialog.location.GetComponent <InputFieldConfig>().Refresh();

            current.editPairDialog.lecturer.text = "";
            current.editPairDialog.lecturer.GetComponent <InputFieldConfig>().Refresh();

            current.editPairDialog.okButtonTitle.text     = okButtonTitle;
            current.editPairDialog.cancelButtonTitle.text = cancelButtonTitle;
        });

        current.editPairDialog.okButton.onClick.RemoveAllListeners();
        current.editPairDialog.cancelButton.onClick.RemoveAllListeners();

        current.editPairDialog.cancelButton.onClick.AddListener(() => { hideAll(); });

        current.editPairDialog.okButton.onClick.AddListener(() => {
            // first validate
            current.editPairDialog.wrongInput.text = "";

            if (current.editPairDialog.selectedDayOfWeek == DayOfWeek.Sunday)
            {
                current.editPairDialog.wrongInput.text = "выберите день недели";
                return;
            }


            var provider      = System.Globalization.CultureInfo.GetCultureInfo("ru-RU");
            var dateTimeStyle = System.Globalization.DateTimeStyles.AssumeLocal;
            DateTime tryParseDateTimeResult;

            if (!DateTime.TryParseExact(current.editPairDialog.startTime.text, "HH:mm", provider, dateTimeStyle, out tryParseDateTimeResult))
            {
                current.editPairDialog.wrongInput.text = "неверный формат начального времени, пример: 09:30";
                return;
            }

            if (!DateTime.TryParseExact(current.editPairDialog.endTime.text, "HH:mm", provider, dateTimeStyle, out tryParseDateTimeResult))
            {
                current.editPairDialog.wrongInput.text = "неверный формат конечного времени, пример: 09:30";
                return;
            }

            if (current.editPairDialog.name.text.Length < 3)
            {
                current.editPairDialog.wrongInput.text = "плохое нзвание пары";
                return;
            }

            if (current.editPairDialog.location.text.Length == 0)
            {
                current.editPairDialog.wrongInput.text = "введите место или кабинет";
                return;
            }

            Debug.LogWarning("week̉̉ " + week + "  is nulL: " + (week == null?"YES":"NO"));
            Debug.LogWarning("weekStartDay " + week.weekStartDay + "  is nulL: " + (week.weekStartDay == null?"YES":"NO"));
            Debug.LogWarning("selected day " + current.editPairDialog.selectedDayOfWeek + " is nulL: " + (current.editPairDialog.selectedDayOfWeek == null?"YES":"NO"));


            var day_t      = week.weekStartDay.AddDays((int)current.editPairDialog.selectedDayOfWeek - 1).Date;
            var name_t     = current.editPairDialog.name.text.Trim();
            var time_t     = current.editPairDialog.startTime.text + "–" + current.editPairDialog.endTime.text.Trim();
            var location_t = current.editPairDialog.location.text.Trim();
            var lecturer_t = current.editPairDialog.lecturer.text.Trim();

            var newPair = new Pair(
                day_t,
                name_t,
                time_t,
                location_t,
                lecturer_t,
                true,
                false,
                TimetableParser.GetPairMD5(day_t.DayOfWeek, time_t, name_t, location_t, lecturer_t, week.weekType)
                );

            // location
            var app = AppScript.getSharedInstance();
            if (newPair.room == "" || newPair.room == null)
            {
                if (app.facilities.hasRoom(newPair.location.Trim()))
                {
                    newPair.room = newPair.location.Trim();
                }
                else
                {
                    Debug.LogWarning("BBB");
                }
            }
            else
            {
                Debug.LogWarning("AAA");
            }

            yesAction(newPair);


            hideAll();
        });
    }
Esempio n. 2
0
    private bool saveTimetableToDatabase(WeekTimetable w1, WeekTimetable w2, bool preserveEdits = false)
    {
        bool anyChanges = false;

//		var start_t = Time.realtimeSinceStartup;
        if (!preserveEdits)
        {
            anyChanges = true;
            // simply remove all pairs from db and add provided
            clearDb();

            using (var db = new SQLiteConnection(db_path)) {
                foreach (var w in new List <WeekTimetable>()
                {
                    w1, w2
                })
                {
                    Debug.LogWarning("Saving week of type " + w.weekType);
                    foreach (var d in w.days)
                    {
                        foreach (var p in d.pairs)
                        {
                            var pair = new TimetableRecord();
                            pair.weekType  = w.weekType;
                            pair.day       = d.day.DayOfWeek;
                            pair.startTime = p.startTime;
                            pair.endTime   = p.endTime;
                            pair.name      = p.name;
                            pair.lecturer  = p.lecturer;
                            pair.location  = p.location;
                            pair.room      = p.room;

                            pair.edited       = p.edited ? 1 : 0;
                            pair.deleted      = p.deleted ? 1 : 0;
                            pair.initial_hash = p.initial_hash;
                            pair.hash         = p.hash;

                            db.Insert(pair);
                        }
                    }
                }

                db.Close();

                //Debug.LogWarning ("Timetable saved to database ("+(Time.renderedFrameCount-start_t)+"s)");
            }
        }
        else
        {
            // foreach exisiting pair check for similar (with hash) in new pairs
            // if found - dont add new pair
            // if not found remove (real remove) existing pair
            // Then add other new pairs

            var old_weeks = new List <WeekTimetable>()
            {
                currentWeek, nextWeek
            };
            var new_weeks = new List <WeekTimetable>()
            {
                w1, w2
            };

            // create one list with all existing pairs
            var exisitingPairs = new List <Pair>();
            foreach (var ex_w in old_weeks)
            {
                foreach (var d in ex_w.days)
                {
                    foreach (var p in d.pairs)
                    {
                        exisitingPairs.Add(p);
                    }
                }
            }

            // create another list with all new pairs
            var newPairs = new List <Pair>();
            foreach (var new_w in new_weeks)
            {
                foreach (var d in new_w.days)
                {
                    foreach (var new_p in d.pairs)
                    {
                        newPairs.Add(new_p);
                    }
                }
            }


            // v1.3 hash migration
            if (exisitingPairs.Where(x => x.hash == null).Count() > 0)
            {
                // needs migration
                //Debug.LogError ("Needs v1.3 pair migration");

                for (int i = 0; i < 2; i++)
                {
                    var old_w = old_weeks [i]; var new_w = new_weeks [i];

                    var old_w_pairs = new List <Pair>();
                    foreach (var d in old_w.days)
                    {
                        foreach (var p in d.pairs)
                        {
                            old_w_pairs.Add(p);
                        }
                    }

                    var new_w_pairs = new List <Pair>();
                    foreach (var d in new_w.days)
                    {
                        foreach (var new_p in d.pairs)
                        {
                            new_w_pairs.Add(new_p);
                        }
                    }


                    foreach (var ex_p in old_w_pairs)
                    {
                        if (ex_p.hash == null)
                        {
                            //Debug.LogError ("Found null hash");
                            // pair needs migration
                            if (ex_p.edited)
                            {
                                // than needs more work:
                                // if its the only pair on this week with such hash
                                if (old_w_pairs.Where(x => x.initial_hash == ex_p.initial_hash).Count() == 1)
                                {
                                    // try to find in new pairs pair with same initial_hash
                                    var new_pairs_with_same_initial_hash = new_w_pairs.Where(
                                        x => ex_p.initial_hash == x.initial_hash
                                        ).ToList();
                                    if (new_pairs_with_same_initial_hash.Count > 0)
                                    {
                                        //Debug.LogWarning ("Found similar pair in new data for migration");
                                        // than take this new pair and calculate old pair's hash using it
                                        var new_pair = new_pairs_with_same_initial_hash [0];
                                        ex_p.hash = new_pair.hash;
                                    }
                                    else
                                    {
                                        //Debug.LogError ("Cant find similar (initila_hash) pair in new data");
                                        // in this case cant restore
                                        // than replace it with new pairs on update
                                    }
                                }
                                else
                                {
                                    //Debug.LogError ("Pair is not single on this week");
                                    // cant restore - replace on update
                                }
                            }
                            else
                            {
                                // just calculate new hash
                                if (ex_p.deleted)
                                {
                                    //Debug.LogWarning ("Set new hash for deleted pair");
                                }
                                ex_p.hash = TimetableParser.GetPairMD5(ex_p.day.DayOfWeek, ex_p.time, ex_p.name, ex_p.location, ex_p.lecturer, old_w.weekType);
                            }
                        }
                        else
                        {
                            //Debug.LogError ("HASH IS NOT NULL!");
                        }
                    }
                }
            }



            // foreach existing pair check if it stays or not
            foreach (var ex_p in exisitingPairs)
            {
                if (ex_p.hash == null)
                {
                    Debug.LogError("Exisiting pair hash is null!");
                }
                if (newPairs.FindAll(x => x.hash == ex_p.hash).Count == 0)
                {
                    // no such pair in new data - delete it from our local data
                    removePair(ex_p, immediateSaveToDB: false);
                    anyChanges = true;
                }
                else
                {
                    // found such pair in new data
                    // remove it from list of new data
                    newPairs.RemoveAll(x => x.hash == ex_p.hash);
                }
            }

            // save our removals to DB
            saveTimetableToDatabase(currentWeek, nextWeek);

            // add rest of new data
            foreach (var new_p in newPairs)
            {
                addPair(new_p, saveState: false);                 // adds directly to db
                anyChanges = true;
            }

            // done saving new data
        }

        return(anyChanges);
    }