mirror of
https://github.com/a-mayb3/KanbanCloneAndroid.git
synced 2026-03-21 18:15:38 +01:00
Made MainActivity.kt load projects in recyclerview and goto ProjectDetailActivity.kt on click
This commit is contained in:
parent
49a0e83e3e
commit
39276513a9
7 changed files with 140 additions and 17 deletions
|
|
@ -16,10 +16,13 @@
|
||||||
android:theme="@style/Theme.KanbanCloneAndroid"
|
android:theme="@style/Theme.KanbanCloneAndroid"
|
||||||
android:usesCleartextTraffic="true">
|
android:usesCleartextTraffic="true">
|
||||||
<activity
|
<activity
|
||||||
android:name="com.campusaula.edbole.kanban_clone_android.ui.LoginActivity"
|
android:name=".ui.ProjectDetailActivity"
|
||||||
android:exported="false" />
|
android:exported="false" />
|
||||||
<activity
|
<activity
|
||||||
android:name="com.campusaula.edbole.kanban_clone_android.ui.MainActivity"
|
android:name=".ui.LoginActivity"
|
||||||
|
android:exported="false" />
|
||||||
|
<activity
|
||||||
|
android:name=".ui.MainActivity"
|
||||||
android:exported="true">
|
android:exported="true">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.view.ViewCompat
|
import androidx.core.view.ViewCompat
|
||||||
import androidx.core.view.WindowInsetsCompat
|
import androidx.core.view.WindowInsetsCompat
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import com.campusaula.edbole.kanban_clone_android.R
|
import com.campusaula.edbole.kanban_clone_android.R
|
||||||
import com.campusaula.edbole.kanban_clone_android.kanban.Project
|
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.ApiService
|
||||||
|
|
@ -20,8 +21,9 @@ class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
private lateinit var api: ApiService
|
private lateinit var api: ApiService
|
||||||
private lateinit var projectList : List<Project>
|
private lateinit var projectList : List<Project>
|
||||||
private lateinit var loggedInAs: TextView;
|
private lateinit var loggedInAs: TextView
|
||||||
private lateinit var logoutButton: Button;
|
private lateinit var logoutButton: Button
|
||||||
|
private lateinit var projectsRecyclerView: RecyclerView
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|
@ -39,6 +41,14 @@ class MainActivity : AppCompatActivity() {
|
||||||
/* Activity components */
|
/* Activity components */
|
||||||
loggedInAs = findViewById(R.id.loggedInAs)
|
loggedInAs = findViewById(R.id.loggedInAs)
|
||||||
logoutButton = findViewById(R.id.logoutButton)
|
logoutButton = findViewById(R.id.logoutButton)
|
||||||
|
projectsRecyclerView = findViewById(R.id.projectsRecyclerView)
|
||||||
|
projectsRecyclerView.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
|
||||||
|
val adapter = ProjectItemAdapter(projectList) { project ->
|
||||||
|
val intent = Intent(this, ProjectDetailActivity::class.java)
|
||||||
|
intent.putExtra("project_id", project.id)
|
||||||
|
startActivity(intent)
|
||||||
|
}
|
||||||
|
projectsRecyclerView.adapter = adapter
|
||||||
|
|
||||||
/* Getting the logged-in user info */
|
/* Getting the logged-in user info */
|
||||||
lifecycleScope.launch{
|
lifecycleScope.launch{
|
||||||
|
|
@ -47,6 +57,8 @@ class MainActivity : AppCompatActivity() {
|
||||||
if (getMe.isSuccessful){
|
if (getMe.isSuccessful){
|
||||||
val user = getMe.body()
|
val user = getMe.body()
|
||||||
loggedInAs.text = "Logged in as: ${user?.name}"
|
loggedInAs.text = "Logged in as: ${user?.name}"
|
||||||
|
projectList = api.getAllProjects().body()!!
|
||||||
|
adapter.submitList(projectList)
|
||||||
} else {
|
} else {
|
||||||
val intent = Intent(this@MainActivity, LoginActivity::class.java)
|
val intent = Intent(this@MainActivity, LoginActivity::class.java)
|
||||||
startActivity(intent)
|
startActivity(intent)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.campusaula.edbole.kanban_clone_android.ui
|
||||||
|
|
||||||
|
import android.os.Bundle
|
||||||
|
import androidx.activity.enableEdgeToEdge
|
||||||
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
import androidx.core.view.ViewCompat
|
||||||
|
import androidx.core.view.WindowInsetsCompat
|
||||||
|
import com.campusaula.edbole.kanban_clone_android.R
|
||||||
|
|
||||||
|
class ProjectDetailActivity : AppCompatActivity() {
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
enableEdgeToEdge()
|
||||||
|
setContentView(R.layout.activity_project_detail)
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,44 @@
|
||||||
|
package com.campusaula.edbole.kanban_clone_android.ui
|
||||||
|
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.widget.TextView
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import com.campusaula.edbole.kanban_clone_android.R
|
||||||
|
import com.campusaula.edbole.kanban_clone_android.kanban.Project
|
||||||
|
|
||||||
|
class ProjectItemAdapter(
|
||||||
|
private var items: List<Project>,
|
||||||
|
private val onItemClick: ((Project) -> Unit)? = null
|
||||||
|
) : RecyclerView.Adapter<ProjectItemAdapter.ViewHolder>() {
|
||||||
|
|
||||||
|
fun submitList(newList: List<Project>) {
|
||||||
|
items = newList
|
||||||
|
notifyDataSetChanged()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||||
|
val view = LayoutInflater.from(parent.context)
|
||||||
|
.inflate(R.layout.item_project, parent, false)
|
||||||
|
return ViewHolder(view)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||||
|
val project = items[position]
|
||||||
|
holder.bind(project)
|
||||||
|
holder.itemView.setOnClickListener { onItemClick?.invoke(project) }
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItemCount(): Int = items.size
|
||||||
|
|
||||||
|
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||||
|
private val nameTv: TextView = itemView.findViewById(R.id.projectName)
|
||||||
|
private val descTv: TextView = itemView.findViewById(R.id.projectDescription)
|
||||||
|
|
||||||
|
fun bind(project: Project) {
|
||||||
|
nameTv.text = project.name
|
||||||
|
descTv.text = project.description
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -37,6 +37,7 @@
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
tools:layout_editor_absoluteX="1dp"
|
tools:layout_editor_absoluteX="1dp"
|
||||||
app:layout_constraintTop_toBottomOf="@+id/logoutButton"
|
app:layout_constraintTop_toBottomOf="@+id/logoutButton"
|
||||||
android:layout_marginTop="8dp" />
|
android:layout_marginTop="8dp"
|
||||||
|
android:id="@+id/projectsRecyclerView" />
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
10
app/src/main/res/layout/activity_project_detail.xml
Normal file
10
app/src/main/res/layout/activity_project_detail.xml
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:id="@+id/main"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
tools:context=".ui.ProjectDetailActivity">
|
||||||
|
|
||||||
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
32
app/src/main/res/layout/item_project.xml
Normal file
32
app/src/main/res/layout/item_project.xml
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
<?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"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:padding="12dp">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/projectName"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="Project name"
|
||||||
|
android:textStyle="bold"
|
||||||
|
android:textSize="16sp"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/projectDescription"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="Project description"
|
||||||
|
android:textSize="14sp"
|
||||||
|
android:ellipsize="end"
|
||||||
|
android:maxLines="2"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@id/projectName"
|
||||||
|
android:layout_marginTop="4dp" />
|
||||||
|
|
||||||
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue