Added all new related activities

This commit is contained in:
Marta Borgia Leiva 2026-02-12 11:45:35 +01:00
parent cbf02a089b
commit 96f7980fb0
2 changed files with 308 additions and 120 deletions

View file

@ -18,6 +18,8 @@ import com.campusaula.edbole.kanban_clone_android.kanban.Task
import com.campusaula.edbole.kanban_clone_android.kanban.TaskStatus
import com.campusaula.edbole.kanban_clone_android.network.ApiService
import com.campusaula.edbole.kanban_clone_android.network.RetrofitInstance
import com.campusaula.edbole.kanban_clone_android.ui.adapters.ProjectCollaboratorAdapter
import com.campusaula.edbole.kanban_clone_android.ui.adapters.ProjectTaskAdapter
import com.google.android.material.floatingactionbutton.FloatingActionButton
import kotlinx.coroutines.launch
@ -28,9 +30,14 @@ class ProjectDetailActivity : AppCompatActivity() {
private lateinit var returnActionButton: FloatingActionButton
private lateinit var addTaskButton: Button
private lateinit var addCollaboratorButton: Button
private lateinit var editProjectButton: Button
private lateinit var deleteProjectButton: Button
private lateinit var taskListRecycler: RecyclerView
private lateinit var collaboratorListRecycler: RecyclerView
// private lateinit var collaboratorListAdapter: CollaboratorListAdapter
private lateinit var collaboratorListAdapter: ProjectCollaboratorAdapter
private lateinit var taskListAdapter: ProjectTaskAdapter
private lateinit var projectTitleText : TextView
private lateinit var projectDescriptionText : TextView
@ -59,23 +66,52 @@ class ProjectDetailActivity : AppCompatActivity() {
returnActionButton.setOnClickListener { finish() }
addTaskButton = findViewById(R.id.addTaskButton)
// addTaskButton.setOnClickListener {
// val intent: Intent = Intent(this, CreateTaskActivity::class.java)
// intent.putExtra("project_id", projectId)
// startActivity(intent)
// }
addTaskButton.setOnClickListener {
val intent = Intent(this, TaskAddActivity::class.java)
intent.putExtra("project_id", projectId)
startActivity(intent)
finish()
}
addCollaboratorButton = findViewById(R.id.addCollaboratorButton)
// addCollaboratorButton.setOnClickListener {
// val intent: Intent = Intent(this, AddCollaboratorActivity::class.java)
// intent.putExtra("project_id", projectId)
// startActivity(intent)
// }
addCollaboratorButton.setOnClickListener {
val intent = Intent(this, CollaboratorAddActivity::class.java)
intent.putExtra("project_id", projectId)
startActivity(intent)
finish()
}
taskListRecycler = findViewById(R.id.taskListRecycler)
taskListRecycler.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
taskListAdapter = ProjectTaskAdapter(emptyList(), api, projectId, {
updateCompletionRate()
}, {
finish()
})
taskListRecycler.adapter = taskListAdapter
collaboratorListRecycler = findViewById(R.id.collaboratorListRecycler)
// collaboratorListAdapter =
collaboratorListRecycler.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
collaboratorListAdapter = ProjectCollaboratorAdapter(emptyList(), api, projectId) {
updateCollaboratorList()
}
collaboratorListRecycler.adapter = collaboratorListAdapter
// Danger Zone buttons
editProjectButton = findViewById(R.id.editProjectButton)
editProjectButton.setOnClickListener {
val intent = Intent(this, ProjectEditActivity::class.java)
intent.putExtra("project_id", projectId)
intent.putExtra("project_name", projectTitleText.text.toString())
intent.putExtra("project_description", projectDescriptionText.text.toString())
startActivity(intent)
finish()
}
deleteProjectButton = findViewById(R.id.deleteProjectButton)
deleteProjectButton.setOnClickListener {
deleteProject(projectId)
}
if (projectId > 0) {
Log.d("ProjectDetailActivity", "Received project ID: $projectId")
@ -94,16 +130,22 @@ class ProjectDetailActivity : AppCompatActivity() {
projectDescriptionText.text = project.description
var percentageFinished = 0.0;
val collaborators = project.users
val tasks: List<Task> = project.tasks
val totalTasks: Int = tasks.size
val perTaskPercentage = if (totalTasks > 0) (1.0 / totalTasks)*100 else 0.0
var completedTasks = 0
for (task in tasks) {
if (task.status == TaskStatus.COMPLETED) {
percentageFinished += perTaskPercentage
completedTasks++
}
}
completedPercentageText.text = "Completed: ${"%.2f".format(percentageFinished * 100)}%"
percentageFinished = if (totalTasks > 0) (completedTasks.toDouble() / totalTasks.toDouble()) * 100 else 0.0
completedPercentageText.text = "Completed: ${"%.2f".format(percentageFinished)}%"
taskListAdapter.submitList(tasks.toMutableList())
collaboratorListAdapter.submitList(collaborators.toMutableList())
} else {
@ -124,4 +166,90 @@ class ProjectDetailActivity : AppCompatActivity() {
}
private fun updateCompletionRate() {
lifecycleScope.launch {
try {
val projectId = intent.getIntExtra("project_id", -1)
val projectResponse = api.getProjectById(projectId)
if (projectResponse.isSuccessful && projectResponse.body() != null) {
val project = projectResponse.body()!!
val tasks: List<Task> = project.tasks
val totalTasks: Int = tasks.size
var completedTasks = 0
for (task in tasks) {
if (task.status == TaskStatus.COMPLETED) {
completedTasks++
}
}
val percentageFinished = if (totalTasks > 0) (completedTasks.toDouble() / totalTasks.toDouble()) * 100 else 0.0
completedPercentageText.text = "Completed: ${"%.2f".format(percentageFinished)}%"
// Actualizar la lista de tareas también
taskListAdapter.submitList(tasks.toMutableList())
}
} catch (e: Exception) {
Log.e("ProjectDetailActivity", "Error updating completion rate", e)
}
}
}
private fun updateCollaboratorList() {
lifecycleScope.launch {
try {
val projectId = intent.getIntExtra("project_id", -1)
val projectResponse = api.getProjectById(projectId)
if (projectResponse.isSuccessful && projectResponse.body() != null) {
val project = projectResponse.body()!!
val collaborators = project.users
collaboratorListAdapter.submitList(collaborators.toMutableList())
}
} catch (e: Exception) {
Log.e("ProjectDetailActivity", "Error updating collaborator list", e)
}
}
}
private fun deleteProject(projectId: Int) {
lifecycleScope.launch {
try {
Log.d("ProjectDetailActivity", "Deleting project: $projectId")
val response = api.deleteProject(projectId)
if (response.isSuccessful) {
Log.d("ProjectDetailActivity", "Project deleted successfully")
android.widget.Toast.makeText(
this@ProjectDetailActivity,
"Project deleted successfully",
android.widget.Toast.LENGTH_SHORT
).show()
finish()
// Volver a MainActivity
val intent = Intent(this@ProjectDetailActivity, MainActivity::class.java)
startActivity(intent)
} else {
val errorBody = response.errorBody()?.string()
Log.e("ProjectDetailActivity", "Error deleting project: $errorBody")
android.widget.Toast.makeText(
this@ProjectDetailActivity,
"Error deleting project: ${response.code()}",
android.widget.Toast.LENGTH_SHORT
).show()
}
} catch (e: Exception) {
Log.e("ProjectDetailActivity", "Exception deleting project: ${e.message}")
android.widget.Toast.makeText(
this@ProjectDetailActivity,
"Failed to delete project: ${e.message}",
android.widget.Toast.LENGTH_SHORT
).show()
}
}
}
}

View file

@ -14,120 +14,180 @@
app:srcCompat="@android:drawable/ic_menu_revert"
android:id="@+id/returnActionButton"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="@dimen/fab_margin_bottom"
android:layout_marginEnd="@dimen/fab_margin_end"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:text="Project Name"
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/projectTitleText"
android:padding="12dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="4dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintEnd_toEndOf="parent"
android:textSize="24sp" />
android:layout_height="match_parent">
<TextView
android:text="Completed: 100%"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/completedPercentageText"
app:layout_constraintTop_toBottomOf="@+id/projectTitleText"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintEnd_toEndOf="parent"
android:paddingRight="12dp"
android:paddingLeft="12dp"
android:layout_marginTop="16dp"
android:textSize="18dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/projectDescriptionText"
app:layout_constraintTop_toBottomOf="@+id/projectDescriptionLabel"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintEnd_toEndOf="parent"
android:text="Project description"
android:padding="12dp"
android:gravity="start|top" />
<TextView
android:text="Project description:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/projectDescriptionLabel"
tools:layout_editor_absoluteX="0dp"
app:layout_constraintTop_toBottomOf="@+id/completedPercentageText"
android:layout_marginTop="16dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:labelFor="@id/projectDescriptionText"
android:textSize="18dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/projectDescriptionText"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="24dp"
android:id="@+id/taskLinearLayout">
<TextView
android:text="Tasks:"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/taskListTitle"
android:paddingLeft="12dp"
android:textSize="18dp" />
android:orientation="vertical"
android:gravity="start|top">
<Button
android:text="Add a task..."
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/addTaskButton"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp" />
</LinearLayout>
<TextView
android:text="Project Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/projectTitleText"
android:padding="@dimen/padding_standard"
android:layout_marginTop="@dimen/margin_tiny"
android:textSize="24sp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="0dp"
android:id="@+id/collaboratorsLinearLayout"
android:gravity="top|center_vertical"
app:layout_constraintTop_toBottomOf="@+id/taskLinearLayout"
android:layout_marginTop="24dp">
<TextView
android:text="Project description:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/projectDescriptionLabel"
android:layout_marginTop="@dimen/margin_medium"
android:paddingLeft="@dimen/padding_standard"
android:paddingRight="@dimen/padding_standard"
android:labelFor="@id/projectDescriptionText"
android:textSize="@dimen/text_size_title"
android:textStyle="bold" />
<TextView
android:text="Collaborators:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/collaboratorListTitle"
android:paddingLeft="12dp"
android:textSize="18dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/projectDescriptionText"
android:text="Project description"
android:padding="@dimen/padding_standard"
android:gravity="start|top" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/collaboratorListRecycler" />
<TextView
android:text="Completed: 100%"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/completedPercentageText"
android:paddingRight="@dimen/padding_standard"
android:paddingLeft="@dimen/padding_standard"
android:layout_marginTop="@dimen/margin_medium"
android:textSize="@dimen/text_size_subtitle" />
<Button
android:text="Add a new collaborator..."
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/addCollaboratorButton"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_large"
android:id="@+id/taskLinearLayout">
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:text="Tasks:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/taskListTitle"
android:paddingLeft="@dimen/padding_standard"
android:textSize="@dimen/text_size_title"
android:textStyle="bold" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/taskListRecycler"
android:padding="8dp"
android:scrollbars="none"
android:isScrollContainer="false" />
<Button
android:text="Add a task..."
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/addTaskButton"
android:layout_marginLeft="@dimen/margin_standard"
android:layout_marginRight="@dimen/margin_standard"
android:backgroundTint="@color/primary_green" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/collaboratorsLinearLayout"
android:gravity="top|center_vertical"
android:layout_marginTop="@dimen/margin_large">
<TextView
android:text="Collaborators:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/collaboratorListTitle"
android:paddingLeft="@dimen/padding_standard"
android:textSize="@dimen/text_size_title"
android:textStyle="bold" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/collaboratorListRecycler"
android:padding="8dp"
android:scrollbars="none"
android:isScrollContainer="false" />
<Button
android:text="Add a new collaborator..."
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/addCollaboratorButton"
android:layout_marginLeft="@dimen/margin_standard"
android:layout_marginRight="@dimen/margin_standard"
android:backgroundTint="@color/primary_green" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dangerZoneLinearLayout"
android:layout_marginTop="@dimen/margin_xlarge"
android:paddingBottom="16dp">
<TextView
android:text="Danger Zone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dangerZoneTitle"
android:paddingLeft="@dimen/padding_standard"
android:paddingRight="@dimen/padding_standard"
android:textSize="@dimen/text_size_title"
android:textStyle="bold"
android:textColor="@color/danger_red" />
<TextView
android:text="These actions cannot be undone. Proceed with caution."
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dangerZoneWarning"
android:paddingLeft="@dimen/padding_standard"
android:paddingRight="@dimen/padding_standard"
android:layout_marginTop="@dimen/margin_small"
android:textSize="@dimen/text_size_body"
android:textColor="@color/text_secondary" />
<Button
android:text="Edit Project"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editProjectButton"
android:layout_marginLeft="@dimen/margin_standard"
android:layout_marginRight="@dimen/margin_standard"
android:layout_marginTop="@dimen/margin_medium"
android:backgroundTint="@color/warning_orange" />
<Button
android:text="Delete Project"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/deleteProjectButton"
android:layout_marginLeft="@dimen/margin_standard"
android:layout_marginRight="@dimen/margin_standard"
android:layout_marginTop="@dimen/margin_small"
android:backgroundTint="@color/danger_red" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>