package com.example.newapplication.taskActivity
import android.content.Context
import android.content.SharedPreferences
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.util.Base64
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.example.newapplication.R
import com.example.newapplication.databinding.ActivityDiedaoBinding
import com.example.newapplication.databinding.ActivityTaskBinding
import com.example.newapplication.entity.Task
import com.example.personalwork.utils.ApiClient
import kotlinx.coroutines.launch
import java.time.format.DateTimeFormatter
class DiedaoActivity : AppCompatActivity() {
var _binding : ActivityDiedaoBinding? = null
val binding get() = _binding!!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_diedao)
_binding = ActivityDiedaoBinding.inflate(layoutInflater)
init()
setContentView(binding.root)
}
fun init(){
initText()
initButton()
}
fun initText(){
val task = intent.getSerializableExtra("task") as? Task
val time = binding.time
val position = binding.position
if (task != null) {
val taskTime = task.time
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
var formattedTime = taskTime?.format(formatter)
if (formattedTime != null) {
formattedTime = formattedTime.replace("T", " ")
}
time.text = formattedTime
position.setText(task.position)
setImage(task.blob.toString())
}
}
fun setImage(image:String){
val byteArrayString = image
val imageBytes = Base64.decode(byteArrayString, Base64.DEFAULT)
// 将字节数组转换为 Bitmap
val bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
val avatar = binding.Diedao
avatar.setImageBitmap(bitmap)
}
fun initButton(){
val task = intent.getSerializableExtra("task") as? Task
val sharedPreferences: SharedPreferences = this.getSharedPreferences("User", Context.MODE_PRIVATE)
val btn = binding.button
val id = task?.id.toString()
btn.setOnClickListener {
lifecycleScope.launch {
try{
val response = ApiClient.apiService.updatetask(id,sharedPreferences.getInt("userId",0).toString())
if(response.body()?.code == 200){
Toast.makeText(baseContext,"已承接", Toast.LENGTH_SHORT).show()
finish()
}else{
if(task?.sentid.toString().equals(sharedPreferences.getInt("userId",1).toString())){
Toast.makeText(baseContext,"该任务是你发布的", Toast.LENGTH_SHORT).show()
}
Toast.makeText(baseContext,"承接失败", Toast.LENGTH_SHORT).show()
}
}catch (e:Exception){
e.printStackTrace()
}
}
}
}
override fun onDestroy() {
super.onDestroy()
_binding = null
}
}
package com.example.newapplication.taskActivity
import android.content.Context
import android.content.SharedPreferences
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.example.newapplication.R
import com.example.newapplication.databinding.ActivityTaskBinding
import com.example.newapplication.entity.Task
import com.example.personalwork.utils.ApiClient
import kotlinx.coroutines.launch
class TaskActivity : AppCompatActivity() {
var _binding : ActivityTaskBinding? = null
val binding get() = _binding!!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_task)
_binding = ActivityTaskBinding.inflate(layoutInflater)
init()
setContentView(binding.root)
}
fun init(){
val sharedPreferences: SharedPreferences = this.getSharedPreferences("User", Context.MODE_PRIVATE)
val task = intent.getSerializableExtra("task") as? Task
val maintitle = binding.maintitle
val description = binding.maintext
val tip1 = binding.tips1
val tip2 = binding.tips2
val tip3 = binding.tips3
val position = binding.posText
val btn = binding.imageButton2
maintitle.setText(task?.name)
description.setText(task?.description)
tip1.setText(task?.tip1)
tip2.setText(task?.tip2)
tip3.setText(task?.tip3)
position.setText(task?.position)
when(task?.state){
0 -> {
btn.setText("承接")
function()
}
1 -> {
btn.setText("已承接")
function2()
}
2 -> {
btn.setText("待确认")
if(task.sentid == sharedPreferences.getInt("userId",0)){
btn.setEnabled(true)
function3()
}else{
btn.setEnabled(false)
}
btn.setBackgroundColor(Color.GREEN)
}
3->{
btn.setText("已完成")
btn.setEnabled(false)
btn.setBackgroundColor(Color.GRAY)
}
}
}
fun function(){
val task = intent.getSerializableExtra("task") as? Task
val sharedPreferences: SharedPreferences = this.getSharedPreferences("User", Context.MODE_PRIVATE)
val btn = binding.imageButton2
val id = task?.id.toString()
btn.setOnClickListener {
lifecycleScope.launch {
try{
val response = ApiClient.apiService.updatetask(id,sharedPreferences.getInt("userId",0).toString())
if(response.body()?.code == 200){
Toast.makeText(baseContext,"已承接",Toast.LENGTH_SHORT).show()
finish()
}else{
if(task?.sentid.toString().equals(sharedPreferences.getInt("userId",1).toString())){
Toast.makeText(baseContext,"该任务是你发布的",Toast.LENGTH_SHORT).show()
}
Toast.makeText(baseContext,"承接失败",Toast.LENGTH_SHORT).show()
}
}catch (e:Exception){
e.printStackTrace()
}
}
}
}
fun function2(){
val task = intent.getSerializableExtra("task") as? Task
val sharedPreferences: SharedPreferences = this.getSharedPreferences("User", Context.MODE_PRIVATE)
val btn = binding.imageButton2
val id = task?.id.toString()
btn.setOnClickListener {
lifecycleScope.launch {
try{
val response = ApiClient.apiService.finishtask(id,sharedPreferences.getInt("userId",1).toString())
if(response.isSuccessful){
Toast.makeText(baseContext,"提交完成申请",Toast.LENGTH_SHORT).show()
finish()
}else{
if(task?.sentid.toString() .equals(sharedPreferences.getInt("userId",1).toString())){
Toast.makeText(baseContext,"该任务是你发布的",Toast.LENGTH_SHORT).show()
}
Toast.makeText(baseContext,"承接失败",Toast.LENGTH_SHORT).show()
}
}catch (e:Exception){
e.printStackTrace()
}
}
}
}
fun function3(){
val task = intent.getSerializableExtra("task") as? Task
val sharedPreferences: SharedPreferences = this.getSharedPreferences("User", Context.MODE_PRIVATE)
val btn = binding.imageButton2
val id = task?.id.toString()
btn.setOnClickListener {
lifecycleScope.launch {
try{
val response = ApiClient.apiService.confirmtask(id,sharedPreferences.getInt("userId",1).toString())
if(response.isSuccessful){
Toast.makeText(baseContext,"确认完成",Toast.LENGTH_SHORT).show()
finish()
}else{
if(task?.sentid.toString() .equals(sharedPreferences.getInt("userId",1).toString())){
Toast.makeText(baseContext,"该任务是你发布的",Toast.LENGTH_SHORT).show()
}
Toast.makeText(baseContext,"承接失败",Toast.LENGTH_SHORT).show()
}
}catch (e:Exception){
e.printStackTrace()
}
}
}
}
override fun onDestroy() {
super.onDestroy()
_binding = null
}
}
package com.example.newapplication.taskActivity
import android.content.Context
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.newapplication.R
import com.example.newapplication.adapter.TaskAdapter
import com.example.newapplication.databinding.ActivityPersonalTaskBinding
import com.example.newapplication.databinding.ActivityTaskBinding
import com.example.newapplication.entity.Task
import com.example.personalwork.utils.ApiClient
import kotlinx.coroutines.launch
class PersonalTaskActivity : AppCompatActivity() {
var _binding :ActivityPersonalTaskBinding? =null
val binding get() = _binding!!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_personal_task)
_binding = ActivityPersonalTaskBinding.inflate(layoutInflater)
init()
setContentView(binding.root)
}
fun init(){
listPage()
val tabHost = binding.tabhost
tabHost.setup()
// 将选项卡与布局中的LinearLayout关联
tabHost.addTab(tabHost.newTabSpec("tab01").setIndicator("我接收的").setContent(R.id.tab01))
tabHost.addTab(tabHost.newTabSpec("tab02").setIndicator("我完成的").setContent(R.id.tab02))
tabHost.addTab(tabHost.newTabSpec("tab03").setIndicator("被接受的").setContent(R.id.tab03))
tabHost.addTab(tabHost.newTabSpec("tab04").setIndicator("执行完的").setContent(R.id.tab04))
tabHost.setOnTabChangedListener {
val tabId = tabHost.currentTabTag
when (tabId) {
"tab01" -> {
listPage()
}
"tab02" -> {
listPage2()
}
"tab03" -> {
listPage3()
}
"tab04" -> {
listPage4()
}
}
}
}
fun listPage() {
val sharedPreferences: SharedPreferences = this.getSharedPreferences("User", Context.MODE_PRIVATE)
val recyclerView: RecyclerView = binding.recyclerView
val taskList = mutableListOf<Task>()
lifecycleScope.launch {
try {
val listResponse = ApiClient.apiService.listPage()
val resultList = listResponse.body()?.data
if (listResponse.isSuccessful) {
if (resultList != null) {
for(i in 0 until resultList.size){
val task = resultList[i]
if(task.state == 1 &&
task.receiveid == sharedPreferences.getInt("userId",0)){
taskList.add(task)
}
}
}
val layoutManager = LinearLayoutManager(baseContext)
recyclerView.layoutManager = layoutManager
val adapter = TaskAdapter(taskList)
recyclerView.adapter = adapter
} else {
Toast.makeText(baseContext, "查找失败", Toast.LENGTH_SHORT).show()
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
fun listPage2() {
val sharedPreferences: SharedPreferences = this.getSharedPreferences("User", Context.MODE_PRIVATE)
val recyclerView: RecyclerView = binding.recyclerView2
val taskList = mutableListOf<Task>()
lifecycleScope.launch {
try {
val listResponse = ApiClient.apiService.listPage()
val resultList = listResponse.body()?.data
if (listResponse.isSuccessful) {
if (resultList != null) {
for(i in 0 until resultList.size){
val task = resultList[i]
if((task.state == 2 || task.state == 3) &&
task.receiveid == sharedPreferences.getInt("userId",0)){
taskList.add(task)
}
}
}
val layoutManager = LinearLayoutManager(baseContext)
recyclerView.layoutManager = layoutManager
val adapter = TaskAdapter(taskList)
recyclerView.adapter = adapter
} else {
Toast.makeText(baseContext, "查找失败", Toast.LENGTH_SHORT).show()
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
fun listPage3() {
val sharedPreferences: SharedPreferences = this.getSharedPreferences("User", Context.MODE_PRIVATE)
val recyclerView3: RecyclerView = binding.recyclerView3
val taskList = mutableListOf<Task>()
lifecycleScope.launch {
try {
val listResponse = ApiClient.apiService.listPage()
val resultList = listResponse.body()?.data
if (listResponse.isSuccessful) {
if (resultList != null) {
for(i in 0 until resultList.size){
val task = resultList[i]
if(task.state == 1 && task.sentid == sharedPreferences.getInt("userId",0)){
taskList.add(task)
}
}
}
val layoutManager = LinearLayoutManager(baseContext)
recyclerView3.layoutManager = layoutManager
val adapter = TaskAdapter(taskList)
recyclerView3.adapter = adapter
} else {
Toast.makeText(baseContext, "查找失败", Toast.LENGTH_SHORT).show()
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
fun listPage4() {
val sharedPreferences: SharedPreferences = this.getSharedPreferences("User", Context.MODE_PRIVATE)
val recyclerView4: RecyclerView = binding.recyclerView4
val taskList = mutableListOf<Task>()
lifecycleScope.launch {
try {
val listResponse = ApiClient.apiService.listPage()
val resultList = listResponse.body()?.data
if (listResponse.isSuccessful) {
if (resultList != null) {
for(i in 0 until resultList.size){
val task = resultList[i]
if((task.state == 2 || task.state == 3) &&
task.sentid == sharedPreferences.getInt("userId",0)){
Log.d(i.toString(),task.toString())
taskList.add(task)
}
}
}
val layoutManager = LinearLayoutManager(baseContext)
recyclerView4.layoutManager = layoutManager
val adapter = TaskAdapter(taskList)
recyclerView4.adapter = adapter
} else {
Toast.makeText(baseContext, "查找失败", Toast.LENGTH_SHORT).show()
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
override fun onDestroy() {
super.onDestroy()
_binding = null
}
}