-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
why
committed
Dec 5, 2023
1 parent
6092446
commit ca28e4a
Showing
33 changed files
with
578 additions
and
188 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
app/src/main/java/com/xiaoyv/bangumi/base/BaseListStubFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.xiaoyv.bangumi.base | ||
|
||
import androidx.annotation.CallSuper | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import com.chad.library.adapter.base.BaseDifferAdapter | ||
import com.chad.library.adapter.base.QuickAdapterHelper | ||
import com.chad.library.adapter.base.loadState.trailing.TrailingLoadStateAdapter | ||
import com.xiaoyv.bangumi.databinding.FragmentListBinding | ||
import com.xiaoyv.blueprint.base.mvvm.normal.BaseViewModelFragment | ||
import com.xiaoyv.blueprint.kts.toJson | ||
import com.xiaoyv.common.kts.GoogleAttr | ||
import com.xiaoyv.common.kts.debugLog | ||
import com.xiaoyv.common.widget.scroll.AnimeLinearLayoutManager | ||
import com.xiaoyv.widget.binder.BaseQuickDiffBindingAdapter | ||
import com.xiaoyv.widget.kts.getAttrColor | ||
|
||
/** | ||
* Class: [BaseListStubFragment] | ||
* | ||
* @author why | ||
* @since 11/29/23 | ||
*/ | ||
abstract class BaseListStubFragment<T, VM : BaseListViewModel<T>> : | ||
BaseViewModelFragment<FragmentListBinding, VM>() { | ||
|
||
abstract val isOnlyOnePage: Boolean | ||
|
||
internal val contentAdapter: BaseDifferAdapter<T, *> by lazy { | ||
onCreateContentAdapter() | ||
} | ||
|
||
private val adapterHelper by lazy { | ||
QuickAdapterHelper.Builder(contentAdapter) | ||
.setTrailingLoadStateAdapter(object : TrailingLoadStateAdapter.OnTrailingListener { | ||
override fun isAllowLoading(): Boolean { | ||
return binding.srlRefresh.isRefreshing.not() && isOnlyOnePage.not() | ||
} | ||
|
||
override fun onFailRetry() { | ||
viewModel.loadMore() | ||
} | ||
|
||
override fun onLoad() { | ||
viewModel.loadMore() | ||
} | ||
}) | ||
.build() | ||
} | ||
|
||
internal open val hasFixedSize = false | ||
|
||
internal open val layoutManager: LinearLayoutManager? | ||
get() = binding.rvContent.layoutManager as? LinearLayoutManager | ||
|
||
abstract fun onCreateContentAdapter(): BaseQuickDiffBindingAdapter<T, *> | ||
|
||
@CallSuper | ||
override fun initView() { | ||
binding.rvContent.setHasFixedSize(hasFixedSize) | ||
binding.srlRefresh.initRefresh { viewModel.isRefresh } | ||
binding.srlRefresh.setColorSchemeColors(hostActivity.getAttrColor(GoogleAttr.colorPrimary)) | ||
} | ||
|
||
@CallSuper | ||
override fun initData() { | ||
binding.rvContent.layoutManager = onCreateLayoutManager() | ||
if (isOnlyOnePage) { | ||
binding.rvContent.adapter = contentAdapter | ||
} else { | ||
binding.rvContent.adapter = adapterHelper.adapter | ||
} | ||
viewModel.refresh() | ||
} | ||
|
||
open fun onCreateLayoutManager(): LinearLayoutManager { | ||
return AnimeLinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false) | ||
} | ||
|
||
@CallSuper | ||
override fun initListener() { | ||
binding.srlRefresh.setOnRefreshListener { | ||
viewModel.refresh() | ||
} | ||
} | ||
|
||
@CallSuper | ||
override fun LifecycleOwner.initViewObserver() { | ||
viewModel.onListLiveData.observe(this) { | ||
debugLog { "List:\n " + it.toJson(true) } | ||
|
||
contentAdapter.submitList(it.orEmpty()) { | ||
if (viewModel.isRefresh) { | ||
layoutManager?.scrollToPositionWithOffset(0, 0) | ||
} | ||
|
||
adapterHelper.trailingLoadState = viewModel.loadingMoreState | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 36 additions & 5 deletions
41
app/src/main/java/com/xiaoyv/bangumi/ui/feature/person/character/PersonCharacterFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 13 additions & 2 deletions
15
app/src/main/java/com/xiaoyv/bangumi/ui/feature/person/character/PersonCharacterViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,24 @@ | ||
package com.xiaoyv.bangumi.ui.feature.person.character | ||
|
||
import com.xiaoyv.blueprint.base.mvvm.normal.BaseViewModel | ||
import com.xiaoyv.bangumi.base.BaseListViewModel | ||
import com.xiaoyv.common.api.BgmApiManager | ||
import com.xiaoyv.common.api.parser.entity.CharacterEntity | ||
import com.xiaoyv.common.api.parser.impl.parserPersonVoices | ||
|
||
/** | ||
* Class: [PersonCharacterViewModel] | ||
* | ||
* @author why | ||
* @since 12/4/23 | ||
*/ | ||
class PersonCharacterViewModel : BaseViewModel() { | ||
class PersonCharacterViewModel : BaseListViewModel<CharacterEntity>() { | ||
/** | ||
* 人物ID和是否为虚拟人物 | ||
*/ | ||
internal var personId: String = "" | ||
internal var isVirtual: Boolean = false | ||
|
||
override suspend fun onRequestListImpl(): List<CharacterEntity> { | ||
return BgmApiManager.bgmWebApi.queryPersonWorkVoices(personId).parserPersonVoices() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.