Skip to content

Commit

Permalink
Copy refresh button to custom feed
Browse files Browse the repository at this point in the history
  • Loading branch information
bricefriha committed May 18, 2024
1 parent e8254e2 commit fe24976
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 132 deletions.
2 changes: 0 additions & 2 deletions AresNews/AresNews/Helpers/Converters/ZeroToTrueConverter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Xamarin.Forms;

namespace AresNews.Helpers
Expand Down
69 changes: 58 additions & 11 deletions AresNews/AresNews/ViewModels/FeedsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,16 @@ private async Task SwitchFeedAsync(Feed feed)
{
await Task.Run(() =>
{
UnnoticedArticles.Clear();
// Set new feed
SwitchTabs(_feeds.IndexOf(feed));
SelectedFeed.IsLoaded = false;
});
}

public App CurrentApp { get; }

public Command UncoverNewArticles { get; private set; }
private FeedsPage CurrentPage { get; set; }

public Xamarin.Forms.Command<Feed> Delete => new Xamarin.Forms.Command<Feed>( (feed) =>
Expand All @@ -180,6 +182,18 @@ await Task.Run(() =>
});

private bool _onTopScroll;

public bool OnTopScroll
{
get { return _onTopScroll; }
set
{
_onTopScroll = value;
OnPropertyChanged(nameof(OnTopScroll));
}
}

public Xamarin.Forms.Command RefreshArticles => new Xamarin.Forms.Command(() =>
{
Expand All @@ -201,7 +215,7 @@ await Task.Run(() =>
SelectedFeed = _feeds[0];
}
Refresh(SelectedFeed);
Refresh(SelectedFeed);
}
catch
{
Expand All @@ -228,8 +242,29 @@ public FeedsViewModel(FeedsPage page)

// Select first item
CurrentFeedIndex = 0;
UncoverNewArticles = new Command(() =>
{
if (UnnoticedArticles == null)
return;
if (UnnoticedArticles.Count <= 0)
return;
CurrentApp.ShowLoadingIndicator();
int indexFeed = _feeds.IndexOf(_feeds.FirstOrDefault(f => f.Id == SelectedFeed.Id));
_ = Task.Run(() =>
{
// Scroll up
CurrentPage.ScrollFeed();
// Add the unnoticed articles
UpdateArticles(UnnoticedArticles.ToList(), SelectedFeed, indexFeed);
UnnoticedArticles.Clear();
}).ContinueWith(res => CurrentApp.RemoveLoadingIndicator());
});



UpdateOrders = new List<UpdateOrder>();

Expand Down Expand Up @@ -322,7 +357,22 @@ public Command ShareArticle
{
get { return _shareArticle; }
}
private ObservableCollection<Article> _unnoticedArticles;

public ObservableCollection<Article> UnnoticedArticles
{
get { return _unnoticedArticles; }
set
{
_unnoticedArticles = value;
if (_unnoticedArticles?.Count > 0)
CurrentPage.ShowRefreshButton();
else
CurrentPage.RemoveRefreshButton();

OnPropertyChanged(nameof(UnnoticedArticles));
}
}
public Command<string> FeedSelect
{
get { return new Command<string> ((feedId) =>
Expand Down Expand Up @@ -435,23 +485,20 @@ private async Task AggregateFeed(Feed feed, bool firstLoad = true)
if (needUpdate)
{
if (articles.Count > 0)
UpdateArticles(articles, feed, indexFeed);
if (OnTopScroll)
UpdateArticles(articles, feed, indexFeed);
else
UnnoticedArticles = new ObservableCollection<Article>( articles);
}
else
{
//Articles = new ObservableCollection<Article>();
// Update list of articles
InsertArticles(articles);
//Articles = new ObservableCollection<Article>(articles);

}
SelectedFeed.IsLoaded = true;
//Feeds[indexFeed] = feed;

IsRefreshing = false;
//_isInCustomFeed = true;
//IsSearchOpen = true;
//_prevSearch = SearchText;

}
/// <summary>
Expand Down Expand Up @@ -679,7 +726,7 @@ private void UpdateFeeds()
// Remove the outdated feeds
foreach (var feed in removedFeeds)
{
RemoveFeed(feed);
_ =RemoveFeed(feed);
}
}

Expand Down
103 changes: 62 additions & 41 deletions AresNews/AresNews/Views/FeedsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,65 +119,67 @@
<On Platform="Android" Value="{StaticResource LightDark}"/>
</OnPlatform>
</RefreshView.BackgroundColor>
<CollectionView ItemsSource="{Binding Articles, Source={x:RelativeSource AncestorType={x:Type vm:FeedsViewModel}}, Mode=OneWay}"
x:DataType="model:Feed"
VerticalScrollBarVisibility="Never"
x:Name="newsCollectionView"
ItemsUpdatingScrollMode="KeepScrollOffset"
HorizontalOptions="Center"
Margin="8,0">
<CollectionView.Header>
<BoxView Margin="0">
<BoxView.HeightRequest>
<OnPlatform x:TypeArguments="x:Double">
<On Platform="Android" Value="0"/>
<On Platform="iOS" Value="75"/>
</OnPlatform>
</BoxView.HeightRequest>
</BoxView>
</CollectionView.Header>
<CollectionView.ItemsLayout>
<LinearItemsLayout Orientation="Vertical" ItemSpacing="25" />
</CollectionView.ItemsLayout>
<CollectionView.EmptyView>
<ContentView IsVisible="{Binding IsBusy, Source={x:RelativeSource AncestorType={x:Type vm:FeedsViewModel}},Converter={StaticResource BoolInverseConverter}}">
<Grid>
<CollectionView ItemsSource="{Binding Articles, Source={x:RelativeSource AncestorType={x:Type vm:FeedsViewModel}}, Mode=OneWay}"
x:DataType="model:Feed"
VerticalScrollBarVisibility="Never"
x:Name="newsCollectionView"
Scrolled="newsCollectionView_Scrolled"
ItemsUpdatingScrollMode="KeepScrollOffset"
HorizontalOptions="Center"
Margin="8,0">
<CollectionView.Header>
<BoxView Margin="0">
<BoxView.HeightRequest>
<OnPlatform x:TypeArguments="x:Double">
<On Platform="Android" Value="0"/>
<On Platform="iOS" Value="75"/>
</OnPlatform>
</BoxView.HeightRequest>
</BoxView>
</CollectionView.Header>
<CollectionView.ItemsLayout>
<LinearItemsLayout Orientation="Vertical" ItemSpacing="25" />
</CollectionView.ItemsLayout>
<CollectionView.EmptyView>
<ContentView IsVisible="{Binding IsBusy, Source={x:RelativeSource AncestorType={x:Type vm:FeedsViewModel}},Converter={StaticResource BoolInverseConverter}}">

<StackLayout VerticalOptions="CenterAndExpand"
<StackLayout VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
IsVisible="{Binding FeedTabs.Count, Source={x:RelativeSource AncestorType={x:Type vm:FeedsViewModel}}, Converter={StaticResource ZeroToFalseConverter}}">
<StackLayout.Triggers>
<DataTrigger Binding="{Binding FeedTabs.Count, Source={x:RelativeSource AncestorType={x:Type vm:FeedsViewModel}}}"
<StackLayout.Triggers>
<DataTrigger Binding="{Binding FeedTabs.Count, Source={x:RelativeSource AncestorType={x:Type vm:FeedsViewModel}}}"
TargetType="StackLayout"
Value="0">
<Setter Property="IsVisible"
<Setter Property="IsVisible"
Value="false" />
</DataTrigger>
</StackLayout.Triggers>
<Label Text="&#xf005;"
</DataTrigger>
</StackLayout.Triggers>
<Label Text="&#xf005;"
FontFamily="FaSolid"
TextColor="{StaticResource Primary}"
FontSize="50"
HorizontalOptions="Center"
/>
<Label Text="Ooopsi 😟"
<Label Text="Ooopsi 😟"
FontFamily="P-Bold"
HorizontalOptions="Center"
FontSize="Title"
TextColor="{StaticResource FontColor}"
/>
<Label Text="it seems like no articles are matching the keyword set for this feed, you should try something else."
<Label Text="it seems like no articles are matching the keyword set for this feed, you should try something else."
FontFamily="P-Regular"
Margin="5,0"
HorizontalTextAlignment="Center"
FontSize="Subtitle"
TextColor="{StaticResource FontColor}"
/>
</StackLayout>
</ContentView>
</CollectionView.EmptyView>
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="model:Article">
<controls:ArticlePreview Headline="{Binding Title, Mode=OneWay}"
</StackLayout>
</ContentView>
</CollectionView.EmptyView>
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="model:Article">
<controls:ArticlePreview Headline="{Binding Title, Mode=OneWay}"
ArticleId="{Binding Id}"
ArticleIsSaved="{Binding IsSaved}"
ArticleTime="{Binding Time, Mode=OneWay, Converter={StaticResource TimeSpanConverter}}"
Expand All @@ -190,9 +192,26 @@
TapCommand="{Binding Source={RelativeSource AncestorType={x:Type vm:FeedsViewModel}}, Path=GoToDetail}"
TapCommandParameter="{Binding Id}"
/>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<Button Text="{Binding UnnoticedArticles.Count, StringFormat='{0} new articles'}"
BackgroundColor="{StaticResource PrimaryAccent}"
TextColor="{StaticResource FontColor}"
IsVisible="{Binding UnnoticedArticles.Count, Converter={StaticResource ZeroToFalseConverter}, Mode=TwoWay}"
FontFamily="P-Bold"
FontSize="15"
Padding="1"
HeightRequest="30"
WidthRequest="150"
Margin="10"
Command="{Binding UncoverNewArticles}"
HorizontalOptions="Center"
VerticalOptions="Start"
CornerRadius="12"
x:Name="refreshButton"
Grid.Row="0" />
</Grid>
</RefreshView>
<!---->

Expand All @@ -202,7 +221,7 @@
BackgroundColor="{StaticResource BgBannerColor }"
Grid.Row="0"
RowDefinitions="*,1">

<Grid.Triggers>
<DataTrigger Binding="{Binding FeedTabs.Count}"
TargetType="Grid"
Expand Down Expand Up @@ -268,6 +287,7 @@
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>

</Grid>
<!---->
<!-- HitBox - Layer that detect touch -->
Expand Down Expand Up @@ -325,6 +345,7 @@
</StackLayout>
</Frame>
<!-- -->

</Grid>
</ContentPage.Content>
</ContentPage>
Loading

0 comments on commit fe24976

Please sign in to comment.