2020-06-17 19:07:47 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020 Conny Duck
|
|
|
|
*
|
|
|
|
* This file is part of Pixelcat.
|
|
|
|
*
|
|
|
|
* Pixelcat is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Pixelcat is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-06-12 15:44:45 +02:00
|
|
|
package at.connyduck.pixelcat.components.timeline
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.View
|
|
|
|
import androidx.fragment.app.viewModels
|
|
|
|
import androidx.lifecycle.lifecycleScope
|
|
|
|
import androidx.paging.ExperimentalPagingApi
|
2020-08-18 21:22:40 +02:00
|
|
|
import androidx.paging.LoadState
|
2020-06-12 15:44:45 +02:00
|
|
|
import androidx.recyclerview.widget.DividerItemDecoration
|
|
|
|
import androidx.recyclerview.widget.SimpleItemAnimator
|
|
|
|
import at.connyduck.pixelcat.R
|
2020-06-18 16:46:35 +02:00
|
|
|
import at.connyduck.pixelcat.components.util.extension.getDisplayWidthInPx
|
2020-06-12 15:44:45 +02:00
|
|
|
import at.connyduck.pixelcat.components.util.getColorForAttr
|
|
|
|
import at.connyduck.pixelcat.dagger.ViewModelFactory
|
|
|
|
import at.connyduck.pixelcat.databinding.FragmentTimelineBinding
|
|
|
|
import at.connyduck.pixelcat.db.entitity.StatusEntity
|
|
|
|
import at.connyduck.pixelcat.util.viewBinding
|
|
|
|
import dagger.android.support.DaggerFragment
|
|
|
|
import kotlinx.coroutines.flow.collectLatest
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import javax.inject.Inject
|
|
|
|
|
2020-06-12 19:58:15 +02:00
|
|
|
class TimelineFragment : DaggerFragment(R.layout.fragment_timeline), TimeLineActionListener {
|
2020-06-12 15:44:45 +02:00
|
|
|
|
|
|
|
@Inject
|
|
|
|
lateinit var viewModelFactory: ViewModelFactory
|
|
|
|
|
|
|
|
private val viewModel: TimelineViewModel by viewModels { viewModelFactory }
|
|
|
|
|
|
|
|
private val binding by viewBinding(FragmentTimelineBinding::bind)
|
|
|
|
|
|
|
|
@ExperimentalPagingApi
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
|
|
|
|
binding.timelineSwipeRefresh.setColorSchemeColors(
|
|
|
|
view.context.getColorForAttr(R.attr.pixelcat_gradient_color_start),
|
|
|
|
view.context.getColorForAttr(R.attr.pixelcat_gradient_color_end)
|
|
|
|
)
|
|
|
|
|
|
|
|
binding.toolbar.setNavigationOnClickListener {
|
|
|
|
binding.timelineRecyclerView.scrollToPosition(0)
|
|
|
|
}
|
|
|
|
|
2020-06-18 16:46:35 +02:00
|
|
|
val adapter = TimelineListAdapter(view.context.getDisplayWidthInPx(), this)
|
2020-06-12 15:44:45 +02:00
|
|
|
|
|
|
|
binding.timelineRecyclerView.adapter = adapter
|
|
|
|
(binding.timelineRecyclerView.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
|
|
|
|
binding.timelineRecyclerView.addItemDecoration(DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL))
|
|
|
|
|
|
|
|
lifecycleScope.launch {
|
2020-06-13 19:55:36 +02:00
|
|
|
viewModel.statusFlow.collectLatest { pagingData ->
|
2020-06-12 15:44:45 +02:00
|
|
|
adapter.submitData(pagingData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
binding.timelineSwipeRefresh.setOnRefreshListener {
|
|
|
|
adapter.refresh()
|
|
|
|
}
|
|
|
|
|
2020-08-18 21:22:40 +02:00
|
|
|
adapter.addLoadStateListener {
|
|
|
|
if (it.refresh != LoadState.Loading) {
|
|
|
|
binding.timelineSwipeRefresh.isRefreshing = false
|
|
|
|
|
|
|
|
}
|
2020-06-12 15:44:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
fun newInstance() = TimelineFragment()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onFavorite(post: StatusEntity) {
|
|
|
|
viewModel.onFavorite(post)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBoost(post: StatusEntity) {
|
2020-06-14 09:13:19 +02:00
|
|
|
viewModel.onBoost(post)
|
2020-06-12 15:44:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onReply(status: StatusEntity) {
|
|
|
|
TODO("Not yet implemented")
|
|
|
|
}
|
2020-06-13 19:55:36 +02:00
|
|
|
|
|
|
|
override fun onMediaVisibilityChanged(status: StatusEntity) {
|
|
|
|
viewModel.onMediaVisibilityChanged(status)
|
|
|
|
}
|
2020-06-12 19:58:15 +02:00
|
|
|
}
|