Esempio n. 1
0
        public async void sendText()
        {
            Message msgResponse = null;

            if (TextMsg != null && TextMsg != "")
            {
                LoadingIndicator = true;

                if (App.User == null)
                {
                    ContentDialog notLoggedInDialog = new ContentDialog()
                    {
                        Title             = "Not logged in",
                        Content           = "You are not logged in and cannot sync with the server",
                        PrimaryButtonText = "Ok"
                    };

                    await ContentDialogHelper.CreateContentDialogAsync(notLoggedInDialog, true);

                    return;
                }

                MessageApi api = MessageApi.getInstance();

                Message sentMsg = new Message
                {
                    SenderId    = App.User.id,
                    GroupChatID = group.group.id,
                    Content     = TextMsg
                };

                msgResponse = await api.sendMessageData(sentMsg);

                // If message was not sent to the server, initialize empty message with group id
                if (msgResponse == null)
                {
                    msgResponse = new PenscribCommon.Models.Message
                    {
                        id          = "---",
                        GroupChatID = group.group.id,
                        Content     = TextMsg
                    };
                    MessageContent badmsg = new MessageContent(msgResponse, App.User, null, MessageContent.errorImage);
                    History.Add(badmsg);
                    // Launch warning popup
                    ContentDialog notLoggedInDialog = new ContentDialog()
                    {
                        Title             = "No Wifi",
                        Content           = "Your message did not send. Please check your wifi signal.",
                        PrimaryButtonText = "Ok"
                    };
                    await ContentDialogHelper.CreateContentDialogAsync(notLoggedInDialog, true);
                }
                else
                {
                    MessageContent msg = new MessageContent(msgResponse, App.User, null);
                    History.Add(msg);
                }

                TextMsg          = "";
                LoadingIndicator = false;
            }
        }
Esempio n. 2
0
        private async void send()
        {
            if (App.User == null)
            {
                ContentDialog notLoggedInDialog = new ContentDialog()
                {
                    Title             = "Not logged in",
                    Content           = "You are not logged in and cannot sync with the server",
                    PrimaryButtonText = "Ok"
                };

                await ContentDialogHelper.CreateContentDialogAsync(notLoggedInDialog, true);

                return;
            }

            try
            {
                LoadingIndicator = true;
                // Create a file for storing the drawing
                string        filename      = DateTime.Now.ToString("yyyy-MM-ddTHHmmssffff") + ".png";
                StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
                StorageFile   targetFile    = await storageFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);

                System.Diagnostics.Debug.WriteLine("Saving message as " + targetFile.Path);

                // Used to pass message back to MessagesHistory
                BitmapImage bitmapImage = new BitmapImage();

                Message msgResponse = null;

                if (targetFile != null)
                {
                    CanvasDevice       device       = CanvasDevice.GetSharedDevice();
                    CanvasRenderTarget renderTarget = new CanvasRenderTarget(device, (int)canvasElements.canvas.ActualWidth,
                                                                             (int)canvasElements.canvas.ActualHeight, BITMAP_DPI_RESOLUTION);

                    using (var drawSession = renderTarget.CreateDrawingSession())
                    {
                        // Set the background color of the image
                        drawSession.Clear(Colors.White);
                        //Transfer the background image to the bitmap image if there is a background image
                        if (BackgroundImagePath != null && BackgroundImagePath.Length > 0)
                        {
                            CanvasBitmap background = await CanvasBitmap.LoadAsync(device,
                                                                                   formatBackgroundFilePath(BackgroundImagePath));

                            drawSession.DrawImage(background);
                        }
                        if (imageFilePath != null)
                        {
                            System.Diagnostics.Debug.WriteLine(" SENDING IMAGE FILE ");
                            CanvasBitmap image = await CanvasBitmap.LoadAsync(device, imageFilePath);

                            drawSession.DrawImage(image, new Windows.Foundation.Rect(xOffset, yOffset, newWidth, newHeight));
                        }
                        // Transfer all the strokes to the bitmap image
                        drawSession.DrawInk(canvasElements.canvas.InkPresenter.StrokeContainer.GetStrokes());
                    }

                    using (IRandomAccessStream stream = await targetFile.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        await renderTarget.SaveAsync(stream, CanvasBitmapFileFormat.Png, 1f);
                    }


                    MessageApi api = MessageApi.getInstance();

                    Message sentMsg = new Message
                    {
                        SenderId    = App.User.id,
                        GroupChatID = group.group.id
                    };

                    msgResponse = await api.sendMessageData(sentMsg);

                    if (msgResponse != null)
                    {
                        using (Stream stream = await targetFile.OpenStreamForReadAsync())
                        {
                            // Send the image file. If successful rename the local file to the id
                            if (await api.sendMessageFile(stream, msgResponse.id))
                            {
                                // rename the locally saved file to incorporate the server generated id
                                await targetFile.RenameAsync(msgResponse.id + ".png");
                            }
                            else
                            {
                                System.Diagnostics.Debug.WriteLine("File failed to send");
                                // Launch warning popup
                                ContentDialog notLoggedInDialog = new ContentDialog()
                                {
                                    Title             = "File failed to send",
                                    Content           = "Your message did not send. Please check your wifi signal.",
                                    PrimaryButtonText = "Ok"
                                };
                                await ContentDialogHelper.CreateContentDialogAsync(notLoggedInDialog, true);
                            }
                        }
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("Message failed to send");
                        // Launch warning popup
                        ContentDialog notLoggedInDialog = new ContentDialog()
                        {
                            Title             = "File failed to send",
                            Content           = "Your message did not send. Please check your wifi signal.",
                            PrimaryButtonText = "Ok"
                        };
                        await ContentDialogHelper.CreateContentDialogAsync(notLoggedInDialog, true);
                    }
                }
                MessageContent msg      = null;
                Uri            imageUri = new Uri(targetFile.Path, UriKind.Absolute);

                // If message was not sent to the server, initialize empty message with group id
                if (msgResponse == null)
                {
                    msgResponse = new PenscribCommon.Models.Message
                    {
                        id          = "---",
                        GroupChatID = group.group.id,
                    };
                    msg = new MessageContent(msgResponse, App.User, imageUri, MessageContent.errorImage);
                }
                else
                {
                    msg = new MessageContent(msgResponse, App.User, imageUri);
                }

                //Uri imageUri = new Uri(targetFile.Path, UriKind.Absolute);
                //MessageContent msg = new MessageContent(msgResponse, App.User, imageUri);

                LeftFrameNavigator.NavigateSubFrame(typeof(MessageHistoryView), msg);

                // Clear the canvas
                canvasElements.canvas.InkPresenter.StrokeContainer.Clear();

                // Clear any background image
                BackgroundImagePath  = null;
                group.backgroundPath = null;

                // Clear any loaded image
                imageFilePath        = null;
                sourceImage          = null;
                ImageBitmap          = sourceImage;
                group.imageContainer = null;
            }
            finally
            {
                LoadingIndicator = false;
            }
        }