Data is not added to List. The program gets data from a GraphQL server and filter it through a switch statements, then appends it to the corresponding list. However, the list is empty if accessed outside the switch statement. If you print one of the list in the switch statement, it will print correctly, however if you print it in the getter functions, it will not return anything.
What is wrong with it? The scope? I tried putting the initialization in a few places like on the same function or on the constructor however the result is the same.
package sukebei.anilog.data.source.AniList;
import android.util.Log;
import com.apollographql.apollo.ApolloCall;
import com.apollographql.apollo.ApolloClient;
import com.apollographql.apollo.api.Response;
import com.apollographql.apollo.exception.ApolloException;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
import okhttp3.OkHttpClient;
import AniList.UserMediaQuery;
import AniList.UserMediaQuery.Data;
import AniList.type.MediaType;
/* Data Received Example
[
Entry {
__typename = MediaList,
progress = 0,
progressVolumes = null,
score = 0.0,
status = PLANNING,
notes = null,
repeat = 0,
media = Media {
__typename = Media,
chapters = null,
volumes = null,
idMal = 17082,
episodes = 12,
title = Title {
__typename = MediaTitle, romaji = Aiura
}
}
},
Entry {
__typename = MediaList,
progress = 0,
progressVolumes = null,
score = 0.0,
status = PLANNING,
notes = null,
repeat = 0,
media = Media {
__typename = Media,
chapters = null,
volumes = null,
idMal = 33218,
episodes = 1,
title = Title {
__typename = MediaTitle,
romaji = Kimi no Koe wo Todoketai
}
}
}
]
*/
public class AniList {
private static final String BASE_URL = "https://graphql.anilist.co";
private List<UserMediaQuery.Entry> planningMedia = new ArrayList<UserMediaQuery.Entry>();
private List<UserMediaQuery.Entry> currentMedia = new ArrayList<UserMediaQuery.Entry>();
private List<UserMediaQuery.Entry> completedMedia = new ArrayList<UserMediaQuery.Entry>();
private List<UserMediaQuery.Entry> droppedMedia = new ArrayList<UserMediaQuery.Entry>();
private List<UserMediaQuery.Entry> pausedMedia = new ArrayList<UserMediaQuery.Entry>();
private List<UserMediaQuery.Entry> repeatingMedia = new ArrayList<UserMediaQuery.Entry>();
private OkHttpClient okHttpClient;
private ApolloClient apolloClient;
public AniList() {
okHttpClient = new OkHttpClient.Builder().build();
apolloClient = ApolloClient.builder()
.serverUrl(BASE_URL)
.okHttpClient(okHttpClient)
.build();
}
public void loadUserData(String username, MediaType mediaType) {
UserMediaQuery mediaQuery = UserMediaQuery.builder()
.username(username)
.type(mediaType)
.build();
apolloClient.query(mediaQuery).enqueue(new ApolloCall.Callback<Data>() {
@Override public void onResponse(@NotNull Response<Data> dataResponse) {
for (UserMediaQuery.List data : dataResponse.data().userMediaList().lists()) {
for (UserMediaQuery.Entry entry: data.entries()) {
switch(entry.status()) {
case PLANNING:
planningMedia.add(entry);
break;
case CURRENT:
currentMedia.add(entry);
break;
case COMPLETED:
completedMedia.add(entry);
break;
case DROPPED:
droppedMedia.add(entry);
break;
case PAUSED:
pausedMedia.add(entry);
break;
case REPEATING:
repeatingMedia.add(entry);
break;
}
}
}
}
@Override
public void onFailure(@NotNull ApolloException t) {
Log.e("AniList Source", t.getMessage(), t);
}
}
);
}
public List getPlanningMedia() {
return planningMedia;
}
public List getCurrentMedia() {
return currentMedia;
}
public List getCompletedMedia() {
return completedMedia;
}
public List getDroppedMedia() {
return droppedMedia;
}
public List getPausedMedia() {
return pausedMedia;
}
public List getRepeatingMedia() {
return repeatingMedia;
}
}
This prints the data however when you print it in the getter function it does not print the data.
for (UserMediaQuery.List data : dataResponse.data().userMediaList().lists()) {
for (UserMediaQuery.Entry entry: data.entries()) {
if (entry.status() == MediaListStatus.PLANNING) {
planningMedia.add(entry);
Log.d("planning", planningMedia.toString());
}
}
}