Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@ import android.widget.Spinner
import android.widget.TextView
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.textfield.TextInputEditText

class CreateTaskBottomSheet(
class CreateOrUpdateTaskBottomSheet(
private val categoryList: List<CategoryUiData>,
private val onCreateClicked: (TaskUiData) -> Unit
private val task: TaskUiData? = null,
private val onCreateClicked: (TaskUiData) -> Unit,
private val onUpdateClicked: (TaskUiData) -> Unit
) : BottomSheetDialogFragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.create_task, container, false)
val view = inflater.inflate(R.layout.create_or_update_task_bottom_sheet, container, false)
val btnCreate = view.findViewById<Button>(R.id.btn_task_create)
val tieTaskName = view.findViewById<TextView>(R.id.tie_task_name)
val tvTitle = view.findViewById<TextView>(R.id.tv_title)
val tieTaskName = view.findViewById<TextInputEditText>(R.id.tie_task_name)
val catSpin = view.findViewById<Spinner>(R.id.category_list)
var taskCategory: String? = null
val categoryStr: List<String> = categoryList.map { it.name }
Expand Down Expand Up @@ -52,17 +56,41 @@ class CreateTaskBottomSheet(
btnCreate.setOnClickListener {
if (taskCategory != null) {
val name = tieTaskName.text.toString()
onCreateClicked.invoke(
TaskUiData(
id = 11,
name = name,
category = requireNotNull(taskCategory)

if (task == null) {
onCreateClicked.invoke(
TaskUiData(
id = 11,
name = name,
category = requireNotNull(taskCategory)
)
)
} else {
onUpdateClicked.invoke(
TaskUiData(
id = task.id,
name = name,
category = requireNotNull(taskCategory)
)
)
)
}


} else {
Snackbar.make(btnCreate, "Please select a category", Snackbar.LENGTH_LONG).show()
}
}
if (task == null) {
tvTitle.setText(R.string.create_task_title)
btnCreate.setText(R.string.create)
} else {
tvTitle.setText(R.string.update_task_title)
btnCreate.setText(R.string.update)
tieTaskName.setText(task.name)

val currentCategory = categoryList.first { it.name == task.category }
catSpin.setSelection(categoryList.indexOf(currentCategory))
}
return view
}
}
49 changes: 34 additions & 15 deletions app/src/main/java/com/devspace/taskbeats/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.devspace.taskbeats
//Onde parei? Terminei aula 11 e parei nos 13:30 da aula 12 - que é click na lista de tarefas. ele vai debugar o código agora.
//Onde parei? Terminei aula 13 e devo iniciar a aula 14
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -12,7 +12,7 @@ import kotlinx.coroutines.launch
class MainActivity : AppCompatActivity() {
private var categoriess = listOf<CategoryUiData>()
private var taskss = listOf<TaskUiData>()
private val taskAdapter by lazy{
private val taskAdapter by lazy {
TaskListAdapter()
}
private val categoryAdapter = CategoryListAdapter()
Expand Down Expand Up @@ -46,6 +46,10 @@ class MainActivity : AppCompatActivity() {
showCreateUpdateTaskBottomSheet()
}

taskAdapter.setOnClickListener { task ->
showCreateUpdateTaskBottomSheet(task)
}

categoryAdapter.setOnClickListener { selected ->
if (selected.name == "+") {
val createCategoryBottomSheet = CreateCategoryBottomSheet { categoryName ->
Expand Down Expand Up @@ -85,9 +89,7 @@ class MainActivity : AppCompatActivity() {
getCategoriesFromDataBase()
}
rvTask.adapter = taskAdapter
taskAdapter.setOnClickListener {
showCreateUpdateTaskBottomSheet()
}


//categoryAdapter.submitList(categories)
GlobalScope.launch(Dispatchers.IO) {
Expand Down Expand Up @@ -173,20 +175,37 @@ class MainActivity : AppCompatActivity() {
}
}

private fun showCreateUpdateTaskBottomSheet() {
val createTaskBottomSheet = CreateTaskBottomSheet(categoriess)
{ taskToBeCreated ->
val taskEntityToBeInsert = TaskEntity(
id = taskToBeCreated.id, //cuidado com essa porra
name = taskToBeCreated.name,
category = taskToBeCreated.category
)
insertTask(taskEntityToBeInsert)
private fun updateTask(taskEntity: TaskEntity){
GlobalScope.launch(Dispatchers.IO){
taskDao.update(taskEntity)
getTasksFromDataBase()
}
}

private fun showCreateUpdateTaskBottomSheet(taskUiData: TaskUiData? = null) {
val createTaskBottomSheet = CreateOrUpdateTaskBottomSheet(
task = taskUiData,
categoryList = categoriess,
onCreateClicked = { taskToBeCreated ->
val taskEntityToBeInsert = TaskEntity(
name = taskToBeCreated.name,
category = taskToBeCreated.category
)
insertTask(taskEntityToBeInsert)
},
onUpdateClicked = { taskToBeUpdated ->
val taskEntityToBeUpdate = TaskEntity(
id = taskToBeUpdated.id, //cuidado com essa porra
name = taskToBeUpdated.name,
category = taskToBeUpdated.category)

updateTask(taskEntityToBeUpdate)
}
)
createTaskBottomSheet.show(supportFragmentManager, "createTaskBottomSheet")
}
}

////Estou Enviando o código
val categories: List<CategoryUiData> = listOf(
//Lista vazia pra testar se o dado foi pro DB mesmo.
CategoryUiData(
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/devspace/taskbeats/TaskDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update

@Dao
interface TaskDao {
Expand All @@ -15,4 +16,8 @@ interface TaskDao {

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(taskEntity: TaskEntity)

@Update
fun update(taskEntity: TaskEntity)

}
2 changes: 1 addition & 1 deletion app/src/main/java/com/devspace/taskbeats/TaskEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import androidx.room.PrimaryKey
@Entity
data class TaskEntity(
@PrimaryKey(autoGenerate = true)
val id: Long,
val id: Long = 11,
@ColumnInfo
val name: String,
@ColumnInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="Create Task"
android:text="@string/create_task_title"
android:textSize="24sp"
android:textStyle="bold" />

Expand Down Expand Up @@ -46,7 +46,7 @@
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="Create" />
android:text="@string/create" />


</LinearLayout>
1 change: 0 additions & 1 deletion app/src/main/res/layout/item_task.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:foreground="?android:selectableItemBackground"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<resources>
<string name="app_name">TaskBeats</string>
<string name="app_name">TaskBeat</string>
<string name="home_title">Task Beat</string>
<string name="create_task_title">Create task</string>
<string name="create">Create</string>
<string name="update_task_title">Update task</string>
<string name="update">Update</string>
</resources>