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.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material.Scaffold
import androidx.fragment.app.viewModels 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.databinding.FragmentRepositoriesXBinding
import com.looker.droidify.service.Connection import com.looker.droidify.service.Connection
import com.looker.droidify.service.SyncService import com.looker.droidify.service.SyncService
import com.looker.droidify.ui.activities.PrefsActivityX 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.looker.droidify.ui.viewmodels.RepositoriesViewModelX
import com.mikepenz.fastadapter.FastAdapter import com.looker.droidify.utility.isDarkTheme
import com.mikepenz.fastadapter.IAdapter
import com.mikepenz.fastadapter.adapters.ItemAdapter
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class PrefsRepositoriesFragment : BaseNavFragment() { class PrefsRepositoriesFragment : BaseNavFragment() {
private lateinit var binding: FragmentRepositoriesXBinding private lateinit var binding: FragmentRepositoriesXBinding
private val reposItemAdapter = ItemAdapter<RepoItem>()
private var reposFastAdapter: FastAdapter<RepoItem>? = null
val viewModel: RepositoriesViewModelX by viewModels { val viewModel: RepositoriesViewModelX by viewModels {
RepositoriesViewModelX.Factory(prefsActivityX.db) RepositoriesViewModelX.Factory(prefsActivityX.db)
} }
@ -46,53 +45,35 @@ class PrefsRepositoriesFragment : BaseNavFragment() {
override fun setupAdapters() { override fun setupAdapters() {
syncConnection.bind(requireContext()) 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() } binding.addRepository.setOnClickListener { viewModel.addRepository() }
} }
override fun setupLayout() { override fun setupLayout() {
viewModel.repositories.observe(requireActivity()) { viewModel.repositories.observe(requireActivity()) {
// Function: sync when an enabled repo got edited binding.reposRecycler.setContent {
val enabledList = it.filter { it.enabled } AppTheme(
reposItemAdapter.adapterItems.filter(RepoItem::isEnabled).forEach { item -> darkTheme = when (Preferences[Preferences.Key.Theme]) {
enabledList.firstOrNull { it.id == item.item.id }?.let { repo -> is Preferences.Theme.System -> isSystemInDarkTheme()
repo.let { data -> is Preferences.Theme.AmoledSystem -> isSystemInDarkTheme()
if (data != item.item) syncConnection.binder?.sync(data) 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 ~ 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/>. ~ along with this program. If not, see <https://www.gnu.org/licenses/>.
--> -->
<layout xmlns:android="http://schemas.android.com/apk/res/android" <layout xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:tools="http://schemas.android.com/tools">
<data> <data>
@ -26,37 +25,27 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView <androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/scrollLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_gravity="top" android:orientation="vertical">
android:clipToPadding="false">
<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_width="match_parent"
android:layout_height="match_parent" android:layout_height="64dp"
android:orientation="vertical"> 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 <androidx.compose.ui.platform.ComposeView
android:id="@+id/add_repository" android:id="@+id/reposRecycler"
style="@style/Widget.Material3.Button.OutlinedButton.Icon" android:layout_width="match_parent"
android:layout_width="match_parent" android:layout_height="0dp"
android:layout_height="64dp" android:layout_weight="1" />
android:layout_margin="@dimen/shape_margin_medium" </androidx.appcompat.widget.LinearLayoutCompat>
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>
</com.google.android.material.circularreveal.CircularRevealFrameLayout> </com.google.android.material.circularreveal.CircularRevealFrameLayout>
</layout> </layout>