Update: Replace traditional recycler with compose lazylist repositories fragment

This commit is contained in:
machiav3lli 2022-02-27 00:28:42 +01:00
parent fca3b7344e
commit c0e1837ab4
2 changed files with 44 additions and 74 deletions

View File

@ -4,25 +4,24 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material.Scaffold
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.looker.droidify.content.Preferences
import com.looker.droidify.databinding.FragmentRepositoriesXBinding
import com.looker.droidify.service.Connection
import com.looker.droidify.service.SyncService
import com.looker.droidify.ui.activities.PrefsActivityX
import com.looker.droidify.ui.items.RepoItem
import com.looker.droidify.ui.compose.RepositoriesRecycler
import com.looker.droidify.ui.compose.theme.AppTheme
import com.looker.droidify.ui.viewmodels.RepositoriesViewModelX
import com.mikepenz.fastadapter.FastAdapter
import com.mikepenz.fastadapter.IAdapter
import com.mikepenz.fastadapter.adapters.ItemAdapter
import com.looker.droidify.utility.isDarkTheme
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
class PrefsRepositoriesFragment : BaseNavFragment() {
private lateinit var binding: FragmentRepositoriesXBinding
private val reposItemAdapter = ItemAdapter<RepoItem>()
private var reposFastAdapter: FastAdapter<RepoItem>? = null
val viewModel: RepositoriesViewModelX by viewModels {
RepositoriesViewModelX.Factory(prefsActivityX.db)
}
@ -46,53 +45,35 @@ class PrefsRepositoriesFragment : BaseNavFragment() {
override fun setupAdapters() {
syncConnection.bind(requireContext())
reposFastAdapter = FastAdapter.with(reposItemAdapter)
reposFastAdapter?.setHasStableIds(false)
reposFastAdapter?.onClickListener =
{ _: View?, _: IAdapter<RepoItem>?, item: RepoItem?, _: Int? ->
item?.item?.let {
it.enabled = !it.enabled
GlobalScope.launch(Dispatchers.IO) {
syncConnection.binder?.setEnabled(it, it.enabled)
}
}
false
}
reposFastAdapter?.onLongClickListener =
{ _: View?, _: IAdapter<RepoItem>?, item: RepoItem?, _: Int? ->
item?.item?.let {
RepositorySheetX(it.id)
.showNow(parentFragmentManager, "Repository ${it.id}")
}
false
}
binding.recyclerView.apply {
layoutManager = LinearLayoutManager(context)
adapter = reposFastAdapter
}
binding.addRepository.setOnClickListener { viewModel.addRepository() }
}
override fun setupLayout() {
viewModel.repositories.observe(requireActivity()) {
// Function: sync when an enabled repo got edited
val enabledList = it.filter { it.enabled }
reposItemAdapter.adapterItems.filter(RepoItem::isEnabled).forEach { item ->
enabledList.firstOrNull { it.id == item.item.id }?.let { repo ->
repo.let { data ->
if (data != item.item) syncConnection.binder?.sync(data)
binding.reposRecycler.setContent {
AppTheme(
darkTheme = when (Preferences[Preferences.Key.Theme]) {
is Preferences.Theme.System -> isSystemInDarkTheme()
is Preferences.Theme.AmoledSystem -> isSystemInDarkTheme()
else -> isDarkTheme
}
) {
Scaffold { _ ->
RepositoriesRecycler(
repositoriesList = it.sortedBy { repo -> !repo.enabled },
onClick = { repo ->
repo.enabled = !repo.enabled
GlobalScope.launch(Dispatchers.IO) {
syncConnection.binder?.setEnabled(repo, repo.enabled)
}
},
onLongClick = { repo ->
RepositorySheetX(repo.id)
.showNow(parentFragmentManager, "Repository ${repo.id}")
})
}
}
}
reposItemAdapter.set(
it.sortedBy { repo -> !repo.enabled }
.mapNotNull { dbRepo ->
RepoItem(dbRepo)
}
)
}
}

View File

@ -15,8 +15,7 @@
~ You should have received a copy of the GNU Affero General Public License
~ along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
@ -26,37 +25,27 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:id="@+id/scrollLayout"
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:clipToPadding="false">
android:orientation="vertical">
<androidx.appcompat.widget.LinearLayoutCompat
<com.google.android.material.button.MaterialButton
android:id="@+id/add_repository"
style="@style/Widget.Material3.Button.OutlinedButton.Icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_height="64dp"
android:layout_margin="@dimen/shape_margin_medium"
android:drawableEnd="@drawable/ic_add"
android:drawableTint="?colorPrimary"
android:text="@string/add_repository" />
<com.google.android.material.button.MaterialButton
android:id="@+id/add_repository"
style="@style/Widget.Material3.Button.OutlinedButton.Icon"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_margin="@dimen/shape_margin_medium"
android:drawableEnd="@drawable/ic_add"
android:drawableTint="?colorPrimary"
android:text="@string/add_repository" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
tools:listitem="@layout/item_repository_x" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.core.widget.NestedScrollView>
<androidx.compose.ui.platform.ComposeView
android:id="@+id/reposRecycler"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</androidx.appcompat.widget.LinearLayoutCompat>
</com.google.android.material.circularreveal.CircularRevealFrameLayout>
</layout>