package com.shsening.autouploadvideos
|
|
import SessionProfile
|
import SessionProfileManager
|
import android.app.Activity
|
import android.app.AlertDialog
|
import android.content.Intent
|
import android.os.Bundle
|
import android.view.LayoutInflater
|
import android.view.View
|
import android.view.ViewGroup
|
import android.widget.ArrayAdapter
|
import android.widget.Button
|
import android.widget.CheckBox
|
import android.widget.ListView
|
import androidx.fragment.app.Fragment
|
import android.widget.TextView
|
import com.shsening.autouploadvideos.utils.Utils
|
|
class UploadListFragment : Fragment() {
|
|
private lateinit var listView: ListView
|
private lateinit var btnAllTask: View
|
private val REQUEST_CODE = 100
|
|
private val sessionProfiles = mutableListOf<SessionProfile>() // Replace with your real data source
|
|
override fun onCreateView(
|
inflater: LayoutInflater, container: ViewGroup?,
|
savedInstanceState: Bundle?
|
): View {
|
val view = inflater.inflate(R.layout.upload_list, container, false)
|
|
listView = view.findViewById(R.id.upload_list_view)
|
btnAllTask = view.findViewById(R.id.btn_all_task)
|
|
setupListView()
|
setupAllTaskButton()
|
|
return view
|
}
|
|
private fun setupListView() {
|
// 清空适配器
|
listView.adapter = null
|
|
// 从 SessionProfileManager 获取所有的 SessionProfile
|
val sessionProfiles = SessionProfileManager(requireContext()).getAllProfiles()
|
|
// 自定义适配器实现删除按钮功能
|
val adapter = object : ArrayAdapter<SessionProfile>(
|
requireContext(),
|
android.R.layout.simple_list_item_2,
|
sessionProfiles
|
) {
|
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
|
val inflater = LayoutInflater.from(context)
|
val view = convertView ?: inflater.inflate(R.layout.list_item_with_checkbox, parent, false)
|
|
// 获取当前的 SessionProfile
|
val sessionProfile = getItem(position)
|
|
// 填充数据
|
val text1 = view.findViewById<TextView>(android.R.id.text1)
|
val textType = Utils.getSessionProfileValueByPosition(sessionProfile?.type)
|
text1.text = "${sessionProfile?.name} (${textType})"
|
|
val text2 = view.findViewById<TextView>(android.R.id.text2)
|
text2.text = sessionProfile?.lastUploadTime
|
|
// 设置删除按钮
|
val deleteButton = view.findViewById<Button>(R.id.deleteButton)
|
deleteButton.setOnClickListener {
|
val intent = Intent(requireContext(), UploadSessionActivity::class.java)
|
val sessionProfiles = arrayListOf(sessionProfile) // 替换为实际的选定数组
|
intent.putParcelableArrayListExtra("sessionProfiles", sessionProfiles)
|
startActivityForResult(intent, REQUEST_CODE)
|
}
|
|
return view
|
}
|
}
|
|
// 设置适配器到 ListView
|
listView.adapter = adapter
|
|
}
|
fun getSelectedSessionProfiles(): ArrayList<SessionProfile> {
|
val selectedProfiles = ArrayList<SessionProfile>()
|
|
// 获取 ListView 的适配器
|
val adapter = listView.adapter as? ArrayAdapter<*>
|
// 确保适配器正确
|
if (adapter != null) {
|
// 遍历 ListView 中的所有项
|
for (i in 0 until adapter.count) {
|
// 获取当前的视图
|
val view = listView.getChildAt(i)
|
// 获取 CheckBox 组件
|
val checkBox = view?.findViewById<CheckBox>(R.id.itemCheckBox)
|
|
// 检查 CheckBox 是否被选中
|
if (checkBox?.isChecked == true) {
|
// 获取对应的 SessionProfile 对象
|
val sessionProfile = adapter.getItem(i) as? SessionProfile
|
// 将选中的 SessionProfile 添加到结果列表中
|
sessionProfile?.let {
|
selectedProfiles.add(it)
|
}
|
}
|
}
|
}
|
|
return selectedProfiles
|
}
|
|
|
// 接收到 Activity 返回结果时刷新 Fragment
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
super.onActivityResult(requestCode, resultCode, data)
|
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
|
// 刷新 Fragment 内容
|
setupListView()
|
}
|
}
|
|
private fun setupAllTaskButton() {
|
btnAllTask.setOnClickListener {
|
|
var selected = getSelectedSessionProfiles()
|
if (selected.isEmpty()){
|
AlertDialog.Builder(requireContext())
|
.setTitle("提示")
|
.setMessage("请至少选择一个项目!")
|
.setPositiveButton("确定", null)
|
.show()
|
}else {
|
val intent = Intent(requireContext(), UploadSessionActivity::class.java)
|
intent.putParcelableArrayListExtra("sessionProfiles", selected)
|
startActivityForResult(intent, REQUEST_CODE)
|
}
|
}
|
}
|
}
|