-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUIManager.cs
More file actions
80 lines (60 loc) · 2.29 KB
/
UIManager.cs
File metadata and controls
80 lines (60 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Perfect. No references to DI-container here!
using System;
using System.Linq;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;
using SimpleContainer.Unity.Example.Dependent.Interfaces;
using SimpleContainer.Unity.Example.Extensions;
using ILogger = SimpleContainer.Unity.Example.Dependent.Interfaces.ILogger;
namespace SimpleContainer.Unity.Example.Dependent
{
public sealed class UIManager : MonoBehaviour
{
public Dropdown dropdownImplementation;
public Text labelTime;
private readonly CancellationTokenSource cts = new CancellationTokenSource();
private int timeZoneIndex;
[Inject]
public ITimeZoneProvider[] TimeZoneProviders { get; set; }
[Inject]
public ICultureInfoFormatter CultureInfoFormatter { get; set; }
[Inject]
public ILogger Logger { get; set; }
private DateTime DateTimeNow => DateTime.UtcNow;
void OnEnable()
{
dropdownImplementation.onValueChanged.AddListener(OnDropdownValueChanged);
}
void OnDisable()
{
dropdownImplementation.onValueChanged.RemoveListener(OnDropdownValueChanged);
}
void OnDestroy()
{
cts.Cancel();
}
/// <summary>
/// Should be called after dependencies was resolved.
/// </summary>
public void Initialize()
{
var options = TimeZoneProviders.Select(zoneProvider => zoneProvider.Name).ToList();
dropdownImplementation.ClearOptions();
dropdownImplementation.AddOptions(options);
_ = this.ReactAsync(manager => manager.DateTimeNow, ShowFormattedTime, cts);
}
private void OnDropdownValueChanged(int value)
{
Logger.LogInfo($"Timezone index changed to {value}");
timeZoneIndex = value;
ShowFormattedTime(DateTime.UtcNow);
}
private void ShowFormattedTime(DateTime dateTime)
{
var movedDateTime = TimeZoneProviders[timeZoneIndex].MoveDateTime(dateTime);
var dateTimeString = CultureInfoFormatter.FormatDateTime(movedDateTime);
labelTime.text = dateTimeString;
}
}
}