mirror of
https://github.com/a-mayb3/KanbanCloneAndroid.git
synced 2026-03-21 18:15:38 +01:00
Created collaborator add form
This commit is contained in:
parent
224d09f1f9
commit
d7eb6ab646
2 changed files with 197 additions and 0 deletions
|
|
@ -0,0 +1,126 @@
|
|||
package com.campusaula.edbole.kanban_clone_android.ui
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.util.Patterns
|
||||
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.network.ApiService
|
||||
import com.campusaula.edbole.kanban_clone_android.network.RetrofitInstance
|
||||
import com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class CollaboratorAddActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var api: ApiService
|
||||
|
||||
private lateinit var returnActionButton: FloatingActionButton
|
||||
private lateinit var collaboratorEmailInput: EditText
|
||||
private lateinit var addCollaboratorButton: Button
|
||||
|
||||
private var projectId: Int = -1
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
setContentView(R.layout.activity_collaborator_add)
|
||||
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 ID from intent
|
||||
projectId = intent.getIntExtra("project_id", -1)
|
||||
|
||||
if (projectId == -1) {
|
||||
Toast.makeText(this, "Error: Invalid project ID", Toast.LENGTH_SHORT).show()
|
||||
finish()
|
||||
return
|
||||
}
|
||||
|
||||
// Initialize views
|
||||
returnActionButton = findViewById(R.id.returnActionButton)
|
||||
collaboratorEmailInput = findViewById(R.id.collaboratorEmailInput)
|
||||
addCollaboratorButton = findViewById(R.id.addCollaboratorButton)
|
||||
|
||||
// Set up button listeners
|
||||
returnActionButton.setOnClickListener {
|
||||
finish()
|
||||
|
||||
val intent = Intent(this@CollaboratorAddActivity, ProjectDetailActivity::class.java)
|
||||
intent.putExtra("project_id", projectId)
|
||||
startActivity(intent)
|
||||
|
||||
}
|
||||
|
||||
addCollaboratorButton.setOnClickListener {
|
||||
addCollaborator()
|
||||
}
|
||||
}
|
||||
|
||||
private fun addCollaborator() {
|
||||
val user_email = collaboratorEmailInput.text.toString().trim()
|
||||
|
||||
// Validate email
|
||||
if (user_email.isEmpty()) {
|
||||
Toast.makeText(this, "Email cannot be empty", Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
}
|
||||
|
||||
if (!Patterns.EMAIL_ADDRESS.matcher(user_email).matches()) {
|
||||
Toast.makeText(this, "Please enter a valid email address", Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
}
|
||||
|
||||
lifecycleScope.launch {
|
||||
try {
|
||||
Log.d("CollaboratorAddActivity", "Adding collaborator: $user_email")
|
||||
val emailBody = mapOf("user_email" to user_email)
|
||||
val response = api.addProjectCollaborator(projectId, emailBody)
|
||||
|
||||
if (response.isSuccessful) {
|
||||
Log.d("CollaboratorAddActivity", "Collaborator added successfully")
|
||||
Toast.makeText(
|
||||
this@CollaboratorAddActivity,
|
||||
"Collaborator added successfully",
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
setResult(RESULT_OK)
|
||||
finish()
|
||||
|
||||
// Reopen ProjectDetailActivity to show the new collaborator
|
||||
val intent = Intent(this@CollaboratorAddActivity, ProjectDetailActivity::class.java)
|
||||
intent.putExtra("project_id", projectId)
|
||||
startActivity(intent)
|
||||
} else {
|
||||
val errorBody = response.errorBody()?.string()
|
||||
Log.e("CollaboratorAddActivity", "Error adding collaborator: $errorBody")
|
||||
Toast.makeText(
|
||||
this@CollaboratorAddActivity,
|
||||
"Error adding collaborator: ${response.code()}",
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e("CollaboratorAddActivity", "Exception adding collaborator: ${e.message}")
|
||||
Toast.makeText(
|
||||
this@CollaboratorAddActivity,
|
||||
"Failed to add collaborator: ${e.message}",
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
71
app/src/main/res/layout/activity_collaborator_add.xml
Normal file
71
app/src/main/res/layout/activity_collaborator_add.xml
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
<?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.CollaboratorAddActivity">
|
||||
|
||||
<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="Add Collaborator"
|
||||
android:textSize="@dimen/text_size_title"
|
||||
android:textStyle="bold"
|
||||
android:padding="@dimen/padding_standard"
|
||||
android:layout_marginTop="@dimen/margin_tiny" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/collaboratorEmailLabel"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Email Address:"
|
||||
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/collaboratorEmailInput"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="Enter collaborator email"
|
||||
android:inputType="textEmailAddress"
|
||||
android:padding="@dimen/padding_standard"
|
||||
android:layout_marginLeft="@dimen/margin_standard"
|
||||
android:layout_marginRight="@dimen/margin_standard"
|
||||
android:layout_marginTop="@dimen/margin_small" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/addCollaboratorButton"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Add Collaborator"
|
||||
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