Update: Migrate PrefRepo to Compose

This commit is contained in:
machiav3lli 2022-04-12 18:16:34 +02:00
parent 0d1ea78922
commit bffcbea4ce
2 changed files with 60 additions and 38 deletions

View File

@ -5,9 +5,17 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material.Scaffold
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.fragment.app.viewModels
import com.looker.droidify.R
import com.looker.droidify.content.Preferences
import com.looker.droidify.database.entity.Repository
import com.looker.droidify.databinding.FragmentRepositoriesXBinding
import com.looker.droidify.service.Connection
import com.looker.droidify.service.SyncService
@ -45,19 +53,54 @@ class PrefsRepositoriesFragment : BaseNavFragment() {
override fun setupLayout() {
syncConnection.bind(requireContext())
binding.addRepository.setOnClickListener { viewModel.addRepository() }
viewModel.repositories.observe(requireActivity()) {
binding.reposRecycler.setContent {
AppTheme(
darkTheme = when (Preferences[Preferences.Key.Theme]) {
is Preferences.Theme.System -> isSystemInDarkTheme()
is Preferences.Theme.AmoledSystem -> isSystemInDarkTheme()
else -> isDarkTheme
}
) {
Scaffold { _ ->
redrawPage(it)
}
}
override fun onDestroyView() {
super.onDestroyView()
syncConnection.unbind(requireContext())
}
@OptIn(ExperimentalMaterial3Api::class)
fun redrawPage(repos: List<Repository>) {
binding.reposRecycler.setContent {
AppTheme(
darkTheme = when (Preferences[Preferences.Key.Theme]) {
is Preferences.Theme.System -> isSystemInDarkTheme()
is Preferences.Theme.AmoledSystem -> isSystemInDarkTheme()
else -> isDarkTheme
}
) {
Scaffold {
Column(
modifier = Modifier.padding(8.dp),
verticalArrangement = Arrangement.Absolute.spacedBy(4.dp)
) {
OutlinedButton(
modifier = Modifier.fillMaxWidth(),
contentPadding = PaddingValues(12.dp),
colors = ButtonDefaults.outlinedButtonColors(
contentColor = MaterialTheme.colorScheme.primary,
containerColor = MaterialTheme.colorScheme.background
),
onClick = { viewModel.addRepository() }
) {
Text(
modifier = Modifier.weight(1f),
text = stringResource(id = R.string.add_repository),
textAlign = TextAlign.Center,
style = MaterialTheme.typography.titleSmall
)
Icon(
painter = painterResource(id = R.drawable.ic_add),
contentDescription = stringResource(id = R.string.add_repository)
)
}
RepositoriesRecycler(
repositoriesList = it.sortedBy { repo -> !repo.enabled },
repositoriesList = repos.sortedBy { repo -> !repo.enabled },
onClick = { repo ->
repo.enabled = !repo.enabled
GlobalScope.launch(Dispatchers.IO) {
@ -73,9 +116,4 @@ class PrefsRepositoriesFragment : BaseNavFragment() {
}
}
}
override fun onDestroyView() {
super.onDestroyView()
syncConnection.unbind(requireContext())
}
}

View File

@ -23,29 +23,13 @@
<com.google.android.material.circularreveal.CircularRevealFrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
<androidx.compose.ui.platform.ComposeView
android:id="@+id/reposRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<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.compose.ui.platform.ComposeView
android:id="@+id/reposRecycler"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
android:layout_height="match_parent" />
</com.google.android.material.circularreveal.CircularRevealFrameLayout>
</layout>