Search in sources :

Example 1 with FavItem

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

the class FavoritesPresenter method handleEvent.

@Override
public void handleEvent(TabNotification event, Sorting sorting, int count) {
    if (event.isWebSocket() && event.getEvent().isNew())
        return;
    RealmResults<FavItemBd> results = realm.where(FavItemBd.class).findAll();
    ArrayList<IFavItem> currentItems = new ArrayList<>();
    for (FavItemBd itemBd : results) {
        currentItems.add(new FavItem(itemBd));
    }
    NotificationEvent loadedEvent = event.getEvent();
    int id = loadedEvent.getSourceId();
    boolean isRead = loadedEvent.isRead();
    if (isRead) {
        count--;
        for (IFavItem item : currentItems) {
            if (item.getTopicId() == id) {
                item.setNew(false);
                break;
            }
        }
    } else {
        count = event.getLoadedEvents().size();
        for (IFavItem item : currentItems) {
            if (item.getTopicId() == id) {
                if (item.getLastUserId() != ClientHelper.getUserId())
                    item.setNew(true);
                item.setLastUserNick(loadedEvent.getUserNick());
                item.setLastUserId(loadedEvent.getUserId());
                item.setPin(loadedEvent.isImportant());
                break;
            }
        }
        if (sorting.getKey().equals(Sorting.Key.TITLE)) {
            Collections.sort(currentItems, (o1, o2) -> {
                if (sorting.getOrder().equals(Sorting.Order.ASC))
                    return o1.getTopicTitle().compareToIgnoreCase(o2.getTopicTitle());
                return o2.getTopicTitle().compareToIgnoreCase(o1.getTopicTitle());
            });
        }
        if (sorting.getKey().equals(Sorting.Key.LAST_POST)) {
            for (IFavItem item : currentItems) {
                if (item.getTopicId() == id) {
                    currentItems.remove(item);
                    int index = 0;
                    if (sorting.getOrder().equals(Sorting.Order.ASC)) {
                        index = currentItems.size();
                    }
                    currentItems.add(index, item);
                    break;
                }
            }
        }
    }
    view.onHandleEvent(count);
}
Also used : IFavItem(forpdateam.ru.forpda.api.favorites.interfaces.IFavItem) FavItem(forpdateam.ru.forpda.api.favorites.models.FavItem) IFavItem(forpdateam.ru.forpda.api.favorites.interfaces.IFavItem) ArrayList(java.util.ArrayList) NotificationEvent(forpdateam.ru.forpda.api.events.models.NotificationEvent) FavItemBd(forpdateam.ru.forpda.data.realm.favorites.FavItemBd)

Example 2 with FavItem

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

the class FavoritesPresenter method saveFavorites.

@Override
public void saveFavorites(List<FavItem> items) {
    if (realm.isClosed())
        return;
    realm.executeTransactionAsync(r -> {
        r.delete(FavItemBd.class);
        List<FavItemBd> bdList = new ArrayList<>();
        for (FavItem item : items) {
            bdList.add(new FavItemBd(item));
        }
        r.copyToRealmOrUpdate(bdList);
        bdList.clear();
    }, this::showFavorites);
}
Also used : IFavItem(forpdateam.ru.forpda.api.favorites.interfaces.IFavItem) FavItem(forpdateam.ru.forpda.api.favorites.models.FavItem) ArrayList(java.util.ArrayList) FavItemBd(forpdateam.ru.forpda.data.realm.favorites.FavItemBd)

Example 3 with FavItem

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

the class FavoritesAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(BaseSectionedViewHolder holder, int section, int relativePosition, int absolutePosition) {
    FavItem item = getItem(section, relativePosition);
    ((ItemHolder) holder).bind(item, section, relativePosition, absolutePosition);
}
Also used : IFavItem(forpdateam.ru.forpda.api.favorites.interfaces.IFavItem) FavItem(forpdateam.ru.forpda.api.favorites.models.FavItem)

Example 4 with FavItem

use of forpdateam.ru.forpda.api.favorites.models.FavItem 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)

Example 5 with FavItem

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

the class FavoritesFragment method onShowFavorite.

@Override
public void onShowFavorite(List<FavItem> items) {
    if (items.isEmpty()) {
        if (!contentController.contains(ContentController.TAG_NO_DATA)) {
            FunnyContent funnyContent = new FunnyContent(getContext()).setImage(R.drawable.ic_star).setTitle(R.string.funny_favorites_nodata_title).setDesc(R.string.funny_favorites_nodata_desc);
            contentController.addContent(funnyContent, ContentController.TAG_NO_DATA);
        }
        contentController.showContent(ContentController.TAG_NO_DATA);
    } else {
        contentController.hideContent(ContentController.TAG_NO_DATA);
    }
    ArrayList<FavItem> pinnedUnread = new ArrayList<>();
    ArrayList<FavItem> itemsUnread = new ArrayList<>();
    ArrayList<FavItem> pinned = new ArrayList<>();
    ArrayList<FavItem> otherItems = new ArrayList<>();
    for (FavItem item : items) {
        if (item.isPin()) {
            if (unreadTop && item.isNew()) {
                pinnedUnread.add(item);
            } else {
                pinned.add(item);
            }
        } else {
            if (unreadTop && item.isNew()) {
                itemsUnread.add(item);
            } else {
                otherItems.add(item);
            }
        }
    }
    adapter.clear();
    if (!pinnedUnread.isEmpty()) {
        adapter.addSection(getString(R.string.fav_unreaded_pinned), pinnedUnread);
    }
    if (!itemsUnread.isEmpty()) {
        adapter.addSection(getString(R.string.fav_unreaded), itemsUnread);
    }
    if (!pinned.isEmpty()) {
        adapter.addSection(getString(R.string.fav_pinned), pinned);
    }
    adapter.addSection(getString(R.string.fav_themes), otherItems);
    adapter.notifyDataSetChanged();
    if (!ClientHelper.getNetworkState(getContext())) {
        ClientHelper.get().notifyCountsChanged();
    }
}
Also used : FunnyContent(forpdateam.ru.forpda.ui.views.FunnyContent) IFavItem(forpdateam.ru.forpda.api.favorites.interfaces.IFavItem) FavItem(forpdateam.ru.forpda.api.favorites.models.FavItem) ArrayList(java.util.ArrayList)

Aggregations

FavItem (forpdateam.ru.forpda.api.favorites.models.FavItem)5 IFavItem (forpdateam.ru.forpda.api.favorites.interfaces.IFavItem)4 ArrayList (java.util.ArrayList)3 FavItemBd (forpdateam.ru.forpda.data.realm.favorites.FavItemBd)2 Uri (android.net.Uri)1 NetworkResponse (forpdateam.ru.forpda.api.NetworkResponse)1 NotificationEvent (forpdateam.ru.forpda.api.events.models.NotificationEvent)1 FavData (forpdateam.ru.forpda.api.favorites.models.FavData)1 FunnyContent (forpdateam.ru.forpda.ui.views.FunnyContent)1 Matcher (java.util.regex.Matcher)1