예제 #1
0
        public static async Task SynchronizeUserImageAsync()
        {
            try
            {
                // download user image
                string localUserImagePath = UserData.Instance.UserInfo == null ? null : AppTools.Instance.GetUserImageLocalPath(UserData.Instance.UserInfo.UserId);

                if (!string.IsNullOrWhiteSpace(localUserImagePath))
                {
                    string urlImage = await UserProfileWebService.GetUserProfileImageRelativeUrlAsync(UserData.Instance.UserInfo.UserId);

                    if (string.IsNullOrEmpty(urlImage))
                    {
                        IFileManager fileManager = Resolver.Resolve <IFileManager>();
                        if (fileManager.FileExist(localUserImagePath))
                        {
                            fileManager.DeleteFile(localUserImagePath);
                        }
                    }
                    else
                    {
                        await HttpConnector.Instance.DownloadFileAsync(urlImage, localUserImagePath);
                    }
                }
            }
            catch (Exception except)
            {
                ILogger.Instance.Error("Unable to synchronize user image", except);
            }
        }
        private async Task SelectUserPictureActionAsync()
        {
            try
            {
                string imagePath = UserData.Instance.UserInfo == null ? null : AppTools.Instance.GetUserImageLocalPath(UserData.Instance.UserInfo.UserId);

                if (string.IsNullOrWhiteSpace(imagePath))
                {
                    return;
                }

                MediaFile mediaFile = null;
                await CrossMedia.Current.Initialize();

                if (await _userDialog.ConfirmAsync(Translation.Get(TRS.DO_YOU_WANT_TAKE_PHOTO_WITH_CAMERA_PI), Translation.Get(TRS.QUESTION), Translation.Get(TRS.YES), Translation.Get(TRS.NO)))
                {
                    // take a photo
                    if (!CrossMedia.Current.IsCameraAvailable)
                    {
                        await _userDialog.AlertAsync(string.Format(Translation.Get(TRS.P0_ISNT_SUPPORTED_ON_THIS_DEVICE), Translation.Get(TRS.TAKE_PHOTO)), Translation.Get(TRS.ERROR), Translation.Get(TRS.OK));

                        return;
                    }
                    StoreCameraMediaOptions cameraOption = new StoreCameraMediaOptions()
                    {
                        Directory   = "BodyReport",
                        Name        = "tempPhoto.jpg",
                        SaveToAlbum = true
                    };
                    mediaFile = await CrossMedia.Current.TakePhotoAsync(cameraOption);
                }
                else
                {
                    if (!CrossMedia.Current.IsTakePhotoSupported)
                    {
                        await _userDialog.AlertAsync(string.Format(Translation.Get(TRS.P0_ISNT_SUPPORTED_ON_THIS_DEVICE), Translation.Get(TRS.SELECT_PICTURE)), Translation.Get(TRS.ERROR), Translation.Get(TRS.OK));

                        return;
                    }
                    mediaFile = await CrossMedia.Current.PickPhotoAsync();
                }

                if (mediaFile == null || string.IsNullOrWhiteSpace(mediaFile.Path))
                {
                    return;
                }

                string pngImagePath = Path.Combine(AppTools.TempDirectory, UserData.Instance.UserInfo.UserId.ToString() + ".png");
                BodyReportMobile.Core.Framework.IMedia.Instance.CompressImageAsPng(mediaFile.Path, pngImagePath, 400);

                //Upload on server
                string uploadedRelativeUrl = await UserProfileWebService.UploadUserProfilePictureAsync(pngImagePath);

                if (string.IsNullOrWhiteSpace(uploadedRelativeUrl))
                {
                    _fileManager.DeleteFile(pngImagePath);
                    await _userDialog.AlertAsync(string.Format(Translation.Get(TRS.IMPOSSIBLE_TO_UPDATE_P0), Translation.Get(TRS.IMAGE)), Translation.Get(TRS.ERROR), Translation.Get(TRS.OK));
                }
                else //Copy file on local
                {
                    _fileManager.CopyFile(pngImagePath, imagePath);
                    _fileManager.DeleteFile(pngImagePath);
                    DisplayUserProfil();
                }
            }
            catch (Exception except)
            {
                ILogger.Instance.Error("Can't take user profile image", except);
            }
        }