Is there a way to simplify these methods? They are the same except for a different method being called.
private async Task ChangePresence(bool args, int voteId)
{
if (GeneralMeetingState.GeneralMeeting != null)
{
_spinnerVisible = true;
await AgendaItemsDetailState.ChangePresence(args, GeneralMeetingState.GeneralMeeting.ID,
AgendaItemId, voteId);
await AgendaState.GetAgendaItem(GeneralMeetingState.GeneralMeeting.ID, AgendaItemId);
await AgendaItemsDetailState.GetVotesList(GeneralMeetingState.GeneralMeeting.ID, AgendaItemId);
_spinnerVisible = false;
}
}
private async Task VoteFor(bool args, int voteId)
{
if (GeneralMeetingState.GeneralMeeting != null)
{
_spinnerVisible = true;
await AgendaItemsDetailState.ChangeVoteFor(args, GeneralMeetingState.GeneralMeeting.ID,
AgendaItemId, voteId, GeneralMeetingState.IsUserOwner);
await AgendaState.GetAgendaItem(GeneralMeetingState.GeneralMeeting.ID, AgendaItemId);
await AgendaItemsDetailState.GetVotesList(GeneralMeetingState.GeneralMeeting.ID, AgendaItemId);
_spinnerVisible = false;
}
}
private async Task VoteAgainst(bool args, int voteId)
{
if (GeneralMeetingState.GeneralMeeting != null)
{
_spinnerVisible = true;
await AgendaItemsDetailState.ChangeVoteAgainst(args, GeneralMeetingState.GeneralMeeting.ID,
AgendaItemId, voteId, GeneralMeetingState.IsUserOwner);
await AgendaState.GetAgendaItem(GeneralMeetingState.GeneralMeeting.ID, AgendaItemId);
await AgendaItemsDetailState.GetVotesList(GeneralMeetingState.GeneralMeeting.ID, AgendaItemId);
_spinnerVisible = false;
}
}
private async Task ChangeVoteAbstinence(bool args, int voteId)
{
if (GeneralMeetingState.GeneralMeeting != null)
{
_spinnerVisible = true;
await AgendaItemsDetailState.ChangeVoteAbstinence(args, GeneralMeetingState.GeneralMeeting.ID,
AgendaItemId, voteId, GeneralMeetingState.IsUserOwner);
await AgendaState.GetAgendaItem(GeneralMeetingState.GeneralMeeting.ID, AgendaItemId);
await AgendaItemsDetailState.GetVotesList(GeneralMeetingState.GeneralMeeting.ID, AgendaItemId);
_spinnerVisible = false;
}
}
I was looking into passing a method as a parameter and using Action<bool, int> but i couldn't figure it out. Any help is much appreciated
CodePudding user response:
The idea to pass Action<bool, int>
seems to be almost the right one. The easiest one would be passing just Func<Task>
. Something along this lines:
// TODO - give more meaningful name
private async Task DoSomething(Func<Task> something)
{
if (GeneralMeetingState.GeneralMeeting != null)
{
_spinnerVisible = true;
await something();
await AgendaState.GetAgendaItem(GeneralMeetingState.GeneralMeeting.ID, AgendaItemId);
await AgendaItemsDetailState.GetVotesList(GeneralMeetingState.GeneralMeeting.ID, AgendaItemId);
_spinnerVisible = false;
}
}
And methods changed to the following:
private async Task ChangePresence(bool args, int voteId)
{
await DoSomething(() => AgendaItemsDetailState.ChangePresence(args, GeneralMeetingState.GeneralMeeting.ID, AgendaItemId, voteId))
}
CodePudding user response:
Something like this perhaps:
private Task ChangePresence(bool args, int voteId) =>
Execute(() => AgendaItemsDetailState.ChangePresence(args, GeneralMeetingState.GeneralMeeting.ID, AgendaItemId, voteId));
private Task VoteFor(bool args, int voteId) =>
Execute(() => AgendaItemsDetailState.ChangeVoteFor(args, GeneralMeetingState.GeneralMeeting.ID, AgendaItemId, voteId, GeneralMeetingState.IsUserOwner));
private Task VoteAgainst(bool args, int voteId) =>
Execute(() => AgendaItemsDetailState.ChangeVoteAgainst(args, GeneralMeetingState.GeneralMeeting.ID, AgendaItemId, voteId, GeneralMeetingState.IsUserOwner));
private Task ChangeVoteAbstinence(bool args, int voteId) =>
Execute(() => AgendaItemsDetailState.ChangeVoteAbstinence(args, GeneralMeetingState.GeneralMeeting.ID, AgendaItemId, voteId, GeneralMeetingState.IsUserOwner));
private async Task Execute(Func<Task> func)
{
if (GeneralMeetingState.GeneralMeeting != null)
{
_spinnerVisible = true;
await func();
await AgendaState.GetAgendaItem(GeneralMeetingState.GeneralMeeting.ID, AgendaItemId);
await AgendaItemsDetailState.GetVotesList(GeneralMeetingState.GeneralMeeting.ID, AgendaItemId);
_spinnerVisible = false;
}
}