Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
uzkns committed Jan 18, 2021
1 parent ec8159f commit f317847
Show file tree
Hide file tree
Showing 10 changed files with 316 additions and 51 deletions.
37 changes: 33 additions & 4 deletions lib/controllers/article_categorization_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,21 @@ import 'package:flutter_app/controllers/article_download_controller.dart';
import 'package:flutter_app/models/article.dart';
import 'package:flutter_app/models/category.dart';

// TODO as Singleton

/// This class controls the article categories. It is reposnsibe for filling the
/// categories with articles and removing duplicate articles.
///
/// From these categories, the top horizontal-scrolling list is built.
///
/// Note: It does NOT hold the saved/dismissed articles! These are in home.dart
class ArticleCategorizationController {

//The language of the articles that are to be fetched.
//TODO make changable by settings
final String _language = "de";

// All categories
Category _business;
Category _entertainment;
Category _general;
Expand All @@ -12,8 +25,10 @@ class ArticleCategorizationController {
Category _sports;
Category _technology;

// All categories as a List
List<Category> _categories;

/// Creates all categories. They are empty after creation.
ArticleCategorizationController() {
_business = new Category.createEmpty("Business", "business", "Business and finance news", "https://images.unsplash.com/photo-1491336477066-31156b5e4f35?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80");
_entertainment = new Category.createEmpty("Entertainment", "entertainment", "Entertainment news and stars", "https://images.unsplash.com/photo-1496337589254-7e19d01cec44?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80");
Expand All @@ -33,6 +48,7 @@ class ArticleCategorizationController {
_categories.add(_technology);
}

// Getter for all categories
Category get business => _business;
Category get general => _general;
Category get entertainment => _entertainment;
Expand All @@ -41,21 +57,34 @@ class ArticleCategorizationController {
Category get sports => _sports;
Category get technology => _technology;

/// Returns a List with all categories.
List<Category> getAllCategories() {
return _categories;
}

/// Fetches articles for a given category.
/// This uses
/// ArticleDownloadController->fetch()
/// with the categories api-name to get the top headlines for the category
/// and language.
Future<void> fetchArticles(Category c) async {
ArticleDownloadController adc = ArticleDownloadController.getTopHeadlines(c.apiName, "de", "");
ArticleDownloadController adc = ArticleDownloadController.getTopHeadlines(c.apiName, _language, "");
await adc.fetch();
c.addArticles(adc.articles);
}


void fetchForAll() {
/// Fetches articles for all categories
/// Unfortuately, the fetching cannot happen in parallel because
/// the view (home.dart) will not wait for it to be finished and display
/// an empty card list. So, we have to "await" every category to finish.
///
/// The categories are filled after this completes.
Future<void> fetchForAll() async {
//TODO see if general can be handled extra
for (Category c in _categories) {
this.fetchArticles(c);
await this.fetchArticles(c);
}
return;
}


Expand Down
9 changes: 7 additions & 2 deletions lib/controllers/article_download_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ class ArticleDownloadController {
final String searchKeyword;



/// Constructor to get the top headlines for the given Category and country.
/// a search keyword can be passed aswell.
ArticleDownloadController.getTopHeadlines(this.articleCategory, this.countryCode, this.searchKeyword) {
this._endpoint = "v2/top-headlines";
_buildURL();
}

/// Constructor to get every headline for the given Category and country.
/// a search keyword can be passed aswell.
ArticleDownloadController.getEverything(this.articleCategory, this.countryCode, this.searchKeyword) {
this._endpoint = "v2/everything";
_buildURL();
}


/// Builds the URL from the parameters given to the constructor.
void _buildURL() {
this._url = "http://newsapi.org/" + this._endpoint + "?";

Expand Down Expand Up @@ -78,7 +82,8 @@ class ArticleDownloadController {
articles.add(a);
}
});

//TODO remove
print("Downloaded all articles for category " + this.articleCategory);
return;
} else {
return;
Expand Down
2 changes: 1 addition & 1 deletion lib/controllers/article_persistence_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class ArticlePersistenceController {

/// Clears the contents of the save file
///
/// This is a debug method that is called via the Settings view
/// This is a debug method that is called in the Settings view
Future<File> clear() async {
final file = await _localFile;
List<Article> emptyList = new List<Article>();
Expand Down
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_app/views/home.dart';

void main() {
void main() async {
runApp(MyApp());
}

Expand Down
2 changes: 2 additions & 0 deletions lib/models/article.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/// The Model for an article
/// The fields are final and set by the download controller.
/// They correspond to the JSON values that newsapi.org provides.
class Article {

final String sourceId;
Expand Down
5 changes: 2 additions & 3 deletions lib/models/category.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import 'package:flutter_app/models/article.dart';

/// Model for an Article category
///
/// Thus far, there are only two categories of Articles:
/// - Saved articles
/// - dismissed Articles
/// It has the necessary information for all controllers and views (api keys, images, description)
/// and a List of all Articles for the Category.
class Category {
final String name;
final String description;
Expand Down
Loading

0 comments on commit f317847

Please sign in to comment.