viewmodel fix I think + small fixes

This commit is contained in:
LagradOst 2021-11-05 22:39:56 +01:00
parent 9b4690f1bb
commit 7cb545f7c9
7 changed files with 50 additions and 30 deletions

View file

@ -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
@ -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)
}

View file

@ -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
@ -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
@ -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
@ -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)
@ -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
@ -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 ->
@ -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

View file

@ -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
@ -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
@ -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)
}
@ -1161,6 +1161,7 @@ class ResultFragment : Fragment() {
}
if (restart || viewModel.resultResponse.value == null) {
viewModel.clear()
viewModel.load(ctx, tempUrl, apiName, showFillers)
}
}

View file

@ -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()
@ -55,7 +71,7 @@ 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()
@ -63,10 +79,10 @@ class ResultViewModel : ViewModel() {
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()
@ -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)
}
}

View file

@ -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))
}
}

View file

@ -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"/>

View file

@ -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>
@ -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>