Skip to content

Commit

Permalink
Refactor code to remove unused variables and add new features
Browse files Browse the repository at this point in the history
- Remove unused private variable in Profile class
- Add a new public method to ObservableCollectionExtensions class that removes the last N elements from an observable collection
- Add a new string resource for "No recent posts" in en.json file
- Modify the Profile class to use a placeholder image if no picture is available, or generate one with the user's name if it is available.
- Modify MainViewModel class to limit the number of posts displayed on screen by removing old ones when there are more than 100 posts.
- Modify LoginView.xaml file to make password entry field hidden while typing
- Add event handler for UnreadBtn_Clicked event in MainView.xaml.cs file.
  • Loading branch information
kfrancis committed May 18, 2023
1 parent 8e26f86 commit 3a7c832
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 16 deletions.
28 changes: 20 additions & 8 deletions src/NuSocial/Core/UsefulExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NuSocial.Core
namespace NuSocial.Core
{
internal static class UsefulExtensions
public static class ObservableCollectionExtensions
{
public static void RemoveLastN<T>(this ObservableCollection<T> collection, int n)
{
if (collection == null)
{
throw new ArgumentNullException(nameof(collection));
}

if (n < 0)
{
throw new ArgumentOutOfRangeException(nameof(n), "n must be non-negative");
}

int removeCount = Math.Min(n, collection.Count);

for (int i = 0; i < removeCount; i++)
{
collection.RemoveAt(collection.Count - 1);
}
}
}
}
3 changes: 2 additions & 1 deletion src/NuSocial/Localization/NuSocial/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"AccountKeyPlaceholder": "nsec1...",
"NuSocial": "NuSocial",
"TapToRegenerate": "Tap to regenerate",
"WelcomeText": "#nostr NuSocial: Your Universe, Nu Connections"
"WelcomeText": "#nostr NuSocial: Your Universe, Nu Connections",
"NoRecentPosts": "No recent posts"
}
}
9 changes: 3 additions & 6 deletions src/NuSocial/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ public string PrivateKeyString

public class Profile
{
private string? _picture;
private string? _picture = "https://placehold.co/60x60";

public string Name { get; internal set; } = string.Empty;
public string? DisplayName { get; internal set; }
public string? Picture
{
get
{
if (string.IsNullOrEmpty(_picture))
if (string.IsNullOrEmpty(_picture) || (_picture.Equals("https://placehold.co/60x60", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrEmpty(Name)))
{
_picture = $"https://placehold.co/60x60?text={Name}";
}
Expand Down Expand Up @@ -70,11 +70,8 @@ public class UserConfiguration
}

/// <summary>
/// A simple model for a contact.
/// A contact on the #nostr network
/// </summary>
/// <param name="Name">Gets the name of the contact.</param>
/// <param name="Email">Gets the email of the contact.</param>
/// <param name="Picture">Gets the picture of the contact.</param>
public sealed record Contact
{
[JsonPropertyName("name")]
Expand Down
8 changes: 8 additions & 0 deletions src/NuSocial/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CommunityToolkit.Mvvm.Messaging;
using Nostr.Client.Messages;
using NuSocial.Core;
using NuSocial.Core.ViewModel;
using NuSocial.Messages;
using System.Collections.Immutable;
Expand All @@ -12,6 +13,7 @@ public partial class MainViewModel : BaseViewModel, ITransientDependency, IDispo
{
private readonly IAuthorService _authorService;
private CancellationTokenSource? _cts = new();
private const int _postThreshold = 100;

[ObservableProperty]
private ObservableCollection<Post> _posts = new();
Expand Down Expand Up @@ -143,8 +145,14 @@ private Task UpdatePostsAsync()
return SetBusyAsync(() =>
{
var posts = _postsWaiting.ToImmutableList();
_postsWaiting.Clear();
OnPropertyChanged(nameof(UnreadLabel));
if (Posts.Count > _postThreshold)
{
Posts.RemoveLastN(posts.Count);
}
foreach (var post in posts)
{
Posts.AddFirst(post);
Expand Down
1 change: 1 addition & 0 deletions src/NuSocial/Views/LoginView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<Label Text="Enter your account key to login:" VerticalOptions="Center" />
<Entry
Grid.Row="1"
IsPassword="True"
Placeholder="{loc:Translate AccountKeyPlaceholder}"
Text="{Binding AccountKey}" />
<Button
Expand Down
2 changes: 1 addition & 1 deletion src/NuSocial/Views/MainView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
CornerRadius="5"
IsEnabled="{Binding IsNotBusy}"
Style="{StaticResource PrimaryActionButtonStyle}"
Text="{Binding UnreadLabel}" />
Text="{Binding UnreadLabel}" x:Name="UnreadBtn" Clicked="UnreadBtn_Clicked" />
</Grid>
<RefreshView Grid.Row="1" Command="{Binding RefreshCommand}">
<CollectionView
Expand Down
7 changes: 7 additions & 0 deletions src/NuSocial/Views/MainView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CommunityToolkit.Mvvm.DependencyInjection;
using Microsoft.Maui.Controls;
using NuSocial.Core.View;

namespace NuSocial.Views;
Expand All @@ -12,4 +13,10 @@ public MainView(MainViewModel vm) : base(vm)
public MainView() : this(Ioc.Default.GetRequiredService<MainViewModel>())
{
}

private void UnreadBtn_Clicked(object sender, EventArgs e)
{
// We should always scroll to the top when loading new posts because we're inserting at the top
//collectionView.ScrollTo(0);
}
}

0 comments on commit 3a7c832

Please sign in to comment.