Esempio n. 1
0
        async void SaveButton_Click(object sender, EventArgs e)
        {
            MessageDialog messageDialog = new MessageDialog();

            messageDialog.ShowLoading();

            if (!ValidateForm())
            {
                messageDialog.HideLoading();
                return;
            }


            StaticData staticData = new StaticData();

            int numberOfFloors = 0;

            if (!String.IsNullOrEmpty(nooOfFoors.Text))
            {
                numberOfFloors = Convert.ToInt32(nooOfFoors.Text);
            }

            if (PhotoIsChanged)
            {
                FileName = SaveImage(((BitmapDrawable)buildingPhoto.Drawable).Bitmap);
            }

            Building item = new Building
            {
                Id                      = building.Id,
                BuildingName            = buildingName.Text,
                BuildingNumber          = building.Id == 0 ? facilityId + staticData.RandomDigits(10) : building.BuildingNumber,
                BuildingType            = buildingType.SelectedItem.ToString(),
                BuildingStandard        = buildingstandard.SelectedItem.ToString(),
                Status                  = utilisationStatus.Text,
                NumberOfFloors          = numberOfFloors,
                FootPrintArea           = Convert.ToDouble(totalFootprintAream2.Text),
                ImprovedArea            = Convert.ToDouble(totalImprovedaAeam2.Text),
                Heritage                = heritage.Checked == true ? true :false,
                OccupationYear          = occupationYear.Text,
                DisabledAccess          = disabledAccesss.SelectedItem.ToString(),
                DisabledComment         = disabledComment.Text,
                ConstructionDescription = constructionDescription.Text,
                GPSCoordinates          = _GPSCoordinates,
                Photo                   = PhotoIsChanged == true ? FileName : building.Photo,
                CreatedDate             = new DateTime(),
                CreatedUserId           = userId,
                ModifiedUserId          = userId,
                Facility                = new Facility
                {
                    Id = facilityId
                }
            };
            bool isAdded = false;

            if (!isEdit)
            {
                isAdded = await ViewModel.AddBuildingAsync(item);
            }
            else
            {
                isAdded = await ViewModel.UpdateBuildingAsync(item);
            }

            if (isAdded)
            {
                if (PhotoIsChanged)
                {
                    PictureViewModel pictureViewModel = new PictureViewModel();
                    Bitmap           _bm = ((BitmapDrawable)buildingPhoto.Drawable).Bitmap;
                    string           bal = "";
                    if (_bm != null)
                    {
                        MemoryStream stream = new MemoryStream();
                        _bm.Compress(Bitmap.CompressFormat.Jpeg, 100, stream);
                        byte[] ba = stream.ToArray();
                        bal = Base64.EncodeToString(ba, Base64.Default);
                    }

                    Models.Picture picture = new Models.Picture()
                    {
                        Name = FileName,
                        File = bal,
                    };
                    List <Models.Picture> pictures = new List <Models.Picture>();
                    pictures.Add(picture);
                    await pictureViewModel.ExecuteSavePictureCommand(pictures);
                }
                messageDialog.HideLoading();
                if (!isEdit)
                {
                    messageDialog.SendToast("Building is added successful.");
                }
                else
                {
                    messageDialog.SendToast("Building is updated successful.");
                }
                Finish();
            }
            else
            {
                messageDialog.HideLoading();
                if (!isEdit)
                {
                    messageDialog.SendToast("Unable to add new building.");
                }
                else
                {
                    messageDialog.SendToast("Unable to update building.");
                }
            }
        }
        private async void SaveFacility()
        {
            if (appPreferences.IsOnline(Application.Context))
            {
                MessageDialog messageDialog = new MessageDialog();
                messageDialog.ShowLoading();
                if (imageNames.Count() == 0)
                {
                    imageNames = new List <string>();
                }

                if (FirstPhotoIsChanged)
                {
                    string thisFileName = appPreferences.SaveImage(((BitmapDrawable)facilityPhoto.Drawable).Bitmap);
                    if (imageNames.Count() > 0)
                    {
                        imageNames[0] = thisFileName;
                    }
                    else
                    {
                        imageNames.Add(thisFileName);
                    }
                }
                if (SecondPhotoIsChanged)
                {
                    var _fileName = String.Format("facility_{0}", Guid.NewGuid());
                    appPreferences.SaveImage(((BitmapDrawable)secondFacilityPhoto.Drawable).Bitmap, _fileName);
                    if (imageNames.Count() > 1)
                    {
                        imageNames[1] = _fileName;
                    }
                    else
                    {
                        imageNames.Add(_fileName);
                    }
                }
                if (FirstPhotoIsChanged)
                {
                    facility.IDPicture = "";

                    foreach (var name in imageNames)
                    {
                        if (!String.IsNullOrEmpty(name))
                        {
                            if (String.IsNullOrEmpty(facility.IDPicture))
                            {
                                facility.IDPicture = name;
                            }
                            else
                            {
                                facility.IDPicture = facility.IDPicture + "," + name;
                            }
                        }
                    }
                }


                bool isUpdated = await ViewModel.ExecuteUpdateFacilityCommand(facility);

                if (isUpdated)
                {
                    PictureViewModel      pictureViewModel = new PictureViewModel();
                    List <Models.Picture> pictures         = new List <Models.Picture>();
                    if (FirstPhotoIsChanged)
                    {
                        Bitmap _bm  = ((BitmapDrawable)facilityPhoto.Drawable).Bitmap;
                        string file = "";
                        if (_bm != null)
                        {
                            MemoryStream stream = new MemoryStream();
                            _bm.Compress(Bitmap.CompressFormat.Jpeg, 100, stream);
                            byte[] ba = stream.ToArray();
                            file = Base64.EncodeToString(ba, Base64.Default);
                        }

                        Models.Picture picture = new Models.Picture()
                        {
                            Name = imageNames[0],
                            File = file,
                        };
                        pictures.Add(picture);
                    }
                    if (SecondPhotoIsChanged && imageNames.Count() > 1)
                    {
                        Bitmap _bm  = ((BitmapDrawable)secondFacilityPhoto.Drawable).Bitmap;
                        string file = "";
                        if (_bm != null)
                        {
                            MemoryStream stream = new MemoryStream();
                            _bm.Compress(Bitmap.CompressFormat.Jpeg, 100, stream);
                            byte[] ba = stream.ToArray();
                            file = Base64.EncodeToString(ba, Base64.Default);
                        }

                        Models.Picture picture = new Models.Picture()
                        {
                            Name = imageNames[1],
                            File = file,
                        };
                        pictures.Add(picture);
                    }
                    bool isSuccess = await pictureViewModel.ExecuteSavePictureCommand(pictures);

                    messageDialog.HideLoading();
                    messageDialog.SendToast("Pictures are saved successful.");
                    var            intent   = new Intent(this, typeof(FacilityDetailActivity));
                    Context        mContext = Android.App.Application.Context;
                    AppPreferences ap       = new AppPreferences(mContext);
                    ap.SaveFacilityId(facility.Id.ToString());
                    intent.PutExtra("data", Newtonsoft.Json.JsonConvert.SerializeObject(facility));
                    this.StartActivity(intent);
                    Finish();
                }
                else
                {
                    messageDialog.HideLoading();
                    messageDialog.SendToast("Pictures are not saved successful.");
                }
            }
        }