Skip to content

Commit 968c374

Browse files
Merge pull request #6 from CoderGamester/develop
0.3.0
2 parents cda3fe1 + 62eee2e commit 968c374

File tree

4 files changed

+63
-20
lines changed

4 files changed

+63
-20
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All notable changes to this package will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html)
66

7+
## [0.3.0] - 2020-02-11
8+
9+
- Added new *UiPresenterData* class for the case where the *UiPresenter* needs to be initialized with a default data value
10+
- Added new *OnInitialize* method that is invoked after the *UiPresenter* is initialized
11+
12+
## [0.2.0] - 2020-01-19
13+
714
## [0.2.1] - 2020-02-09
815

916
- Added the possibility to open the ui after adding or loading it to the *UiService*

Runtime/UiPresenter.cs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ namespace GameLovers.UiService
1111
public abstract class UiPresenter : MonoBehaviour
1212
{
1313
private IUiService _uiService;
14-
15-
/// <summary>
16-
/// Sets the UI <paramref name="data"/>
17-
/// </summary>
18-
public virtual void SetData<T>(T data) where T : struct {}
1914

2015
/// <summary>
2116
/// Refreshes this opened UI
2217
/// </summary>
2318
public virtual void Refresh() {}
2419

20+
/// <summary>
21+
/// Allows the ui presenter implementation to have extra behaviour when it is initialized
22+
/// </summary>
23+
protected virtual void OnInitialized() {}
24+
2525
/// <summary>
2626
/// Allows the ui presenter implementation to have extra behaviour when it is opened
2727
/// </summary>
@@ -43,6 +43,7 @@ protected void Close()
4343
internal void Init(IUiService uiService)
4444
{
4545
_uiService = uiService;
46+
OnInitialized();
4647
}
4748

4849
internal void InternalOpen()
@@ -57,4 +58,33 @@ internal void InternalClose()
5758
OnClosed();
5859
}
5960
}
61+
62+
/// <summary>
63+
/// Tags the <see cref="UiPresenter"/> as a <see cref="UiPresenterData{T}"/> to allow definning a specific state when
64+
/// opening it via the <see cref="UiService"/>
65+
/// </summary>
66+
public interface IUiPresenterData {}
67+
68+
/// <inheritdoc cref="UiPresenter"/>
69+
/// <remarks>
70+
/// Extends the <see cref="UiPresenter"/> behaviour with defined data of type <typeparamref name="T"/>
71+
/// </remarks>
72+
public abstract class UiPresenterData<T> : UiPresenter, IUiPresenterData
73+
where T : struct
74+
{
75+
/// <summary>
76+
/// The Ui data defined when opened via the <see cref="UiService"/>
77+
/// </summary>
78+
public T Data { get; protected set; }
79+
80+
/// <summary>
81+
/// Allows the ui presenter implementation to have extra behaviour when the data defined for the presenter is set
82+
/// </summary>
83+
protected virtual void OnSetData(T data) {}
84+
85+
internal void InternalSetData(T data)
86+
{
87+
Data = data;
88+
}
89+
}
6090
}

Runtime/UiService.cs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ public interface IUiService
3434
/// Adds the given <paramref name="uiPresenter"/> to the service and to be included inside the given <paramref name="layer"/>.
3535
/// If the given <paramref name="openAfter"/> is true, will open the <see cref="UiPresenter"/> after adding it to the service
3636
/// </summary>
37-
/// <exception cref="NullReferenceException">
38-
/// Thrown if the given <paramref name="uiPresenter"/> is null
39-
/// </exception>
4037
/// <exception cref="ArgumentException">
4138
/// Thrown if the service already contains the given <paramref name="uiPresenter"/>
4239
/// </exception>
@@ -163,9 +160,14 @@ public interface IUiService
163160
/// <remarks>
164161
/// It sets the given <paramref name="initialData"/> data BEFORE opening the UI
165162
/// </remarks>
166-
T OpenUi<T, TData>(TData initialData) where T : UiPresenter where TData : struct;
163+
T OpenUi<T, TData>(TData initialData)
164+
where T : class, IUiPresenterData
165+
where TData : struct;
167166

168167
///<inheritdoc cref="OpenUi(Type)"/>
168+
/// <exception cref="ArgumentException">
169+
/// Thrown if the the given <paramref name="type"/> is not of inhereting from <see cref="UiPresenterData{T}"/> class
170+
/// </exception>
169171
/// <remarks>
170172
/// It sets the given <paramref name="initialData"/> data BEFORE opening the UI
171173
/// </remarks>
@@ -348,11 +350,6 @@ public void AddUi<T>(T uiPresenter, int layer, bool openAfter = false) where T :
348350
{
349351
var type = uiPresenter.GetType().UnderlyingSystemType;
350352

351-
if (uiPresenter == null)
352-
{
353-
throw new NullReferenceException($"The Ui {type} cannot be null");
354-
}
355-
356353
if (HasUiPresenter(type))
357354
{
358355
throw new ArgumentException($"The Ui {type} was already added");
@@ -418,7 +415,7 @@ public T RemoveUi<T>(T uiPresenter) where T : UiPresenter
418415
/// <inheritdoc />
419416
public async Task<T> LoadUiAsync<T>(bool openAfter = false) where T : UiPresenter
420417
{
421-
UiPresenter uiPresenter = await LoadUiAsync(typeof(T), openAfter);
418+
var uiPresenter = await LoadUiAsync(typeof(T), openAfter);
422419

423420
return uiPresenter as T;
424421
}
@@ -509,7 +506,7 @@ public T OpenUi<T>() where T : UiPresenter
509506
/// <inheritdoc />
510507
public UiPresenter OpenUi(Type type)
511508
{
512-
UiPresenter ui = GetUi(type);
509+
var ui = GetUi(type);
513510

514511
if (!_visibleUiList.Contains(type))
515512
{
@@ -525,15 +522,24 @@ public UiPresenter OpenUi(Type type)
525522
}
526523

527524
/// <inheritdoc />
528-
public T OpenUi<T, TData>(TData initialData) where T : UiPresenter where TData : struct
525+
public T OpenUi<T, TData>(TData initialData)
526+
where T : class, IUiPresenterData
527+
where TData : struct
529528
{
530529
return OpenUi(typeof(T), initialData) as T;
531530
}
532531

533532
/// <inheritdoc />
534533
public UiPresenter OpenUi<TData>(Type type, TData initialData) where TData : struct
535534
{
536-
GetUi(type).SetData(initialData);
535+
var uiPresenterData = GetUi(type) as UiPresenterData<TData>;
536+
537+
if (uiPresenterData == null)
538+
{
539+
throw new ArgumentException($"The UiPresenter {type} is not of a {nameof(UiPresenterData<TData>)}");
540+
}
541+
542+
uiPresenterData.InternalSetData(initialData);
537543

538544
return OpenUi(type);
539545
}
@@ -547,7 +553,7 @@ public T CloseUi<T>() where T : UiPresenter
547553
/// <inheritdoc />
548554
public UiPresenter CloseUi(Type type)
549555
{
550-
UiPresenter ui = GetUi(type);
556+
var ui = GetUi(type);
551557

552558
if (_visibleUiList.Contains(type))
553559
{

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.gamelovers.uiservice",
33
"displayName": "UiService",
4-
"version": "0.2.1",
4+
"version": "0.3.0",
55
"unity": "2019.3",
66
"description": "This package provides a service to help manage an Unity's, game UI.\nIt allows to open, close, load, unload and request any Ui Configured in the game.\nThe package provides a Ui Set that allows to group a set of Ui Presenters to help load, open and close multiple Uis at the same time.\n\nTo help configure the game's UI you need to create a UiConfigs Scriptable object by:\n- Right Click on the Project View > Create > ScriptableObjects > Configs > UiConfigs",
77
"dependencies": {

0 commit comments

Comments
 (0)