Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix twireapp#431 empty "My Stream" list #436

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.perflyst.twire.activities.main;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

Expand All @@ -12,6 +13,7 @@
import com.perflyst.twire.model.StreamInfo;
import com.perflyst.twire.service.JSONService;
import com.perflyst.twire.service.Service;
import com.perflyst.twire.service.Settings;
import com.perflyst.twire.service.TempStorage;
import com.perflyst.twire.tasks.GetFollowsFromDB;
import com.perflyst.twire.views.recyclerviews.AutoSpanRecyclerView;
Expand Down Expand Up @@ -56,10 +58,25 @@ public void addToAdapter(List<StreamInfo> aObjectList) {
}

@Override
public List<StreamInfo> getVisualElements() throws JSONException, ExecutionException, InterruptedException, MalformedURLException {
// build the api link
String helix_url = "https://api.twitch.tv/helix/streams?first=100";
public List<StreamInfo> getVisualElements()
throws JSONException, ExecutionException, InterruptedException, MalformedURLException {
List<StreamInfo> resultList = new ArrayList<>();
resultList.addAll(getDbFollows());
resultList.addAll(getTwitchFollows());

int elementsToFetch = mAdapter.getItemCount() + resultList.size();
if (!resultList.isEmpty()) {
elementsToFetch += 1;
}
setMaxElementsToFetch(elementsToFetch);

shouldShowErrorView();

return resultList;
}

private List<StreamInfo> getDbFollows()
throws ExecutionException, InterruptedException, JSONException, MalformedURLException {
ArrayList<ChannelInfo> channels;
if (TempStorage.hasLoadedStreamers()) {
channels = new ArrayList<>(TempStorage.getLoadedStreamers());
Expand All @@ -68,12 +85,13 @@ public List<StreamInfo> getVisualElements() throws JSONException, ExecutionExcep
subscriptionsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, getBaseContext());
channels = new ArrayList<>(subscriptionsTask.get().values());
}

// loop over all the channels in the DB in chunks of 100
List<StreamInfo> mResultList = new ArrayList<>();
List<StreamInfo> resultList = new ArrayList<>();
final String ARRAY_KEY = "data";
for (List<ChannelInfo> chunk : Lists.partition(channels, 100)) {
String url = helix_url + Joiner.on("").join(Lists.transform(chunk, channelInfo -> "&user_id=" + channelInfo.getUserId()));
final String helix_url = "https://api.twitch.tv/helix/streams";
String url = helix_url + Joiner.on("")
.join(Lists.transform(chunk, channelInfo -> "?user_id=" + channelInfo.getUserId()));
// request the url
String temp_jsonString = Service.urlToJSONStringHelix(url, this);
JSONObject fullDataObject = new JSONObject(temp_jsonString);
Expand All @@ -82,18 +100,26 @@ public List<StreamInfo> getVisualElements() throws JSONException, ExecutionExcep
// append the new array to the final one
for (int i = 0; i < temp_array.length(); i++) {
JSONObject streamObject = temp_array.getJSONObject(i);
mResultList.add(JSONService.getStreamInfo(getBaseContext(), streamObject, false));
resultList.add(JSONService.getStreamInfo(getBaseContext(), streamObject, false));
}
}
return resultList;
}

int elementsToFetch = mAdapter.getItemCount() + mResultList.size();
if (!mResultList.isEmpty()) {
elementsToFetch += 1;
private List<StreamInfo> getTwitchFollows() throws JSONException, MalformedURLException {
Settings mSettings = new Settings(getBaseContext());
final String helix_url = "https://api.twitch.tv/helix/streams/followed?user_id=" + mSettings.getGeneralTwitchUserID();
String temp_jsonString = Service.urlToJSONStringHelix(helix_url, this);
JSONObject fullDataObject = new JSONObject(temp_jsonString);
final String ARRAY_KEY = "data";
// create the array
JSONArray temp_array = fullDataObject.getJSONArray(ARRAY_KEY);
List<StreamInfo> resultList = new ArrayList<>();
// append the new array to the final one
for (int i = 0; i < temp_array.length(); i++) {
JSONObject streamObject = temp_array.getJSONObject(i);
resultList.add(JSONService.getStreamInfo(getBaseContext(), streamObject, false));
}
setMaxElementsToFetch(elementsToFetch);

shouldShowErrorView();

return mResultList;
return resultList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class LoginActivity extends SetupBaseActivity {
"?client_id=" + Service.getApplicationClientID() +
"&redirect_uri=http%3A%2F%2Flocalhost/oauth_authorizing" +
"&response_type=token" +
"&scope=user:read:email%20user:edit:follows%20user:read:subscriptions%20chat:edit%20chat:read";
"&scope=user:read:email%20user:edit:follows%20user:read:subscriptions%20chat:edit%20chat:read%20user:read:follows";
private final int SHOW_WEBVIEW_ANIMATION_DURATION = 900;
private final int SHOW_SUCCESS_ICON_DURATION = 800;
private final int REVEAL_ANIMATION_DURATION = 650;
Expand Down
Loading