Skip to content

Commit

Permalink
NClientV2 2.7.7
Browse files Browse the repository at this point in the history
+ Added division by page for favorite galleries
  • Loading branch information
Dar9586 committed Feb 10, 2021
1 parent c9d30aa commit 575248e
Show file tree
Hide file tree
Showing 19 changed files with 326 additions and 137 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.dar.nclientv2"
minSdkVersion 14
targetSdkVersion 30
versionCode 276
versionName "2.7.6-stable"
versionCode 277
versionName "2.7.7-beta"
vectorDrawables.useSupportLibrary true
proguardFiles 'proguard-rules.pro'
}
Expand Down
42 changes: 39 additions & 3 deletions app/src/main/java/com/dar/nclientv2/FavoriteActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,32 @@
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;

import com.dar.nclientv2.adapters.FavoriteAdapter;
import com.dar.nclientv2.api.components.Gallery;
import com.dar.nclientv2.async.database.Queries;
import com.dar.nclientv2.async.downloader.DownloadGalleryV2;
import com.dar.nclientv2.components.activities.BaseActivity;
import com.dar.nclientv2.components.views.PageSwitcher;
import com.dar.nclientv2.settings.Global;
import com.dar.nclientv2.utility.Utility;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;


public class FavoriteActivity extends BaseActivity {
private static final int ENTRY_PER_PAGE = 24;
private FavoriteAdapter adapter = null;
private boolean sortByTitle = false;
private PageSwitcher pageSwitcher;
private SearchView searchView;

public static int getEntryPerPage() {
return Global.isInfiniteScrollFavorite() ? Integer.MAX_VALUE : ENTRY_PER_PAGE;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -33,18 +44,32 @@ protected void onCreate(Bundle savedInstanceState) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle(R.string.favorite_manga);
pageSwitcher = findViewById(R.id.page_switcher);
recycler = findViewById(R.id.recycler);
refresher = findViewById(R.id.refresher);
refresher.setRefreshing(true);
adapter = new FavoriteAdapter(this);

findViewById(R.id.page_switcher).setVisibility(View.GONE);

refresher.setOnRefreshListener(adapter::forceReload);
changeLayout(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE);
recycler.setAdapter(adapter);
pageSwitcher.setPages(1, 1);
pageSwitcher.setChanger(new PageSwitcher.DefaultPageChanger() {
@Override
public void pageChanged(PageSwitcher switcher, int page) {
if (adapter != null) adapter.changePage();
}
});

}

public int getActualPage() {
return pageSwitcher.getActualPage();
}

public void changePages(int totalPages, int actualPages) {
pageSwitcher.setPages(totalPages, actualPages);
}

@Override
Expand All @@ -57,10 +82,20 @@ protected int getPortraitColumnCount() {
return Global.getColPortFavorite();
}

private int calculatePages(@Nullable String text) {
int perPage = getEntryPerPage();
int totalEntries = Queries.FavoriteTable.countFavorite(text);
int div = totalEntries / perPage;
int mod = totalEntries % perPage;
return div + (mod == 0 ? 0 : 1);
}

@Override
protected void onResume() {
refresher.setEnabled(true);
refresher.setRefreshing(true);
String query = searchView == null ? null : searchView.getQuery().toString();
pageSwitcher.setTotalPage(calculatePages(query));
adapter.forceReload();
super.onResume();
}
Expand All @@ -74,7 +109,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
menu.findItem(R.id.only_language).setVisible(false);
menu.findItem(R.id.add_bookmark).setVisible(false);

final androidx.appcompat.widget.SearchView searchView = (androidx.appcompat.widget.SearchView) menu.findItem(R.id.search).getActionView();
searchView = (androidx.appcompat.widget.SearchView) menu.findItem(R.id.search).getActionView();
searchView.setOnQueryTextListener(new androidx.appcompat.widget.SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Expand All @@ -83,6 +118,7 @@ public boolean onQueryTextSubmit(String query) {

@Override
public boolean onQueryTextChange(String newText) {
pageSwitcher.setTotalPage(calculatePages(newText));
if (adapter != null)
adapter.getFilter().filter(newText);
return true;
Expand Down
68 changes: 13 additions & 55 deletions app/src/main/java/com/dar/nclientv2/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -49,8 +47,8 @@
import com.dar.nclientv2.components.GlideX;
import com.dar.nclientv2.components.activities.BaseActivity;
import com.dar.nclientv2.components.views.CustomWebView;
import com.dar.nclientv2.components.views.PageSwitcher;
import com.dar.nclientv2.components.widgets.CustomGridLayoutManager;
import com.dar.nclientv2.settings.DefaultDialogs;
import com.dar.nclientv2.settings.Global;
import com.dar.nclientv2.settings.Login;
import com.dar.nclientv2.settings.TagV2;
Expand Down Expand Up @@ -97,14 +95,11 @@ public void onSuccess(List<GenericGallery> galleries) {
private InspectorV3 inspector = null;
private NavigationView navigationView;
private ModeType modeType = ModeType.UNKNOWN;
private int actualPage = 1, totalPage;
private int idOpenedGallery = -1;//Position in the recycler of the opened gallery
private boolean inspecting = false, filteringTag = false;
private SortType temporaryType;
private Snackbar snackbar = null;
private ImageButton prevPageButton, nextPageButton;
private EditText pageIndexText;
private View pageSwitcher;
private PageSwitcher pageSwitcher;
private final InspectorV3.InspectorResponse
resetDataset = new MainInspectorResponse() {
@Override
Expand Down Expand Up @@ -141,7 +136,7 @@ protected void onCreate(Bundle savedInstanceState) {
loadStringLogin();
refresher.setOnRefreshListener(() -> {
inspector = inspector.cloneInspector(MainActivity.this, resetDataset);
if (Global.isInfiniteScroll()) inspector.setPage(1);
if (Global.isInfiniteScrollMain()) inspector.setPage(1);
inspector.start();
});

Expand Down Expand Up @@ -194,21 +189,14 @@ private void initializeToolbar() {
}

private void initializePageSwitcherActions() {
prevPageButton.setOnClickListener(v -> {
if (actualPage > 1) {
inspector = inspector.cloneInspector(MainActivity.this, resetDataset);
inspector.setPage(inspector.getPage() - 1);
inspector.start();
}
});
nextPageButton.setOnClickListener(v -> {
if (actualPage < totalPage) {
pageSwitcher.setChanger(new PageSwitcher.DefaultPageChanger() {
@Override
public void pageChanged(PageSwitcher switcher, int page) {
inspector = inspector.cloneInspector(MainActivity.this, resetDataset);
inspector.setPage(inspector.getPage() + 1);
inspector.setPage(pageSwitcher.getActualPage());
inspector.start();
}
});
pageIndexText.setOnClickListener(v -> loadDialog());
}

private void initializeRecyclerView() {
Expand All @@ -220,11 +208,12 @@ private void initializeRecyclerView() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
if (inspecting) return;
if (!Global.isInfiniteScroll()) return;
if (!Global.isInfiniteScrollMain()) return;
if (refresher.isRefreshing()) return;

CustomGridLayoutManager manager = (CustomGridLayoutManager) recycler.getLayoutManager();
if (actualPage < totalPage && lastGalleryReached(manager)) {
assert manager != null;
if (!pageSwitcher.lastPageReached() && lastGalleryReached(manager)) {
inspecting = true;
inspector = inspector.cloneInspector(MainActivity.this, addDataset);
inspector.setPage(inspector.getPage() + 1);
Expand Down Expand Up @@ -260,9 +249,6 @@ private void findUsefulViews() {
navigationView = findViewById(R.id.nav_view);
recycler = findViewById(R.id.recycler);
refresher = findViewById(R.id.refresher);
prevPageButton = findViewById(R.id.prev);
nextPageButton = findViewById(R.id.next);
pageIndexText = findViewById(R.id.page_index);
pageSwitcher = findViewById(R.id.page_switcher);
drawerLayout = findViewById(R.id.drawer_layout);
loginItem = navigationView.getMenu().findItem(R.id.action_login);
Expand Down Expand Up @@ -478,43 +464,15 @@ public void hidePageSwitcher() {
}

public void showPageSwitcher(final int actualPage, final int totalPage) {
this.actualPage = actualPage;
this.totalPage = totalPage;
pageSwitcher.setPages(totalPage, actualPage);

if (Global.isInfiniteScroll()) {

if (Global.isInfiniteScrollMain()) {
hidePageSwitcher();
return;
}

runOnUiThread(() -> {
pageSwitcher.setVisibility(totalPage <= 1 ? View.GONE : View.VISIBLE);
prevPageButton.setAlpha(actualPage > 1 ? 1f : .5f);
prevPageButton.setEnabled(actualPage > 1);
nextPageButton.setAlpha(actualPage < totalPage ? 1f : .5f);
nextPageButton.setEnabled(actualPage < totalPage);
pageIndexText.setText(String.format(Locale.US, "%d/%d", actualPage, totalPage));
});

}

private void loadDialog() {
DefaultDialogs.pageChangerDialog(
new DefaultDialogs.Builder(this)
.setActual(actualPage)
.setMin(1)
.setMax(totalPage)
.setTitle(R.string.change_page)
.setDrawable(R.drawable.ic_find_in_page)
.setDialogs(new DefaultDialogs.CustomDialogResults() {
@Override
public void positive(int actual) {
inspector = inspector.cloneInspector(MainActivity.this, resetDataset);
inspector.setPage(actual);
inspector.start();
}
})
);
}

private void showLogoutForm() {
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this);
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/com/dar/nclientv2/adapters/FavoriteAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import java.util.Locale;

public class FavoriteAdapter extends RecyclerView.Adapter<GenericAdapter.ViewHolder> implements Filterable {
private final int perPage = FavoriteActivity.getEntryPerPage();
private final SparseIntArray statuses = new SparseIntArray();
private Gallery[] galleries;
private final FavoriteActivity activity;
private CharSequence lastQuery;
private Cursor cursor;
Expand Down Expand Up @@ -58,9 +60,12 @@ public GenericAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, i

@Nullable
private Gallery galleryFromPosition(int position) {
if (galleries[position] != null) return galleries[position];
cursor.moveToPosition(position);
try {
return Queries.GalleryTable.cursorToGallery(cursor);
Gallery g = Queries.GalleryTable.cursorToGallery(cursor);
galleries[position] = g;
return g;
} catch (IOException e) {
e.printStackTrace();
return null;
Expand Down Expand Up @@ -123,6 +128,10 @@ private void startGallery(Gallery ent) {
activity.startActivity(intent);
}

public void changePage() {
forceReload();
}

public void updateColor(int position) {
Gallery ent = galleryFromPosition(position);
if (ent == null) return;
Expand All @@ -149,7 +158,7 @@ protected FilterResults performFiltering(CharSequence constraint) {
lastQuery = constraint.toString();
LogUtility.d(lastQuery + "LASTQERY");
force = false;
Cursor c = Queries.FavoriteTable.getAllFavoriteGalleriesCursor(lastQuery, sortByTitle);
Cursor c = Queries.FavoriteTable.getAllFavoriteGalleriesCursor(lastQuery, sortByTitle, perPage, (activity.getActualPage() - 1) * perPage);
results.count = c.getCount();
results.values = c;
LogUtility.d("FILTERING3");
Expand Down Expand Up @@ -198,6 +207,7 @@ public void clearGalleries() {

private void updateCursor(Cursor c) {
if (cursor != null) cursor.close();
galleries = new Gallery[c.getCount()];
cursor = c;
statuses.clear();
}
Expand Down
49 changes: 34 additions & 15 deletions app/src/main/java/com/dar/nclientv2/async/database/Queries.java
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,19 @@ public static class FavoriteTable {
static final String ID_GALLERY = "id_gallery";
static final String TIME = "time";

private static final String TITLE_CLAUSE = String.format(Locale.US, "%s LIKE ? OR %s LIKE ? OR %s LIKE ?",
GalleryTable.TITLE_ENG,
GalleryTable.TITLE_JP,
GalleryTable.TITLE_PRETTY
);

private static final String FAVORITE_JOIN_GALLERY = String.format(Locale.US, "%s INNER JOIN %s ON %s=%s",
FavoriteTable.TABLE_NAME,
GalleryTable.TABLE_NAME,
FavoriteTable.ID_GALLERY,
GalleryTable.IDGALLERY
);

public static void addFavorite(Gallery gallery) {
GalleryTable.insert(gallery);
FavoriteTable.insert(gallery.getId());
Expand All @@ -784,20 +797,11 @@ static String titleTypeToColumn(TitleType type) {
* @param orderByTitle true if order by title, false order by latest
* @return cursor which points to the galleries
*/
public static Cursor getAllFavoriteGalleriesCursor(CharSequence query, boolean orderByTitle) {
String q = String.format(Locale.US, "SELECT * FROM %s INNER JOIN %s ON %s=%s WHERE %s LIKE ? OR %s LIKE ? OR %s LIKE ?",
FavoriteTable.TABLE_NAME,
GalleryTable.TABLE_NAME,
FavoriteTable.ID_GALLERY,
GalleryTable.IDGALLERY,
GalleryTable.TITLE_ENG,
GalleryTable.TITLE_JP,
GalleryTable.TITLE_PRETTY
);
if (orderByTitle) q += "ORDER BY " + titleTypeToColumn(Global.getTitleType());
else q += "ORDER BY " + FavoriteTable.TIME + " DESC";
public static Cursor getAllFavoriteGalleriesCursor(CharSequence query, boolean orderByTitle, int limit, int offset) {
String order = orderByTitle ? titleTypeToColumn(Global.getTitleType()) : FavoriteTable.TIME + " DESC";
String param = "%" + query + "%";
return db.rawQuery(q, new String[]{param, param, param});
String limitString = String.format(Locale.US, "%d OFFSET %d", limit, offset);
return db.query(FAVORITE_JOIN_GALLERY, null, TITLE_CLAUSE, new String[]{param, param, param}, null, null, order, limitString);
}

/**
Expand Down Expand Up @@ -836,10 +840,25 @@ public static void removeFavorite(int id) {
db.delete(TABLE_NAME, ID_GALLERY + "=?", new String[]{"" + id});
}

public static int countFavorite(@Nullable String text) {
if (text == null || text.trim().isEmpty()) return countFavorite();
int totalFavorite = 0;
String param = "%" + text + "%";
Cursor c = db.query(FAVORITE_JOIN_GALLERY, new String[]{"COUNT(*)"}, TITLE_CLAUSE, new String[]{param, param, param}, null, null, null);
if (c.moveToFirst()) {
totalFavorite = c.getInt(0);
}
c.close();
return totalFavorite;
}

public static int countFavorite() {
String query = "SELECT * FROM " + TABLE_NAME;
int totalFavorite = 0;
String query = "SELECT COUNT(*) FROM " + TABLE_NAME;
Cursor c = db.rawQuery(query, null);
int totalFavorite = c.getCount();
if (c.moveToFirst()) {
totalFavorite = c.getInt(0);
}
c.close();
return totalFavorite;
}
Expand Down
Loading

0 comments on commit 575248e

Please sign in to comment.