Esempio n. 1
0
        // Shows a file picker to select the wallet file
        private async void onSelectFile()
        {
            byte[] _data = null;
            try
            {
                var picker_service = DependencyService.Get <IFilePicker>();

                SpixiImageData fileData = await picker_service.PickFileAsync();

                if (fileData == null)
                {
                    return; // User canceled file picking
                }
                var stream = fileData.stream;
                _data = new byte[stream.Length];
                stream.Read(_data, 0, (int)stream.Length);
            }
            catch (Exception ex)
            {
                Logging.error("Exception choosing file: " + ex.ToString());
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                displaySpixiAlert(SpixiLocalization._SL("intro-restore-file-error-title"), SpixiLocalization._SL("intro-restore-file-selecterror-text"), SpixiLocalization._SL("global-dialog-ok"));
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                return;
            }

            if (_data == null)
            {
                await displaySpixiAlert(SpixiLocalization._SL("intro-restore-file-error-title"), SpixiLocalization._SL("intro-restore-file-readerror-text"), SpixiLocalization._SL("global-dialog-ok"));

                return;
            }

            string docpath  = Config.spixiUserFolder;
            string filepath = Path.Combine(docpath, Config.walletFile + ".tmp");
            try
            {
                File.WriteAllBytes(filepath, _data);
            }
            catch (Exception ex)
            {
                Logging.error("Exception caught in process: {0}", ex);
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                displaySpixiAlert(SpixiLocalization._SL("intro-restore-file-error-title"), SpixiLocalization._SL("intro-restore-file-writeerror-text"), SpixiLocalization._SL("global-dialog-ok"));
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                return;
            }

            Utils.sendUiCommand(webView, "enableRestore");
        }
Esempio n. 2
0
        public async Task onChangeAvatarAsync(object sender, EventArgs e)
        {
            var picker_service = DependencyService.Get <IFilePicker>();

            SpixiImageData spixi_img_data = await picker_service.PickImageAsync();

            if (spixi_img_data == null)
            {
                return;
            }

            Stream stream = spixi_img_data.stream;

            if (stream == null)
            {
                return;
            }

            var file_path = Path.Combine(Node.localStorage.avatarsPath, "avatar-tmp.jpg");

            try
            {
                byte[] image_bytes = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    stream.CopyTo(ms);
                    stream.Close();
                    image_bytes = picker_service.ResizeImage(ms.ToArray(), 960, 960, 80);
                    if (image_bytes == null)
                    {
                        return;
                    }
                }

                FileStream fs = new FileStream(file_path, FileMode.OpenOrCreate, FileAccess.Write);
                fs.Write(image_bytes, 0, image_bytes.Length);
                fs.Close();
            }
            catch (Exception ex)
            {
                await displaySpixiAlert(SpixiLocalization._SL("intro-new-avatarerror-title"), ex.ToString(), SpixiLocalization._SL("global-dialog-ok"));

                return;
            }

            Utils.sendUiCommand(webView, "loadAvatar", file_path);
            Node.changedSettings = true;
        }
Esempio n. 3
0
        public async Task <SpixiImageData> PickFileAsync()
        {
            FileResult fileData = await FilePicker.PickAsync();

            if (fileData == null)
            {
                return(null); // User canceled file picking
            }
            SpixiImageData spixi_img_data = new SpixiImageData()
            {
                name = Path.GetFileName(fileData.FullPath), path = fileData.FullPath, stream = await fileData.OpenReadAsync()
            };

            // Return Task object
            return(spixi_img_data);
        }
Esempio n. 4
0
        public async Task <SpixiImageData> PickFileAsync()
        {
            OpenFileDialog file_dialog = new OpenFileDialog();

            file_dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            SpixiImageData spixi_img_data = null;

            if (file_dialog.ShowDialog() == true)
            {
                spixi_img_data = new SpixiImageData()
                {
                    name = Path.GetFileName(file_dialog.FileName), path = file_dialog.FileName, stream = File.OpenRead(file_dialog.FileName)
                };
            }

            // Return Task object
            return(spixi_img_data);
        }
Esempio n. 5
0
        public async Task onChangeAvatarAsync(object sender, EventArgs e)
        {
            var picker_service = DependencyService.Get <IPicturePicker>();

            SpixiImageData spixi_img_data = await picker_service.PickImageAsync();

            Stream stream = spixi_img_data.stream;

            if (stream == null)
            {
                return;
            }

            var file_path = Node.localStorage.getOwnAvatarPath(false);

            try
            {
                byte[] image_bytes = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    stream.CopyTo(ms);
                    stream.Close();
                    image_bytes = picker_service.ResizeImage(ms.ToArray(), 960, 960);
                    if (image_bytes == null)
                    {
                        return;
                    }
                }

                FileStream fs = new FileStream(file_path, FileMode.OpenOrCreate, FileAccess.Write);
                fs.Write(image_bytes, 0, image_bytes.Length);
                fs.Close();
            }
            catch (Exception ex)
            {
                await displaySpixiAlert("Error", ex.ToString(), "ok");

                return;
            }

            Utils.sendUiCommand(webView, "loadAvatar", file_path);
        }
        public Task <SpixiImageData> PickImageAsync()
        {
            OpenFileDialog file_dialog = new OpenFileDialog();

            file_dialog.Filter           = "Image Files (*.jpeg, *.jpg, *.png)|*.jpeg;*.jpg;*.png";
            file_dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            SpixiImageData spixi_img_data = null;

            if (file_dialog.ShowDialog() == true)
            {
                spixi_img_data = new SpixiImageData()
                {
                    name = Path.GetFileName(file_dialog.FileName), path = file_dialog.FileName, stream = File.OpenRead(file_dialog.FileName)
                };
            }

            // Return Task object
            return(Task.FromResult(spixi_img_data));
        }
Esempio n. 7
0
        void OnImagePickerFinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs args)
        {
            UIImage image = args.EditedImage ?? args.OriginalImage;

            if (image != null)
            {
                // Convert UIImage to .NET Stream object
                NSData data = image.AsJPEG(1);

                SpixiImageData spixi_img_data = new SpixiImageData()
                {
                    name = Path.GetFileName(args.ImageUrl.AbsoluteString), path = "", stream = data.AsStream()
                };

                // Set the Stream as the completion of the Task
                taskCompletionSource.SetResult(spixi_img_data);
            }
            else
            {
                taskCompletionSource.SetResult(null);
            }
            imagePicker.DismissModalViewController(true);
        }
Esempio n. 8
0
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
        {
            base.OnActivityResult(requestCode, resultCode, intent);

            if (requestCode == PickImageId)
            {
                if ((resultCode == Result.Ok) && (intent != null))
                {
                    Android.Net.Uri uri = intent.Data;

                    SpixiImageData spixi_img_data = new SpixiImageData()
                    {
                        name = Path.GetFileName(uri.Path), path = uri.Path, stream = ContentResolver.OpenInputStream(uri)
                    };

                    // Set the Stream as the completion of the Task
                    PickImageTaskCompletionSource.SetResult(spixi_img_data);
                }
                else
                {
                    PickImageTaskCompletionSource.SetResult(null);
                }
            }
        }
Esempio n. 9
0
        public async Task onSendFile()
        {
            // Show file picker and send the file
            try
            {
                Stream stream   = null;
                string fileName = null;
                string filePath = null;

                // Special case for iOS platform
                if (Device.RuntimePlatform == Device.iOS)
                {
                    var picker_service = DependencyService.Get <IPicturePicker>();

                    SpixiImageData spixi_img_data = await picker_service.PickImageAsync();

                    stream = spixi_img_data.stream;

                    if (stream == null)
                    {
                        return;
                    }

                    fileName = spixi_img_data.name;
                    filePath = spixi_img_data.path;
                }
                else
                {
                    FileData fileData = await CrossFilePicker.Current.PickFile();

                    if (fileData == null)
                    {
                        return; // User canceled file picking
                    }
                    stream = fileData.GetStream();

                    fileName = fileData.FileName;
                    filePath = fileData.FilePath;
                }

                FileTransfer transfer = TransferManager.prepareFileTransfer(fileName, stream, filePath);
                Logging.info("File Transfer uid: " + transfer.uid);

                SpixiMessage spixi_message = new SpixiMessage(SpixiMessageCode.fileHeader, transfer.getBytes());

                StreamMessage message = new StreamMessage();
                message.type        = StreamMessageCode.data;
                message.recipient   = friend.walletAddress;
                message.sender      = Node.walletStorage.getPrimaryAddress();
                message.transaction = new byte[1];
                message.sigdata     = new byte[1];
                message.data        = spixi_message.getBytes();

                StreamProcessor.sendMessage(friend, message);


                string message_data = string.Format("{0}:{1}", transfer.uid, transfer.fileName);

                // store the message and display it
                FriendMessage friend_message = FriendList.addMessageWithType(message.id, FriendMessageType.fileHeader, friend.walletAddress, message_data, true);

                friend_message.transferId = transfer.uid;
                friend_message.filePath   = transfer.filePath;

                Node.localStorage.writeMessages(friend.walletAddress, friend.messages);
            }
            catch (Exception ex)
            {
                Logging.error("Exception choosing file: " + ex.ToString());
            }
        }