From e46361a3e61582764060b26924321f7117233f12 Mon Sep 17 00:00:00 2001 From: GiuseppeNovielli Date: Sun, 5 Mar 2023 16:07:50 +0100 Subject: [PATCH] sync MVVMCrud 0.0.2 --- .../Services/Request/IRequestService.cs | 6 +- .../Services/Request/RequestService.cs | 10 +- .../RequestProvider/IRequestProvider.cs | 2 +- .../RequestProvider/RequestProvider.cs | 33 ++++- .../xamarin_forms_5/MVVMCrud/Utils/Utils.cs | 4 +- .../Base/BaseItemEditAdvancedViewModel.cs | 21 ++- ...seListPaginationAdvancedHeaderViewModel.cs | 43 +++--- .../BaseListPaginationAdvancedViewModel.cs | 125 +++++++++--------- .../MVVMCrud/Views/Base/BaseContentPage.cs | 20 --- 9 files changed, 145 insertions(+), 119 deletions(-) diff --git a/MVVMCrud/xamarin_forms_5/MVVMCrud/Services/Request/IRequestService.cs b/MVVMCrud/xamarin_forms_5/MVVMCrud/Services/Request/IRequestService.cs index 557d7f2..dbe609c 100644 --- a/MVVMCrud/xamarin_forms_5/MVVMCrud/Services/Request/IRequestService.cs +++ b/MVVMCrud/xamarin_forms_5/MVVMCrud/Services/Request/IRequestService.cs @@ -87,7 +87,8 @@ Task RequestDefaultCreateUpdateItem bool getIfError = false, bool showLoading = true, List httpStatusCodes = null, - Action setupItem = null) + Action setupItem = null, + bool partialUpdate = false) where TItemRoot : BaseModelItemRoot, new() where TItem : BaseItem, new(); @@ -140,7 +141,8 @@ Task CreateUpdate string pk = null, MultipartFormDataContent formData = null, HttpContent httpContent = null, - HttpClient httpClient = null) + HttpClient httpClient = null, + bool partialUpdate = false) where TItemRoot : BaseModelItemRoot, new() where TItem : BaseItem, new(); diff --git a/MVVMCrud/xamarin_forms_5/MVVMCrud/Services/Request/RequestService.cs b/MVVMCrud/xamarin_forms_5/MVVMCrud/Services/Request/RequestService.cs index 39f66fc..43d6a55 100644 --- a/MVVMCrud/xamarin_forms_5/MVVMCrud/Services/Request/RequestService.cs +++ b/MVVMCrud/xamarin_forms_5/MVVMCrud/Services/Request/RequestService.cs @@ -221,7 +221,8 @@ public async Task RequestDefaultCreateUpdateItem bool getIfError = false, bool showLoading = true, List httpStatusCodes = null, - Action setupItem = null) + Action setupItem = null, + bool partialUpdate = false) where TItemRoot : BaseModelItemRoot, new() where TItem : BaseItem, new() @@ -237,7 +238,7 @@ public async Task RequestDefaultCreateUpdateItem return await RequestItem( async () => { - return await CreateUpdate(url, pk, formDataContent, httpContent, httpClient); + return await CreateUpdate(url, pk, formDataContent, httpContent, httpClient, partialUpdate); }, title, getIfError, showLoading, httpCodes, setupItem); } @@ -377,7 +378,8 @@ public async Task CreateUpdate string pk = null, MultipartFormDataContent formData = null, HttpContent httpContent = null, - HttpClient httpClient = null) + HttpClient httpClient = null, + bool partialUpdate = false) where TItemRoot : BaseModelItemRoot, new() where TItem : BaseItem, new() @@ -405,7 +407,7 @@ public async Task CreateUpdate } else { - respondData = await _requestProvider.PutAsync(apiUrl, formData, httpClient); + respondData = await _requestProvider.PutAsync(apiUrl, formData, httpClient, partialUpdate); } diff --git a/MVVMCrud/xamarin_forms_5/MVVMCrud/Services/RequestProvider/IRequestProvider.cs b/MVVMCrud/xamarin_forms_5/MVVMCrud/Services/RequestProvider/IRequestProvider.cs index 7bc4f8f..94850ae 100644 --- a/MVVMCrud/xamarin_forms_5/MVVMCrud/Services/RequestProvider/IRequestProvider.cs +++ b/MVVMCrud/xamarin_forms_5/MVVMCrud/Services/RequestProvider/IRequestProvider.cs @@ -12,7 +12,7 @@ public interface IRequestProvider Task PostMultipartFormAsync(string apiUrl, MultipartFormDataContent data = null, HttpClient httpClient = null); Task GetAsync(string apiUrl, HttpClient httpClient = null, FormUrlEncodedContent content_query = null); Task GetAsyncPagination(string apiUrl, HttpClient httpClient = null, string urlPagination = null, bool pagination = true, FormUrlEncodedContent content_query = null, int paginationSize = 0, string string_query = null, Action>, bool, int> paginationRequest = null); - Task PutAsync(string apiUrl, HttpContent data = null, HttpClient httpClient = null); + Task PutAsync(string apiUrl, HttpContent data = null, HttpClient httpClient = null, bool partialUpdate = false); Task DeleteAsync(string apiUrl, HttpClient httpClient = null); Task GetByteArray(string apiUrl, HttpClient httpClient = null, bool header = true); } diff --git a/MVVMCrud/xamarin_forms_5/MVVMCrud/Services/RequestProvider/RequestProvider.cs b/MVVMCrud/xamarin_forms_5/MVVMCrud/Services/RequestProvider/RequestProvider.cs index 7d63c0b..412746d 100644 --- a/MVVMCrud/xamarin_forms_5/MVVMCrud/Services/RequestProvider/RequestProvider.cs +++ b/MVVMCrud/xamarin_forms_5/MVVMCrud/Services/RequestProvider/RequestProvider.cs @@ -273,11 +273,19 @@ public async Task DeleteAsync(string apiUrl, HttpClient htt } - public async Task PutAsync(string apiUrl, HttpContent data = null, HttpClient httpClient = null) + public async Task PutAsync(string apiUrl, HttpContent data = null, HttpClient httpClient = null, bool partialUpdate = false) { System.Diagnostics.Debug.WriteLine("\nMVVMCrud PutAsync"); System.Diagnostics.Debug.WriteLine("PutAsync apiUrl = " + apiUrl); - System.Diagnostics.Debug.WriteLine("PutAsync data = " + data); + System.Diagnostics.Debug.WriteLine("PutAsync partialUpdate = " + partialUpdate); + + if (data != null) + { + System.Diagnostics.Debug.WriteLine("PutAsync data = " + await data?.ReadAsStringAsync()); + } + + System.Diagnostics.Debug.WriteLine("PutAsync HttpClient = " + httpClient); + RequestResponseItem responseItem = null; @@ -289,9 +297,24 @@ public async Task PutAsync(string apiUrl, HttpContent data try { - var response = await client.PutAsync(apiUrl, data); + HttpResponseMessage response = null; - string json = await response.Content.ReadAsStringAsync(); + if (!partialUpdate) + { + response = await client.PutAsync(apiUrl, data); + } + else + { + //https://stackoverflow.com/questions/47463000/c-sharp-xamarin-forms-add-custom-header-patch + var method = new HttpMethod("PATCH"); + var request = new HttpRequestMessage(method, apiUrl) + { + Content = data + }; + response = await client.SendAsync(request); + } + + var json = await response.Content.ReadAsStringAsync(); var responseStatus = response.StatusCode; responseItem = new RequestResponseItem(responseStatus, json, response.Headers); @@ -384,4 +407,4 @@ static HttpClient CreateHttpClient(HttpClient httpClient = null, bool header = t } } -} \ No newline at end of file +} diff --git a/MVVMCrud/xamarin_forms_5/MVVMCrud/Utils/Utils.cs b/MVVMCrud/xamarin_forms_5/MVVMCrud/Utils/Utils.cs index 3926f1c..ccf9190 100644 --- a/MVVMCrud/xamarin_forms_5/MVVMCrud/Utils/Utils.cs +++ b/MVVMCrud/xamarin_forms_5/MVVMCrud/Utils/Utils.cs @@ -79,7 +79,7 @@ public static FormUrlEncodedContent GetFormUrlEncodedContent(object item, JsonSe public static string[] SplitOnUppercase(string item) { //https://www.codegrepper.com/code-examples/csharp/c%23+split+string+by+caps - return Regex.Split(item, @"(? SetupRequest(TItemInput item) public virtual async void SetupReturnItem(TItem item) { - var navParams = new NavigationParameters - { - { "newEditItem", new NewEditItem(item, Position, Section) } - }; + var navParams = SetupReturnItemParams(item); - var navResult = await NavigationService.GoBackAsync(parameters: navParams); + var navResult = await SetupReturnNavigation(navParams, item); + } + + public virtual async Task SetupReturnNavigation(NavigationParameters navParams, TItem item) + { + return await NavigationService.GoBackAsync(parameters: navParams); } + public virtual NewEditItem SetupReturnItemRawParams(TItem item) => new NewEditItem(item, Position, Section); + + public virtual NavigationParameters SetupReturnItemParams(TItem item) + { + return new NavigationParameters + { + { "newEditItem", SetupReturnItemRawParams(item) } + }; + } } public class NewEditItem diff --git a/MVVMCrud/xamarin_forms_5/MVVMCrud/ViewModels/Base/BaseListPaginationAdvancedHeaderViewModel.cs b/MVVMCrud/xamarin_forms_5/MVVMCrud/ViewModels/Base/BaseListPaginationAdvancedHeaderViewModel.cs index 87cc123..a84ffe8 100644 --- a/MVVMCrud/xamarin_forms_5/MVVMCrud/ViewModels/Base/BaseListPaginationAdvancedHeaderViewModel.cs +++ b/MVVMCrud/xamarin_forms_5/MVVMCrud/ViewModels/Base/BaseListPaginationAdvancedHeaderViewModel.cs @@ -221,30 +221,41 @@ THeaderCellVM InstanceHeaderCellVM(THeaderItem item) public virtual async void SetupHeaderEditItem(THeaderCellVM cellVM) { - var navParams = new NavigationParameters - { - { "position", HeaderPosition }, - { "endpoint", HeaderEndpoint }, - { "itemSerialized", SetupHeaderCreateUpdatePageItemSerialized(cellVM) }, - }; + var page = SetupHeaderCreateUpdatePage(cellVM); + var navParams = SetupHeaderEditItemNavParams(cellVM); + var useModalNavigation = SetupHeaderCreateUpdatePageIsModal(cellVM); - var page = SetupHeaderCreateUpdatePage(); - if (!string.IsNullOrWhiteSpace(page)) - { - //var navResult = await NavigationService.NavigateAsync(page, navParams, useModalNavigation: SetupCreateUpdatePageIsModal()); - var navResult = await NavigationService.NavigateAsync(page, navParams); - } + var navResult = await SetupHeaderNavigationPage(page, navParams, cellVM, useModalNavigation); + } + public virtual bool SetupHeaderCreateUpdatePageIsModal(THeaderCellVM cellVM) + { + return base.SetupCreateUpdatePageIsModal(); } - public virtual string SetupHeaderCreateUpdatePageName() + public virtual async Task SetupHeaderNavigationPage(string page, INavigationParameters navParams, THeaderCellVM cellVM = null, bool useModalNavigation = false) + { + return await NavigationService.NavigateAsync(page, navParams, useModalNavigation: useModalNavigation); + } + + public virtual string SetupHeaderCreateUpdatePageName(THeaderCellVM cellVM) { return FromPagelViewModelName; } - public virtual string SetupHeaderCreateUpdatePage() + public virtual NavigationParameters SetupHeaderEditItemNavParams(THeaderCellVM cellVM) + { + return new NavigationParameters + { + { "position", HeaderPosition }, + { "endpoint", HeaderEndpoint }, + { "itemSerialized", SetupHeaderCreateUpdatePageItemSerialized(cellVM) }, + }; + } + + public virtual string SetupHeaderCreateUpdatePage(THeaderCellVM cellVM) { - var pageVMName = SetupHeaderCreateUpdatePageName(); + var pageVMName = SetupHeaderCreateUpdatePageName(cellVM); var pageContext = pageVMName.Split(new[] { "PageViewModel" }, StringSplitOptions.None); if (pageContext?.Length == 2) @@ -253,8 +264,6 @@ public virtual string SetupHeaderCreateUpdatePage() var pageNewEdit = pageName + "NewEditPage"; - return pageNewEdit; - if (!SetupCreateUpdatePageIsModal()) { return pageNewEdit; diff --git a/MVVMCrud/xamarin_forms_5/MVVMCrud/ViewModels/Base/BaseListPaginationAdvancedViewModel.cs b/MVVMCrud/xamarin_forms_5/MVVMCrud/ViewModels/Base/BaseListPaginationAdvancedViewModel.cs index 5953744..ec0d78d 100644 --- a/MVVMCrud/xamarin_forms_5/MVVMCrud/ViewModels/Base/BaseListPaginationAdvancedViewModel.cs +++ b/MVVMCrud/xamarin_forms_5/MVVMCrud/ViewModels/Base/BaseListPaginationAdvancedViewModel.cs @@ -43,6 +43,8 @@ public override void Initialize(INavigationParameters parameters) { base.Initialize(parameters); + SubscribeMessagingCenter(); + TitlePage = SetupTitlePage(); Endpoint = SetupEndpoint(); @@ -52,10 +54,6 @@ public override void Initialize(INavigationParameters parameters) { _ = SetupGet(); } - else - { - ShowMessage(MVVMCrudApplication.GetLoadingText()); - } } @@ -339,19 +337,21 @@ public virtual string GetCancelText() public virtual async void SetupEditItem(TCellVM cellVM) { - var navParams = new NavigationParameters + var page = SetupCreateUpdatePage(cellVM); + var navParams = SetupEditItemNavParams(cellVM); + var useModalNavigation = SetupCreateUpdatePageIsModal(cellVM); + + var navResult = await SetupNavigationPage(page, navParams, cellVM, useModalNavigation); + } + + public virtual NavigationParameters SetupEditItemNavParams(TCellVM cellVM) + { + return new NavigationParameters { { "endpoint", SetupCreateUpdatePageEndpoint() }, { "itemSerialized", SetupCreateUpdatePageItemSerialized(cellVM) }, { "position", ItemsList.IndexOf(cellVM) } }; - - var page = SetupCreateUpdatePage(); - if (!string.IsNullOrWhiteSpace(page)) - { - //var navResult = await NavigationService.NavigateAsync(page, navParams, useModalNavigation: SetupCreateUpdatePageIsModal()); - var navResult = await NavigationService.NavigateAsync(page, navParams); - } } public virtual string SetupCreateUpdatePageItemSerialized(TCellVM cellVM) @@ -370,28 +370,34 @@ public virtual string SetupCreateUpdatePageItemSerialized(TCellVM cellVM) public override async void TlbAddClick() { + var navParams = SetupCreateNavParams(); var page = SetupCreateUpdatePage(); - if (!string.IsNullOrWhiteSpace(page)) + var useModalNavigation = SetupCreateUpdatePageIsModal(); + var navResult = await SetupNavigationPage(page, navParams, useModalNavigation: useModalNavigation); + } + + public virtual NavigationParameters SetupCreateNavParams() + { + return new NavigationParameters { - var navParams = new NavigationParameters - { - { "endpoint", SetupCreateUpdatePageEndpoint() }, - }; + { "endpoint", SetupCreateUpdatePageEndpoint() }, + }; + } - //var navResult = await NavigationService.NavigateAsync(page, navParams, useModalNavigation: SetupCreateUpdatePageIsModal()); - var navResult = await NavigationService.NavigateAsync(page, navParams); - } + public virtual async Task SetupNavigationPage(string page, INavigationParameters navParams, TCellVM cellVM = null, bool useModalNavigation = false) + { + return await NavigationService.NavigateAsync(page, navParams, useModalNavigation: useModalNavigation); } - public virtual string SetupCreateUpdatePageName() + public virtual string SetupCreateUpdatePageName(TCellVM cellVM = null) { return GetType().Name; } - public virtual string SetupCreateUpdatePage() + public virtual string SetupCreateUpdatePage(TCellVM cellVM = null) { - var pageVMName = SetupCreateUpdatePageName(); + var pageVMName = SetupCreateUpdatePageName(cellVM); var pageContext = pageVMName.Split(new[] { "PageViewModel" }, StringSplitOptions.None); if (pageContext?.Length == 2) @@ -400,8 +406,6 @@ public virtual string SetupCreateUpdatePage() var pageNewEdit = pageName + "NewEditPage"; - return pageNewEdit; - if (!SetupCreateUpdatePageIsModal()) { return pageNewEdit; @@ -417,7 +421,7 @@ public virtual string SetupCreateUpdatePage() return null; } - public virtual bool SetupCreateUpdatePageIsModal() + public virtual bool SetupCreateUpdatePageIsModal(TCellVM cellVM = null) { if (DeviceInfo.Platform == DevicePlatform.iOS) { @@ -532,11 +536,14 @@ public virtual bool IsDetailPageModal(TCellVM obj) public virtual async void SetupDetailPage(TCellVM obj) { - var pageName = SetupDetailPageName(obj); + var navResult = await SetupNavigationPage(SetupDetailPageName(obj), SetupDetailPageNavParams(obj), obj, IsDetailPageModal(obj)); + } + + public virtual NavigationParameters SetupDetailPageNavParams(TCellVM obj) + { var withHeader = IsDetailPageWithHeader(obj); var id = SetupDetailPageID(obj); - var modal = IsDetailPageModal(obj); - + var position = ItemsSource.IndexOf(obj); var fromPageName = GetType().Name; @@ -556,20 +563,10 @@ public virtual async void SetupDetailPage(TCellVM obj) navParams.Add("id", id); } - modal = false; - if (!modal) - { - var navResult = await NavigationService.NavigateAsync(pageName, navParams); - } - else - { - pageName = nameof(MVVMCrudModalNavigationPage) + "/" + pageName; - //var navResult = await NavigationService.NavigateAsync(pageName, navParams, useModalNavigation=modal); - } - - + return navParams; } + public override void PerformSearch(string newText) { if ( @@ -713,32 +710,30 @@ public override void OnNavigatedTo(INavigationParameters parameters) { base.OnNavigatedTo(parameters); - SubscribeMessagingCenter(); - - if (parameters.ContainsKey("newEditItem")) + if (parameters.ContainsKey("deleteItem")) { - var newEditItem = parameters["newEditItem"] as NewEditItem; - if (newEditItem != null) - { - var pos = newEditItem.Position; - if (pos == -1) - { - //New - AddNewItem(newEditItem.Item); - } - else - { - //Edit - UpdateEditItem(newEditItem); - } - } + var pos = parameters.GetValue("deleteItem"); + SetupDeleteItem(pos); } else { - if (parameters.ContainsKey("deleteItem")) + if (parameters.ContainsKey("newEditItem")) { - var pos = parameters.GetValue("deleteItem"); - SetupDeleteItem(pos); + var newEditItem = parameters["newEditItem"] as NewEditItem; + if (newEditItem != null) + { + var pos = newEditItem.Position; + if (pos == -1) + { + //New + AddNewItem(newEditItem.Item); + } + else + { + //Edit + UpdateEditItem(newEditItem); + } + } } } } @@ -747,7 +742,11 @@ public override void OnNavigatedFrom(INavigationParameters parameters) { base.OnNavigatedFrom(parameters); - UnsubscribeMessagingCenter(); + if (parameters.GetNavigationMode() == Prism.Navigation.NavigationMode.Back) + { + UnsubscribeMessagingCenter(); + } + } public virtual int AddNewItemPosition() => 0; diff --git a/MVVMCrud/xamarin_forms_5/MVVMCrud/Views/Base/BaseContentPage.cs b/MVVMCrud/xamarin_forms_5/MVVMCrud/Views/Base/BaseContentPage.cs index 047f944..628ed36 100644 --- a/MVVMCrud/xamarin_forms_5/MVVMCrud/Views/Base/BaseContentPage.cs +++ b/MVVMCrud/xamarin_forms_5/MVVMCrud/Views/Base/BaseContentPage.cs @@ -18,25 +18,5 @@ public BaseContentPage() SetBinding(TitleProperty, new Binding() { Path = "TitlePage" }); } - - protected override void OnAppearing() - { - base.OnAppearing(); - - MessagingCenter.Send(this as ContentPage, "Page_OnAppearing"); - } - - protected override void OnDisappearing() - { - base.OnDisappearing(); - - MVVMCrudApplication.Instance?.IdMessagingCenterActiveList.Remove(_uuid); - - MessagingCenter.Send(this as ContentPage, "Page_OnDisappearing"); - - System.Diagnostics.Debug.WriteLine("disappearing"); - - - } } }