Skip to content

Commit

Permalink
Added All <=> Source filtering swap toggle
Browse files Browse the repository at this point in the history
Added All <=> Source filtering swap toggle
Added Indexing for faster fetching webPages from DB
Added new icons
  • Loading branch information
gmathi committed Jun 10, 2018
1 parent 931fc5f commit 0ea047f
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ app/release/output.json

keystore.properties
.idea/caches/build_file_checksums.ser
.idea/assetWizardSettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import io.github.gmathi.novellibrary.adapter.GenericFragmentStatePagerAdapter
import io.github.gmathi.novellibrary.database.*
import io.github.gmathi.novellibrary.dbHelper
import io.github.gmathi.novellibrary.model.*
import io.github.gmathi.novellibrary.network.HostNames
import io.github.gmathi.novellibrary.network.NovelApi
import io.github.gmathi.novellibrary.network.getChapterUrls
import io.github.gmathi.novellibrary.util.Constants
Expand All @@ -25,7 +24,7 @@ import kotlinx.android.synthetic.main.activity_chapters_pager.*
import kotlinx.android.synthetic.main.content_chapters_pager.*
import org.greenrobot.eventbus.EventBus
import java.io.File
import java.net.URI
import java.util.*


class ChaptersPagerActivity : BaseActivity(), ActionMode.Callback {
Expand All @@ -38,16 +37,26 @@ class ChaptersPagerActivity : BaseActivity(), ActionMode.Callback {
private val sources: ArrayList<Pair<Long, String>> = ArrayList()
private var actionMode: ActionMode? = null
private var confirmDialog: MaterialDialog? = null
private var showSources = false


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chapters_pager)
setSupportActionBar(toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)

novel = intent.getSerializableExtra("novel") as Novel
showSources = novel.metaData[Constants.MetaDataKeys.SHOW_SOURCES]?.toBoolean() ?: false
dbHelper.updateNewReleasesCount(novel.id, 0L)

sourcesToggle.setOnClickListener {
showSources = !showSources
novel.metaData[Constants.MetaDataKeys.SHOW_SOURCES] = showSources.toString()
dbHelper.updateNovelMetaData(novel)
setViewPager()
}

progressLayout.showLoading()
if (novel.id != -1L)
getChaptersFromDB()
Expand All @@ -61,8 +70,13 @@ class ChaptersPagerActivity : BaseActivity(), ActionMode.Callback {
supportFragmentManager.popBackStack()

sources.clear()
val sourceIds = chapters.distinctBy { it.sourceId }.map { it.sourceId }
sourceIds.forEach { dbHelper.getSource(it)?.let { sources.add(it) } }

if (showSources) {
val sourceIds = chapters.distinctBy { it.sourceId }.map { it.sourceId }
sourceIds.forEach { dbHelper.getSource(it)?.let { sources.add(it) } }
} else {
dbHelper.getSource(-1L)?.let { sources.add(it) }
}

val titles = Array(sources.size, init = {
sources[it].second
Expand Down Expand Up @@ -93,6 +107,7 @@ class ChaptersPagerActivity : BaseActivity(), ActionMode.Callback {
//region Data
private fun getChaptersFromDB() {
async {
Utils.error("CPA", "chpaDBStart, " + Calendar.getInstance().timeInMillis.toString())
chapters = await { ArrayList(dbHelper.getAllWebPages(novel.id)) }
if (chapters.isEmpty() || chapters.size < novel.chaptersCount.toInt()) {
novel.metaData[Constants.MetaDataKeys.LAST_UPDATED_DATE] = Utils.getCurrentFormattedDate()
Expand All @@ -102,10 +117,12 @@ class ChaptersPagerActivity : BaseActivity(), ActionMode.Callback {
progressLayout.showContent()
setViewPager()
}
Utils.error("CPA", "chpaDBEnd, " + Calendar.getInstance().timeInMillis.toString())
}
}

private fun getChapters() {
Utils.error("CPA", "chpaNetwork, " + Calendar.getInstance().timeInMillis.toString())
async chapters@{
progressLayout.showLoading()
if (!Utils.isConnectedToNetwork(this@ChaptersPagerActivity)) {
Expand All @@ -119,12 +136,17 @@ class ChaptersPagerActivity : BaseActivity(), ActionMode.Callback {

//Download latest chapters from network
try {
Utils.error("CPA", "callStart, " + Calendar.getInstance().timeInMillis.toString())
chapters = await { NovelApi.getChapterUrls(novel) } ?: ArrayList()
Utils.error("CPA", "callEnd, " + Calendar.getInstance().timeInMillis.toString())

//Save to DB if the novel is in Library
if (novel.id != -1L)
if (novel.id != -1L) {
Utils.error("CPA", "dbStart, " + Calendar.getInstance().timeInMillis.toString())
await { addChaptersToDB() }

Utils.error("CPA", "dbEnd, " + Calendar.getInstance().timeInMillis.toString())
}
actionMode?.finish()
progressLayout.showContent()
setViewPager()

Expand All @@ -147,7 +169,7 @@ class ChaptersPagerActivity : BaseActivity(), ActionMode.Callback {
chapters[i].id = dbHelper.createWebPage(chapters[i])
} else {
chapters[i].copyFrom(webPage)
if (webPage.sourceId == -1L && URI(novel.url).host.contains(HostNames.NOVEL_UPDATES)) {
if (webPage.sourceId == -1L && chapters[i].sourceId != -1L) {
dbHelper.updateWebPage(chapters[i])
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.os.FileObserver.CREATE
import com.google.gson.Gson
import io.github.gmathi.novellibrary.dataCenter
import io.github.gmathi.novellibrary.model.Novel
Expand Down Expand Up @@ -37,7 +38,8 @@ private constructor(context: Context) : SQLiteOpenHelper(context, DBKeys.DATABAS
db.execSQL(DBKeys.CREATE_TABLE_SOURCE)
db.execSQL(DBKeys.CREATE_TABLE_NOVEL_SECTION)
db.execSQL(DBKeys.CREATE_TABLE_LARGE_PREFERENCE)
db.execSQL("INSERT INTO " + DBKeys.TABLE_SOURCE + " (" + DBKeys.KEY_ID + ", " + DBKeys.KEY_NAME + ") VALUES (-1, 'All')")
db.execSQL("INSERT INTO ${DBKeys.TABLE_SOURCE} (${DBKeys.KEY_ID}, ${DBKeys.KEY_NAME}) VALUES (-1, 'All')")
db.execSQL("CREATE INDEX web_pages_url_id_index ON ${DBKeys.TABLE_WEB_PAGE}(${DBKeys.KEY_ID}, ${DBKeys.KEY_URL})")

}

Expand All @@ -46,46 +48,46 @@ private constructor(context: Context) : SQLiteOpenHelper(context, DBKeys.DATABAS
var version = oldVersion

if (version == DBKeys.INITIAL_VERSION) {
db.execSQL("ALTER TABLE " + DBKeys.TABLE_NOVEL + " ADD COLUMN " + DBKeys.KEY_ORDER_ID + " INTEGER")
db.execSQL("UPDATE " + DBKeys.TABLE_NOVEL + " SET " + DBKeys.KEY_ORDER_ID + "=" + DBKeys.KEY_ID)
db.execSQL("ALTER TABLE ${DBKeys.TABLE_NOVEL} ADD COLUMN ${DBKeys.KEY_ORDER_ID} INTEGER")
db.execSQL("UPDATE ${DBKeys.TABLE_NOVEL} SET ${DBKeys.KEY_ORDER_ID}=${DBKeys.KEY_ID}")
version = DBKeys.VER_NOVEL_ORDER_ID
}

if (version == DBKeys.VER_NOVEL_ORDER_ID) {
db.execSQL("ALTER TABLE " + DBKeys.TABLE_NOVEL + " ADD COLUMN " + DBKeys.KEY_NEW_RELEASES_COUNT + " INTEGER")
db.execSQL("UPDATE " + DBKeys.TABLE_NOVEL + " SET " + DBKeys.KEY_NEW_RELEASES_COUNT + "=0")
db.execSQL("UPDATE " + DBKeys.TABLE_NOVEL + " SET " + DBKeys.KEY_CURRENT_WEB_PAGE_ID + "=-1")
db.execSQL("DROP TABLE IF EXISTS " + DBKeys.TABLE_WEB_PAGE)
db.execSQL("ALTER TABLE ${DBKeys.TABLE_NOVEL} ADD COLUMN ${DBKeys.KEY_NEW_RELEASES_COUNT} INTEGER")
db.execSQL("UPDATE ${DBKeys.TABLE_NOVEL} SET ${DBKeys.KEY_NEW_RELEASES_COUNT}=0")
db.execSQL("UPDATE ${DBKeys.TABLE_NOVEL} SET ${DBKeys.KEY_CURRENT_WEB_PAGE_ID}=-1")
db.execSQL("DROP TABLE IF EXISTS ${DBKeys.TABLE_WEB_PAGE}")
db.execSQL(DBKeys.CREATE_TABLE_WEB_PAGE)
version = DBKeys.VER_WEB_PAGE_ORDER_ID
}

if (version == DBKeys.VER_WEB_PAGE_ORDER_ID) {
db.execSQL("ALTER TABLE " + DBKeys.TABLE_NOVEL + " ADD COLUMN " + DBKeys.KEY_CHAPTERS_COUNT + " INTEGER")
db.execSQL("UPDATE " + DBKeys.TABLE_NOVEL + " SET " + DBKeys.KEY_CHAPTERS_COUNT + "=" + DBKeys.KEY_NEW_RELEASES_COUNT)
db.execSQL("ALTER TABLE ${DBKeys.TABLE_NOVEL} ADD COLUMN ${DBKeys.KEY_CHAPTERS_COUNT} INTEGER")
db.execSQL("UPDATE ${DBKeys.TABLE_NOVEL} SET ${DBKeys.KEY_CHAPTERS_COUNT}=${DBKeys.KEY_NEW_RELEASES_COUNT}")
version = DBKeys.VER_NOVEL_SYNC
}

if (version == DBKeys.VER_NOVEL_SYNC) {
db.execSQL("ALTER TABLE " + DBKeys.TABLE_WEB_PAGE + " ADD COLUMN " + DBKeys.KEY_SOURCE_ID + " INTEGER")
db.execSQL("UPDATE " + DBKeys.TABLE_WEB_PAGE + " SET " + DBKeys.KEY_SOURCE_ID + "= -1")
db.execSQL("ALTER TABLE ${DBKeys.TABLE_WEB_PAGE} ADD COLUMN ${DBKeys.KEY_SOURCE_ID} INTEGER")
db.execSQL("UPDATE ${DBKeys.TABLE_WEB_PAGE} SET ${DBKeys.KEY_SOURCE_ID}= -1")
db.execSQL(DBKeys.CREATE_TABLE_SOURCE)
db.execSQL("INSERT INTO " + DBKeys.TABLE_SOURCE + " (" + DBKeys.KEY_ID + ", " + DBKeys.KEY_NAME + ") VALUES (-1, 'All')")
db.execSQL("INSERT INTO ${DBKeys.TABLE_SOURCE} (${DBKeys.KEY_ID}, ${DBKeys.KEY_NAME}) VALUES (-1, 'All')")
version = DBKeys.VER_CHAPTER_SOURCE
}

if (version == DBKeys.VER_CHAPTER_SOURCE) {
db.execSQL("DROP TABLE IF EXISTS " + DBKeys.TABLE_DOWNLOAD_QUEUE)
db.execSQL("DROP TABLE IF EXISTS ${DBKeys.TABLE_DOWNLOAD_QUEUE}")
db.execSQL(DBKeys.CREATE_TABLE_DOWNLOAD)
version = DBKeys.VER_DOWNLOADS
}

if (version == DBKeys.VER_DOWNLOADS) {
db.execSQL("UPDATE " + DBKeys.TABLE_NOVEL + " SET " + DBKeys.KEY_CHAPTERS_COUNT + "=" + DBKeys.KEY_NEW_RELEASES_COUNT)
db.execSQL("UPDATE " + DBKeys.TABLE_NOVEL + " SET " + DBKeys.KEY_NEW_RELEASES_COUNT + "= 0")
db.execSQL("UPDATE ${DBKeys.TABLE_NOVEL} SET ${DBKeys.KEY_CHAPTERS_COUNT}=${DBKeys.KEY_NEW_RELEASES_COUNT}")
db.execSQL("UPDATE ${DBKeys.TABLE_NOVEL} SET ${DBKeys.KEY_NEW_RELEASES_COUNT}= 0")
db.execSQL(DBKeys.CREATE_TABLE_NOVEL_SECTION)
db.execSQL("ALTER TABLE " + DBKeys.TABLE_NOVEL + " ADD COLUMN " + DBKeys.KEY_NOVEL_SECTION_ID + " INTEGER")
db.execSQL("UPDATE " + DBKeys.TABLE_NOVEL + " SET " + DBKeys.KEY_NOVEL_SECTION_ID + "= -1")
db.execSQL("ALTER TABLE ${DBKeys.TABLE_NOVEL} ADD COLUMN ${DBKeys.KEY_NOVEL_SECTION_ID} INTEGER")
db.execSQL("UPDATE ${DBKeys.TABLE_NOVEL} SET ${DBKeys.KEY_NOVEL_SECTION_ID}= -1")
version = DBKeys.VER_NEW_RELEASES
}

Expand All @@ -100,6 +102,8 @@ private constructor(context: Context) : SQLiteOpenHelper(context, DBKeys.DATABAS
db.insert(DBKeys.TABLE_LARGE_PREFERENCE, null, values)
dataCenter.removeNovelHistory()

db.execSQL("CREATE INDEX web_pages_url_id_index ON ${DBKeys.TABLE_WEB_PAGE}(${DBKeys.KEY_ID}, ${DBKeys.KEY_URL})")

version = DBKeys.VER_LARGE_PREFERENCE
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class ChaptersFragment : BaseFragment(),
private fun setData() {
val chaptersPagerActivity = activity as? ChaptersPagerActivity
if (chaptersPagerActivity != null) {
val chapters = chaptersPagerActivity.chapters.filter { it.sourceId == sourceId }
val chapters = if (sourceId == -1L) chaptersPagerActivity.chapters else chaptersPagerActivity.chapters.filter { it.sourceId == sourceId }
if (!chapters.isEmpty()) {
adapter.updateData(if (isSortedAsc) ArrayList(chapters) else ArrayList(chapters.reversed()))
progressLayout.showContent()
Expand Down Expand Up @@ -223,7 +223,7 @@ class ChaptersFragment : BaseFragment(),

@Subscribe(threadMode = ThreadMode.MAIN)
fun onChapterActionModeEvent(chapterActionModeEvent: ChapterActionModeEvent) {
if (chapterActionModeEvent.eventType == EventType.COMPLETE || (chapterActionModeEvent.eventType == EventType.UPDATE && chapterActionModeEvent.sourceId == sourceId)) {
if (chapterActionModeEvent.eventType == EventType.COMPLETE || (chapterActionModeEvent.eventType == EventType.UPDATE && (chapterActionModeEvent.sourceId == sourceId || sourceId == -1L))) {
setData()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ data class WebPage(var url: String, var chapter: String) : Serializable {
title = if (other.title != null) other.title else title
filePath = if (other.filePath != null) other.filePath else filePath
isRead = if (other.isRead != 0) other.isRead else isRead
sourceId = if (other.sourceId != -1L) other.sourceId else sourceId

other.metaData.keys.forEach {
metaData[it] = other.metaData[it]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import java.net.URI
fun NovelApi.getChapterUrls(novel: Novel): ArrayList<WebPage>? {
val host = URI(novel.url).host
when {
host.contains(HostNames.NOVEL_UPDATES) -> return getNUChapterUrlsWithSources(novel)
host.contains(HostNames.NOVEL_UPDATES) -> return getNUALLChapterUrlsWithSources(novel)
host.contains(HostNames.ROYAL_ROAD) -> return getRRChapterUrls(novel.url)
host.contains(HostNames.WLN_UPDATES) -> return getWLNUChapterUrls(novel.url)
}
Expand Down Expand Up @@ -72,18 +72,60 @@ fun getNUALLChapterUrls(novel: Novel): ArrayList<WebPage> {
.userAgent(USER_AGENT)
.post()

doc?.getElementsByAttribute("data-id")?.mapTo(chapters) {
WebPage("https:" + it?.attr("href")!!, it.getElementsByAttribute("title").attr("title"))
var orderId = 0L
doc?.getElementsByAttribute("data-id")?.reversed()?.forEach {

val webPageUrl = "https:" + it?.attr("href")
val webPage = WebPage(webPageUrl, it.getElementsByAttribute("title").attr("title"))
webPage.orderId = orderId++
webPage.novelId = novel.id
chapters.add(webPage)
}

return chapters
}

fun getNUALLChapterUrlsForSource(novel: Novel, sourceId: Int? = null, sourceName: String? = null): ArrayList<WebPage> {
val dbSourceId = dbHelper.getSource(sourceName!!)?.first ?: -1L
fun getNUALLChapterUrlsWithSources(novel: Novel): ArrayList<WebPage> {
val chapters = ArrayList<WebPage>()
if (!novel.metaData.containsKey("PostId")) throw Exception("No PostId Found!")

//val sourceMapList = ArrayList<HashMap<String, Long>>()
val sourceMapList = getNUChapterUrlsWithSources(novel)

val novelUpdatesNovelId = novel.metaData["PostId"]
val url = "https://www.novelupdates.com/wp-admin/admin-ajax.php"

val doc = Jsoup.connect(url)
.data("action", "nd_getchapters")
.cookies(NovelApi.cookiesMap)
.data("mypostid", novelUpdatesNovelId)
.userAgent(USER_AGENT)
.post()

var orderId = 0L
doc?.getElementsByAttribute("data-id")?.reversed()?.forEach {

val webPageUrl = "https:" + it?.attr("href")
val webPage = WebPage(webPageUrl, it.getElementsByAttribute("title").attr("title"))
webPage.orderId = orderId++
webPage.novelId = novel.id
for (sourceMap in sourceMapList) {
webPage.sourceId = sourceMap[webPageUrl] ?: continue
break
}
chapters.add(webPage)
}

return chapters
}

private fun getNUALLChapterUrlsForSource(novel: Novel, sourceId: Int? = null, sourceName: String? = null): HashMap<String, Long> {

val sourceMap = HashMap<String, Long>()

val dbSourceId = dbHelper.getSource(sourceName!!)?.first ?: -1L
if (!novel.metaData.containsKey("PostId")) throw Exception("No PostId Found!")

val novelUpdatesNovelId = novel.metaData["PostId"]
val url = "https://www.novelupdates.com/wp-admin/admin-ajax.php"

Expand All @@ -97,21 +139,17 @@ fun getNUALLChapterUrlsForSource(novel: Novel, sourceId: Int? = null, sourceName

val doc = connection.post()

var orderId = 0L
doc?.select("a[href][data-id]")?.reversed()?.forEach {
val webPage = WebPage("https:" + it.attr("href"), it.selectFirst("span[title]").text())
webPage.sourceId = dbSourceId
webPage.orderId = orderId++
webPage.novelId = novel.id
chapters.add(webPage)
//var orderId = 0L
doc?.select("a[href][data-id]")?.forEach {
sourceMap["https:" + it.attr("href")] = dbSourceId
}

return chapters
return sourceMap
}

fun getNUChapterUrlsWithSources(novel: Novel): ArrayList<WebPage> {
private fun getNUChapterUrlsWithSources(novel: Novel): ArrayList<HashMap<String, Long>> {

val chapters = ArrayList<WebPage>()
val sourceMap = ArrayList<HashMap<String, Long>>()
if (!novel.metaData.containsKey("PostId")) throw Exception("No PostId Found!")

val novelUpdatesNovelId = novel.metaData["PostId"]
Expand All @@ -126,12 +164,12 @@ fun getNUChapterUrlsWithSources(novel: Novel): ArrayList<WebPage> {

doc?.select("div.checkbox")?.forEach {
dbHelper.createSource(it.text())
val webPages = getNUALLChapterUrlsForSource(novel,
val tempSourceMap = getNUALLChapterUrlsForSource(novel,
it.selectFirst("input.grp-filter-attr[value]").attr("value").toInt(),
it.text()
)
chapters.addAll(webPages)
sourceMap.add(tempSourceMap)
}

return chapters
return sourceMap
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ object Constants {
object MetaDataKeys {
const val LAST_READ_DATE = "lastReadDate"
const val LAST_UPDATED_DATE = "lastUpdatedDate"
const val SHOW_SOURCES = "showSources"
}

object LargePrefenceKeys {
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_swap_horiz_vector.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M6.99,11L3,15l3.99,4v-3H14v-2H6.99v-3zM21,9l-3.99,-4v3H10v2h7.01v3L21,9z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_swap_vert_vector.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M16,17.01V10h-2v7.01h-3L15,21l4,-3.99h-3zM9,3L5,6.99h3V14h2V6.99h3L9,3z"/>
</vector>
Loading

0 comments on commit 0ea047f

Please sign in to comment.