-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClockModel.cs
94 lines (78 loc) · 2.17 KB
/
ClockModel.cs
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using _Project.Core.HttpRequests.YandexTimeRequest;
using _Project.Core.View;
using Sirenix.OdinInspector;
using UnityEngine;
namespace _Project.Core
{
public class ClockModel : MonoBehaviour
{
[SerializeField] YandexTimeProvider _yandex;
[SerializeField] NumericClockView _numericClockView;
[SerializeField] AnalogClockView _analogClockView;
[SerializeField] float _localUpdatePeriodSeconds = 1;
[SerializeField] int _secondsBetweenServerSync = 3600;
[SerializeField] bool _mockWithoutHttpRequestAndWithoutServerSync;
const int MoscowTimeShift = 3;
DateTime _cashedTime;
public void SetTime( DateTime newDateTime )
{
_cashedTime = newDateTime;
UpdateUi( newDateTime );
}
void Awake( )
{
if ( _mockWithoutHttpRequestAndWithoutServerSync )
{
_cashedTime = new DateTime( 2024, 9, 29, 23, 59, 55 );
}
else
{
InvokeRepeating( nameof( SyncTimeWithWebRequest ), 0f, _secondsBetweenServerSync ); //also will be invoked instantly
}
UpdateUi();
InvokeRepeating( nameof( TickLocallyRepeating ), 0f, _localUpdatePeriodSeconds );
}
void SyncTimeWithWebRequest( )
{
SetMoscowTime();
UpdateUi();
}
void SetMoscowTime( )
{
_cashedTime = GetMoscowTime();
}
DateTime GetMoscowTime( )
{
DateTime utcDateTime = _yandex.GetCurrentUtcDateTime();
return utcDateTime.AddHours( MoscowTimeShift );
}
void TickLocallyRepeating( )
{
_cashedTime = _cashedTime.AddSeconds( _localUpdatePeriodSeconds );
UpdateUi();
}
void UpdateUi( )
{
UpdateUi( _cashedTime );
}
void UpdateUi( DateTime time )
{
_analogClockView.Set(
hour: time.Hour,
minute: time.Minute,
second: time.Second
);
_numericClockView.Set(
hour: time.Hour.ToString(),
minute: time.Minute.ToString(),
second: time.Second.ToString()
);
}
[Button]
void LogMoscowTime( )
{
Debug.Log( $"<color=cyan> {GetMoscowTime()} </color>" );
}
}
}