mirror of
https://github.com/TwidereProject/Twidere-Android
synced 2025-01-31 00:45:00 +01:00
added cursor support
This commit is contained in:
parent
1f262d5075
commit
2fe5ca0c85
@ -1,100 +0,0 @@
|
|||||||
/*
|
|
||||||
* Twidere - Twitter client for Android
|
|
||||||
*
|
|
||||||
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.mariotaku.twidere.api.twitter.model.impl;
|
|
||||||
|
|
||||||
import com.bluelinelabs.logansquare.JsonMapper;
|
|
||||||
import com.bluelinelabs.logansquare.LoganSquare;
|
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
|
||||||
import com.fasterxml.jackson.core.JsonParser;
|
|
||||||
import com.fasterxml.jackson.core.JsonToken;
|
|
||||||
|
|
||||||
import org.mariotaku.twidere.api.twitter.util.TwitterConverter;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.mariotaku.twidere.api.twitter.model.Status;
|
|
||||||
import org.mariotaku.twidere.api.twitter.model.User;
|
|
||||||
|
|
||||||
public final class PageableResponseListMapper<T> extends JsonMapper<PageableResponseListImpl<T>> {
|
|
||||||
|
|
||||||
private static final Map<Class<?>, PageableResponseListMapper<?>> instanceCache = new HashMap<>();
|
|
||||||
|
|
||||||
private final String listField;
|
|
||||||
private final Class<T> elementType;
|
|
||||||
|
|
||||||
public PageableResponseListMapper(String listField, Class<T> elementType) {
|
|
||||||
this.listField = listField;
|
|
||||||
this.elementType = elementType;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PageableResponseListImpl<T> parse(JsonParser jsonParser) throws IOException {
|
|
||||||
PageableResponseListImpl<T> instance = new PageableResponseListImpl<>();
|
|
||||||
if (jsonParser.getCurrentToken() == null) {
|
|
||||||
jsonParser.nextToken();
|
|
||||||
}
|
|
||||||
if (jsonParser.getCurrentToken() != JsonToken.START_OBJECT) {
|
|
||||||
jsonParser.skipChildren();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
|
|
||||||
String fieldName = jsonParser.getCurrentName();
|
|
||||||
jsonParser.nextToken();
|
|
||||||
parseField(instance, fieldName, jsonParser);
|
|
||||||
jsonParser.skipChildren();
|
|
||||||
}
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void parseField(PageableResponseListImpl<T> instance, String fieldName, JsonParser jsonParser) throws IOException {
|
|
||||||
if (listField.equals(fieldName)) {
|
|
||||||
instance.addAll(LoganSquare.mapperFor(elementType).parseList(jsonParser));
|
|
||||||
} else if ("previous_cursor".equals(fieldName)) {
|
|
||||||
instance.previousCursor = jsonParser.getValueAsLong(0);
|
|
||||||
} else if ("next_cursor".equals(fieldName)) {
|
|
||||||
instance.previousCursor = jsonParser.getValueAsLong(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void serialize(PageableResponseListImpl object, JsonGenerator jsonGenerator, boolean writeStartAndEnd) throws IOException {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized static <C> PageableResponseListMapper<C> get(Class<C> cls) {
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
final PageableResponseListMapper<C> cache = (PageableResponseListMapper<C>) instanceCache.get(cls);
|
|
||||||
if (cache != null) return cache;
|
|
||||||
final String listField;
|
|
||||||
if (User.class.isAssignableFrom(cls)) {
|
|
||||||
listField = "users";
|
|
||||||
} else if (Status.class.isAssignableFrom(cls)) {
|
|
||||||
listField = "statuses";
|
|
||||||
} else {
|
|
||||||
throw new TwitterConverter.UnsupportedTypeException(cls);
|
|
||||||
}
|
|
||||||
final PageableResponseListMapper<C> created = new PageableResponseListMapper<>(listField, cls);
|
|
||||||
instanceCache.put(cls, created);
|
|
||||||
return created;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -22,15 +22,14 @@ package org.mariotaku.twidere.api.twitter.model.impl;
|
|||||||
import com.bluelinelabs.logansquare.annotation.JsonField;
|
import com.bluelinelabs.logansquare.annotation.JsonField;
|
||||||
import com.bluelinelabs.logansquare.annotation.JsonObject;
|
import com.bluelinelabs.logansquare.annotation.JsonObject;
|
||||||
|
|
||||||
import org.mariotaku.twidere.api.twitter.util.TwitterConverter;
|
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
import org.mariotaku.twidere.api.twitter.model.PageableResponseList;
|
import org.mariotaku.twidere.api.twitter.model.PageableResponseList;
|
||||||
import org.mariotaku.twidere.api.twitter.model.Status;
|
import org.mariotaku.twidere.api.twitter.model.Status;
|
||||||
import org.mariotaku.twidere.api.twitter.model.User;
|
import org.mariotaku.twidere.api.twitter.model.User;
|
||||||
import org.mariotaku.twidere.api.twitter.model.UserList;
|
import org.mariotaku.twidere.api.twitter.model.UserList;
|
||||||
|
import org.mariotaku.twidere.api.twitter.util.TwitterConverter;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by mariotaku on 15/5/7.
|
* Created by mariotaku on 15/5/7.
|
||||||
@ -56,13 +55,18 @@ public class PageableResponseListWrapper extends TwitterResponseImpl implements
|
|||||||
public PageableResponseList<?> getWrapped(Object extra) {
|
public PageableResponseList<?> getWrapped(Object extra) {
|
||||||
final Type[] typeArgs = (Type[]) extra;
|
final Type[] typeArgs = (Type[]) extra;
|
||||||
final Class<?> elementType = (Class<?>) typeArgs[0];
|
final Class<?> elementType = (Class<?>) typeArgs[0];
|
||||||
|
PageableResponseListImpl<?> list;
|
||||||
if (User.class.isAssignableFrom(elementType)) {
|
if (User.class.isAssignableFrom(elementType)) {
|
||||||
return new PageableResponseListImpl<>(users);
|
list = new PageableResponseListImpl<>(users);
|
||||||
} else if (Status.class.isAssignableFrom(elementType)) {
|
} else if (Status.class.isAssignableFrom(elementType)) {
|
||||||
return new PageableResponseListImpl<>(statuses);
|
list = new PageableResponseListImpl<>(statuses);
|
||||||
} else if (UserList.class.isAssignableFrom(elementType)) {
|
} else if (UserList.class.isAssignableFrom(elementType)) {
|
||||||
return new PageableResponseListImpl<>(userLists);
|
list = new PageableResponseListImpl<>(userLists);
|
||||||
|
} else {
|
||||||
|
throw new TwitterConverter.UnsupportedTypeException(elementType);
|
||||||
}
|
}
|
||||||
throw new TwitterConverter.UnsupportedTypeException(elementType);
|
list.previousCursor = previousCursor;
|
||||||
|
list.nextCursor = nextCursor;
|
||||||
|
return list;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user