Skip to content

Commit

Permalink
Added Episode duration tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
kl3jvi committed Dec 5, 2021
1 parent 0fdef23 commit 3107704
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GetEpisodeInfoUseCase @Inject constructor(
private val playerRepository: PlayerRepository,
private val ioDispatcher: CoroutineDispatcher
) {
fun fetchEpisodeMediaUrl(url: String) = flow {
operator fun invoke(url: String) = flow {
emit(Resource.Loading())
try {
val response = HtmlParser.parseMediaUrl(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.kl3jvi.animity.domain

import com.kl3jvi.animity.model.AnimeMetaModel
import com.kl3jvi.animity.repository.SearchRepository
import com.kl3jvi.animity.utils.Constants
import com.kl3jvi.animity.utils.Resource
import com.kl3jvi.animity.repository.SearchRepository
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
Expand All @@ -19,37 +19,39 @@ class GetSearchResultUseCase @Inject constructor(
private val ioDispatcher: CoroutineDispatcher
) {

fun getSearchData(searchQuery: String): Flow<Resource<List<AnimeMetaModel>>> = flow {
try {
emit(Resource.Loading())
val response =
Constants.parseList(
searchRepository.fetchSearchData(
Constants.getHeader(),
searchQuery,
1
).string(),
Constants.TYPE_MOVIE
).toList()
emit(
Resource.Success(
data = response
operator fun invoke(searchQuery: String): Flow<Resource<List<AnimeMetaModel>>> =
flow {
try {
emit(Resource.Loading())
val response =
Constants.parseList(
searchRepository.fetchSearchData(
Constants.getHeader(),
searchQuery,
1
).string(),
Constants.TYPE_MOVIE
).toList()
emit(
Resource.Success(
data = response
)
)
)
} catch (e: HttpException) {
emit(
Resource.Error(
message = e.localizedMessage ?: "An unexpected error occurred",
} catch (e: HttpException) {
emit(
Resource.Error(
message = e.localizedMessage ?: "An unexpected error occurred",
)
)
)
} catch (e: IOException) {
emit(
Resource.Error(
e.localizedMessage ?: "Couldn't reach server. Check your internet connection.",
} catch (e: IOException) {
emit(
Resource.Error(
e.localizedMessage
?: "Couldn't reach server. Check your internet connection.",

)
)
}
}.flowOn(ioDispatcher)
)
)
}
}.flowOn(ioDispatcher)

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface EpisodeDao {
suspend fun insertEpisode(content: Content)

@Query("SELECT * FROM Content WHERE episodeUrl =:episodeUrl")
fun getEpisodeContent(episodeUrl: String): Content
suspend fun getEpisodeContent(episodeUrl: String): Content

@Update
suspend fun updateEpisode(content: Content)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class PlayerActivity : AppCompatActivity() {
lateinit var episodeNumberLocal: String
lateinit var episodeUrlLocal: String
lateinit var content: Content


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(viewBinding.root)
Expand All @@ -80,7 +82,6 @@ class PlayerActivity : AppCompatActivity() {
initialisePlayerLayout()
viewModel.updateEpisodeUrl(getIntentData?.episodeUrl.toString())


}
}

Expand Down Expand Up @@ -154,6 +155,7 @@ class PlayerActivity : AppCompatActivity() {
exoPlayer.seekTo(playbackPosition)
exoPlayer.prepare()
}

player!!.addListener(object : Player.Listener {
override fun onPlayerStateChanged(
playWhenReady: Boolean,
Expand Down Expand Up @@ -206,12 +208,18 @@ class PlayerActivity : AppCompatActivity() {
})
}

override fun onBackPressed() {
super.onBackPressed()
if (::content.isInitialized)
insertEpisodeToDatabase(content.copy(watchedDuration = player!!.currentPosition))
}

private fun initialisePlayerLayout() {

val backButton = viewBinding.videoView.findViewById<ImageView>(R.id.back)
backButton.setOnClickListener {
insertEpisodeToDatabase(content.copy(watchedDuration = player!!.currentPosition))
if (::content.isInitialized)
insertEpisodeToDatabase(content.copy(watchedDuration = player!!.currentPosition))
finish()
}

Expand All @@ -237,11 +245,6 @@ class PlayerActivity : AppCompatActivity() {
}
}

private fun getVideoDurationSeconds(player: ExoPlayer): Long {
val timeMs = player.duration
return timeMs / 1000
}

private fun showQualityDialog() {
mappedTrackInfo = trackSelector?.currentMappedTrackInfo
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PlayerViewModel @Inject constructor(

@ExperimentalCoroutinesApi
val videoUrlLiveData = Transformations.switchMap(_vidUrl) { url ->
getEpisodeInfoUseCase.fetchEpisodeMediaUrl(url).flatMapLatest { episodeInfo ->
getEpisodeInfoUseCase(url).flatMapLatest { episodeInfo ->
getEpisodeInfoUseCase.fetchM3U8(episodeInfo.data?.vidCdnUrl)
}.asLiveData()
}
Expand All @@ -53,4 +53,5 @@ class PlayerViewModel @Inject constructor(
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ class DetailsFragment : Fragment() {
diskCachePolicy(CachePolicy.ENABLED)
}
episodeListRecycler.layoutManager = LinearLayoutManager(requireContext())
episodeAdapter =
CustomEpisodeAdapter(this@DetailsFragment, animeInfo.title)
episodeAdapter = CustomEpisodeAdapter(this@DetailsFragment, animeInfo.title)
resultTitle.text = animeInfo.title

title = animeInfo.title
Expand Down Expand Up @@ -129,6 +128,7 @@ class DetailsFragment : Fragment() {
}
}
info.genre.forEach { data ->
binding.genreGroup.removeAllViews()
val chip = Chip(requireContext())
chip.apply {
text = data.genreName
Expand Down Expand Up @@ -234,7 +234,6 @@ class DetailsFragment : Fragment() {
super.onResume()
if (requireActivity() is MainActivity) {
(activity as MainActivity?)?.hideBottomNavBar()
episodeAdapter
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,9 @@ class DetailsViewModel @Inject constructor(
}.asLiveData(Dispatchers.Default + viewModelScope.coroutineContext)
}




@ExperimentalCoroutinesApi
val downloadEpisodeUrl = Transformations.switchMap(_downloadUrl) { url ->
getEpisodeInfoUseCase.fetchEpisodeMediaUrl(url).flatMapLatest { episodeInfo ->
getEpisodeInfoUseCase(url).flatMapLatest { episodeInfo ->
getEpisodeInfoUseCase.fetchM3U8(episodeInfo.data?.vidCdnUrl)
}.asLiveData()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ package com.kl3jvi.animity.ui.fragments.search
import androidx.lifecycle.*
import com.kl3jvi.animity.domain.GetSearchResultUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.CoroutineDispatcher
import javax.inject.Inject

@HiltViewModel
class SearchViewModel @Inject constructor(
private val getSearchResultUseCase: GetSearchResultUseCase,
private val ioDispatcher: CoroutineDispatcher
) : ViewModel() {

private val _query = MutableLiveData<String>()

val searchResult = Transformations.switchMap(_query) { query ->
getSearchResultUseCase.getSearchData(query)
.asLiveData(Dispatchers.IO + viewModelScope.coroutineContext)
getSearchResultUseCase(query).asLiveData(ioDispatcher + viewModelScope.coroutineContext)
}

fun passQuery(query: String) {
_query.value = query
}
}
}

0 comments on commit 3107704

Please sign in to comment.