-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
276 additions
and
28 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
app/src/debug/java/me/kbai/zhenxunui/extends/CoroutineExtends.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,39 @@ | ||
package me.kbai.zhenxunui.extends | ||
|
||
import androidx.fragment.app.Fragment | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.lifecycleScope | ||
import androidx.lifecycle.repeatOnLifecycle | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.FlowCollector | ||
import kotlinx.coroutines.launch | ||
import me.kbai.zhenxunui.repository.Resource | ||
|
||
/** | ||
* @author sean on 2022/4/15 | ||
*/ | ||
|
||
fun LifecycleOwner.launchRepeatOnLifeCycle( | ||
state: Lifecycle.State, | ||
block: suspend CoroutineScope.() -> Unit | ||
) = lifecycleScope.launch { | ||
repeatOnLifecycle(state, block) | ||
} | ||
|
||
val Fragment.viewLifecycleScope: CoroutineScope | ||
get() = viewLifecycleOwner.lifecycleScope | ||
|
||
inline fun <reified T> Flow<T>.launchAndCollectIn( | ||
owner: LifecycleOwner, | ||
activeState: Lifecycle.State = Lifecycle.State.STARTED, | ||
collector: FlowCollector<T> | ||
) = owner.lifecycleScope.launch { | ||
logI(T::class.java.name, "launchAndCollectIn") | ||
assert(T::class.java != Resource::class.java) { "Resource 类型要用 apiCollect 实现 token 过期重新登录!" } | ||
|
||
owner.repeatOnLifecycle(activeState) { | ||
collect(collector) | ||
} | ||
} |
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
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
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
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
29 changes: 29 additions & 0 deletions
29
app/src/main/java/me/kbai/zhenxunui/ui/db/SqlResultDialogFragment.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,29 @@ | ||
package me.kbai.zhenxunui.ui.db | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.fragment.app.viewModels | ||
import me.kbai.zhenxunui.base.BaseDialogFragment | ||
import me.kbai.zhenxunui.databinding.DialogSqlResultBinding | ||
import me.kbai.zhenxunui.viewmodel.DbManageViewModel | ||
|
||
class SqlResultDialogFragment : BaseDialogFragment() { | ||
|
||
private lateinit var mBinding: DialogSqlResultBinding | ||
|
||
private val mViewModel by viewModels<DbManageViewModel>({ requireParentFragment() }) | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
) = DialogSqlResultBinding.inflate(inflater, container, false).also { mBinding = it }.root | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
mViewModel.executeSqlResult.observe(viewLifecycleOwner) { | ||
mBinding.rvRows.adapter = SqlResultRowAdapter(it) | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
app/src/main/java/me/kbai/zhenxunui/ui/db/SqlResultRowAdapter.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,24 @@ | ||
package me.kbai.zhenxunui.ui.db | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.RecyclerView | ||
import me.kbai.zhenxunui.databinding.ItemSqlResultRowBinding | ||
|
||
class SqlResultRowAdapter( | ||
val data: List<Map<String, *>> | ||
) : RecyclerView.Adapter<SqlResultRowAdapter.ItemViewHolder>() { | ||
|
||
class ItemViewHolder(val binding: ItemSqlResultRowBinding) : | ||
RecyclerView.ViewHolder(binding.root) | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ItemViewHolder( | ||
ItemSqlResultRowBinding.inflate(LayoutInflater.from(parent.context), parent, false) | ||
) | ||
|
||
override fun getItemCount() = data.size | ||
|
||
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) { | ||
holder.binding.rvValues.adapter = SqlResultValueAdapter(data[position]) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
app/src/main/java/me/kbai/zhenxunui/ui/db/SqlResultValueAdapter.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,33 @@ | ||
package me.kbai.zhenxunui.ui.db | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.RecyclerView | ||
import me.kbai.zhenxunui.databinding.ItemSqlResultValueBinding | ||
|
||
class SqlResultValueAdapter( | ||
map: Map<String, *> | ||
) : RecyclerView.Adapter<SqlResultValueAdapter.ItemViewHolder>() { | ||
val data: List<Map.Entry<String, *>> = ArrayList<Map.Entry<String, *>>(map.entries) | ||
|
||
class ItemViewHolder(val binding: ItemSqlResultValueBinding) : | ||
RecyclerView.ViewHolder(binding.root) | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ItemViewHolder( | ||
ItemSqlResultValueBinding.inflate(LayoutInflater.from(parent.context), parent, false) | ||
) | ||
|
||
override fun getItemCount() = data.size | ||
|
||
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) { | ||
val item = data[position] | ||
|
||
holder.binding.run { | ||
tvField.text = item.key | ||
tvValue.text = when (item.value) { | ||
is String -> "\"${item.value}\"" | ||
else -> item.value.toString() | ||
} | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.