saispastrop
This commit is contained in:
parent
606cfe8aeb
commit
e680376c54
|
@ -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<NewsModel> newsArr = new ArrayList<NewsModel>();
|
||||||
|
|
||||||
|
/* (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"));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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<arr.length() && i<20;i++){
|
||||||
|
|
||||||
|
String source_i = (JSONObject) arr.get(i);
|
||||||
|
|
||||||
|
this.context.sources.add( source_i.getString("id") );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
this.context.isRetreivingSources = false;
|
||||||
|
this.context.apiError = false;
|
||||||
|
|
||||||
|
//tout s'est bien passé, on peut notifier du succès
|
||||||
|
this.context.notifyObservers(new Event("NewsModel", "SourcesUpdated"));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(String errDesc) {
|
||||||
|
//on notifie de l'échec et on garde en mémoire le fait que l'on a échoué (afin d'annuler la recherche de news qui va suivre)
|
||||||
|
this.context.isRetreivingSources = false;
|
||||||
|
this.context.apiError = true;
|
||||||
|
this.context.notifyObservers(new Event("NewsModel", "SourcesUpdateFailed"));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -191,7 +191,7 @@ public class RootLayout extends Application implements EventObserver {
|
||||||
public void handleMainLayoutChange(String layout) {
|
public void handleMainLayoutChange(String layout) {
|
||||||
|
|
||||||
NewsListModel.getInstance().addObserver("MainClass", this);
|
NewsListModel.getInstance().addObserver("MainClass", this);
|
||||||
NewsListModel.getInstance().setCategory(Category.business);
|
NewsListModel.getInstance().setCategory(Category.science);
|
||||||
NewsListModel.getInstance().setSortType(SortTypes.publishedAt);
|
NewsListModel.getInstance().setSortType(SortTypes.publishedAt);
|
||||||
NewsListModel.getInstance().query("bitcoin");
|
NewsListModel.getInstance().query("bitcoin");
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package model;
|
package model;
|
||||||
|
|
||||||
import java.text.ParseException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
|
@ -12,6 +10,7 @@ import Classes.ApiCall;
|
||||||
import Classes.Category;
|
import Classes.Category;
|
||||||
import Classes.Event;
|
import Classes.Event;
|
||||||
import Classes.SortTypes;
|
import Classes.SortTypes;
|
||||||
|
import Classes.api.fetchArticles;
|
||||||
import Interfaces.Callback;
|
import Interfaces.Callback;
|
||||||
import Interfaces.DelayedCallback;
|
import Interfaces.DelayedCallback;
|
||||||
import Interfaces.EventObserver;
|
import Interfaces.EventObserver;
|
||||||
|
@ -23,39 +22,35 @@ public class NewsListModel implements Observable{
|
||||||
//instance du singleton
|
//instance du singleton
|
||||||
private static NewsListModel instance;
|
private static NewsListModel instance;
|
||||||
|
|
||||||
//Clé d'API
|
|
||||||
private String APIKey;
|
private String APIKey; // Clé d'API
|
||||||
//catégorie choise parl'utilisateur
|
private Category cat = Category.all; // catégorie choise parl'utilisateur
|
||||||
private Category cat = Category.all;
|
private Boolean isRetreivingSources = false; // est ce que le modele est en train de récupérer les sources
|
||||||
//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 Boolean isRetreivingSources = false;
|
private ArrayList<String> sources; // liste des sources
|
||||||
//est ce que l'api a rencontré une erreur
|
private HashMap<String,EventObserver> observers; // liste des observers
|
||||||
private Boolean apiError = false;
|
private ArrayList<NewsModel> news; // liste des news finale
|
||||||
//liste des sources
|
private SortTypes sortType = SortTypes.relevancy; // critère de tri
|
||||||
private ArrayList<String> sources;
|
private String query = ""; // critère de recherche de l'utilisateur
|
||||||
//liste des observers
|
|
||||||
private HashMap<String,EventObserver> observers;
|
|
||||||
//liste des news finale
|
|
||||||
private ArrayList<NewsModel> news;
|
|
||||||
//critère de tri
|
|
||||||
private SortTypes sortType = SortTypes.relevancy;
|
|
||||||
//critère de recherche de l'utilisateur
|
|
||||||
private String query = "";
|
|
||||||
|
|
||||||
private NewsListModel(String APIKey) {
|
private NewsListModel(String APIKey) {
|
||||||
|
|
||||||
this.APIKey = 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
|
//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<String>(20);
|
this.sources = new ArrayList<String>(20);
|
||||||
this.observers = new HashMap<String, EventObserver>();
|
this.observers = new HashMap<String, EventObserver>();
|
||||||
this.news = new ArrayList<NewsModel>();
|
this.news = new ArrayList<NewsModel>();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static NewsListModel getInstance() {
|
public static NewsListModel getInstance() {
|
||||||
if(NewsListModel.instance == null) {
|
|
||||||
|
if( NewsListModel.instance == null )
|
||||||
NewsListModel.instance = new NewsListModel("0e72f765c5c84313ae31a5a7e9e61735");
|
NewsListModel.instance = new NewsListModel("0e72f765c5c84313ae31a5a7e9e61735");
|
||||||
}
|
|
||||||
|
|
||||||
return NewsListModel.instance;
|
return NewsListModel.instance;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCategory(Category cat) {
|
public void setCategory(Category cat) {
|
||||||
|
@ -67,16 +62,16 @@ public class NewsListModel implements Observable{
|
||||||
}
|
}
|
||||||
|
|
||||||
//on vide la liste des sources
|
//on vide la liste des sources
|
||||||
this.sources = new ArrayList<String>(20);
|
this.sources.clear();
|
||||||
this.cat = cat;
|
this.cat = cat;
|
||||||
|
|
||||||
//on créé l'URL d'appel de l'API
|
//on créé l'URL d'appel de l'API
|
||||||
String lang = LangModel.getInstance().getToLang().name();
|
String lang = LangModel.getInstance().getToLang().name();
|
||||||
String URL = "http://beta.newsapi.org/v2/sources?language="+lang;
|
String URL = "http://beta.newsapi.org/v2/sources?language="+lang;
|
||||||
|
|
||||||
//on rajoute la catégorie
|
//on rajoute la catégorie
|
||||||
if(cat != Category.all) {
|
if( cat != Category.all )
|
||||||
URL += "&category="+cat.name();
|
URL += "&category="+cat.name();
|
||||||
}
|
|
||||||
|
|
||||||
//on rajoute la clé d'api
|
//on rajoute la clé d'api
|
||||||
URL += "&apiKey="+this.APIKey;
|
URL += "&apiKey="+this.APIKey;
|
||||||
|
@ -108,6 +103,7 @@ public class NewsListModel implements Observable{
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this.isRetreivingSources = true;
|
this.isRetreivingSources = true;
|
||||||
api.send();
|
api.send();
|
||||||
}
|
}
|
||||||
|
@ -138,50 +134,10 @@ public class NewsListModel implements Observable{
|
||||||
URL += sources;
|
URL += sources;
|
||||||
}
|
}
|
||||||
|
|
||||||
ApiCall api = new ApiCall(URL,"GET",new Callback() {
|
ApiCall api = new ApiCall(URL,
|
||||||
|
"GET",
|
||||||
@Override
|
new fetchArticles(this, q)
|
||||||
public void onSuccess(JSONObject response) {
|
);
|
||||||
//on parcours le JSON en créant les objets NewsModel corespondant
|
|
||||||
NewsListModel.this.news = new ArrayList<NewsModel>();
|
|
||||||
JSONArray arr = response.getJSONArray("articles");
|
|
||||||
for(int i = 0;i<arr.length();i++) {
|
|
||||||
JSONObject jsonNews = (JSONObject)arr.get(i);
|
|
||||||
NewsModel news = new NewsModel();
|
|
||||||
|
|
||||||
news.setAuthor(jsonNews.getString("author"))
|
|
||||||
.setDescription(jsonNews.getString("description"))
|
|
||||||
.setTitle(jsonNews.getString("title"))
|
|
||||||
.setNewsURL(jsonNews.getString("url"))
|
|
||||||
.setImageURL(jsonNews.getString("urlToImage"))
|
|
||||||
.setSource(jsonNews.getJSONObject("source").getString("name"))
|
|
||||||
.addTag(NewsListModel.this.cat);
|
|
||||||
|
|
||||||
try {
|
|
||||||
news.setDate(jsonNews.getString("publishedAt"));
|
|
||||||
}catch (ParseException e) {
|
|
||||||
news.setDate(new Date());
|
|
||||||
}
|
|
||||||
|
|
||||||
NewsListModel.this.news.add(news);
|
|
||||||
}
|
|
||||||
|
|
||||||
NewsListModel.this.query = q;
|
|
||||||
//ne pas oublier d'enlever l'api des observer, sinon il y a des risques de récurrence
|
|
||||||
NewsListModel.this.removeObserver("newsApiCall");
|
|
||||||
NewsListModel.this.notifyObservers(new Event("NewsModel","NewsQuerySuccess"));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onError(String errDesc) {
|
|
||||||
//L'appel a échoué :(
|
|
||||||
System.out.println("Error: "+errDesc);
|
|
||||||
NewsListModel.this.removeObserver("newsApiCall");
|
|
||||||
NewsListModel.this.notifyObservers(new Event("NewsModel","NewsQueryFailed"));
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
//on delay uniquement si on est en train de récupérer les sources
|
//on delay uniquement si on est en train de récupérer les sources
|
||||||
if(this.isRetreivingSources) {
|
if(this.isRetreivingSources) {
|
||||||
|
@ -233,6 +189,14 @@ public class NewsListModel implements Observable{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setQuery(String query){
|
||||||
|
this.query = query;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNews(ArrayList<NewsModel> news){
|
||||||
|
this.news = news;
|
||||||
|
}
|
||||||
|
|
||||||
public ArrayList<NewsModel> getNews(){
|
public ArrayList<NewsModel> getNews(){
|
||||||
return this.news;
|
return this.news;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue