-
Notifications
You must be signed in to change notification settings - Fork 0
Hw 10 coroutines #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Hw 10 coroutines #10
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.itis.androidlabproject | ||
|
||
import android.app.Application | ||
import androidx.room.Room | ||
import com.itis.androidlabproject.data.AppDatabase | ||
import com.itis.androidlabproject.data.dao.TaskDao | ||
|
||
class App : Application() { | ||
lateinit var database: AppDatabase | ||
lateinit var taskDao: TaskDao | ||
|
||
companion object { | ||
private lateinit var instance: App | ||
|
||
fun getInstance(): App { | ||
return instance | ||
} | ||
} | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
instance = this | ||
database = Room.databaseBuilder( | ||
applicationContext, | ||
AppDatabase::class.java, | ||
"app-db" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше добавить расширение .db, и вынести в константу |
||
).run { | ||
allowMainThreadQueries() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Для корутин можно было уже убрать |
||
build() | ||
} | ||
taskDao = database.taskDao() | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.itis.androidlabproject.adapter | ||
|
||
import android.os.Bundle | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.ListAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.itis.androidlabproject.callbacks.TaskDiffItemCallback | ||
import com.itis.androidlabproject.models.Task | ||
import java.util.ArrayList | ||
|
||
class TaskAdapter( | ||
private val actionChoose: (Int) -> (Unit), | ||
private val actionDelete: (Int) -> (Unit) | ||
) : ListAdapter<Task, TaskHolder>(TaskDiffItemCallback()) { | ||
override fun onCreateViewHolder( | ||
parent: ViewGroup, | ||
viewType: Int | ||
): TaskHolder = TaskHolder.create(parent, actionChoose, actionDelete) | ||
|
||
override fun onBindViewHolder( | ||
holder: TaskHolder, | ||
position: Int) = holder.bind(getItem(position)) | ||
|
||
override fun onBindViewHolder( | ||
holder: TaskHolder, | ||
position: Int, | ||
payloads: MutableList<Any> | ||
) { | ||
if (payloads.isEmpty()) { | ||
super.onBindViewHolder(holder, position, payloads) | ||
} else { | ||
payloads.last().takeIf { | ||
it is Bundle | ||
}?.let { | ||
holder.updateFields(it as Bundle) | ||
} | ||
} | ||
} | ||
|
||
override fun submitList(list: MutableList<Task>?) { | ||
super.submitList( | ||
if (list == null) null | ||
else ArrayList(list) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. перенос строки тут не нужен |
||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.itis.androidlabproject.adapter | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.itis.androidlabproject.databinding.TaskListItemBinding | ||
import com.itis.androidlabproject.models.DateToStringConverter.convertDateToString | ||
import com.itis.androidlabproject.models.Task | ||
|
||
class TaskHolder( | ||
private val binding: TaskListItemBinding, | ||
private val actionChoose: (Int) -> (Unit), | ||
private val actionDelete: (Int) -> (Unit) | ||
) : RecyclerView.ViewHolder(binding.root) { | ||
private var task: Task? = null | ||
|
||
fun bind(item: Task) { | ||
this.task = item | ||
with(binding) { | ||
tvTaskTitle.text = item.title | ||
tvTaskDate.text = convertDateToString(item.date) | ||
|
||
itemView.setOnClickListener { | ||
actionChoose(item.id) | ||
} | ||
ivDelete.setOnClickListener { | ||
actionDelete(item.id) | ||
} | ||
} | ||
} | ||
|
||
fun updateFields(bundle: Bundle?) { | ||
bundle?.run { | ||
getString("TITLE")?.also { | ||
updateTitle(it) | ||
} | ||
getString("DATE")?.also { | ||
updateDate(it) | ||
} | ||
} | ||
} | ||
|
||
private fun updateTitle(title: String) { | ||
binding.tvTaskTitle.text = title | ||
} | ||
|
||
private fun updateDate(date: String) { | ||
binding.tvTaskDate.text = date | ||
} | ||
|
||
companion object { | ||
fun create( | ||
parent: ViewGroup, | ||
actionChoose: (Int) -> Unit, | ||
actionDelete: (Int) -> Unit | ||
) = TaskHolder( | ||
TaskListItemBinding.inflate( | ||
LayoutInflater.from(parent.context), | ||
parent, | ||
false | ||
), | ||
actionChoose, | ||
actionDelete | ||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.itis.androidlabproject.callbacks | ||
|
||
import android.annotation.SuppressLint | ||
import android.os.Bundle | ||
import androidx.core.os.bundleOf | ||
import androidx.recyclerview.widget.DiffUtil | ||
import com.itis.androidlabproject.models.DateToStringConverter | ||
import com.itis.androidlabproject.models.Task | ||
|
||
class TaskDiffItemCallback : DiffUtil.ItemCallback<Task>(){ | ||
override fun areItemsTheSame( | ||
oldItem: Task, | ||
newItem: Task | ||
): Boolean = oldItem.id == newItem.id | ||
|
||
override fun areContentsTheSame( | ||
oldItem: Task, | ||
newItem: Task | ||
): Boolean { | ||
return oldItem.equals(newItem) | ||
} | ||
|
||
override fun getChangePayload( | ||
oldItem: Task, | ||
newItem: Task | ||
): Any? { | ||
val bundle = Bundle().apply { | ||
if (oldItem.title != newItem.title) { | ||
putString("TITLE", newItem.title) | ||
} | ||
|
||
if (oldItem.description != newItem.description) { | ||
putString("DESCRIPTION", newItem.description) | ||
} | ||
|
||
if (oldItem.date != newItem.date) { | ||
putString("DATE", DateToStringConverter.convertDateToString(newItem.date)) | ||
} | ||
|
||
if (oldItem.latitude != newItem.latitude) { | ||
newItem.latitude?.let { | ||
putDouble("LATITUDE", it) | ||
} | ||
} | ||
|
||
if (oldItem.longitude != newItem.longitude) { | ||
newItem.longitude?.let { | ||
putDouble("LONGITUDE", it) | ||
} | ||
} | ||
} | ||
|
||
|
||
if (bundle.isEmpty) return null | ||
return bundle | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.itis.androidlabproject.data | ||
|
||
import android.content.Context | ||
import androidx.room.Database | ||
import androidx.room.Room | ||
import androidx.room.RoomDatabase | ||
import androidx.room.TypeConverters | ||
import com.itis.androidlabproject.data.dao.TaskDao | ||
import com.itis.androidlabproject.models.DateConverter | ||
import com.itis.androidlabproject.models.Task | ||
|
||
@Database( | ||
entities = [Task::class], | ||
version = 1, | ||
exportSchema = false | ||
) | ||
@TypeConverters(DateConverter::class) | ||
abstract class AppDatabase : RoomDatabase() { | ||
abstract fun taskDao(): TaskDao | ||
|
||
companion object { | ||
private const val DB_NAME = "task.db" | ||
|
||
@Volatile | ||
private var instance: AppDatabase? = null | ||
private val LOCK = Any() | ||
|
||
operator fun invoke(context: Context) = Companion.instance ?: synchronized(LOCK) { | ||
buildDatabase(context) | ||
} | ||
|
||
private fun buildDatabase(context: Context) { | ||
instance = Room.databaseBuilder( | ||
context, | ||
AppDatabase::class.java, | ||
DB_NAME | ||
).run { | ||
allowMainThreadQueries() | ||
fallbackToDestructiveMigration() | ||
build() | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.itis.androidlabproject.data.dao | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.room.Delete | ||
import androidx.room.Insert | ||
import androidx.room.Query | ||
import androidx.room.Update | ||
import com.itis.androidlabproject.models.Task | ||
|
||
interface TaskDao { | ||
@Query("SELECT * FROM Task") | ||
suspend fun getAll(): List<Task> | ||
|
||
@Query("SELECT * FROM Task") | ||
suspend fun getAllLiveData(): LiveData<List<Task>> | ||
|
||
@Query("SELECT * FROM Task WHERE id IN (:todoIds)") | ||
suspend fun loadAllByIds(todoIds: IntArray): List<Task> | ||
|
||
@Query("SELECT * FROM Task WHERE title = :title LIMIT 1") | ||
suspend fun findByName(title: String): Task | ||
|
||
@Query("SELECT * FROM Task WHERE id = :id LIMIT 1") | ||
suspend fun findById(id: Int): Task | ||
|
||
@Insert | ||
suspend fun insert(task: Task) | ||
|
||
@Update | ||
suspend fun update(task: Task) | ||
|
||
@Delete | ||
suspend fun delete(task: Task) | ||
|
||
@Query("DELETE FROM Task") | ||
suspend fun deleteAllTasks() | ||
|
||
@Query("DELETE FROM task WHERE id=:id") | ||
suspend fun deleteTaskById(id: Int) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.itis.androidlabproject.extentions | ||
|
||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.navigation.NavController | ||
import androidx.navigation.fragment.NavHostFragment | ||
|
||
fun AppCompatActivity.findController (id: Int) : NavController { | ||
return (supportFragmentManager.findFragmentById(id) as NavHostFragment).navController | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
лучше код создание бд оставить внутри класса AppDatabase, чтобы потом можно было в другом месте переиспользовать при необходисомти