Skip to content

Commit 1eaf309

Browse files
committed
Refactor
1 parent 743ca8c commit 1eaf309

File tree

8 files changed

+35
-45
lines changed

8 files changed

+35
-45
lines changed

src/main/kotlin/me/proxer/app/anime/stream/StreamActivity.kt

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import me.proxer.app.anime.resolver.StreamResolutionResult.Video.Companion.NAME_
7474
import me.proxer.app.anime.resolver.StreamResolutionResult.Video.Companion.REFERER_EXTRA
7575
import me.proxer.app.anime.stream.StreamPlayerManager.PlayerState
7676
import me.proxer.app.base.BaseActivity
77+
import me.proxer.app.util.extension.getSafeStringExtra
7778
import me.proxer.app.util.extension.loadRequests
7879
import me.proxer.app.util.extension.logErrors
7980
import me.proxer.app.util.extension.newTask
@@ -100,20 +101,20 @@ class StreamActivity : BaseActivity() {
100101
private const val RESUME_DIALOG_SHOWN = "resume_dialog_shown"
101102
}
102103

103-
internal val id: String?
104-
get() = intent.getStringExtra(ID_EXTRA)
104+
internal val id: String
105+
get() = intent.getSafeStringExtra(ID_EXTRA)
105106

106-
internal val name: String?
107-
get() = intent.getStringExtra(NAME_EXTRA)
107+
internal val name: String
108+
get() = intent.getSafeStringExtra(NAME_EXTRA)
108109

109-
internal val episode: Int?
110-
get() = intent.getIntExtra(EPISODE_EXTRA, -1).let { if (it <= 0) null else it }
110+
internal val episode: Int
111+
get() = intent.getIntExtra(EPISODE_EXTRA, -1).let { if (it <= 0) 1 else it }
111112

112-
internal val language: AnimeLanguage?
113-
get() = intent.getSerializableExtra(LANGUAGE_EXTRA) as? AnimeLanguage
113+
internal val language: AnimeLanguage
114+
get() = intent.getSerializableExtra(LANGUAGE_EXTRA) as AnimeLanguage
114115

115116
internal val coverUri: Uri?
116-
get() = intent.getParcelableExtra<Uri>(COVER_EXTRA)
117+
get() = intent.getParcelableExtra(COVER_EXTRA)
117118

118119
internal val referer: String?
119120
get() = intent.getStringExtra(REFERER_EXTRA)
@@ -343,21 +344,13 @@ class StreamActivity : BaseActivity() {
343344

344345
getSafeCastContext()?.addCastStateListener(castStateListener)
345346

346-
val safeId = id
347-
val safeEpisode = episode
348-
val safeLanguage = language
347+
val lastPosition = storageHelper.getLastAnimePosition(id, episode, language)
349348

350-
if (safeId != null && safeEpisode != null && safeLanguage != null) {
351-
val lastPosition = storageHelper.getLastAnimePosition(safeId, safeEpisode, safeLanguage)
349+
if (lastPosition != null && lastPosition > 0) {
350+
if (!resumeDialogShown) {
351+
resumeDialogShown = true
352352

353-
if (lastPosition != null && lastPosition > 0) {
354-
if (!resumeDialogShown) {
355-
resumeDialogShown = true
356-
357-
StreamResumeDialog.show(this, lastPosition)
358-
} else {
359-
playerManager.play()
360-
}
353+
StreamResumeDialog.show(this, lastPosition)
361354
} else {
362355
playerManager.play()
363356
}
@@ -371,13 +364,10 @@ class StreamActivity : BaseActivity() {
371364

372365
playerManager.pause()
373366

374-
val safeId = id
375-
val safeEpisode = episode
376-
val safeLanguage = language
377367
val lastPosition = playerManager.currentPlayer.currentPosition
378368

379-
if (safeId != null && safeEpisode != null && safeLanguage != null && lastPosition > 0) {
380-
storageHelper.putLastAnimePosition(safeId, safeEpisode, safeLanguage, lastPosition)
369+
if (lastPosition > 0) {
370+
storageHelper.putLastAnimePosition(id, episode, language, lastPosition)
381371
}
382372

383373
super.onStop()
@@ -445,7 +435,7 @@ class StreamActivity : BaseActivity() {
445435
private fun setupUi() {
446436
title = name
447437
supportActionBar?.setDisplayHomeAsUpEnabled(true)
448-
supportActionBar?.subtitle = episode?.let { Category.ANIME.toEpisodeAppString(this, it) }
438+
supportActionBar?.subtitle = Category.ANIME.toEpisodeAppString(this, episode)
449439

450440
coverUri?.also {
451441
GlideApp.with(playerView)

src/main/kotlin/me/proxer/app/anime/stream/StreamPlayerManager.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import com.google.android.gms.cast.MediaInfo
2929
import com.google.android.gms.cast.MediaMetadata
3030
import com.google.android.gms.cast.MediaQueueItem
3131
import com.google.android.gms.common.images.WebImage
32+
import io.reactivex.subjects.BehaviorSubject
3233
import io.reactivex.subjects.PublishSubject
3334
import me.proxer.app.MainApplication.Companion.USER_AGENT
3435
import me.proxer.app.util.DefaultActivityLifecycleCallbacks
@@ -47,7 +48,10 @@ class StreamPlayerManager(context: StreamActivity, rawClient: OkHttpClient, adTa
4748
private const val LAST_POSITION_EXTRA = "last_position"
4849
}
4950

50-
val playerReadySubject = PublishSubject.create<Player>()
51+
private val localPlayer = buildLocalPlayer(context)
52+
private val castPlayer = buildCastPlayer(context)
53+
54+
val playerReadySubject = BehaviorSubject.createDefault<Player>(localPlayer)
5155
val playerStateSubject = PublishSubject.create<PlayerState>()
5256
val errorSubject = PublishSubject.create<ErrorUtils.ErrorAction>()
5357

@@ -126,9 +130,6 @@ class StreamPlayerManager(context: StreamActivity, rawClient: OkHttpClient, adTa
126130

127131
private val client = buildClient(rawClient)
128132

129-
private val localPlayer = buildLocalPlayer(context)
130-
private val castPlayer = buildCastPlayer(context)
131-
132133
private var adsLoader: ImaAdsLoader? = when {
133134
adTag != null -> ImaAdsLoader(context, adTag).apply {
134135
setPlayer(localPlayer)

src/main/kotlin/me/proxer/app/anime/stream/TouchablePlayerView.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@ class TouchablePlayerView @JvmOverloads constructor(
4141
private val audioManager = requireNotNull(context.getSystemService<AudioManager>())
4242
private val notificationManager = requireNotNull(context.getSystemService<NotificationManager>())
4343

44-
private val safePlayer get() = requireNotNull(player)
45-
4644
private val audioStreamType
47-
get() = Util.getStreamTypeForAudioUsage(safePlayer.audioComponent?.audioAttributes?.usage ?: C.USAGE_MEDIA)
45+
get() = Util.getStreamTypeForAudioUsage(player?.audioComponent?.audioAttributes?.usage ?: C.USAGE_MEDIA)
4846

4947
private val canChangeAudio
5048
get() = audioManager.isVolumeFixed.not() ||
@@ -142,7 +140,9 @@ class TouchablePlayerView @JvmOverloads constructor(
142140
}
143141

144142
fun rewind(triggerSubject: Boolean = false) {
145-
if (safePlayer.isCurrentWindowSeekable) {
143+
val safePlayer = player
144+
145+
if (safePlayer != null && safePlayer.isCurrentWindowSeekable) {
146146
safePlayer.seekTo(max(safePlayer.currentPosition - 10_000, 0))
147147

148148
if (triggerSubject) {
@@ -152,7 +152,9 @@ class TouchablePlayerView @JvmOverloads constructor(
152152
}
153153

154154
fun fastForward(triggerSubject: Boolean = false) {
155-
if (safePlayer.isCurrentWindowSeekable) {
155+
val safePlayer = player
156+
157+
if (safePlayer != null && safePlayer.isCurrentWindowSeekable) {
156158
val durationMs = safePlayer.duration
157159

158160
val seekPositionMs = if (durationMs != C.TIME_UNSET) {

src/main/kotlin/me/proxer/app/chat/prv/conference/ConferenceViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ConferenceViewModel(searchQuery: String) : BaseViewModel<List<ConferenceWi
2929
.flatMap {
3030
if (!MessengerWorker.isRunning) MessengerWorker.enqueueSynchronization()
3131

32-
Single.never<List<ConferenceWithMessage>>()
32+
Single.never()
3333
}
3434

3535
var searchQuery: String = searchQuery

src/main/kotlin/me/proxer/app/chat/prv/message/MessengerViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class MessengerViewModel(initialConference: LocalConference) : PagedViewModel<Lo
4646
else -> if (!hasReachedEnd) MessengerWorker.enqueueMessageLoad(safeConference.id)
4747
}
4848

49-
Single.never<List<LocalMessage>>()
49+
Single.never()
5050
}
5151

5252
private val dataSource: (List<LocalMessage>?) -> Unit = {

src/main/kotlin/me/proxer/app/comment/EditCommentViewModel.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package me.proxer.app.comment
22

33
import androidx.lifecycle.MutableLiveData
4-
import com.gojuno.koptional.Optional
54
import com.gojuno.koptional.rxjava2.filterSome
65
import com.gojuno.koptional.toOptional
76
import io.reactivex.Single
@@ -71,10 +70,10 @@ class EditCommentViewModel(
7170
.let { comment ->
7271
when {
7372
comment == null || comment.content.isBlank() && comment.overallRating == 0 -> null
74-
comment.content.length > MAX_LENGTH -> Single.error<Optional<Unit>>(CommentTooLongException())
73+
comment.content.length > MAX_LENGTH -> Single.error(CommentTooLongException())
7574

7675
comment.mediaProgress == UserMediaProgress.WILL_WATCH -> Single
77-
.error<Optional<Unit>>(CommentInvalidProgressException())
76+
.error(CommentInvalidProgressException())
7877

7978
comment.id.isNotEmpty() -> api.comment.update(comment.id)
8079
.comment(comment.content.trim())

src/main/kotlin/me/proxer/app/manga/MangaViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class MangaViewModel(
3939

4040
override val dataSingle: Single<MangaChapterInfo>
4141
get() = Single.fromCallable { validate() }
42-
.flatMap<EntryCore> { entrySingle() }
42+
.flatMap { entrySingle() }
4343
.doOnSuccess {
4444
if (it.isAgeRestricted && !preferenceHelper.isAgeRestrictedMediaAllowed) {
4545
throw AgeConfirmationRequiredException()

src/main/kotlin/me/proxer/app/ui/view/ProxerWebView.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ class ProxerWebView @JvmOverloads constructor(
4848
val showPageSubject = PublishSubject.create<HttpUrl>()
4949
val loadingFinishedSubject = PublishSubject.create<Unit>()
5050

51-
private val client by safeInject<OkHttpClient>()
52-
5351
init {
5452
setBackgroundColor(Color.TRANSPARENT)
5553
setInitialScale(1)

0 commit comments

Comments
 (0)