// CONSTRUCTORS /// <summary> /// Initialize a new instance of <see cref="MainWindowViewModel"/> /// </summary> public MainWindowViewModel() { this.selectedPhotoIndex = Core.Configuration.Constants.WRONG_INDEX; this.selectedPhoto = null; this.photos = new Collections.ReverseCollection <DataAccess.Entities.Photo>(DataStorage.ShownUser.Photos); if (IsCurrentUserShown) { this.isFollowing = false; } else { this.isFollowing = DataStorage.ShownUser.Followers.Contains(DataStorage.LoggedUser); } this.goHomeCommand = new Commands.User.MainWindow.GoHomeCommand(this); this.askQuestionCommand = new Commands.User.MainWindow.AskQuestionCommand(this); this.followCommand = new Commands.User.MainWindow.FollowCommand(this); this.logOutCommand = new Commands.User.MainWindow.LogOutCommand(); this.searchUserCommand = new Commands.User.MainWindow.SearchUserCommand(this); this.settingCommand = new Commands.User.MainWindow.SettingCommand(this); this.showFollowListCommand = new Commands.User.MainWindow.ShowFollowListCommand(this); this.showPhotoCommand = new Commands.User.MainWindow.ShowPhotoCommand(this); this.uploadPhotoCommand = new Commands.User.MainWindow.UploadPhotoCommand(this); }
/// <summary> /// Executes command /// </summary> /// <param name="parameter"> /// Command parameters /// </param> public override void Execute(object parameter) { Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, $"Execute {nameof(UploadPhotoCommand)}"); Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Open file dialog to upload photo"); string[] photoNames = Services.WindowManager.Instance.OpenFileDialog(filterString: Core.Configuration.DBConfig.PHOTO_EXTENSION, isMultiselectAllowed: true); // user select a photo if (photoNames != null) { Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Adding photo"); foreach (string photoPath in photoNames) { Core.Logger.GetLogger.LogAsync(Core.LogMode.Info, $"Photo path {photoPath}"); // get random free photo name string serverPath = GetRandomFreeName(mainWindowViewModel.DataStorage.LoggedUser.Id.ToString(), photoPath); Core.Logger.GetLogger.LogAsync(Core.LogMode.Info, $"Server photo path {serverPath}"); // copy photo to server Core.Logger.GetLogger.LogAsync(Core.LogMode.Info, "Copy photo to server"); CopyPhotoToServer(userId: mainWindowViewModel.DataStorage.LoggedUser.Id.ToString(), pathToPhoto: photoPath, serverPath: serverPath); // create photo DataAccess.Entities.Photo photo = new DataAccess.Entities.Photo { User = mainWindowViewModel.DataStorage.LoggedUser, Path = serverPath }; // add photo to a view, if only user is on his own page if (mainWindowViewModel.IsCurrentUserShown) { Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Add photo to view"); mainWindowViewModel.Photos.Add(photo); } // insert photo to DB Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Insert photo to photo repositories"); mainWindowViewModel.UnitOfWork.PhotoRepository.Insert(photo); } // save to DB Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Save changes to DataBase"); mainWindowViewModel.UnitOfWork.Save(); // go to your profile, if can if (mainWindowViewModel.GoHomeCommand.CanExecute(null)) { Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Go to your profile"); mainWindowViewModel.GoHomeCommand.Execute(null); } } }
// CONSTRUCTORS /// <summary> /// Initialize a new instance of <see cref="PhotoInsideViewModel"/> /// </summary> public PhotoInsideViewModel(DataAccess.Entities.Photo photo) { this.photo = photo; this.selectedCommentIndex = Core.Configuration.Constants.WRONG_INDEX; this.comments = new ObservableCollection <CommentWrapper>(photo.Comments.Select(comment => new CommentWrapper(comment))); this.commentText = string.Empty; this.likeDislikeAmount = UnitOfWork.PhotoRepository.GetLikeDislikeAmount(photo); this.likeValue = UnitOfWork.PhotoLikeRepository.HasLiked(photo, Services.DataStorage.Instance.LoggedUser); this.likePhotoCommand = new Commands.User.PhotoInside.LikePhotoCommand(this); this.likeCommentCommand = new Commands.User.PhotoInside.LikeCommentCommand(this); this.writeCommentCommand = new Commands.User.PhotoInside.WriteCommentCommand(this); this.deleteCommentCommand = new Commands.User.PhotoInside.DeleteCommentCommand(this); }
// CONSTRUCTORS /// <summary> /// Initializes a new instance of <see cref="SingleViewModel"/> /// </summary> /// <param name="photo"> /// An instance of <see cref="DataAccess.Entities.Photo"/> to show /// </param> /// <param name="isEditingEnabled"> /// Determines if entities can be edited /// </param> public SingleViewModel(DataAccess.Entities.Photo photo, bool isEditingEnabled) : base(shownEntity: photo, isWritingEnabled: isEditingEnabled) { ILookup <bool, string> groupLikes = photo.Likes.ToLookup(l => l.IsLiked, p => p.User.NickName); likedUserName = groupLikes[true].ToArray(); disLikedUserName = groupLikes[false].ToArray(); // commands this.deleteCommand = new Commands.MultipleCommand(new CommandBase[] { new Commands.Admin.DeleteCommand(), new Commands.Shared.DeletePhotoFromServerCommand(photo.Path) }); Logger.LogAsync(Core.LogMode.Debug, $"Initializes {nameof(SingleViewModel)}"); }
/// <summary> /// Executes command /// </summary> /// <param name="parameter"> /// Command parameters /// <para/> /// 1 — bool, is like or dislike /// </param> public override void Execute(object parameter) { Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, $"Execute {nameof(LikePhotoCommand)}"); // gets value bool isLike = System.Convert.ToBoolean(parameter); DataAccess.Entities.Photo photo = photoInsideViewModel.Photo; DataAccess.Entities.User user = Services.DataStorage.Instance.LoggedUser; DataAccess.Entities.PhotoLike photoLike = photoInsideViewModel.UnitOfWork.PhotoLikeRepository.TryGetUserLike(photo, user); // suitable for likes and dislikes // STEPS // 1. if like is new -> add it // 2. else if there is like or dislike // 2.1. if click on the same button type -> remove like // 2.2 else if click on the opposite button type -> toggle like Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, isLike ? "Liking" : "Disliking"); if (photoLike == null)// 1. add like { Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Add like"); Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Add to data base"); // update data base DataAccess.Context.UnitOfWork.Instance.PhotoLikeRepository.Insert(new DataAccess.Entities.PhotoLike { IsLiked = isLike, Photo = photo, User = user }); // update view Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Update View"); if (isLike) { ++photoInsideViewModel.LikeDislikeAmount.LikesAmount; } else { ++photoInsideViewModel.LikeDislikeAmount.DisLikesAmount; } // liked photoInsideViewModel.LikeValue = isLike; } else// 2. there is a like { if (photoLike.IsLiked == isLike)// 2.1 remove like { Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Remove like"); Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Update valuse in data base"); // updata data base DataAccess.Context.UnitOfWork.Instance.PhotoLikeRepository.Delete(photoLike); DataAccess.Context.UnitOfWork.Instance.PhotoRepository.Update(photo); // update view Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Update View"); if (isLike) { --photoInsideViewModel.LikeDislikeAmount.LikesAmount; } else { --photoInsideViewModel.LikeDislikeAmount.DisLikesAmount; } // same button, remove photoInsideViewModel.LikeValue = null; } else// 2.2 toggle dislike to like { Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Toggle like"); Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Update valuse in data base"); // updata database photoLike.IsLiked = isLike; // toggle DataAccess.Context.UnitOfWork.Instance.PhotoLikeRepository.Update(photoLike); // update view, toggle value Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Update View"); if (isLike) { ++photoInsideViewModel.LikeDislikeAmount.LikesAmount; --photoInsideViewModel.LikeDislikeAmount.DisLikesAmount; } else { --photoInsideViewModel.LikeDislikeAmount.LikesAmount; ++photoInsideViewModel.LikeDislikeAmount.DisLikesAmount; } // toggle like photoInsideViewModel.LikeValue = isLike; } } // notify view Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Update view. Raise event"); photoInsideViewModel.UpdateLikes(); // save changes to database Core.Logger.GetLogger.LogAsync(Core.LogMode.Debug, "Save changes to data base"); DataAccess.Context.UnitOfWork.Instance.Save(); }