Skip to content

Commit

Permalink
viewmodel fix I think + small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
LagradOst committed Nov 5, 2021
1 parent 9b4690f commit 7cb545f
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import android.widget.FrameLayout
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.fragment.app.activityViewModels
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
Expand Down Expand Up @@ -97,15 +97,15 @@ class HomeFragment : Fragment() {
}
}

private lateinit var homeViewModel: HomeViewModel
private val homeViewModel: HomeViewModel by activityViewModels()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
homeViewModel =
ViewModelProvider(this).get(HomeViewModel::class.java)
//homeViewModel =
// ViewModelProvider(this).get(HomeViewModel::class.java)

return inflater.inflate(R.layout.fragment_home, container, false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import android.widget.Toast.LENGTH_SHORT
import androidx.appcompat.app.AlertDialog
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.fragment.app.activityViewModels
import androidx.preference.PreferenceManager
import androidx.transition.Fade
import androidx.transition.Transition
Expand Down Expand Up @@ -86,7 +86,6 @@ import com.lagradost.cloudstream3.utils.CastHelper.startCast
import com.lagradost.cloudstream3.utils.DataStore.getKey
import com.lagradost.cloudstream3.utils.DataStore.setKey
import com.lagradost.cloudstream3.utils.DataStoreHelper.setLastWatched
import com.lagradost.cloudstream3.utils.DataStoreHelper.setViewPos
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showDialog
import com.lagradost.cloudstream3.utils.UIHelper.colorFromAttribute
import com.lagradost.cloudstream3.utils.UIHelper.getNavigationBarHeight
Expand Down Expand Up @@ -248,7 +247,7 @@ class PlayerFragment : Fragment() {

private var isFullscreen = false
private var isPlayerPlaying = true
private lateinit var viewModel: ResultViewModel
private val viewModel: ResultViewModel by activityViewModels()
private lateinit var playerData: PlayerData
private lateinit var uriData: UriData
private var isDownloadedFile = false
Expand Down Expand Up @@ -791,20 +790,20 @@ class PlayerFragment : Fragment() {
if (this::exoPlayer.isInitialized) {
if (exoPlayer.duration > 0 && exoPlayer.currentPosition > 0) {
context?.let { ctx ->
if (this::viewModel.isInitialized) {
//if (this::viewModel.isInitialized) {
viewModel.setViewPos(
ctx,
if (isDownloadedFile) uriData.id else getEpisode()?.id,
exoPlayer.currentPosition,
exoPlayer.duration
)
} else {
/*} else {
ctx.setViewPos(
if (isDownloadedFile) uriData.id else getEpisode()?.id,
exoPlayer.currentPosition,
exoPlayer.duration
)
}
}*/

if (isDownloadedFile) {
ctx.setLastWatched(uriData.parentId, uriData.id, uriData.episode, uriData.season, true)
Expand Down Expand Up @@ -880,6 +879,7 @@ class PlayerFragment : Fragment() {
val isClick = !isLocked

exo_play?.isClickable = isClick
sources_btt?.isClickable = isClick
exo_pause?.isClickable = isClick
exo_ffwd?.isClickable = isClick
exo_rew?.isClickable = isClick
Expand Down Expand Up @@ -1055,8 +1055,9 @@ class PlayerFragment : Fragment() {
showToast(activity, resizeModes[resizeMode].second, LENGTH_SHORT)
}
PlayerEventType.ShowSpeed.value -> {
val speedsText = listOf("0.5x", "0.75x", "1x", "1.25x", "1.5x", "1.75x", "2x")
val speedsNumbers = listOf(0.5f, 0.75f, 1f, 1.25f, 1.5f, 1.75f, 2f)
val speedsText =
listOf("0.5x", "0.75x", "0.85x", "1x", "1.15x", "1.25x", "1.4x", "1.5x", "1.75x", "2x")
val speedsNumbers = listOf(0.5f, 0.75f, 0.85f, 1f, 1.15f, 1.25f, 1.4f, 1.5f, 1.75f, 2f)
val speedIndex = speedsNumbers.indexOf(playbackSpeed)

context?.let { ctx ->
Expand Down Expand Up @@ -1383,7 +1384,7 @@ class PlayerFragment : Fragment() {
}

if (!isDownloadedFile) {
viewModel = ViewModelProvider(activity ?: this).get(ResultViewModel::class.java)
//viewModel = ViewModelProvider(activity ?: this).get(ResultViewModel::class.java)

observeDirectly(viewModel.episodes) { _episodes ->
episodes = _episodes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import androidx.core.text.color
import androidx.core.view.isVisible
import androidx.core.widget.NestedScrollView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.fragment.app.activityViewModels
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
Expand Down Expand Up @@ -165,7 +165,7 @@ class ResultFragment : Fragment() {
}

private var currentLoadingCount = 0 // THIS IS USED TO PREVENT LATE EVENTS, AFTER DISMISS WAS CLICKED
private lateinit var viewModel: ResultViewModel
private val viewModel: ResultViewModel by activityViewModels()
private var allEpisodes: HashMap<Int, ArrayList<ExtractorLink>> = HashMap()
private var allEpisodesSubs: HashMap<Int, ArrayList<SubtitleFile>> = HashMap()
private var currentHeaderName: String? = null
Expand All @@ -178,8 +178,8 @@ class ResultFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
viewModel =
ViewModelProvider(activity ?: this).get(ResultViewModel::class.java)
// viewModel =
// ViewModelProvider(activity ?: this).get(ResultViewModel::class.java)
return inflater.inflate(R.layout.fragment_result, container, false)
}

Expand Down Expand Up @@ -1161,6 +1161,7 @@ class ResultFragment : Fragment() {
}

if (restart || viewModel.resultResponse.value == null) {
viewModel.clear()
viewModel.load(ctx, tempUrl, apiName, showFillers)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ const val EPISODE_RANGE_SIZE = 50
const val EPISODE_RANGE_OVERLOAD = 60

class ResultViewModel : ViewModel() {
fun clear() {
repo = null
_resultResponse.value = null
_episodes.value = null
episodeById.value = null
_publicEpisodes.value = null
_publicEpisodesCount.value = null
_rangeOptions.value = null
selectedRange.value = null
selectedRangeInt.value = null
_dubStatus.value = null
id.value = null
selectedSeason.value = -2
_dubSubEpisodes.value = null
}

private var repo: APIRepository? = null

private val _resultResponse: MutableLiveData<Resource<Any?>> = MutableLiveData()
Expand All @@ -55,18 +71,18 @@ class ResultViewModel : ViewModel() {
val publicEpisodes: LiveData<Resource<List<ResultEpisode>>> get() = _publicEpisodes
val publicEpisodesCount: LiveData<Int> get() = _publicEpisodesCount

val dubStatus: MutableLiveData<DubStatus> get() = _dubStatus
val dubStatus: LiveData<DubStatus> get() = _dubStatus
private val _dubStatus: MutableLiveData<DubStatus> = MutableLiveData()

private val page: MutableLiveData<LoadResponse> = MutableLiveData()
val id: MutableLiveData<Int> = MutableLiveData()
val selectedSeason: MutableLiveData<Int> = MutableLiveData(-2)
val seasonSelections: MutableLiveData<List<Int?>> = MutableLiveData()

val dubSubSelections: MutableLiveData<Set<DubStatus>> get() = _dubSubSelections
val dubSubSelections: LiveData<Set<DubStatus>> get() = _dubSubSelections
private val _dubSubSelections: MutableLiveData<Set<DubStatus>> = MutableLiveData()

val dubSubEpisodes: MutableLiveData<Map<DubStatus, List<ResultEpisode>>?> get() = _dubSubEpisodes
val dubSubEpisodes: LiveData<Map<DubStatus, List<ResultEpisode>>?> get() = _dubSubEpisodes
private val _dubSubEpisodes: MutableLiveData<Map<DubStatus, List<ResultEpisode>>?> = MutableLiveData()

private val _watchStatus: MutableLiveData<WatchType> = MutableLiveData()
Expand Down Expand Up @@ -180,7 +196,7 @@ class ResultViewModel : ViewModel() {

fun changeDubStatus(context: Context, status: DubStatus?) {
dubSubEpisodes.value?.get(status)?.let { episodes ->
dubStatus.postValue(status)
_dubStatus.postValue(status)
updateEpisodes(context, null, episodes, null)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ object AppUtils {
return ""
}

//private val viewModel: ResultViewModel by activityViewModels()

fun AppCompatActivity.loadResult(url: String, apiName: String, startAction: Int = 0, startValue: Int = 0) {
this.runOnUiThread {
viewModelStore.clear()
// viewModelStore.clear()
this.navigate(R.id.global_to_navigation_results, ResultFragment.newInstance(url, apiName, startAction, startValue))
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_baseline_play_arrow_24.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<vector android:height="24dp" android:tint="#FFFFFF"
<vector android:height="24dp" android:tint="?attr/white"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M8,5v14l11,-7z"/>
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@
<FrameLayout
android:id="@+id/home_loading_statusbar"
android:layout_width="match_parent"
android:layout_height="60dp">
android:layout_height="70dp">
<ImageView
android:id="@+id/home_change_api_loading"
android:layout_margin="10dp"
android:layout_gravity="center_vertical|end"
android:background="?android:attr/selectableItemBackgroundBorderless"

android:src="@drawable/ic_outline_settings_24"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/ic_baseline_keyboard_arrow_down_24"
android:layout_width="30dp"
android:layout_height="30dp"
android:contentDescription="@string/home_change_provider_img_des">
</ImageView>
</FrameLayout>
Expand Down Expand Up @@ -146,9 +146,9 @@
android:layout_gravity="center|end"
android:background="?android:attr/selectableItemBackgroundBorderless"

android:src="@drawable/ic_outline_settings_24"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/ic_baseline_keyboard_arrow_down_24"
android:layout_width="30dp"
android:layout_height="30dp"
android:contentDescription="@string/home_change_provider_img_des">
<requestFocus/>
</ImageView>
Expand Down

0 comments on commit 7cb545f

Please sign in to comment.