Exemplo n.º 1
0
        public EditTagsViewModel(IOfflineTaskService offlineTaskService, ILoggingService loggingService, SQLite.Net.SQLiteConnection database, INavigationService navigation)
        {
            _offlineTaskService = offlineTaskService;
            _loggingService     = loggingService;
            _database           = database;
            _navigationService  = navigation;

            _loggingService.WriteLine("Creating new instance of EditTagsViewModel.");

            FinishCommand = new RelayCommand(async() => await FinishAsync());
            CancelCommand = new RelayCommand(() => Cancel());

            TagQueryChangedCommand = new RelayCommand(() =>
            {
                _loggingService.WriteLine($"Tag query changed: {TagQuery}");
                UpdateSuggestions();
            });
            TagSubmittedCommand = new RelayCommand <Tag>(suggestion =>
            {
                _loggingService.WriteLine($"Tag was submitted.");

                if (suggestion != null && !Tags.Contains(suggestion))
                {
                    _loggingService.WriteLine("Tag wasn't in list yet. Added.");
                    Tags.Add(suggestion);

                    UpdateSuggestions();
                }
                else if (!string.IsNullOrEmpty(TagQuery))
                {
                    var tags = TagQuery.Split(',').ToList();
                    _loggingService.WriteLine($"Adding {tags.Count} tags to the list.");

                    foreach (string item in tags)
                    {
                        if (!string.IsNullOrWhiteSpace(item))
                        {
                            var existingTag = _database.FindWithQuery <Tag>("select * from Tag where Label=?", item);
                            Tag newTag      = null;

                            if (existingTag != null)
                            {
                                newTag = existingTag;
                            }
                            else
                            {
                                newTag = new Tag()
                                {
                                    Label = item, Id = Tags.Count + 1
                                }
                            };

                            if (Tags.Contains(newTag) == false)
                            {
                                Tags.Add(newTag);
                            }
                        }
                    }
                }

                TagQuery = string.Empty;
                RaisePropertyChanged(nameof(TagsCountIsZero));
            });
        }