From e680376c546feca90576caa4fab6c32bf35a0aca Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 21 Nov 2017 08:03:58 +0100 Subject: [PATCH] saispastrop --- Classes/api/fetchArticles.java | 85 +++++++++++++++++++++++++ Classes/api/fetchSources.java | 54 ++++++++++++++++ controller/RootLayout.java | 4 +- model/NewsListModel.java | 110 +++++++++++---------------------- 4 files changed, 178 insertions(+), 75 deletions(-) create mode 100644 Classes/api/fetchArticles.java create mode 100644 Classes/api/fetchSources.java diff --git a/Classes/api/fetchArticles.java b/Classes/api/fetchArticles.java new file mode 100644 index 0000000..642bf78 --- /dev/null +++ b/Classes/api/fetchArticles.java @@ -0,0 +1,85 @@ +package Classes.api; + +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Date; + +import org.json.JSONArray; +import org.json.JSONObject; + +import Classes.Event; +import Interfaces.Callback; +import model.NewsListModel; +import model.NewsModel; + +public class fetchArticles implements Callback{ + + private NewsListModel context; + private String nextQuery; + + public fetchArticles(NewsListModel context, String nextQuery){ + + this.context = context; + this.nextQuery = nextQuery; + + } + + + @Override + public void onSuccess(JSONObject response){ + + /* (1) Création des NewsModel depuis le json + ---------------------------------------------------------*/ + /* (1) Init. news list */ + ArrayList newsArr = new ArrayList(); + + /* (2) Get article list */ + JSONArray article_list = response.getJSONArray("articles"); + + /* (3) For each article -> create corresponding NewsModel */ + for( int i = 0, il = article_list.length() ; i < il ; i++ ){ + + // {3.1} Get local copy of the JSON // + JSONObject news_i = (JSONObject) article_list.get(i); + + // {3.2} Create new NewsModel // + NewsModel news = new NewsModel(); + + // {3.3} Set attributes // + news.setAuthor( news_i.getString("author") ) + .setDescription( news_i.getString("description") ) + .setTitle( news_i.getString("title") ) + .setNewsURL( news_i.getString("url") ) + .setImageURL( news_i.getString("urlToImage") ) + .setSource( news_i.getJSONObject("source").getString("name") ) + .addTag( this.context.getCategory() ); + + // {3.4} Add the date (can throw Ex) // + try { news.setDate(news_i.getString("publishedAt")); } + catch( ParseException e ){ news.setDate(new Date()); } + + // {3.5} Add to the set // + newsArr.add(news); + + } + + /* (4) Apply to Context */ + this.context.setNews( newsArr ); + this.context.setQuery( this.nextQuery ); + + /* (5) Remove observers */ + this.context.removeObserver("newsApiCall"); + this.context.notifyObservers(new Event("NewsModel","NewsQuerySuccess")); + + } + + @Override + public void onError(String errDesc) { + + System.out.println("Error: "+errDesc); + this.context.removeObserver("newsApiCall"); + this.context.notifyObservers(new Event("NewsModel","NewsQueryFailed")); + + } + +} \ No newline at end of file diff --git a/Classes/api/fetchSources.java b/Classes/api/fetchSources.java new file mode 100644 index 0000000..1c55ede --- /dev/null +++ b/Classes/api/fetchSources.java @@ -0,0 +1,54 @@ +package Classes.api; + +import org.json.JSONArray; +import org.json.JSONObject; + +import Classes.Event; +import Interfaces.Callback; +import model.NewsListModel; + +public class fetchSources implements Callback{ + + private NewsListModel context; + private String nextQuery; + + public fetchSources(NewsListModel context){ + + this.context = context; + this.nextQuery = nextQuery; + + } + + + @Override + public void onSuccess(JSONObject response){ + + //on récupère toutes les sources (dans la limite de 20 sources) + JSONArray arr = response.getJSONArray("sources"); + + for(int i = 0;i sources; - //liste des observers - private HashMap observers; - //liste des news finale - private ArrayList news; - //critère de tri - private SortTypes sortType = SortTypes.relevancy; - //critère de recherche de l'utilisateur - private String query = ""; + + private String APIKey; // Clé d'API + private Category cat = Category.all; // catégorie choise parl'utilisateur + private Boolean isRetreivingSources = false; // est ce que le modele est en train de récupérer les sources + private Boolean apiError = false; // est ce que l'api a rencontré une erreur + private ArrayList sources; // liste des sources + private HashMap observers; // liste des observers + private ArrayList news; // liste des news finale + private SortTypes sortType = SortTypes.relevancy; // critère de tri + private String query = ""; // critère de recherche de l'utilisateur private NewsListModel(String APIKey) { + this.APIKey = APIKey; + //comme la liste des sources retournée par l'API est de 20 éléments max, pas la peine de déclarer une arraylist sans borne - this.sources = new ArrayList(20); - this.observers = new HashMap(); - this.news = new ArrayList(); + this.sources = new ArrayList(20); + this.observers = new HashMap(); + this.news = new ArrayList(); + } public static NewsListModel getInstance() { - if(NewsListModel.instance == null) { + + if( NewsListModel.instance == null ) NewsListModel.instance = new NewsListModel("0e72f765c5c84313ae31a5a7e9e61735"); - } return NewsListModel.instance; + } public void setCategory(Category cat) { - if(cat == Category.all) { + if( cat == Category.all ){ this.sources = null; this.cat = cat; return; } //on vide la liste des sources - this.sources = new ArrayList(20); + this.sources.clear(); this.cat = cat; + //on créé l'URL d'appel de l'API String lang = LangModel.getInstance().getToLang().name(); String URL = "http://beta.newsapi.org/v2/sources?language="+lang; //on rajoute la catégorie - if(cat != Category.all) { + if( cat != Category.all ) URL += "&category="+cat.name(); - } //on rajoute la clé d'api URL += "&apiKey="+this.APIKey; @@ -108,6 +103,7 @@ public class NewsListModel implements Observable{ } }); + this.isRetreivingSources = true; api.send(); } @@ -138,50 +134,10 @@ public class NewsListModel implements Observable{ URL += sources; } - ApiCall api = new ApiCall(URL,"GET",new Callback() { - - @Override - public void onSuccess(JSONObject response) { - //on parcours le JSON en créant les objets NewsModel corespondant - NewsListModel.this.news = new ArrayList(); - JSONArray arr = response.getJSONArray("articles"); - for(int i = 0;i news){ + this.news = news; + } + public ArrayList getNews(){ return this.news; }