mirror of
https://github.com/a-mayb3/KanbanCloneAndroid.git
synced 2026-03-21 10:05:39 +01:00
Made MainActivity.kt get list of projects from logged-in user
This commit is contained in:
parent
9c27038313
commit
23dba7e1d0
6 changed files with 110 additions and 24 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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/")
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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>
|
||||
Loading…
Add table
Add a link
Reference in a new issue