mirror of
https://github.com/a-mayb3/KanbanCloneAndroid.git
synced 2026-03-21 10:05:39 +01:00
Adapted project editing form activity to be aesthetically coherent
This commit is contained in:
parent
74da0d98bc
commit
224d09f1f9
2 changed files with 231 additions and 0 deletions
|
|
@ -0,0 +1,134 @@
|
|||
package com.campusaula.edbole.kanban_clone_android.ui
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.widget.Button
|
||||
import android.widget.EditText
|
||||
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.campusaula.edbole.kanban_clone_android.R
|
||||
import com.campusaula.edbole.kanban_clone_android.kanban.ProjectCreate
|
||||
import com.campusaula.edbole.kanban_clone_android.network.ApiService
|
||||
import com.campusaula.edbole.kanban_clone_android.network.RetrofitInstance
|
||||
import com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class ProjectEditActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var api: ApiService
|
||||
|
||||
private lateinit var returnActionButton: FloatingActionButton
|
||||
private lateinit var projectNameInput: EditText
|
||||
private lateinit var projectDescriptionInput: EditText
|
||||
private lateinit var saveProjectButton: Button
|
||||
|
||||
private var projectId: Int = -1
|
||||
private var currentName: String = ""
|
||||
private var currentDescription: String = ""
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
setContentView(R.layout.activity_project_edit)
|
||||
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
|
||||
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
|
||||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
|
||||
insets
|
||||
}
|
||||
|
||||
api = RetrofitInstance.getRetrofit(applicationContext).create(ApiService::class.java)
|
||||
|
||||
// Get project data from intent
|
||||
projectId = intent.getIntExtra("project_id", -1)
|
||||
currentName = intent.getStringExtra("project_name") ?: ""
|
||||
currentDescription = intent.getStringExtra("project_description") ?: ""
|
||||
|
||||
if (projectId == -1) {
|
||||
Toast.makeText(this, "Error: Invalid project ID", Toast.LENGTH_SHORT).show()
|
||||
finish()
|
||||
return
|
||||
}
|
||||
|
||||
// Initialize views
|
||||
returnActionButton = findViewById(R.id.returnActionButton)
|
||||
projectNameInput = findViewById(R.id.projectNameInput)
|
||||
projectDescriptionInput = findViewById(R.id.projectDescriptionInput)
|
||||
saveProjectButton = findViewById(R.id.saveProjectButton)
|
||||
|
||||
// Populate fields with current project data
|
||||
projectNameInput.setText(currentName)
|
||||
projectDescriptionInput.setText(currentDescription)
|
||||
|
||||
// Set up button listeners
|
||||
returnActionButton.setOnClickListener {
|
||||
finish()
|
||||
|
||||
val intent = Intent(this@ProjectEditActivity, ProjectDetailActivity::class.java)
|
||||
intent.putExtra("project_id", projectId)
|
||||
startActivity(intent)
|
||||
}
|
||||
|
||||
saveProjectButton.setOnClickListener {
|
||||
saveProject()
|
||||
}
|
||||
}
|
||||
|
||||
private fun saveProject() {
|
||||
val newName = projectNameInput.text.toString().trim()
|
||||
val newDescription = projectDescriptionInput.text.toString().trim()
|
||||
|
||||
if (newName.isEmpty()) {
|
||||
Toast.makeText(this, "Project name cannot be empty", Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
}
|
||||
|
||||
lifecycleScope.launch {
|
||||
try {
|
||||
Log.d("ProjectEditActivity", "Updating project: $projectId")
|
||||
val projectCreate = ProjectCreate(
|
||||
name = newName,
|
||||
description = newDescription
|
||||
)
|
||||
|
||||
val response = api.updateProject(projectId, projectCreate)
|
||||
|
||||
if (response.isSuccessful) {
|
||||
Log.d("ProjectEditActivity", "Project updated successfully")
|
||||
Toast.makeText(
|
||||
this@ProjectEditActivity,
|
||||
"Project updated successfully",
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
setResult(RESULT_OK)
|
||||
finish()
|
||||
|
||||
// Reopen ProjectDetailActivity to show the updated project
|
||||
val intent = Intent(this@ProjectEditActivity, ProjectDetailActivity::class.java)
|
||||
intent.putExtra("project_id", projectId)
|
||||
startActivity(intent)
|
||||
} else {
|
||||
val errorBody = response.errorBody()?.string()
|
||||
Log.e("ProjectEditActivity", "Error updating project: $errorBody")
|
||||
Toast.makeText(
|
||||
this@ProjectEditActivity,
|
||||
"Error updating project: ${response.code()}",
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e("ProjectEditActivity", "Exception updating project: ${e.message}")
|
||||
Toast.makeText(
|
||||
this@ProjectEditActivity,
|
||||
"Failed to update project: ${e.message}",
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
97
app/src/main/res/layout/activity_project_edit.xml
Normal file
97
app/src/main/res/layout/activity_project_edit.xml
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/main"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".ui.ProjectEditActivity">
|
||||
|
||||
<com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||
android:id="@+id/returnActionButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginEnd="@dimen/fab_margin_end"
|
||||
android:layout_marginBottom="@dimen/fab_margin_bottom"
|
||||
android:contentDescription="Return"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:srcCompat="@android:drawable/ic_menu_revert" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:paddingBottom="@dimen/bottom_padding_for_fab">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/titleLabel"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Edit Project"
|
||||
android:textSize="@dimen/text_size_title"
|
||||
android:textStyle="bold"
|
||||
android:padding="@dimen/padding_standard"
|
||||
android:layout_marginTop="@dimen/margin_tiny" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/projectNameLabel"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Project Name:"
|
||||
android:textSize="@dimen/text_size_subtitle"
|
||||
android:textStyle="bold"
|
||||
android:layout_marginTop="@dimen/margin_medium"
|
||||
android:paddingLeft="@dimen/padding_standard"
|
||||
android:paddingRight="@dimen/padding_standard" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/projectNameInput"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="Enter project name"
|
||||
android:inputType="text"
|
||||
android:padding="@dimen/padding_standard"
|
||||
android:layout_marginLeft="@dimen/margin_standard"
|
||||
android:layout_marginRight="@dimen/margin_standard"
|
||||
android:layout_marginTop="@dimen/margin_small" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/projectDescriptionLabel"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Description:"
|
||||
android:textSize="@dimen/text_size_subtitle"
|
||||
android:textStyle="bold"
|
||||
android:layout_marginTop="@dimen/margin_medium"
|
||||
android:paddingLeft="@dimen/padding_standard"
|
||||
android:paddingRight="@dimen/padding_standard" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/projectDescriptionInput"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="Enter project description"
|
||||
android:inputType="textMultiLine"
|
||||
android:minLines="3"
|
||||
android:maxLines="5"
|
||||
android:padding="@dimen/padding_standard"
|
||||
android:layout_marginLeft="@dimen/margin_standard"
|
||||
android:layout_marginRight="@dimen/margin_standard"
|
||||
android:layout_marginTop="@dimen/margin_small"
|
||||
android:gravity="start|top" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/saveProjectButton"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Save Changes"
|
||||
android:backgroundTint="@color/primary_green"
|
||||
android:layout_marginTop="@dimen/margin_large"
|
||||
android:layout_marginLeft="@dimen/margin_standard"
|
||||
android:layout_marginRight="@dimen/margin_standard" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue