Skip to content

Commit

Permalink
sync MVVMCrud 0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
GiuseppeNovielli committed Mar 5, 2023
1 parent 461f811 commit e46361a
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ Task<TItemRoot> RequestDefaultCreateUpdateItem<TItemRoot, TItem>
bool getIfError = false,
bool showLoading = true,
List<HttpStatusCode> httpStatusCodes = null,
Action setupItem = null)
Action setupItem = null,
bool partialUpdate = false)

where TItemRoot : BaseModelItemRoot<TItem>, new()
where TItem : BaseItem, new();
Expand Down Expand Up @@ -140,7 +141,8 @@ Task<TItemRoot> CreateUpdate<TItemRoot, TItem>
string pk = null,
MultipartFormDataContent formData = null,
HttpContent httpContent = null,
HttpClient httpClient = null)
HttpClient httpClient = null,
bool partialUpdate = false)

where TItemRoot : BaseModelItemRoot<TItem>, new()
where TItem : BaseItem, new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ public async Task<TItemRoot> RequestDefaultCreateUpdateItem<TItemRoot, TItem>
bool getIfError = false,
bool showLoading = true,
List<HttpStatusCode> httpStatusCodes = null,
Action setupItem = null)
Action setupItem = null,
bool partialUpdate = false)

where TItemRoot : BaseModelItemRoot<TItem>, new()
where TItem : BaseItem, new()
Expand All @@ -237,7 +238,7 @@ public async Task<TItemRoot> RequestDefaultCreateUpdateItem<TItemRoot, TItem>
return await RequestItem<TItemRoot, TItem>(
async () =>
{
return await CreateUpdate<TItemRoot, TItem>(url, pk, formDataContent, httpContent, httpClient);
return await CreateUpdate<TItemRoot, TItem>(url, pk, formDataContent, httpContent, httpClient, partialUpdate);
}, title, getIfError, showLoading, httpCodes, setupItem);
}

Expand Down Expand Up @@ -377,7 +378,8 @@ public async Task<TItemRoot> CreateUpdate<TItemRoot, TItem>
string pk = null,
MultipartFormDataContent formData = null,
HttpContent httpContent = null,
HttpClient httpClient = null)
HttpClient httpClient = null,
bool partialUpdate = false)

where TItemRoot : BaseModelItemRoot<TItem>, new()
where TItem : BaseItem, new()
Expand Down Expand Up @@ -405,7 +407,7 @@ public async Task<TItemRoot> CreateUpdate<TItemRoot, TItem>
}
else
{
respondData = await _requestProvider.PutAsync(apiUrl, formData, httpClient);
respondData = await _requestProvider.PutAsync(apiUrl, formData, httpClient, partialUpdate);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface IRequestProvider
Task<RequestResponseItem> PostMultipartFormAsync(string apiUrl, MultipartFormDataContent data = null, HttpClient httpClient = null);
Task<RequestResponseItem> GetAsync(string apiUrl, HttpClient httpClient = null, FormUrlEncodedContent content_query = null);
Task<RequestResponseItem> GetAsyncPagination(string apiUrl, HttpClient httpClient = null, string urlPagination = null, bool pagination = true, FormUrlEncodedContent content_query = null, int paginationSize = 0, string string_query = null, Action<List<KeyValuePair<string, string>>, bool, int> paginationRequest = null);
Task<RequestResponseItem> PutAsync(string apiUrl, HttpContent data = null, HttpClient httpClient = null);
Task<RequestResponseItem> PutAsync(string apiUrl, HttpContent data = null, HttpClient httpClient = null, bool partialUpdate = false);
Task<RequestResponseItem> DeleteAsync(string apiUrl, HttpClient httpClient = null);
Task<RequestResponseItem> GetByteArray(string apiUrl, HttpClient httpClient = null, bool header = true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,19 @@ public async Task<RequestResponseItem> DeleteAsync(string apiUrl, HttpClient htt

}

public async Task<RequestResponseItem> PutAsync(string apiUrl, HttpContent data = null, HttpClient httpClient = null)
public async Task<RequestResponseItem> 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;

Expand All @@ -289,9 +297,24 @@ public async Task<RequestResponseItem> 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);
Expand Down Expand Up @@ -384,4 +407,4 @@ static HttpClient CreateHttpClient(HttpClient httpClient = null, bool header = t
}

}
}
}
4 changes: 2 additions & 2 deletions MVVMCrud/xamarin_forms_5/MVVMCrud/Utils/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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, @"(?<!^)(?=[A-Z])");
return Regex.Split(item, @"(?<!^)(?=[A-Z])");
}

public static string GetPageNameWithUnderscore(string className, string contextName)
Expand All @@ -104,6 +104,6 @@ public static string GetPageNameWithUnderscore(string className, string contextN
}

return pageWithUnderscore.ToLower();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,25 @@ public virtual async Task<TItemRoot> SetupRequest(TItemInput item)

public virtual async void SetupReturnItem(TItem item)
{
var navParams = new NavigationParameters
{
{ "newEditItem", new NewEditItem<TItem>(item, Position, Section) }
};
var navParams = SetupReturnItemParams(item);

var navResult = await NavigationService.GoBackAsync(parameters: navParams);
var navResult = await SetupReturnNavigation(navParams, item);
}

public virtual async Task<object> SetupReturnNavigation(NavigationParameters navParams, TItem item)
{
return await NavigationService.GoBackAsync(parameters: navParams);
}

public virtual NewEditItem<TItem> SetupReturnItemRawParams(TItem item) => new NewEditItem<TItem>(item, Position, Section);

public virtual NavigationParameters SetupReturnItemParams(TItem item)
{
return new NavigationParameters
{
{ "newEditItem", SetupReturnItemRawParams(item) }
};
}
}

public class NewEditItem<TItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<object> 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)
Expand All @@ -253,8 +264,6 @@ public virtual string SetupHeaderCreateUpdatePage()

var pageNewEdit = pageName + "NewEditPage";

return pageNewEdit;

if (!SetupCreateUpdatePageIsModal())
{
return pageNewEdit;
Expand Down
Loading

0 comments on commit e46361a

Please sign in to comment.