-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial implementation for listing multiple configs
- Loading branch information
Showing
10 changed files
with
194 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
app/src/main/kotlin/dev/yashgarg/qbit/ui/server/adapter/ServerConfigAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package dev.yashgarg.qbit.ui.server.adapter | ||
|
||
import android.annotation.SuppressLint | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.RecyclerView | ||
import dev.yashgarg.qbit.R | ||
import dev.yashgarg.qbit.data.models.ServerConfig | ||
import dev.yashgarg.qbit.ui.common.ListTileTextView | ||
import javax.inject.Inject | ||
|
||
class ServerConfigAdapter @Inject constructor() : | ||
RecyclerView.Adapter<ServerConfigAdapter.ViewHolder>() { | ||
|
||
var configs = emptyList<ServerConfig>() | ||
@SuppressLint("NotifyDataSetChanged") | ||
set(value) { | ||
notifyDataSetChanged() | ||
field = value | ||
} | ||
|
||
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { | ||
val serverTitle: ListTileTextView = view.findViewById(R.id.server_title) | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||
val view = LayoutInflater.from(parent.context).inflate(R.layout.config_item, parent, false) | ||
|
||
return ViewHolder(view) | ||
} | ||
|
||
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
val config = configs[position] | ||
|
||
with(holder) { | ||
serverTitle.apply { | ||
title = config.serverName | ||
subtitle = "${config.baseUrl}:${config.port}" | ||
} | ||
} | ||
} | ||
|
||
override fun getItemCount() = configs.size | ||
} |
54 changes: 54 additions & 0 deletions
54
app/src/main/kotlin/dev/yashgarg/qbit/ui/server/manager/ServerManagerFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package dev.yashgarg.qbit.ui.server.manager | ||
|
||
import android.os.Bundle | ||
import android.view.View | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.viewModels | ||
import androidx.lifecycle.flowWithLifecycle | ||
import androidx.lifecycle.lifecycleScope | ||
import com.google.android.material.transition.MaterialSharedAxis | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import dev.yashgarg.qbit.R | ||
import dev.yashgarg.qbit.databinding.ServerManagerFragmentBinding | ||
import dev.yashgarg.qbit.ui.server.adapter.ServerConfigAdapter | ||
import dev.yashgarg.qbit.utils.viewBinding | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
|
||
@AndroidEntryPoint | ||
class ServerManagerFragment : Fragment(R.layout.server_manager_fragment) { | ||
private val binding by viewBinding(ServerManagerFragmentBinding::bind) | ||
private val viewModel by viewModels<ServerManagerViewModel>() | ||
|
||
@Inject lateinit var serverConfigAdapter: ServerConfigAdapter | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
exitTransition = MaterialSharedAxis(MaterialSharedAxis.Z, true) | ||
reenterTransition = MaterialSharedAxis(MaterialSharedAxis.Z, false) | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
|
||
binding.serverRv.adapter = serverConfigAdapter | ||
observeFlows() | ||
} | ||
|
||
private fun observeFlows() { | ||
viewModel.uiState | ||
.flowWithLifecycle(viewLifecycleOwner.lifecycle) | ||
.onEach(::render) | ||
.launchIn(viewLifecycleOwner.lifecycleScope) | ||
} | ||
|
||
private fun render(state: ServerManagerState) { | ||
with(state) { | ||
if (!configsLoading && !error && configs.isNotEmpty()) { | ||
serverConfigAdapter.configs = configs | ||
} | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
app/src/main/kotlin/dev/yashgarg/qbit/ui/server/manager/ServerManagerState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package dev.yashgarg.qbit.ui.server.manager | ||
|
||
import dev.yashgarg.qbit.data.models.ServerConfig | ||
|
||
data class ServerManagerState( | ||
val configsLoading: Boolean = true, | ||
val configs: List<ServerConfig> = emptyList(), | ||
val error: Boolean = false | ||
) |
32 changes: 32 additions & 0 deletions
32
app/src/main/kotlin/dev/yashgarg/qbit/ui/server/manager/ServerManagerViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package dev.yashgarg.qbit.ui.server.manager | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import dev.yashgarg.qbit.data.daos.ConfigDao | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.collectLatest | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
|
||
@HiltViewModel | ||
class ServerManagerViewModel @Inject constructor(private val configDao: ConfigDao) : ViewModel() { | ||
private val _uiState = MutableStateFlow(ServerManagerState()) | ||
val uiState = _uiState.asStateFlow() | ||
|
||
init { | ||
viewModelScope.launch { loadConfigs() } | ||
} | ||
|
||
private suspend fun loadConfigs() { | ||
val configs = configDao.getConfigs() | ||
configs | ||
.catch { _uiState.update { it.copy(error = true, configsLoading = false) } } | ||
.collectLatest { | ||
_uiState.update { state -> state.copy(configs = it, configsLoading = false) } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content"> | ||
|
||
<dev.yashgarg.qbit.ui.common.ListTileTextView | ||
android:id="@+id/server_title" | ||
android:layout_width="match_parent" | ||
android:layout_height="80dp" /> | ||
|
||
</FrameLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
|
||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:fitsSystemWindows="true" | ||
android:orientation="vertical"> | ||
|
||
<com.google.android.material.appbar.MaterialToolbar | ||
android:id="@+id/toolbar" | ||
android:layout_width="match_parent" | ||
android:layout_height="?attr/actionBarSize" | ||
android:elevation="0dp" | ||
app:navigationIcon="?attr/homeAsUpIndicator" | ||
app:title="@string/manage_configs" /> | ||
|
||
<androidx.recyclerview.widget.RecyclerView | ||
android:id="@+id/server_rv" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:scrollbars="vertical" | ||
android:visibility="visible" | ||
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" /> | ||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters