Skip to content

Commit

Permalink
feat: implement anilib repository
Browse files Browse the repository at this point in the history
1. By the way change database structure for compatibility with anilib
2. Add "watch" button to the episodes bottom sheet
3. Rename AniLibria repository to just Libria
4. Episodes fetching optimization
  • Loading branch information
urFate committed Apr 15, 2024
1 parent bec10da commit ef20d4b
Show file tree
Hide file tree
Showing 28 changed files with 823 additions and 348 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ dependencies {
// Compose UI
implementation ("de.mr-pine.utils:zoomables:1.4.0")
implementation ("androidx.compose.material3:material3:1.2.1")
implementation ("androidx.compose.material:material:1.6.5")
implementation ("androidx.compose.material:material-icons-extended:$composeVersion")
implementation ("androidx.navigation:navigation-compose:2.7.7")
implementation ("com.google.accompanist:accompanist-systemuicontroller:0.30.1")
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/live/shirabox/shirabox/db/Converters.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package live.shirabox.shirabox.db
import androidx.room.TypeConverter
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import live.shirabox.core.model.ActingTeam
import live.shirabox.core.model.ContentType
import live.shirabox.core.model.Quality

Expand Down Expand Up @@ -69,4 +70,14 @@ class Converters {
return Json.decodeFromString(videoMarkers)
}

@TypeConverter
fun encodeActingTeam(actingTeam: ActingTeam): String {
return Json.encodeToString(actingTeam)
}

@TypeConverter
fun decodeActingTeam(actingTeam: String): ActingTeam {
return Json.decodeFromString<ActingTeam>(actingTeam)
}

}
6 changes: 3 additions & 3 deletions app/src/main/java/live/shirabox/shirabox/db/dao/ContentDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import androidx.room.Transaction
import androidx.room.Update
import kotlinx.coroutines.flow.Flow
import live.shirabox.core.entity.ContentEntity
import live.shirabox.core.entity.relation.CollectedContent
import live.shirabox.core.entity.relation.CombinedContent

@Dao
interface ContentDao {
@Transaction
@Query("SELECT * FROM content")
fun allCollectedContent(): Flow<List<ContentEntity>>
fun allCombinedContent(): Flow<List<ContentEntity>>

@Query("SELECT * FROM content WHERE favourite IS 1")
fun getFavourites(): Flow<List<ContentEntity>>

@Transaction
@Query("SELECT * FROM content WHERE shikimori_id IS :shikimoriId")
fun collectedContent(shikimoriId: Int): CollectedContent
fun combinedContent(shikimoriId: Int): CombinedContent

@Query("SELECT * FROM content WHERE shikimori_id IS :shikimoriId")
fun getContent(shikimoriId: Int): ContentEntity?
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/live/shirabox/shirabox/db/dao/EpisodeDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.room.Query
import androidx.room.Update
import kotlinx.coroutines.flow.Flow
import live.shirabox.core.entity.EpisodeEntity
import live.shirabox.core.model.ActingTeam

@Dao
interface EpisodeDao {
Expand All @@ -17,8 +18,11 @@ interface EpisodeDao {
@Query("SELECT * FROM episode WHERE content_uid = :contentUid AND episode = :episode LIMIT 1")
fun getEpisodeByParentAndEpisode(contentUid: Long, episode: Int): EpisodeEntity

@Query("SELECT * FROM episode WHERE content_uid = :contentUid")
fun getEpisodesByParent(contentUid: Long): Flow<List<EpisodeEntity>>
@Query("SELECT * FROM episode WHERE content_uid = :contentUid AND acting_team = :actingTeam AND episode = :episode LIMIT 1")
fun getEpisode(contentUid: Long, episode: Int, actingTeam: ActingTeam): EpisodeEntity

@Query("SELECT * FROM episode WHERE content_uid = :contentUid AND acting_team = :actingTeam")
fun getEpisodes(contentUid: Long, actingTeam: ActingTeam): Flow<List<EpisodeEntity>>

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertEpisodes(vararg episodeEntity: EpisodeEntity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class PlayerActivity : ComponentActivity() {
contentUid = arguments!!.getLong("content_uid"),
contentName = arguments.getString("name").toString(),
contentEnName = arguments.getString("en_name").toString(),
actingTeam = Json.decodeFromString(arguments.getString("acting_team") ?: ""),
episode = arguments.getInt("episode"),
startIndex = arguments.getInt("start_index"),
playlist = Json.decodeFromString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fun ControlsScaffold(exoPlayer: ExoPlayer, model: PlayerViewModel) {
var currentPosition by remember { mutableLongStateOf(exoPlayer.currentPosition) }
var totalDuration by remember { mutableLongStateOf(exoPlayer.duration) }
var playbackState by remember { mutableIntStateOf(exoPlayer.playbackState) }
var hasNextMediaItem by remember { mutableStateOf(exoPlayer.hasNextMediaItem()) }
var hasNextMediaItem by remember { mutableStateOf(model.playlist.lastIndex != exoPlayer.currentMediaItemIndex) }
var hasPreviousMediaItem by remember { mutableStateOf(exoPlayer.hasPreviousMediaItem()) }
var currentMediaItemIndex by remember { mutableIntStateOf(exoPlayer.currentMediaItemIndex) }

Expand Down Expand Up @@ -147,11 +147,11 @@ fun ControlsScaffold(exoPlayer: ExoPlayer, model: PlayerViewModel) {
totalDuration = exoPlayer.duration
currentPosition = exoPlayer.contentPosition
playbackState = exoPlayer.playbackState
hasNextMediaItem = exoPlayer.hasNextMediaItem()
hasNextMediaItem = model.playlist.lastIndex != exoPlayer.currentMediaItemIndex
hasPreviousMediaItem = exoPlayer.hasPreviousMediaItem()
currentMediaItemIndex = exoPlayer.currentMediaItemIndex + 1
currentMediaItemIndex = exoPlayer.currentMediaItemIndex.inc()

delay(400)
delay(200)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.launch
import live.shirabox.core.datastore.AppDataStore
import live.shirabox.core.datastore.DataStoreScheme
import live.shirabox.core.model.ActingTeam
import live.shirabox.core.model.PlaylistVideo
import live.shirabox.data.animeskip.AnimeSkipRepository
import live.shirabox.shirabox.db.AppDatabase
Expand All @@ -26,6 +27,7 @@ class PlayerViewModel(
val contentUid: Long,
val contentName: String,
val contentEnName: String,
val actingTeam: ActingTeam,
val episode: Int,
val startIndex: Int,
val playlist: List<PlaylistVideo>
Expand All @@ -43,15 +45,15 @@ class PlayerViewModel(
fun saveEpisodePosition(episode: Int, time: Long) {
viewModelScope.launch(Dispatchers.IO) {
val episodeEntity =
db?.episodeDao()?.getEpisodeByParentAndEpisode(contentUid, episode)
db?.episodeDao()?.getEpisode(contentUid, episode, actingTeam)

episodeEntity?.let { db?.episodeDao()?.updateEpisodes(it.copy(watchingTime = time)) }
}
}

fun fetchEpisodePositions() {
viewModelScope.launch(Dispatchers.IO) {
db?.episodeDao()?.getEpisodesByParent(contentUid)?.collect { entityList ->
db?.episodeDao()?.getEpisodes(contentUid, actingTeam)?.collect { entityList ->
episodesPositions.putAll(entityList.associate {
it.episode to it.watchingTime
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.outlined.ArrowBack
import androidx.compose.material.icons.filled.Favorite
Expand All @@ -38,6 +39,9 @@ import androidx.compose.material.icons.outlined.LiveTv
import androidx.compose.material.icons.outlined.MoreVert
import androidx.compose.material.icons.outlined.MovieCreation
import androidx.compose.material.icons.outlined.PlayArrow
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.CircularProgressIndicator
Expand Down Expand Up @@ -130,7 +134,9 @@ class ResourceActivity : ComponentActivity() {
}
}

@OptIn(ExperimentalMaterial3Api::class, ExperimentalLayoutApi::class)
@OptIn(ExperimentalMaterial3Api::class, ExperimentalLayoutApi::class,
ExperimentalMaterialApi::class
)
@Composable
fun Resource(
id: Int,
Expand All @@ -149,6 +155,7 @@ fun Resource(
}
}
val isFavourite = model.isFavourite.value
val isRefreshing = model.isRefreshing.value

val isReady = remember(content) {
content != null
Expand All @@ -158,6 +165,13 @@ fun Resource(
mutableStateOf(false)
}

val pullRefreshState = rememberPullRefreshState(
refreshing = isRefreshing,
onRefresh = {
model.content.value?.let { model.refresh(it) }
}
)

LaunchedEffect(Unit) {
model.fetchContent(id)
model.fetchRelated(id)
Expand Down Expand Up @@ -205,6 +219,7 @@ fun Resource(
Column(
modifier = Modifier
.fillMaxSize()
.pullRefresh(pullRefreshState)
.verticalScroll(rememberScrollState()),
) {
Box {
Expand Down Expand Up @@ -533,6 +548,16 @@ fun Resource(
Spacer(Modifier.height(56.dp))
}

Box(
modifier = Modifier.padding(64.dp).fillMaxSize(),
contentAlignment = Alignment.TopCenter
) {
PullRefreshIndicator(
refreshing = isRefreshing,
state = pullRefreshState
)
}

ResourceBottomSheet(
content = content,
model = model,
Expand Down Expand Up @@ -584,6 +609,7 @@ fun CommentComponent(username: String, avatar: String, timestamp: String, text:
supportingContent = { Text(text) },
coverImage = avatar,
clickable = false,
headlineText = username,
trailingIcon = null,
)
}
Expand Down
Loading

0 comments on commit ef20d4b

Please sign in to comment.