Search in sources :

Example 1 with FavData

use of forpdateam.ru.forpda.api.favorites.models.FavData in project ForPDA by RadiationX.

the class FavoritesPresenter method getFavorites.

@Override
public void getFavorites(int st, boolean all, Sorting sorting) {
    view.setRefreshing(true);
    subscribe(RxApi.Favorites().getFavorites(st, all, sorting), favData -> {
        view.setRefreshing(false);
        view.onLoadFavorites(favData);
    }, new FavData(), v -> getFavorites(st, all, sorting));
}
Also used : FavData(forpdateam.ru.forpda.api.favorites.models.FavData)

Example 2 with FavData

use of forpdateam.ru.forpda.api.favorites.models.FavData in project ForPDA by RadiationX.

the class Favorites method getFavorites.

public FavData getFavorites(int st, boolean all, Sorting sorting) throws Exception {
    FavData data = new FavData();
    Uri.Builder builder = new Uri.Builder();
    builder.scheme("https").authority("4pda.ru");
    builder.appendPath("forum");
    builder.appendQueryParameter("act", "fav");
    builder.appendQueryParameter("type", "all");
    builder.appendQueryParameter("st", Integer.toString(st));
    builder.appendQueryParameter(Sorting.Key.HEADER, sorting.getKey());
    builder.appendQueryParameter(Sorting.Order.HEADER, sorting.getOrder());
    NetworkResponse response = Api.getWebClient().get(builder.build().toString());
    Matcher matcher = mainPattern.matcher(response.getBody());
    FavItem item;
    while (matcher.find()) {
        item = new FavItem();
        boolean isForum = matcher.group(19) != null;
        item.setFavId(Integer.parseInt(matcher.group(1)));
        item.setTrackType(matcher.group(2));
        item.setPin(matcher.group(3).equals("1"));
        if (matcher.group(4) != null) {
            item.setInfoColor(matcher.group(4));
        }
        String tmp;
        if (matcher.group(5) != null) {
            tmp = matcher.group(5);
            item.setNew(tmp.contains("+"));
            item.setPoll(tmp.contains("^"));
            item.setClosed(tmp.contains("Х"));
        }
        int iId = Integer.parseInt(matcher.group(6));
        if (isForum) {
            item.setForumId(iId);
        } else {
            item.setTopicId(iId);
        }
        item.setNew(matcher.group(7) != null);
        item.setTopicTitle(ApiUtils.fromHtml(matcher.group(8)));
        if (isForum) {
            item.setDate(matcher.group(19));
            item.setLastUserId(Integer.parseInt(matcher.group(20)));
            item.setLastUserNick(ApiUtils.fromHtml(matcher.group(21)));
            item.setForum(true);
        } else {
            if (matcher.group(9) != null) {
                item.setStParam(Integer.parseInt(matcher.group(9)));
                item.setPages((item.getStParam() / 20) + 1);
            }
            if (matcher.group(10) != null)
                item.setDesc(ApiUtils.fromHtml(matcher.group(10)));
            item.setForumId(Integer.parseInt(matcher.group(12)));
            item.setForumTitle(ApiUtils.fromHtml(matcher.group(13)));
            item.setAuthorId(Integer.parseInt(matcher.group(14)));
            item.setAuthorUserNick(ApiUtils.fromHtml(matcher.group(15)));
            item.setLastUserId(Integer.parseInt(matcher.group(16)));
            item.setLastUserNick(ApiUtils.fromHtml(matcher.group(17)));
            item.setDate(matcher.group(18));
            tmp = matcher.group(22);
            if (tmp != null) {
                item.setCuratorId(Integer.parseInt(matcher.group(22)));
                item.setCuratorNick(ApiUtils.fromHtml(matcher.group(23)));
            }
            item.setSubType(matcher.group(24).trim().toLowerCase());
        }
        data.addItem(item);
    }
    data.setPagination(Pagination.parseForum(response.getBody()));
    data.setSorting(Sorting.parse(response.getBody()));
    if (all) {
        while (true) {
            if (data.getPagination().getCurrent() >= data.getPagination().getAll()) {
                break;
            }
            FavData favData = getFavorites(data.getPagination().getPage(data.getPagination().getCurrent()), false, sorting);
            data.setPagination(favData.getPagination());
            if (favData.getItems().isEmpty()) {
                break;
            }
            for (FavItem favItem : favData.getItems()) {
                data.addItem(favItem);
            }
        }
        data.getPagination().setAll(1);
        if (data.getSorting().getKey().equals(Sorting.Key.TITLE)) {
            if (data.getSorting().getOrder().equals(Sorting.Order.DESC)) {
                Collections.sort(data.getItems(), DESC_ORDER);
            } else if (data.getSorting().getOrder().equals(Sorting.Order.ASC)) {
                Collections.sort(data.getItems(), ASC_ORDER);
            }
        }
    }
    return data;
}
Also used : FavItem(forpdateam.ru.forpda.api.favorites.models.FavItem) Matcher(java.util.regex.Matcher) NetworkResponse(forpdateam.ru.forpda.api.NetworkResponse) FavData(forpdateam.ru.forpda.api.favorites.models.FavData) Uri(android.net.Uri)

Aggregations

FavData (forpdateam.ru.forpda.api.favorites.models.FavData)2 Uri (android.net.Uri)1 NetworkResponse (forpdateam.ru.forpda.api.NetworkResponse)1 FavItem (forpdateam.ru.forpda.api.favorites.models.FavItem)1 Matcher (java.util.regex.Matcher)1