Skip to content

Commit

Permalink
#252, add page viewing (at least for 4chan)
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamantcheese committed Mar 1, 2019
1 parent 2dab852 commit 1410bf2
Show file tree
Hide file tree
Showing 15 changed files with 301 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

public class BrowsePresenter implements Observer {
private final DatabaseManager databaseManager;
private final BoardManager boardManager;
private Callback callback;

private boolean hadBoards = false;
Expand All @@ -42,7 +41,6 @@ public class BrowsePresenter implements Observer {
@Inject
public BrowsePresenter(DatabaseManager databaseManager, BoardManager boardManager) {
this.databaseManager = databaseManager;
this.boardManager = boardManager;

savedBoardsObservable = boardManager.getSavedBoardsObservable();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@
import org.floens.chan.core.model.orm.SavedReply;
import org.floens.chan.core.pool.ChanLoaderFactory;
import org.floens.chan.core.settings.ChanSettings;
import org.floens.chan.core.site.Page;
import org.floens.chan.core.site.Pages;
import org.floens.chan.core.site.Site;
import org.floens.chan.core.site.SiteActions;
import org.floens.chan.core.site.ThreadTime;
import org.floens.chan.core.site.http.DeleteRequest;
import org.floens.chan.core.site.http.DeleteResponse;
import org.floens.chan.core.site.http.HttpCall;
Expand Down Expand Up @@ -623,6 +626,22 @@ public ChanThread getChanThread() {
return chanLoader == null ? null : chanLoader.getThread();
}

public Page getPage(Post op) {
Pages pages = chanLoader.getPages();
if(pages == null) {
return null;
} else {
for (Page page : pages.pages) {
for (ThreadTime threadTime : page.threads) {
if (op.no == threadTime.no) {
return page;
}
}
}
}
return null;
}

@Override
public void onListStatusClicked() {
chanLoader.requestMoreDataAndResetTimer();
Expand Down
17 changes: 17 additions & 0 deletions Clover/app/src/main/java/org/floens/chan/core/site/Boards.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Clover - 4chan browser https://github.com/Floens/Clover/
* Copyright (C) 2014 Floens
*
* 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.floens.chan.core.site;

import org.floens.chan.core.model.orm.Board;
Expand Down
30 changes: 30 additions & 0 deletions Clover/app/src/main/java/org/floens/chan/core/site/Page.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Clover - 4chan browser https://github.com/Floens/Clover/
* Copyright (C) 2014 Floens
*
* 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.floens.chan.core.site;

import java.util.List;

public class Page {
public final int page;
public final List<ThreadTime> threads;

public Page(int page, List<ThreadTime> threads) {
this.page = page;
this.threads = threads;
}
}
28 changes: 28 additions & 0 deletions Clover/app/src/main/java/org/floens/chan/core/site/Pages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Clover - 4chan browser https://github.com/Floens/Clover/
* Copyright (C) 2014 Floens
*
* 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.floens.chan.core.site;

import java.util.List;

public class Pages {
public final List<Page> pages;

public Pages(List<Page> pages) {
this.pages = pages;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@

public interface SiteActions {
void boards(BoardsListener boardsListener);
void pages(Board board, PagesListener pagesListener);

interface BoardsListener {
void onBoardsReceived(Boards boards);
}

interface PagesListener {
void onPagesReceived(Pages pages);
}

void post(Reply reply, PostListener postListener);

interface PostListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public interface SiteEndpoints {

HttpUrl boards();

HttpUrl pages(Board board);

HttpUrl archive(Board board);

HttpUrl reply(Loadable thread);
Expand Down
28 changes: 28 additions & 0 deletions Clover/app/src/main/java/org/floens/chan/core/site/ThreadTime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Clover - 4chan browser https://github.com/Floens/Clover/
* Copyright (C) 2014 Floens
*
* 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.floens.chan.core.site;

public class ThreadTime {
public final int no;
public final long modified;

public ThreadTime(int no, long modified) {
this.no = no;
this.modified = modified;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.floens.chan.core.model.orm.Loadable;
import org.floens.chan.core.settings.json.JsonSettings;
import org.floens.chan.core.site.Boards;
import org.floens.chan.core.site.Pages;
import org.floens.chan.core.site.Site;
import org.floens.chan.core.site.SiteActions;
import org.floens.chan.core.site.SiteAuthentication;
Expand Down Expand Up @@ -527,6 +528,10 @@ public void boards(BoardsListener boardsListener) {
}
}

@Override
public void pages(Board board, PagesListener pagesListener) {
}

@Override
public void archive(Board board, ArchiveListener archiveListener) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public HttpUrl icon(Post.Builder post, String icon, Map<String, String> arg) {
return stat.url();
}

@Override
public HttpUrl pages(Board board) {
return root.builder().s(board.code).s("threads.json").url();
}

@Override
public HttpUrl reply(Loadable loadable) {
return sys.builder().s("post.php").url();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import org.floens.chan.core.model.ChanThread;
import org.floens.chan.core.model.Post;
import org.floens.chan.core.model.orm.Loadable;
import org.floens.chan.core.site.Page;
import org.floens.chan.core.site.Pages;
import org.floens.chan.core.site.SiteActions;
import org.floens.chan.core.site.parser.ChanReader;
import org.floens.chan.core.site.parser.ChanReaderRequest;
import org.floens.chan.ui.helper.PostHelper;
Expand All @@ -52,7 +55,7 @@
* {@link ChanLoaderCallback}.
* <p>For threads timers can be started with {@link #setTimer()} to do a request later.
*/
public class ChanThreadLoader implements Response.ErrorListener, Response.Listener<ChanLoaderResponse> {
public class ChanThreadLoader implements Response.ErrorListener, Response.Listener<ChanLoaderResponse>, SiteActions.PagesListener {
private static final String TAG = "ChanThreadLoader";
private static final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();

Expand All @@ -72,6 +75,9 @@ public class ChanThreadLoader implements Response.ErrorListener, Response.Listen
private long lastLoadTime;
private ScheduledFuture<?> pendingFuture;

private boolean requestedPages = false;
private Pages pages;

/**
* <b>Do not call this constructor yourself, obtain ChanLoaders through {@link org.floens.chan.core.pool.ChanLoaderFactory}</b>
*/
Expand Down Expand Up @@ -245,6 +251,11 @@ private ChanLoaderRequest getData() {

volleyRequestQueue.add(request.getVolleyRequest());

if(!requestedPages) {
loadable.site.actions().pages(loadable.board, this);
requestedPages = true;
}

return request;
}

Expand Down Expand Up @@ -340,6 +351,16 @@ private void clearPendingRunnable() {
}
}

@Override
public void onPagesReceived(Pages pages) {
this.pages = pages;
requestedPages = false;
}

public Pages getPages() {
return pages;
}

public interface ChanLoaderCallback {
void onChanLoaderData(ChanThread result);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.floens.chan.core.settings.SharedPreferencesSettingProvider;
import org.floens.chan.core.settings.StringSetting;
import org.floens.chan.core.site.Boards;
import org.floens.chan.core.site.Page;
import org.floens.chan.core.site.Pages;
import org.floens.chan.core.site.Site;
import org.floens.chan.core.site.SiteActions;
import org.floens.chan.core.site.SiteAuthentication;
Expand Down Expand Up @@ -253,6 +255,14 @@ public HttpUrl boards() {
.build();
}

@Override
public HttpUrl pages(Board board) {
return a.newBuilder()
.addPathSegment(board.code)
.addPathSegment("threads.json")
.build();
}

@Override
public HttpUrl archive(Board board) {
return b.newBuilder()
Expand Down Expand Up @@ -345,6 +355,16 @@ public void boards(final BoardsListener listener) {
}));
}

@Override
public void pages(Board board, PagesListener listener) {
requestQueue.add(new Chan4PagesRequest(Chan4.this, board, response -> {
listener.onPagesReceived(new Pages(response));
}, (error) -> {
Logger.e(TAG, "Failed to get threads for board " + board.code);
listener.onPagesReceived(new Pages(new ArrayList<Page>()));
}));
}

@Override
public void archive(Board board, ArchiveListener archiveListener) {
requestQueue.add(new Chan4ArchiveRequest(Chan4.this, board,
Expand Down
Loading

0 comments on commit 1410bf2

Please sign in to comment.