Update details activity

This commit is contained in:
Admin
2020-02-02 13:15:47 +02:00
parent 6eea278b4d
commit ef32c49310
13 changed files with 154 additions and 45 deletions

View File

@@ -16,6 +16,9 @@ abstract class HistoryDao {
@Query("SELECT * FROM history ORDER BY :orderBy LIMIT :limit OFFSET :offset")
abstract suspend fun getAll(offset: Int, limit: Int, orderBy: String): List<HistoryWithManga>
@Query("SELECT * FROM history WHERE manga_id = :id")
abstract suspend fun getOneOrNull(id: Long): HistoryEntity?
@Query("DELETE FROM history")
abstract suspend fun clear()

View File

@@ -8,6 +8,7 @@ import org.koitharu.kotatsu.core.db.entity.HistoryWithManga
import org.koitharu.kotatsu.core.db.entity.MangaEntity
import org.koitharu.kotatsu.core.model.*
import java.io.Closeable
import java.util.*
class HistoryRepository() : KoinComponent, MangaRepository, Closeable {
@@ -61,6 +62,17 @@ class HistoryRepository() : KoinComponent, MangaRepository, Closeable {
)
}
suspend fun getHistory(manga: Manga): MangaHistory? {
return db.historyDao().getOneOrNull(manga.id)?.let {
MangaHistory(
createdAt = Date(it.createdAt),
updatedAt = Date(it.updatedAt),
chapterId = it.chapterId,
page = it.page
)
}
}
suspend fun clear() {
db.historyDao().clear()
}

View File

@@ -8,8 +8,9 @@ import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.fragment_chapters.*
import moxy.ktx.moxyPresenter
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.model.Manga
import org.koitharu.kotatsu.core.model.MangaChapter
import org.koitharu.kotatsu.core.model.MangaHistory
import org.koitharu.kotatsu.core.model.MangaInfo
import org.koitharu.kotatsu.ui.common.BaseFragment
import org.koitharu.kotatsu.ui.common.list.OnRecyclerItemClickListener
import org.koitharu.kotatsu.ui.reader.ReaderActivity
@@ -20,20 +21,25 @@ class ChaptersFragment : BaseFragment(R.layout.fragment_chapters), MangaDetailsV
@Suppress("unused")
private val presenter by moxyPresenter { (activity as MangaDetailsActivity).presenter }
private var manga: Manga? = null
private var data: MangaInfo<MangaHistory?>? = null
private lateinit var adapter: ChaptersAdapter
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
adapter = ChaptersAdapter(this)
recyclerView_chapters.addItemDecoration(DividerItemDecoration(view.context, RecyclerView.VERTICAL))
recyclerView_chapters.addItemDecoration(
DividerItemDecoration(
view.context,
RecyclerView.VERTICAL
)
)
recyclerView_chapters.adapter = adapter
}
override fun onMangaUpdated(manga: Manga) {
this.manga = manga
adapter.replaceData(manga.chapters.orEmpty())
override fun onMangaUpdated(data: MangaInfo<MangaHistory?>) {
this.data = data
adapter.replaceData(data.manga.chapters.orEmpty())
}
override fun onLoadingStateChanged(isLoading: Boolean) {
@@ -45,10 +51,12 @@ class ChaptersFragment : BaseFragment(R.layout.fragment_chapters), MangaDetailsV
}
override fun onItemClick(item: MangaChapter, position: Int, view: View) {
startActivity(ReaderActivity.newIntent(
context ?: return,
manga ?: return,
item.id
))
startActivity(
ReaderActivity.newIntent(
context ?: return,
data?.manga ?: return,
item.id
)
)
}
}

View File

@@ -8,6 +8,8 @@ import kotlinx.android.synthetic.main.activity_details.*
import moxy.ktx.moxyPresenter
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.model.Manga
import org.koitharu.kotatsu.core.model.MangaHistory
import org.koitharu.kotatsu.core.model.MangaInfo
import org.koitharu.kotatsu.ui.common.BaseActivity
import org.koitharu.kotatsu.utils.ext.getDisplayMessage
@@ -26,8 +28,8 @@ class MangaDetailsActivity : BaseActivity(), MangaDetailsView {
} ?: finish()
}
override fun onMangaUpdated(manga: Manga) {
title = manga.title
override fun onMangaUpdated(data: MangaInfo<MangaHistory?>) {
title = data.manga.title
}
override fun onLoadingStateChanged(isLoading: Boolean) = Unit

View File

@@ -6,7 +6,10 @@ import kotlinx.android.synthetic.main.fragment_details.*
import moxy.ktx.moxyPresenter
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.model.Manga
import org.koitharu.kotatsu.core.model.MangaHistory
import org.koitharu.kotatsu.core.model.MangaInfo
import org.koitharu.kotatsu.ui.common.BaseFragment
import org.koitharu.kotatsu.ui.reader.ReaderActivity
import org.koitharu.kotatsu.utils.ext.setChips
import kotlin.math.roundToInt
@@ -15,24 +18,46 @@ class MangaDetailsFragment : BaseFragment(R.layout.fragment_details), MangaDetai
@Suppress("unused")
private val presenter by moxyPresenter { (activity as MangaDetailsActivity).presenter }
override fun onMangaUpdated(manga: Manga) {
imageView_cover.load(manga.largeCoverUrl ?: manga.coverUrl)
textView_title.text = manga.title
textView_subtitle.text = manga.localizedTitle
textView_description.text = manga.description
if (manga.rating == Manga.NO_RATING) {
override fun onMangaUpdated(data: MangaInfo<MangaHistory?>) {
imageView_cover.load(data.manga.largeCoverUrl ?: data.manga.coverUrl)
textView_title.text = data.manga.title
textView_subtitle.text = data.manga.localizedTitle
textView_description.text = data.manga.description
if (data.manga.rating == Manga.NO_RATING) {
ratingBar.isVisible = false
} else {
ratingBar.progress = (ratingBar.max * manga.rating).roundToInt()
ratingBar.progress = (ratingBar.max * data.manga.rating).roundToInt()
ratingBar.isVisible = true
}
chips_tags.setChips(manga.tags) {
chips_tags.setChips(data.manga.tags) {
create(
text = it.title,
iconRes = R.drawable.ic_chip_tag,
tag = it
)
}
if (data.manga.chapters.isNullOrEmpty()) {
button_read.isEnabled = false
} else {
button_read.isEnabled = true
if (data.extra == null) {
button_read.setText(R.string.read)
button_read.setIconResource(R.drawable.ic_read)
} else {
button_read.setText(R.string.continue_)
button_read.setIconResource(R.drawable.ic_play)
}
val chapterId = data.extra?.chapterId ?: data.manga.chapters.first().id
button_read.setOnClickListener {
startActivity(
ReaderActivity.newIntent(
context ?: return@setOnClickListener,
data.manga,
chapterId
)
)
}
}
}
override fun onLoadingStateChanged(isLoading: Boolean) {

View File

@@ -1,11 +1,14 @@
package org.koitharu.kotatsu.ui.details
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import moxy.InjectViewState
import org.koitharu.kotatsu.BuildConfig
import org.koitharu.kotatsu.core.model.Manga
import org.koitharu.kotatsu.core.model.MangaInfo
import org.koitharu.kotatsu.domain.HistoryRepository
import org.koitharu.kotatsu.domain.MangaProviderFactory
import org.koitharu.kotatsu.ui.common.BasePresenter
@@ -18,14 +21,20 @@ class MangaDetailsPresenter : BasePresenter<MangaDetailsView>() {
if (isLoaded) {
return
}
viewState.onMangaUpdated(manga)
viewState.onMangaUpdated(MangaInfo(manga, null))
launch {
try {
viewState.onLoadingStateChanged(true)
val details = withContext(Dispatchers.IO) {
MangaProviderFactory.create(manga.source).getDetails(manga)
val data = withContext(Dispatchers.IO) {
val details = async {
MangaProviderFactory.create(manga.source).getDetails(manga)
}
val history = async {
HistoryRepository().use { it.getHistory(manga) }
}
MangaInfo(details.await(), history.await())
}
viewState.onMangaUpdated(details)
viewState.onMangaUpdated(data)
isLoaded = true
} catch (e: Exception) {
if (BuildConfig.DEBUG) {

View File

@@ -4,12 +4,13 @@ import moxy.MvpView
import moxy.viewstate.strategy.AddToEndSingleStrategy
import moxy.viewstate.strategy.OneExecutionStateStrategy
import moxy.viewstate.strategy.StateStrategyType
import org.koitharu.kotatsu.core.model.Manga
import org.koitharu.kotatsu.core.model.MangaHistory
import org.koitharu.kotatsu.core.model.MangaInfo
interface MangaDetailsView : MvpView {
@StateStrategyType(AddToEndSingleStrategy::class)
fun onMangaUpdated(manga: Manga)
fun onMangaUpdated(data: MangaInfo<MangaHistory?>)
@StateStrategyType(AddToEndSingleStrategy::class)
fun onLoadingStateChanged(isLoading: Boolean)