Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Mobile | Move ObservableCollections to ObservableRangeCollection #1035

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 30 additions & 66 deletions src/MobileUI/Features/Activity/ActivityPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using SSW.Rewards.ApiClient.Services;
Expand All @@ -19,9 +18,8 @@ public enum ActivityPageSegments
public partial class ActivityPageViewModel(IActivityFeedService activityService, IUserService userService) : BaseViewModel
{
public ActivityPageSegments CurrentSegment { get; set; }

[ObservableProperty]
private ObservableCollection<ActivityFeedItemDto> _feed = [];

public ObservableRangeCollection<ActivityFeedItemDto> Feed { get; set; } = [];

[ObservableProperty]
private List<Segment> _segments = [];
Expand All @@ -34,12 +32,7 @@ public partial class ActivityPageViewModel(IActivityFeedService activityService,

private bool _loaded;

private List<ActivityFeedItemDto> _allFeed = [];

private List<ActivityFeedItemDto> _friendsFeed = [];
private bool _friendsLoaded;

private const int _take = 50;
private const int Take = 50;
private int _skip;
private bool _limitReached;

Expand All @@ -50,7 +43,6 @@ public async Task Initialise()
if (_loaded)
return;

IsBusy = true;
if (Segments.Count == 0)
{
Segments =
Expand All @@ -62,13 +54,10 @@ public async Task Initialise()

userService.MyUserIdObservable().Subscribe(myUserId => _myUserId = myUserId);

await RefreshFeed();

IsBusy = false;
_loaded = true;
await LoadFeed();
}

private string GetMessage(UserAchievementDto achievement)
private static string GetMessage(UserAchievementDto achievement)
{
string name = achievement.AchievementName;
string action = string.Empty;
Expand All @@ -94,46 +83,29 @@ private string GetMessage(UserAchievementDto achievement)
break;
}

action = char.ToUpper(action[0]) + action.Substring(1);
action = char.ToUpper(action[0]) + action[1..];
return $"{action} {name}";
}

private async Task RefreshFeed()
private async Task LoadFeed(bool isRefreshing = false)
{
var feed = await GetFeedData();
_skip = 0;

if (CurrentSegment == ActivityPageSegments.Friends)
if (!isRefreshing)
{
_friendsFeed = feed;
_friendsLoaded = true;
Feed.Clear();
}
else
{
_allFeed = feed;
}

LoadFeed();
}

private void LoadFeed()
{
Feed.Clear();

if (CurrentSegment == ActivityPageSegments.Friends)
{
foreach (var f in _friendsFeed)
{
Feed.Add(f);
}
}
else
IsBusy = true;
_skip = 0;
var feed = await GetFeedData();

if (isRefreshing)
{
foreach (var f in _allFeed)
{
Feed.Add(f);
}
Feed.Clear();
}

Feed.AddRange(feed);
IsBusy = false;
_loaded = true;
}

private async Task<List<ActivityFeedItemDto>> GetFeedData()
Expand All @@ -143,8 +115,8 @@ private async Task<List<ActivityFeedItemDto>> GetFeedData()
try
{
feed = (CurrentSegment == ActivityPageSegments.Friends
? await activityService.GetFriendsActivities(_take, _skip, CancellationToken.None)
: await activityService.GetAllActivities(_take, _skip, CancellationToken.None)).Feed.Select(x =>
? await activityService.GetFriendsActivities(Take, _skip, CancellationToken.None)
: await activityService.GetAllActivities(Take, _skip, CancellationToken.None)).Feed.Select(x =>
{
x.UserAvatar = string.IsNullOrWhiteSpace(x.UserAvatar)
? "v2sophie"
Expand All @@ -171,45 +143,37 @@ private async Task LoadMore()
if (_limitReached)
return;

_skip += _take;
IsBusy = true;
_skip += Take;
var feed = await GetFeedData();

if (feed.Count == 0)
{
_limitReached = true;
IsBusy = false;
return;
}

foreach (var f in feed)
{
Feed.Add(f);
}

IsBusy = false;
Feed.AddRange(feed);
}

[RelayCommand]
private async Task FilterBySegment()
{
CurrentSegment = (ActivityPageSegments)SelectedSegment.Value;
_limitReached = false;
_skip = 0;

if (CurrentSegment == ActivityPageSegments.Friends && !_friendsLoaded)
if (!_loaded || CurrentSegment == (ActivityPageSegments)SelectedSegment.Value)
{
await RefreshFeed();
return;
}

LoadFeed();
CurrentSegment = (ActivityPageSegments)SelectedSegment.Value;
_limitReached = false;
_skip = 0;

await LoadFeed();
}

[RelayCommand]
private async Task Refresh()
{
await RefreshFeed();
await LoadFeed(true);
IsRefreshing = false;
}

Expand Down
9 changes: 4 additions & 5 deletions src/MobileUI/Features/Leaderboard/LeaderboardViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ public partial class LeaderboardViewModel : BaseViewModel
{
private int _myUserId;

private ILeaderService _leaderService;
private readonly ILeaderService _leaderService;
private readonly IUserService _userService;
private bool _loaded;

[ObservableProperty]
private ObservableCollection<LeaderViewModel> searchResults = new ();

public ObservableRangeCollection<LeaderViewModel> SearchResults { get; set; } = [];

public LeaderboardViewModel(ILeaderService leaderService, IUserService userService)
{
Expand Down Expand Up @@ -144,7 +143,7 @@ private async Task UpdateSearchResults(IEnumerable<LeaderViewModel> sortedLeader
var newList = new ObservableCollection<LeaderViewModel>(sortedLeaders);
await App.Current.MainPage.Dispatcher.DispatchAsync(() =>
{
SearchResults = newList;
SearchResults.ReplaceRange(newList);
});
}

Expand Down
Loading