-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improving code styles which should increase performances
- Loading branch information
Showing
5 changed files
with
78 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,42 @@ | ||
using Microsoft.Extensions.Logging; | ||
namespace HackerNews; | ||
|
||
namespace HackerNews; | ||
|
||
public partial class MainPage | ||
public partial class MainPage : ContentPage | ||
{ | ||
private readonly NewsViewModel _newsViewModel; | ||
|
||
public MainPage() | ||
|
||
// Constructor accepting NewsViewModel via Dependency Injection | ||
public MainPage(NewsViewModel newsViewModel) | ||
{ | ||
InitializeComponent(); | ||
_newsViewModel = newsViewModel; | ||
|
||
// Set up the logger | ||
var loggerFactory = LoggerFactory.Create(builder => | ||
{ | ||
builder.AddConsole(); // Add other logging providers as necessary | ||
}); | ||
|
||
var logger = loggerFactory.CreateLogger<NewsViewModel>(); | ||
|
||
// Create an instance of NewsService, passing the loggerFactory if needed | ||
var newsService = new NewsService(loggerFactory); | ||
|
||
// Create the NewsViewModel instance | ||
_newsViewModel = new NewsViewModel(newsService, logger); | ||
|
||
// Set the ItemsSource for the NewsListView | ||
NewsListView.ItemsSource = _newsViewModel.TopStoryCollection; | ||
// Set the BindingContext for data binding in XAML | ||
BindingContext = _newsViewModel; | ||
} | ||
|
||
protected override async void OnAppearing() | ||
{ | ||
base.OnAppearing(); | ||
await _newsViewModel.Refresh(); | ||
await _newsViewModel.RefreshAsync(); | ||
} | ||
|
||
private async void NewsListView_OnItemSelected(object sender, SelectedItemChangedEventArgs e) | ||
private async void NewsCollectionView_OnSelectionChanged(object sender, SelectionChangedEventArgs e) | ||
{ | ||
var listView = (ListView)sender; | ||
listView.SelectedItem = null; | ||
|
||
if (e.SelectedItem is not StoryModel storyModel) return; | ||
if (!string.IsNullOrEmpty(storyModel.Url)) | ||
{ | ||
var browserOptions = new BrowserLaunchOptions(); | ||
await Browser.Default.OpenAsync(storyModel.Url, browserOptions); | ||
} | ||
else | ||
// Check if there is a selected item and access it directly by index | ||
if (e.CurrentSelection.Count > 0 && e.CurrentSelection[0] is StoryModel storyModel) | ||
{ | ||
await DisplayAlert("Invalid Article", "ASK HN articles have no url", "OK"); | ||
// Clear selection | ||
((CollectionView)sender).SelectedItem = null; | ||
|
||
if (!string.IsNullOrEmpty(storyModel.Url)) | ||
{ | ||
var browserOptions = new BrowserLaunchOptions { LaunchMode = BrowserLaunchMode.SystemPreferred }; | ||
await Browser.Default.OpenAsync(storyModel.Url, browserOptions); | ||
} | ||
else | ||
{ | ||
await DisplayAlert("Invalid Article", "ASK HN articles have no URL", "OK"); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters