Made MainActivity.kt get list of projects from logged-in user

This commit is contained in:
Marta Borgia Leiva 2026-02-06 10:57:02 +01:00
parent 9c27038313
commit 23dba7e1d0
6 changed files with 110 additions and 24 deletions

View file

@ -51,6 +51,7 @@ dependencies {
implementation("com.squareup.retrofit2:converter-gson:2.9.0")
implementation("com.auth0.android:jwtdecode:2.0.1")
implementation(libs.androidx.recyclerview)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)

View file

@ -1,20 +0,0 @@
package com.campusaula.edbole.kanban_clone_android
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
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
}
}
}

View file

@ -11,7 +11,7 @@ interface ApiService {
@POST("auth/login/")
suspend fun login(@Body userLogin: UserLogin): Response<LoginResponse>
@POST("me/logout/")
@GET("me/logout/")
suspend fun logout(): Response<Unit>
@DELETE("me/delete-me/")

View file

@ -0,0 +1,80 @@
package com.campusaula.edbole.kanban_clone_android.ui
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
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.Project
import com.campusaula.edbole.kanban_clone_android.network.ApiService
import com.campusaula.edbole.kanban_clone_android.network.RetrofitInstance
import kotlinx.coroutines.launch
class MainActivity : AppCompatActivity() {
private lateinit var api: ApiService
private lateinit var projectList : List<Project>
private lateinit var loggedInAs: TextView;
private lateinit var logoutButton: Button;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
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)
projectList = emptyList()
/* Activity components */
loggedInAs = findViewById(R.id.loggedInAs)
logoutButton = findViewById(R.id.logoutButton)
/* Getting the logged-in user info */
lifecycleScope.launch{
val getMe = api.getMe()
if (getMe.isSuccessful){
val user = getMe.body()
loggedInAs.text = "Logged in as: ${user?.name}"
} else {
val intent = Intent(this@MainActivity, LoginActivity::class.java)
startActivity(intent)
}
}
logoutButton.setOnClickListener {
lifecycleScope.launch {
val logoutResponse = api.logout()
if (logoutResponse.isSuccessful) {
// Clear cookies for the API host
RetrofitInstance.clearCookiesForHost(
"10.0.2.2:8000"
)
// Navigate back to the login screen
val intent =
Intent(this@MainActivity, LoginActivity::class.java)
startActivity(intent)
finish() // Optional: close the MainActivity so it's removed from the back stack
} else {
Toast.makeText(
this@MainActivity,
"Logout failed",
Toast.LENGTH_SHORT
).show()
}
}
}
}
}

View file

@ -5,15 +5,38 @@
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.campusaula.edbole.kanban_clone_android.MainActivity">
tools:context="com.campusaula.edbole.kanban_clone_android.ui.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:text="Logged in as: "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/loggedInAs"
android:textAlignment="textStart"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.0"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp" />
<Button
android:text="Not you? Log out here..."
android:layout_width="wrap_content"
android:layout_height="33dp"
android:id="@+id/logoutButton"
app:layout_constraintTop_toBottomOf="@+id/loggedInAs"
app:layout_constraintStart_toStartOf="@+id/loggedInAs"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:textSize="8sp" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="1dp"
app:layout_constraintTop_toBottomOf="@+id/logoutButton"
android:layout_marginTop="8dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -10,6 +10,7 @@ activity = "1.12.2"
constraintlayout = "2.2.1"
okhttp = "4.11.0"
retrofit = "2.9.0"
recyclerview = "1.4.0"
[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
@ -24,6 +25,7 @@ okhttp = { group = "com.squareup.okhttp3", name = "okhttp", version.ref = "okhtt
okhttp-logging-interceptor = { group = "com.squareup.okhttp3", name = "logging-interceptor", version.ref = "okhttp" }
retrofit = { group = "com.squareup.retrofit2", name = "retrofit", version.ref = "retrofit" }
retrofit-converter-gson = { group = "com.squareup.retrofit2", name = "converter-gson", version.ref = "retrofit" }
androidx-recyclerview = { group = "androidx.recyclerview", name = "recyclerview", version.ref = "recyclerview" }
[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }