Skip to content

Commit

Permalink
Merge pull request #30 from ranobe-org/fix/cleanups
Browse files Browse the repository at this point in the history
Fix/cleanups
  • Loading branch information
ap-atul authored Aug 19, 2023
2 parents 3b5766e + 803536a commit 7fea938
Show file tree
Hide file tree
Showing 37 changed files with 162 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

public class DateConverter {
@TypeConverter
public static Date toDate(Long dateLong){
return dateLong == null ? null: new Date(dateLong);
public static Date toDate(Long dateLong) {
return dateLong == null ? null : new Date(dateLong);
}

@TypeConverter
public static Long fromDate(Date date){
public static Long fromDate(Date date) {
return date == null ? null : date.getTime();
}
}
23 changes: 11 additions & 12 deletions app/src/main/java/org/ranobe/ranobe/models/Chapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@

@Entity
public class Chapter implements Parcelable {
public static final Creator<Chapter> CREATOR = new Creator<Chapter>() {
@Override
public Chapter createFromParcel(Parcel in) {
return new Chapter(in);
}

@Override
public Chapter[] newArray(int size) {
return new Chapter[size];
}
};
@PrimaryKey
@NonNull
public String url;
Expand Down Expand Up @@ -39,18 +50,6 @@ protected Chapter(Parcel in) {
id = in.readFloat();
}

public static final Creator<Chapter> CREATOR = new Creator<Chapter>() {
@Override
public Chapter createFromParcel(Parcel in) {
return new Chapter(in);
}

@Override
public Chapter[] newArray(int size) {
return new Chapter[size];
}
};

@NonNull
@Override
public String toString() {
Expand Down
24 changes: 11 additions & 13 deletions app/src/main/java/org/ranobe/ranobe/models/Novel.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,23 @@

@Entity
public class Novel implements Parcelable {
public static final Creator<Novel> CREATOR = new Creator<Novel>() {
@Override
public Novel createFromParcel(Parcel in) {
return new Novel(in);
}

@Override
public Novel[] newArray(int size) {
return new Novel[size];
}
};
@PrimaryKey
public long id;
public int sourceId;
public String name;
public String cover;
public String url;

public String status;
public String summary;
public List<String> alternateNames;
Expand Down Expand Up @@ -49,18 +59,6 @@ protected Novel(Parcel in) {
year = in.readInt();
}

public static final Creator<Novel> CREATOR = new Creator<Novel>() {
@Override
public Novel createFromParcel(Parcel in) {
return new Novel(in);
}

@Override
public Novel[] newArray(int size) {
return new Novel[size];
}
};

@NonNull
@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,10 @@ public class GithubRepo {
}};
private final Executor executor;

public GithubRepo(){
public GithubRepo() {
this.executor = Executors.newCachedThreadPool();
}

public interface Callback<T> {
void onComplete(T result);

void onError(Exception e);
}

public void getLatestRelease(Callback<GithubRelease> callback) {
this.executor.execute(() -> {
try {
Expand All @@ -45,18 +39,24 @@ private GithubRelease fetchLatestRelease() throws Exception {
Version latestVersion = new Version(Version.extractVersionNumber(tag));
Version currentVersion = new Version(Version.extractVersionNumber(BuildConfig.VERSION_NAME));

if(latestVersion.get().equals(currentVersion.get())) {
if (latestVersion.get().equals(currentVersion.get())) {
return new GithubRelease(false, currentVersion.get(), null);
}

if(latestVersion.compareTo(currentVersion) > 0) {
if (latestVersion.compareTo(currentVersion) > 0) {
String url = response.getString("html_url");
return new GithubRelease(true, tag, url);
}

return new GithubRelease(false, currentVersion.get(), null);
}

public interface Callback<T> {
void onComplete(T result);

void onError(Exception e);
}

public static class GithubRelease {
public boolean updateAvailable;
public String newReleaseVersion;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.ranobe.ranobe.sources.en;

import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.ranobe.ranobe.models.Chapter;
Expand Down
23 changes: 13 additions & 10 deletions app/src/main/java/org/ranobe/ranobe/sources/en/Neovel.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,27 @@ private String getCoverImage(String bookId) {

private String getStatus(int completion) {
switch (completion) {
case 1: return "Ongoing";
case 3: return "Completed";
default: return "N.A.";
case 1:
return "Ongoing";
case 3:
return "Completed";
default:
return "N.A.";
}
}

private int getYear(String date) {
Pattern pattern = Pattern.compile("/\\d{4}/gm");
Matcher m = pattern.matcher(date);
if(m.find()) {
if (m.find()) {
return Integer.parseInt(m.group());
}
return 0;
}

private List<String> jsonArrayToString(JSONArray arr) throws JSONException {
List<String> values = new ArrayList<>();
for(int i = 0; i < arr.length(); i++) {
for (int i = 0; i < arr.length(); i++) {
values.add(arr.getString(0));
}
return values;
Expand All @@ -74,15 +77,15 @@ private String encodeKeyword(String keyword) {

@Override
public List<Novel> novels(int page) throws Exception {
String web = BASE_URL +"/V2/books/search?language=EN&filter=0&name=&sort=6&page=".concat(String.valueOf(page)) + "&onlyOffline=true&genreIds=0&genreCombining=0&tagIds=0&tagCombining=0&minChapterCount=0&maxChapterCount=9999&completion=5&onlyPremium=false&blacklistedTagIds=&onlyMature=false";
String web = BASE_URL + "/V2/books/search?language=EN&filter=0&name=&sort=6&page=".concat(String.valueOf(page)) + "&onlyOffline=true&genreIds=0&genreCombining=0&tagIds=0&tagCombining=0&minChapterCount=0&maxChapterCount=9999&completion=5&onlyPremium=false&blacklistedTagIds=&onlyMature=false";
return parseNovels(web);
}

private List<Novel> parseNovels(String web) throws Exception {
List<Novel> items = new ArrayList<>();
JSONArray data = new JSONArray(HttpClient.GET(web, new HashMap<>()));

for(int i = 0; i < data.length(); i++) {
for (int i = 0; i < data.length(); i++) {
JSONObject d = data.getJSONObject(i);
String url = BASE_URL + "/" + d.getString("id");

Expand Down Expand Up @@ -122,7 +125,7 @@ public List<Chapter> chapters(Novel novel) throws Exception {
String url = String.format("https://neovel.io/V5/chapters?bookId=%s&language=EN", bookId);
JSONArray data = new JSONArray(HttpClient.GET(url, new HashMap<>()));

for(int i = 0; i < data.length(); i++) {
for (int i = 0; i < data.length(); i++) {
JSONObject o = data.getJSONObject(i);
String u = BASE_URL + "/chapter/" + o.getString("chapterId");

Expand All @@ -145,7 +148,7 @@ public Chapter chapter(Chapter chapter) throws Exception {
Element doc = Jsoup.parse(data.getString("chapterContent"));
List<String> paras = new ArrayList<>();

for(String bits: doc.wholeText().split("\n")){
for (String bits : doc.wholeText().split("\n")) {
String text = bits.trim();
if (text.length() > 0) {
paras.add(text);
Expand All @@ -162,7 +165,7 @@ public List<Novel> search(Filter filters, int page) throws Exception {

if (filters.hashKeyword()) {
String encodedKeyword = encodeKeyword(filters.getKeyword()).trim();
String web = BASE_URL +"/V2/books/search?language=EN&filter=0&name=" + encodedKeyword + "&sort=6&page=".concat(String.valueOf(page - 1)) + "&onlyOffline=true&genreIds=0&genreCombining=0&tagIds=0&tagCombining=0&minChapterCount=0&maxChapterCount=9999&completion=5&onlyPremium=false&blacklistedTagIds=&onlyMature=false";
String web = BASE_URL + "/V2/books/search?language=EN&filter=0&name=" + encodedKeyword + "&sort=6&page=".concat(String.valueOf(page - 1)) + "&onlyOffline=true&genreIds=0&genreCombining=0&tagIds=0&tagCombining=0&minChapterCount=0&maxChapterCount=9999&completion=5&onlyPremium=false&blacklistedTagIds=&onlyMature=false";
return parseNovels(web);
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/ranobe/ranobe/sources/en/Ranobe.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.ranobe.ranobe.util.NumberUtils;
import org.ranobe.ranobe.util.SourceUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -54,6 +53,7 @@ public List<Novel> novels(int page) throws Exception {

return items;
}

@Override
public Novel details(Novel novel) throws Exception {
Element doc = Jsoup.parse(HttpClient.GET(novel.url, new HashMap<>()));
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/org/ranobe/ranobe/ui/browse/Browse.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.ranobe.ranobe.ui.browse;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package org.ranobe.ranobe.ui.browse.viewmodel;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import org.ranobe.ranobe.config.Ranobe;
import org.ranobe.ranobe.models.Novel;
import org.ranobe.ranobe.network.repository.Repository;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

public class ChapterAdapter extends RecyclerView.Adapter<ChapterAdapter.MyViewHolder> {
private final List<Chapter> items;
private List<String> readingList;
private final OnChapterItemClickListener listener;
private List<String> readingList;

public ChapterAdapter(List<Chapter> items, OnChapterItemClickListener listener) {
this.items = items;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import org.ranobe.ranobe.models.Chapter;
import org.ranobe.ranobe.models.Chapter;
import org.ranobe.ranobe.models.Novel;
import org.ranobe.ranobe.network.repository.Repository;
Expand All @@ -12,6 +11,7 @@

public class ChaptersViewModel extends ViewModel {
private MutableLiveData<String> error = new MutableLiveData<>();

public MutableLiveData<String> getError() {
return error = new MutableLiveData<>();
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/ranobe/ranobe/ui/details/Details.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private void setUpError(String error) {
}

private void navigateToChapterList() {
if(novel == null) return;
if (novel == null) return;
Bundle bundle = new Bundle();
bundle.putParcelable(Ranobe.KEY_NOVEL, novel);
Chapters chapters = new Chapters();
Expand Down Expand Up @@ -117,7 +117,7 @@ private void addChips(List<String> genres) {

private void addToLibrary(Novel novel) {
if (novel == null) return;
RanobeDatabase.databaseExecutor.execute( () -> RanobeDatabase.database().novels().save(novel));
RanobeDatabase.databaseExecutor.execute(() -> RanobeDatabase.database().novels().save(novel));
Snackbar.make(binding.getRoot(), "Added novel to library", Snackbar.LENGTH_SHORT).show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

public class DetailsViewModel extends ViewModel {
private MutableLiveData<String> error = new MutableLiveData<>();

public MutableLiveData<String> getError() {
return error = new MutableLiveData<>();
}

public MutableLiveData<Novel> details(Novel novel) {
MutableLiveData<Novel> details = new MutableLiveData<>();
new Repository().details(novel, new Repository.Callback<Novel>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat

private void setNovels(List<Novel> novels) {
binding.progress.hide();
if (novels.size() == 0) {
if (novels.size() == 0) {
showNoNovels();
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import org.ranobe.ranobe.models.Chapter;
import org.ranobe.ranobe.models.Chapter;
import org.ranobe.ranobe.models.Novel;
import org.ranobe.ranobe.network.repository.Repository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import java.util.List;

public class SearchViewModel extends ViewModel {
private final MutableLiveData<LinkedHashMap<DataSource, List<Novel>>> results = new MutableLiveData<>();
private MutableLiveData<String> error = new MutableLiveData<>();
private Filter filter = new Filter();
private final MutableLiveData<LinkedHashMap<DataSource, List<Novel>>> results = new MutableLiveData<>();

public MutableLiveData<String> getError() {
return error = new MutableLiveData<>();
Expand All @@ -30,7 +30,7 @@ public MutableLiveData<LinkedHashMap<DataSource, List<Novel>>> search(List<DataS
}

this.filter = filter;
for(DataSource source: sources) {
for (DataSource source : sources) {
new Repository(source.sourceId).search(filter, page, new Repository.Callback<List<Novel>>() {
@Override
public void onComplete(List<Novel> result) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package org.ranobe.ranobe.ui.settings.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

Expand Down
Loading

0 comments on commit 7fea938

Please sign in to comment.