2

MVP アーキテクチャでページング ライブラリを使用しています。dataSource はインタラクターで作成され、以下に示すように Fragment で dataSource を無効にしようとしています。

マイフラグメント

class MyFragment : BaseFragment<MyFragment>(), MyView {

@Inject
lateinit var mPresenter: ActivePrescriptionPresenter<ActivePrescriptionView>
private var isRefreshRequest = false // Used to handle the visibility of toast message and Swipe to Refresh layout when Swipe to refresh is used

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_active_prescription, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    initDagger()
    mPresenter.setView(this)

    initializeActivePrescriptionList(pId)


    // Swipe to refresh
    srlActivePrescription.setOnRefreshListener {
        isRefreshRequest = true
        context?.toast("Refreshing")
       mPresenter.getActivePrescriptionLiveData().value?.dataSource?.invalidate()
    }

}

private fun initializeActivePrescriptionList(patientId: Int) {

    mPresenter.getActivePrescriptionLiveData().observe(this, Observer<PagedList<PrescriptionResponse.Data.Result>> { pagedList ->
        activePrescriptionAdapter.submitList(pagedList)

        // Show toast message if the swipe to refresh was used
        if (isRefreshRequest) {
            srlActivePrescription.isRefreshing = false
            context?.toast(getString(R.string.text_refreshed))
            isRefreshRequest = false
        }
    })

    mPresenter.getNetworkState().observe(this, Observer<NetworkState> { networkState ->
        activePrescriptionAdapter.setNetworkState(networkState)
    })
}
}

MyPresenterImpl

class MyPresenterImpl @Inject constructor(var mInteractor: myInteractor) : MyPresenter<MyView> {

var mView: WeakReference<MyView>? = null

override fun setView(view: MyView?) {
    mView = WeakReference<MyView>(view)
}

override fun getNetworkState(): LiveData<NetworkState> {
    return mInteractor.getNetworkState()
}

override fun getActivePrescriptionLiveData(): LiveData<PagedList<PrescriptionResponse.Data.Result>> {
    return mInteractor.getActivePrescriptionLiveData()
}
}

MyInteractorImpl

class MyInteractorImpl(val activePrescriptionService: ActivePrescriptionService,
                                   val networkUtils: NetworkUtils<PrescriptionResponse.Data>,
                                   val networkExecutor: Executor,
                                   val pagingConfig: PagedList.Config)
: MyInteractor {

private var activePrescriptionLiveData: LiveData<PagedList<PrescriptionResponse.Data.Result>>
private var networkStateLiveData: LiveData<NetworkState>

companion object {
    lateinit var activePrescriptionDataSource: ActivePrescriptionDataSource
}

init {
    activePrescriptionDataSource = ActivePrescriptionDataSource(activePrescriptionService, networkUtils, networkExecutor)
    val dataSourceFactory = object : DataSource.Factory<Int, PrescriptionResponse.Data.Result>() {
        override fun create(): DataSource<Int, PrescriptionResponse.Data.Result> {
            return activePrescriptionDataSource
        }
    }
    val pagedListBuilder = LivePagedListBuilder<Int, PrescriptionResponse.Data.Result>(dataSourceFactory, pagingConfig)

    activePrescriptionLiveData = pagedListBuilder.build()
    networkStateLiveData = activePrescriptionDataSource.getNetworkStateLiveData()
}

override fun getNetworkState(): LiveData<NetworkState> {
    return networkStateLiveData
}

override fun getActivePrescriptionLiveData(): LiveData<PagedList<PrescriptionResponse.Data.Result>> {
    return activePrescriptionLiveData
}
}

しかし問題は、データ ソースが無効化されても何も起こらないことです。スワイプでリストを再取得して更新する必要があります。どんな助けでも大歓迎です。

PS MVP でページング ライブラリを使用する例を見つけることができなかったため、独自に実装しました。ここで何か間違ったことをしている場合は教えてください。

4

1 に答える 1