void RowDelete_Click(object sender, EventArgs e) { var adapter = (RoutePharmacyAdapter)PharmacyTable.Adapter; var rowForDelete = (LinearLayout)((ImageView)sender).Parent; var routeItemUUID = (string)rowForDelete.GetTag(Resource.String.RouteItemUUID); MainDatabase.DeleteEntity <RouteItem>(routeItemUUID); int pos = (int)rowForDelete.GetTag(Resource.String.Position); int index = (int)rowForDelete.GetTag(Resource.String.RouteItemOrder); RouteTable.RemoveView(rowForDelete); using (var trans = MainDatabase.BeginTransaction()) { for (int c = index; c < RouteTable.ChildCount; c++) { var rowForUpdate = (LinearLayout)RouteTable.GetChildAt(c); routeItemUUID = (string)rowForUpdate.GetTag(Resource.String.RouteItemUUID); var updRouteItem = MainDatabase.GetEntity <RouteItem>(routeItemUUID); updRouteItem.Order = c; updRouteItem.IsSynced = false; updRouteItem.UpdatedAt = DateTimeOffset.Now; rowForUpdate.SetTag(Resource.String.RouteItemOrder, c); rowForUpdate.FindViewById <TextView>(Resource.Id.riOrderTV).Text = (c + 1).ToString(); } trans.Commit(); } if (pos != -1) { adapter.SwitchVisibility(pos); } }
//SuggestClient Api; protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); RequestWindowFeature(WindowFeatures.NoTitle); Window.AddFlags(WindowManagerFlags.KeepScreenOn); // Create your application here SetContentView(Resource.Layout.Pharmacy); FindViewById <Button>(Resource.Id.paCloseB).Click += (s, e) => { Finish(); }; FindViewById <Button>(Resource.Id.paSaveB).Click += (s, e) => { var transaction = MainDatabase.BeginTransaction(); Pharmacy item; if (Pharmacy == null) { item = MainDatabase.Create2 <Pharmacy>(); item.CreatedAt = DateTimeOffset.Now; /* Contracts */ if (!string.IsNullOrEmpty(ContractsNames.Text)) { var ll = ContractsNames.Parent as LinearLayout; var contractUUIDs = (string)ll.GetTag(Resource.String.ContractUUIDs); if (!string.IsNullOrEmpty(contractUUIDs)) { foreach (var contract in contractUUIDs.Split(';')) { var contractData = MainDatabase.Create <ContractData>(); contractData.Pharmacy = item.UUID; contractData.Contract = contract; } } } /* ./Contracts */ } else { item = Pharmacy; /* Contracts */ if (string.IsNullOrEmpty(ContractsNames.Text)) { var contractDatas = MainDatabase.GetPharmacyDatas <ContractData>(item.UUID); foreach (var contractData in contractDatas) { MainDatabase.DeleteEntity(transaction, contractData);; } contractDatas = null; } else { var ll = ContractsNames.Parent as LinearLayout; var contractUUIDs = (string)ll.GetTag(Resource.String.ContractUUIDs); if (!string.IsNullOrEmpty(contractUUIDs)) { var contracts = contractUUIDs.Split(';'); var contractDatas = MainDatabase.GetPharmacyDatas <ContractData>(item.UUID); foreach (var contractData in contractDatas) { MainDatabase.DeleteEntity(transaction, contractData); } contractDatas = null; foreach (var contract in contractUUIDs.Split(';')) { var contractData = MainDatabase.Create2 <ContractData>(); contractData.Pharmacy = item.UUID; contractData.Contract = contract; } } } /* ./Contracts */ } item.UpdatedAt = DateTimeOffset.Now; item.IsSynced = false; item.SetState((PharmacyState)State.SelectedItemPosition); item.Brand = FindViewById <EditText>(Resource.Id.paBrandET).Text; item.NumberName = FindViewById <EditText>(Resource.Id.paNumberNameET).Text; item.LegalName = FindViewById <EditText>(Resource.Id.paLegalNameET).Text; if (string.IsNullOrEmpty(NetName.Text)) { item.Net = string.Empty; } else { item.Net = NetUUID; } var address = FindViewById <AutoCompleteTextView>(Resource.Id.paAddressACTV); item.Address = address.Text; bool isChanged = (bool)address.GetTag(Resource.String.IsChanged); if (isChanged) { item.AddressFiasId = (string)address.GetTag(Resource.String.fias_id); item.AddressQCGeo = (string)address.GetTag(Resource.String.qc_geo); item.AddressGeoLat = (string)address.GetTag(Resource.String.geo_lat); item.AddressGeoLon = (string)address.GetTag(Resource.String.geo_lon); } if (string.IsNullOrEmpty(Subway.Text)) { item.Subway = string.Empty; } else { var subwayUUID = (string)Subway.GetTag(Resource.String.SubwayUUID); if (!string.IsNullOrEmpty(subwayUUID)) { item.Subway = subwayUUID; } } if (string.IsNullOrEmpty(Region.Text)) { item.Region = string.Empty; } else { var regionUUID = (string)Region.GetTag(Resource.String.RegionUUID); if (!string.IsNullOrEmpty(regionUUID)) { item.Region = regionUUID; } } item.Phone = FindViewById <EditText>(Resource.Id.paPhoneET).Text; if (string.IsNullOrEmpty(Place.Text)) { item.Place = string.Empty; } else { var placeUUID = (string)Place.GetTag(Resource.String.PlaceUUID); if (!string.IsNullOrEmpty(placeUUID)) { item.Place = placeUUID; } } if (Category.SelectedItemPosition > 0) { item.Category = Categories[Category.SelectedItemPosition].uuid; } else { item.Category = string.Empty; } //if (string.IsNullOrEmpty(Category.Text)) { // item.Category = string.Empty; //} else { // var categoryUUID = (string)Category.GetTag(Resource.String.CategoryUUID); // if (!string.IsNullOrEmpty(categoryUUID)) { // item.Category = categoryUUID; // } //} item.TurnOver = Helper.ToInt(FindViewById <EditText>(Resource.Id.paTurnOverET).Text); item.Comment = FindViewById <EditText>(Resource.Id.paCommentET).Text; if (!item.IsManaged) { MainDatabase.SaveEntity(transaction, item); } transaction.Commit(); //var sync = new SyncItem() { // Path = @"Pharmacy", // ObjectUUID = Pharmacy.UUID, // JSON = JsonConvert.SerializeObject(Pharmacy) //}; //MainDatabase.AddToQueue(sync); //StartService(new Intent("com.xamarin.SyncService")); GetSharedPreferences(MainActivity.C_MAIN_PREFS, FileCreationMode.Private) .Edit() .PutString(MainActivity.C_SAVED_PHARMACY_UUID, item.UUID) .Commit(); Finish(); }; //Api = new SuggestClient(Secret.DadataApiToken, Secret.DadataApiURL); #region State State = FindViewById <Spinner>(Resource.Id.paStateS); States = MainDatabase.GetStates(); var stateAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleSpinnerItem, States.ToArray()); stateAdapter.SetDropDownViewResource(Resource.Layout.SpinnerItem); State.Adapter = stateAdapter; #endregion #region Net Nets = MainDatabase.GetNets(); NetName = FindViewById <AutoCompleteTextView>(Resource.Id.paNetACTV); var netChoiceButton = FindViewById <Button>(Resource.Id.paNetB); netChoiceButton.Click += (object sender, EventArgs e) => { new AlertDialog.Builder(this) .SetTitle("Аптечная сеть") .SetCancelable(true) .SetItems(Nets.Select(item => item.name).ToArray(), (caller, arguments) => { SetNet(arguments.Which); //Toast.MakeText(this, @"Selected " + arguments.Which, ToastLength.Short).Show(); }) .Show(); }; #endregion ContractsNames = FindViewById <AutoCompleteTextView>(Resource.Id.paContractsACTV); ContractsChoice = FindViewById <Button>(Resource.Id.paContractsB); ContractsChoice.Click += ContractsChoice_Click; Address = FindViewById <AutoCompleteTextView>(Resource.Id.paAddressACTV); Address.SetTag(Resource.String.IsChanged, false); Subway = FindViewById <AutoCompleteTextView>(Resource.Id.paSubwayACTV); Region = FindViewById <AutoCompleteTextView>(Resource.Id.paRegionACTV); Place = FindViewById <AutoCompleteTextView>(Resource.Id.paPlaceACTV); //Category = FindViewById<AutoCompleteTextView>(Resource.Id.paCategoryACTV); #region Category Category = FindViewById <Spinner>(Resource.Id.paCategoryS); Categories = new List <Category>(); Categories.Add(new Category { name = @"Выберите категорию!", uuid = Guid.Empty.ToString() }); Categories.AddRange(MainDatabase.GetItems <Category>());; var categoryAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleSpinnerItem, Categories.Select(cat => cat.name).ToArray()); categoryAdapter.SetDropDownViewResource(Resource.Layout.SpinnerItem); Category.Adapter = categoryAdapter; #endregion var pharmacyUUID = Intent.GetStringExtra("UUID"); if (string.IsNullOrEmpty(pharmacyUUID)) { var shared = GetSharedPreferences(MainActivity.C_MAIN_PREFS, FileCreationMode.Private); var agentUUID = shared.GetString(SigninDialog.C_AGENT_UUID, string.Empty); try { var agent = MainDatabase.GetItem <Agent>(agentUUID); Address.Text = agent.city; } catch (Exception ex) { Console.WriteLine(ex.Message); } FindViewById <TextView>(Resource.Id.paInfoTV).Text = @"ДОБАВЛЕНИЕ НОВОЙ АПТЕКИ"; FindViewById <TableRow>(Resource.Id.paRowLastAttendance).Visibility = ViewStates.Gone; FindViewById <TableRow>(Resource.Id.paRowNextAttendanceDate).Visibility = ViewStates.Gone; InitViews(); return; } Pharmacy = MainDatabase.GetEntity <Pharmacy>(pharmacyUUID); FindViewById <TextView>(Resource.Id.paInfoTV).Text = "АПТЕКА : " + Pharmacy.GetName(); FindViewById <TextView>(Resource.Id.paUUIDTV).Text = Pharmacy.UUID; State.SetSelection((int)Pharmacy.GetState()); FindViewById <EditText>(Resource.Id.paBrandET).Text = Pharmacy.Brand; FindViewById <EditText>(Resource.Id.paNumberNameET).Text = Pharmacy.NumberName; FindViewById <EditText>(Resource.Id.paLegalNameET).Text = Pharmacy.LegalName; //NetName.Text = string.IsNullOrEmpty(Pharmacy.Net) ? // string.Empty : MainDatabase.GetNet(Pharmacy.Net).name; //NetUUID = Pharmacy.Net; if (!string.IsNullOrEmpty(Pharmacy.Net)) { SetNet(Nets.FindIndex(net => string.Compare(net.uuid, Pharmacy.Net) == 0)); } ContractDatas = MainDatabase.GetPharmacyDatas <ContractData>(Pharmacy.UUID); if (ContractDatas.Count > 0) { ContractsNames.Text = string.Join(", ", ContractDatas.Select(cd => MainDatabase.GetItem <Contract>(cd.Contract).name).ToArray()); var ll = ContractsNames.Parent as LinearLayout; ll.SetTag(Resource.String.ContractUUIDs, string.Join(@";", ContractDatas.Select(cd => cd.Contract).ToArray()) ); } Address.Text = Pharmacy.Address; Subway.Text = string.IsNullOrEmpty(Pharmacy.Subway) ? string.Empty : MainDatabase.GetItem <Subway>(Pharmacy.Subway).name; Region.Text = string.IsNullOrEmpty(Pharmacy.Region) ? string.Empty : MainDatabase.GetItem <Region>(Pharmacy.Region).name; FindViewById <EditText>(Resource.Id.paPhoneET).Text = Pharmacy.Phone; Place.Text = string.IsNullOrEmpty(Pharmacy.Place) ? string.Empty : MainDatabase.GetItem <Place>(Pharmacy.Place).name; //Category.Text = string.IsNullOrEmpty(Pharmacy.Category) ? // string.Empty : MainDatabase.GetItem<Category>(Pharmacy.Category).name; if (!string.IsNullOrEmpty(Pharmacy.Category)) { Category.SetSelection(Categories.FindIndex(cat => string.Compare(cat.uuid, Pharmacy.Category) == 0)); } FindViewById <EditText>(Resource.Id.paTurnOverET).Text = Pharmacy.TurnOver.HasValue ? Pharmacy.TurnOver.Value.ToString() : string.Empty; FindViewById <TextView>(Resource.Id.paLastAttendanceTV).Text = Pharmacy.LastAttendanceDate.HasValue ? Pharmacy.LastAttendanceDate.Value.ToString(@"dd.MM.yyyy") : @"<нет визита>"; FindViewById <TextView>(Resource.Id.paNextAttendanceDateTV).Text = Pharmacy.NextAttendanceDate.HasValue ? Pharmacy.NextAttendanceDate.Value.ToString(@"dd.MM.yyyy") : DateTimeOffset.Now.ToString(@"dd.MM.yyyy"); FindViewById <EditText>(Resource.Id.paCommentET).Text = Pharmacy.Comment; InitViews(); }
public override void OnPause() { base.OnPause(); var transaction = MainDatabase.BeginTransaction(); Pharmacy item; item = Pharmacy; /* Contracts */ if (string.IsNullOrEmpty(ContractsNames.Text)) { var contractDatas = MainDatabase.GetPharmacyDatas <ContractData>(item.UUID); foreach (var contractData in contractDatas) { MainDatabase.DeleteEntity(transaction, contractData); } contractDatas = null; } else { var ll = ContractsNames.Parent as LinearLayout; var contractUUIDs = (string)ll.GetTag(Resource.String.ContractUUIDs); if (!string.IsNullOrEmpty(contractUUIDs)) { var contracts = contractUUIDs.Split(';'); var contractDatas = MainDatabase.GetPharmacyDatas <ContractData>(item.UUID); foreach (var contractData in contractDatas) { MainDatabase.DeleteEntity(transaction, contractData); } contractDatas = null; foreach (var contract in contractUUIDs.Split(';')) { var contractData = MainDatabase.Create2 <ContractData>(); contractData.Pharmacy = item.UUID; contractData.Contract = contract; } } } /* ./Contracts */ item.UpdatedAt = DateTimeOffset.Now; item.IsSynced = false; item.SetState((PharmacyState)State.SelectedItemPosition); item.Brand = View.FindViewById <EditText>(Resource.Id.pfBrandET).Text; item.NumberName = View.FindViewById <EditText>(Resource.Id.pfNumberNameET).Text; item.LegalName = View.FindViewById <EditText>(Resource.Id.pfLegalNameET).Text; if (string.IsNullOrEmpty(NetName.Text)) { item.Net = string.Empty; } else { item.Net = NetUUID; } var address = View.FindViewById <AutoCompleteTextView>(Resource.Id.pfAddressACTV); item.Address = address.Text; bool isChanged = (bool)address.GetTag(Resource.String.IsChanged); if (isChanged) { item.AddressFiasId = (string)address.GetTag(Resource.String.fias_id); item.AddressQCGeo = (string)address.GetTag(Resource.String.qc_geo); item.AddressGeoLat = (string)address.GetTag(Resource.String.geo_lat); item.AddressGeoLon = (string)address.GetTag(Resource.String.geo_lon); } if (string.IsNullOrEmpty(Subway.Text)) { item.Subway = string.Empty; } else { var subwayUUID = (string)Subway.GetTag(Resource.String.SubwayUUID); if (!string.IsNullOrEmpty(subwayUUID)) { item.Subway = subwayUUID; } } if (string.IsNullOrEmpty(Region.Text)) { item.Region = string.Empty; } else { var regionUUID = (string)Region.GetTag(Resource.String.RegionUUID); if (!string.IsNullOrEmpty(regionUUID)) { item.Region = regionUUID; } } item.Phone = View.FindViewById <EditText>(Resource.Id.pfPhoneET).Text; if (string.IsNullOrEmpty(Place.Text)) { item.Place = string.Empty; } else { var placeUUID = (string)Place.GetTag(Resource.String.PlaceUUID); if (!string.IsNullOrEmpty(placeUUID)) { item.Place = placeUUID; } } if (Category.SelectedItemPosition > 0) { item.Category = Categories[Category.SelectedItemPosition].uuid; } else { item.Category = string.Empty; } //if (string.IsNullOrEmpty(Category.Text)) { // item.Category = string.Empty; //} else { // var categoryUUID = (string)Category.GetTag(Resource.String.CategoryUUID); // if (!string.IsNullOrEmpty(categoryUUID)) { // item.Category = categoryUUID; // } //} item.TurnOver = Helper.ToInt(View.FindViewById <EditText>(Resource.Id.pfTurnOverET).Text); item.Comment = View.FindViewById <EditText>(Resource.Id.pfCommentET).Text; if (!item.IsManaged) { MainDatabase.SaveEntity(transaction, item); } transaction.Commit(); //var sync = new SyncItem() { // Path = @"Pharmacy", // ObjectUUID = Pharmacy.UUID, // JSON = JsonConvert.SerializeObject(Pharmacy) //}; //MainDatabase.AddToQueue(sync); //StartService(new Intent("com.xamarin.SyncService")); Activity.GetSharedPreferences(MainActivity.C_MAIN_PREFS, FileCreationMode.Private) .Edit() .PutString(MainActivity.C_SAVED_PHARMACY_UUID, item.UUID) .Commit(); }