comment #330 - Friends Stories + Story Item Object

This commit is contained in:
tom79 2019-11-02 10:24:11 +01:00
parent f35c2bb71c
commit 941ed792ed
2 changed files with 102 additions and 8 deletions

View File

@ -32,6 +32,7 @@ import app.fedilab.android.client.Entities.Notification;
import app.fedilab.android.client.Entities.Peertube;
import app.fedilab.android.client.Entities.PeertubeNotification;
import app.fedilab.android.client.Entities.PixelFedStory;
import app.fedilab.android.client.Entities.PixelFedStoryItem;
import app.fedilab.android.client.Entities.Playlist;
import app.fedilab.android.client.Entities.Relationship;
import app.fedilab.android.client.Entities.Report;
@ -71,7 +72,8 @@ public class APIResponse {
private List<AccountAdmin> accountAdmins = null;
private List<Report> reports = null;
private Context context = null;
private PixelFedStory pixelFedStory = null;
private List<PixelFedStory> pixelFedStories = null;
private List<PixelFedStoryItem> pixelFedStoryItems = null;
public List<Account> getAccounts() {
return accounts;
@ -281,11 +283,20 @@ public class APIResponse {
this.context = context;
}
public PixelFedStory getPixelFedStory() {
return pixelFedStory;
public List<PixelFedStory> getPixelFedStories() {
return pixelFedStories;
}
public void setPixelFedStory(PixelFedStory pixelFedStory) {
this.pixelFedStory = pixelFedStory;
public void setPixelFedStories(List<PixelFedStory> pixelFedStories) {
this.pixelFedStories = pixelFedStories;
}
public List<PixelFedStoryItem> getPixelFedStoryItems() {
return pixelFedStoryItems;
}
public void setPixelFedStoryItems(List<PixelFedStoryItem> pixelFedStoryItems) {
this.pixelFedStoryItems = pixelFedStoryItems;
}
}

View File

@ -33,7 +33,8 @@ public class PixelfedAPI {
private Context context;
private PixelFedStory pixelFedStory;
private List<PixelFedStory> pixelFedStories;
private List<PixelFedStoryItem> pixelFedStoryItems;
private int tootPerPage;
private String instance;
private String prefKeyOauthTokenT;
@ -78,13 +79,15 @@ public class PixelfedAPI {
* @return APIResponse
*/
public APIResponse getMyStories() {
pixelFedStories = new ArrayList<>();
PixelFedStory pixelFedStory;
try {
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
String response = httpsConnection.get(getAbsoluteUrl("/me"), 10, null, prefKeyOauthTokenT);
apiResponse.setSince_id(httpsConnection.getSince_id());
apiResponse.setMax_id(httpsConnection.getMax_id());
pixelFedStory = parseStory(new JSONObject(response));
pixelFedStories.add(pixelFedStory);
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) {
@ -92,7 +95,60 @@ public class PixelfedAPI {
}
if (apiResponse == null)
apiResponse = new APIResponse();
apiResponse.setPixelFedStory(pixelFedStory);
apiResponse.setPixelFedStories(pixelFedStories);
return apiResponse;
}
/**
* Retrieves Pixelfed Own Stories *synchronously*
*
* @return APIResponse
*/
public APIResponse getFriendStories(String max_id) {
pixelFedStories = new ArrayList<>();
HashMap<String, String> params = new HashMap<>();
if (max_id != null)
params.put("max_id", max_id);
try {
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
String response = httpsConnection.get(getAbsoluteUrl("/recent"), 10, params, prefKeyOauthTokenT);
apiResponse.setSince_id(httpsConnection.getSince_id());
apiResponse.setMax_id(httpsConnection.getMax_id());
pixelFedStories = parseStories(new JSONArray(response));
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) {
e.printStackTrace();
}
if (apiResponse == null)
apiResponse = new APIResponse();
apiResponse.setPixelFedStories(pixelFedStories);
return apiResponse;
}
/**
* Retrieves an item from its ID
*
* @return APIResponse
*/
public APIResponse getStoryItem(String id) {
pixelFedStoryItems = new ArrayList<>();
try {
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
String response = httpsConnection.get(getAbsoluteUrl(String.format("/item/%s", id)), 10, null, prefKeyOauthTokenT);
apiResponse.setSince_id(httpsConnection.getSince_id());
apiResponse.setMax_id(httpsConnection.getMax_id());
PixelFedStoryItem pixelFedStoryItem = parseStoryItem(new JSONObject(response));
pixelFedStoryItems.add(pixelFedStoryItem);
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) {
e.printStackTrace();
}
if (apiResponse == null)
apiResponse = new APIResponse();
apiResponse.setPixelFedStoryItems(pixelFedStoryItems);
return apiResponse;
}
@ -119,6 +175,33 @@ public class PixelfedAPI {
}
/**
* Parse json response for several stories
*
* @param jsonArray JSONArray
* @return List<PixelFedStory>
*/
private static List<PixelFedStory> parseStories(JSONArray jsonArray) {
List<PixelFedStory> pixelFedStories = new ArrayList<>();
try {
int i = 0;
while (i < jsonArray.length()) {
JSONObject resobj = jsonArray.getJSONObject(i);
PixelFedStory pixelFedStory = parseStory(resobj);
i++;
pixelFedStories.add(pixelFedStory);
}
} catch (JSONException e) {
e.printStackTrace();
}
return pixelFedStories;
}
/**
* Parse a single item for stories
* @param jsonObject JSONObject