-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathCommunityFetchingControllerBase.cs
More file actions
81 lines (62 loc) · 2.38 KB
/
Copy pathCommunityFetchingControllerBase.cs
File metadata and controls
81 lines (62 loc) · 2.38 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
81
using Cysharp.Threading.Tasks;
using System;
using System.Threading;
namespace DCL.Communities.CommunitiesCard
{
/// <summary>
/// A base class for controllers that handle fetching data for community sections in order to wrap common behavior such as:
/// - Handling the fetching state
/// - Storing the cancellation token given by the main controller
/// - Handling the fetch logic + loading and empty states
/// </summary>
public abstract class CommunityFetchingControllerBase<T, U> : IDisposable
where U : ICommunityFetchingView
{
private readonly int pageSize;
protected CancellationToken cancellationToken;
protected bool isFetching;
protected abstract SectionFetchData<T> currentSectionFetchData { get; }
private U view;
protected CommunityFetchingControllerBase(U view,
int pageSize)
{
this.view = view;
this.pageSize = pageSize;
this.view.NewDataRequested += OnNewDataRequested;
}
public virtual void Dispose()
{
view.NewDataRequested -= OnNewDataRequested;
}
private void OnNewDataRequested()
{
if (isFetching) return;
FetchNewDataAsync(cancellationToken).Forget();
}
protected async UniTaskVoid FetchNewDataAsync(CancellationToken ct)
{
isFetching = true;
try
{
SectionFetchData<T> membersData = currentSectionFetchData;
view.SetEmptyStateActive(false);
if (membersData.pageNumber == 0)
view.SetLoadingStateActive(true);
int count = membersData.items.Count;
membersData.pageNumber++;
membersData.totalToFetch = await FetchDataAsync(ct);
membersData.totalFetched = membersData.pageNumber * pageSize;
view.SetLoadingStateActive(false);
view.SetEmptyStateActive(membersData.totalToFetch == 0);
view.RefreshGrid(count == membersData.items.Count);
}
finally { isFetching = false; }
}
protected abstract UniTask<int> FetchDataAsync(CancellationToken ct);
public virtual void Reset()
{
isFetching = false;
view.RefreshGrid(true);
}
}
}