-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add PopupController with zone IDs
- Loading branch information
Showing
8 changed files
with
153 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using Cysharp.Threading.Tasks; | ||
using System.Collections.Generic; | ||
|
||
using System; | ||
|
||
namespace Adrenak.UGX { | ||
public static class PopupController { | ||
const string DEFAULT = "default"; | ||
|
||
readonly static Dictionary<string, bool> isShowingMap = new Dictionary<string, bool>(); | ||
|
||
/// <summary> | ||
/// Returns whether any popup is active in a given zone ID | ||
/// </summary> | ||
/// <param name="zoneID"></param> | ||
/// <returns></returns> | ||
public static bool IsPopupActive(string zoneID = DEFAULT) { | ||
if (isShowingMap.ContainsKey(zoneID)) | ||
return isShowingMap[zoneID]; | ||
return false; | ||
} | ||
|
||
/// <summary> | ||
/// Displays a popup and returns the response as a task. Optionally takes the zone ID too. | ||
/// </summary> | ||
public async static UniTask<K> Display<T, K>(Popup<T, K> popup, string zoneID = DEFAULT) where T : PopupState where K : PopupResponse { | ||
await UniTask.WaitWhile(() => IsPopupActive(zoneID)); | ||
isShowingMap.SetPair(zoneID, true); | ||
var response = await popup.Display(); | ||
isShowingMap.SetPair(zoneID, false); | ||
return response; | ||
} | ||
|
||
/// <summary> | ||
/// Displays a popup and returns the response as a callback. Optionally takes the zone ID too. | ||
/// </summary> | ||
public async static void Display<T, K>(Popup<T, K> popup, Action<K> resultCallback, string zoneID = DEFAULT) where T : PopupState where K : PopupResponse => | ||
resultCallback?.Invoke(await Display(popup, zoneID)); | ||
|
||
/// <summary> | ||
/// Takes a popup, it's state and displays it before returning the response as a task. Optionally takes the zone ID too. | ||
/// </summary> | ||
public async static UniTask<K> SetStateAndDisplay<T, K>(Popup<T, K> popup, T state, string zoneID = DEFAULT) where T : PopupState where K : PopupResponse { | ||
await UniTask.WaitWhile(() => IsPopupActive(zoneID)); | ||
isShowingMap.SetPair(zoneID, true); | ||
var response = await popup.SetStateAndDisplay(state); | ||
isShowingMap.SetPair(zoneID, false); | ||
return response; | ||
} | ||
|
||
/// <summary> | ||
/// Takes a popup, it's state and displays it before returning the response as a callback. Optionally takes the zone ID too. | ||
/// </summary> | ||
public async static void SetStateAndDisplay<T, K>(Popup<T, K> popup, T state, Action<K> resultCallback, string zoneID = DEFAULT) where T : PopupState where K : PopupResponse => | ||
resultCallback?.Invoke(await SetStateAndDisplay(popup, state, zoneID)); | ||
|
||
/// <summary> | ||
/// Takes a popup and a method for state change before displaying. Returns the response as a task and takes an optional zone ID | ||
/// </summary> | ||
public async static UniTask<K> ModifyStateAndDisplay<T, K>(Popup<T, K> popup, Action<T> modification, string zoneID = DEFAULT) where T : PopupState where K : PopupResponse { | ||
await UniTask.WaitWhile(() => IsPopupActive(zoneID)); | ||
isShowingMap.SetPair(zoneID, true); | ||
var response = await popup.ModifyStateAndDisplay(modification); | ||
isShowingMap.SetPair(zoneID, false); | ||
return response; | ||
} | ||
|
||
/// <summary> | ||
/// Takes a popup and a method for state change before displaying. Returns the response as a callback and takes an optional zone ID | ||
/// </summary> | ||
public async static void ModifyStateAndDisplay<T, K>(Popup<T, K> popup, Action<T> modification, Action<K> resultCallback, string zoneID = DEFAULT) where T : PopupState where K : PopupResponse => | ||
resultCallback?.Invoke(await ModifyStateAndDisplay(popup, modification, zoneID)); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters