Skip to content

Commit

Permalink
Текущее время и дата, изменение цвета фона и текстов, принятие данных…
Browse files Browse the repository at this point in the history
… с NoteDetail в NoteFragment
  • Loading branch information
aruyume committed May 22, 2024
1 parent e1ad013 commit fc85fae
Show file tree
Hide file tree
Showing 28 changed files with 449 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

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

8 changes: 7 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.jetbrains.kotlin.android)

//SaveArgs
id("androidx.navigation.safeargs.kotlin")
//Ksp
id("com.google.devtools.ksp")

}

Expand Down Expand Up @@ -63,4 +65,8 @@ dependencies {
implementation ("com.airbnb.android:lottie:$lottieVersion")
implementation ("com.google.code.gson:gson:2.8.8")

//Room
val roomVersion = "2.6.1"
implementation("androidx.room:room-ktx:$roomVersion")
ksp("androidx.room:room-compiler:$roomVersion") //подключила ksp
}
24 changes: 23 additions & 1 deletion app/src/main/java/com/example/noteapp/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,33 @@ package com.example.noteapp

import PreferenceHelper
import android.app.Application
import androidx.room.Database
import androidx.room.Room
import com.example.noteapp.data.db.AppDatabase

class App : Application() {

companion object {
var appDatabase: AppDatabase? = null
}

override fun onCreate() {
super.onCreate()
val sharedPreferences = PreferenceHelper(this)
sharedPreferences.unit(this)
getInstance()
}

fun getInstance(): AppDatabase? {
if (appDatabase == null) {
appDatabase = applicationContext?.let {
Room.databaseBuilder(
it,
AppDatabase::class.java,
"note,database"
).fallbackToDestructiveMigration().allowMainThreadQueries().build()
}
}
return appDatabase
}
}
}
3 changes: 0 additions & 3 deletions app/src/main/java/com/example/noteapp/data/NoteModel.kt

This file was deleted.

12 changes: 12 additions & 0 deletions app/src/main/java/com/example/noteapp/data/db/AppDatabase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.noteapp.data.db

import androidx.room.Database
import androidx.room.RoomDatabase
import com.example.noteapp.data.db.daos.NoteDao
import com.example.noteapp.data.model.NoteModel

@Database(entities = [NoteModel::class], version = 4)
abstract class AppDatabase: RoomDatabase() {

abstract fun noteDao(): NoteDao
}
18 changes: 18 additions & 0 deletions app/src/main/java/com/example/noteapp/data/db/daos/NoteDao.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.noteapp.data.db.daos

import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.example.noteapp.data.model.NoteModel

@Dao
interface NoteDao {

@Query ("SELECT * FROM noteModel")
fun getAll(): LiveData<List<NoteModel>>

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertNote(noteModel: NoteModel)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.noteapp.data
package com.example.noteapp.data.extension

import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/example/noteapp/data/model/NoteModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.noteapp.data.model

import androidx.room.Entity
import androidx.room.PrimaryKey
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

@Entity(tableName = "noteModel")
data class NoteModel(
val title: String,
val description: String,
val date: String,
val time: String,
val selectedColor: String
) {
@PrimaryKey(autoGenerate = true)
var id: Int = 0
}
50 changes: 47 additions & 3 deletions app/src/main/java/com/example/noteapp/ui/adapter/NoteAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,60 @@ package com.example.noteapp.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.noteapp.data.NoteModel
import com.example.noteapp.R
import com.example.noteapp.data.model.NoteModel
import com.example.noteapp.databinding.ItemNoteBinding

class NoteAdapter : ListAdapter<NoteModel, NoteAdapter.ViewHolder>(DiffCallback()) {

class ViewHolder(private val binding: ItemNoteBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(item: NoteModel) {
binding.itemText.text = item.title
binding.itemTitle.text = item.title
binding.itemDescription.text = item.description
binding.itemDate.text = item.date
binding.itemTime.text = item.time

val background: Int
val title: Int
val description: Int
val date: Int
val time: Int

val context = binding.root.context

when (item.selectedColor) {
"white" -> {
background = R.drawable.bg_item_white
title = R.color.beige
description = R.color.beige
date = R.color.beige
time = R.color.beige
}
"red" -> {
background = R.drawable.bg_item_red
title = R.color.orange
description = R.color.orange
date = R.color.orange
time = R.color.orange
}
else -> {
background = R.drawable.bg_item_black
title = R.color.white
description = R.color.white
date = R.color.white
time = R.color.white
}
}

binding.root.background = ContextCompat.getDrawable(context, background)
binding.itemTitle.setTextColor(ContextCompat.getColor(context, title))
binding.itemDescription.setTextColor(ContextCompat.getColor(context, description))
binding.itemDate.setTextColor(ContextCompat.getColor(context, date))
binding.itemTime.setTextColor(ContextCompat.getColor(context, time))
}
}

Expand All @@ -30,7 +74,7 @@ class NoteAdapter : ListAdapter<NoteModel, NoteAdapter.ViewHolder>(DiffCallback(
}

override fun areContentsTheSame(oldItem: NoteModel, newItem: NoteModel): Boolean {
return oldItem.title == newItem.title
return oldItem.id == newItem.id
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@

package com.example.noteapp.ui.fragment.note

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.navigation.fragment.findNavController
import com.example.noteapp.App
import com.example.noteapp.R
import com.example.noteapp.data.setBackStackData
import com.example.noteapp.data.extension.setBackStackData
import com.example.noteapp.data.model.NoteModel
import com.example.noteapp.databinding.FragmentDetailNoteBinding
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

class DetailNoteFragment : Fragment() {

private lateinit var binding: FragmentDetailNoteBinding
private var selectedColor: String = "black"

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
Expand All @@ -25,12 +32,68 @@ class DetailNoteFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setupListener()
currentDateTime()
setupRadioGroupListener()
}

private fun setupListener() {
binding.btnAddText.setOnClickListener {
val et = binding.etAdd.text.toString()
setBackStackData("key", et, true)
binding.btnAddText.setOnClickListener{
val etTitle = binding.etTitle.text.toString()
val etDescription = binding.etDescription.text.toString()
val currentDate = binding.tvDate.text.toString()
val currentTime = binding.tvTime.text.toString()

App().getInstance()?.noteDao()?.insertNote(NoteModel(etTitle, etDescription, currentDate, currentTime, selectedColor))
findNavController().navigateUp()
}
binding.imgBack.setOnClickListener {
findNavController().navigateUp()
}
}

private fun currentDateTime() {
val currentDate = SimpleDateFormat("dd MMM", Locale.getDefault()).format(Date())
binding.tvDate.text = currentDate

val currentTime = SimpleDateFormat("HH:mm", Locale.getDefault()).format(Date())
binding.tvTime.text = currentTime
}

private fun setupRadioGroupListener() {
binding.radioGroup.setOnCheckedChangeListener { _, checkedId ->
selectedColor = when (checkedId) {
R.id.radio_btn_black -> "black"
R.id.radio_btn_white -> "white"
R.id.radio_btn_red -> "red"
else -> "black"
}
updateBackgroundColor()
}
}
private fun updateBackgroundColor() {
val colorResId = when (selectedColor) {
"white" -> {
binding.etDescription.setTextColor(ContextCompat.getColor(requireContext(), R.color.beige))
binding.etTitle.setTextColor(ContextCompat.getColor(requireContext(), R.color.beige))
binding.tvDate.setTextColor(ContextCompat.getColor(requireContext(), R.color.beige))
binding.tvTime.setTextColor(ContextCompat.getColor(requireContext(), R.color.beige))
R.color.whiteRadio
}
"red" -> {
binding.etDescription.setTextColor(ContextCompat.getColor(requireContext(), R.color.orange))
binding.etTitle.setTextColor(ContextCompat.getColor(requireContext(), R.color.orange))
binding.tvDate.setTextColor(ContextCompat.getColor(requireContext(), R.color.orange))
binding.tvTime.setTextColor(ContextCompat.getColor(requireContext(), R.color.orange))
R.color.redRadio
}
else -> {
binding.etDescription.setTextColor(ContextCompat.getColor(requireContext(), R.color.white))
binding.etTitle.setTextColor(ContextCompat.getColor(requireContext(), R.color.white))
binding.tvDate.setTextColor(ContextCompat.getColor(requireContext(), R.color.white))
binding.tvTime.setTextColor(ContextCompat.getColor(requireContext(), R.color.white))
R.color.blackRadio
}
}
binding.root.setBackgroundColor(ContextCompat.getColor(requireContext(), colorResId))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.noteapp.App
import com.example.noteapp.R
import com.example.noteapp.data.NoteModel
import com.example.noteapp.data.getBackStackData
import com.example.noteapp.databinding.FragmentNoteBinding
import com.example.noteapp.ui.adapter.NoteAdapter

class NoteFragment : Fragment() {

private lateinit var binding: FragmentNoteBinding
private val noteAdapter = NoteAdapter()
private val list: ArrayList<NoteModel> = ArrayList()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
Expand Down Expand Up @@ -47,10 +45,8 @@ class NoteFragment : Fragment() {
}

private fun getData() {
getBackStackData<String>("key") { data ->
val noteModel = NoteModel(data)
list.add(noteModel)
noteAdapter.submitList(list)
App().getInstance()?.noteDao()?.getAll()?.observe(viewLifecycleOwner) {
noteAdapter.submitList(it)
}
}
}
Binary file added app/src/main/res/drawable/back.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/bg_ed.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="12dp" />
<corners android:radius="20dp" />
<solid android:color="@color/black3" />
</shape>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/bg_item_black.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/blackRadio" />
<corners android:radius="16dp" />
</shape>
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/bg_item_note.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="12dp" />
<corners android:radius="20dp" />
<solid android:color="@color/black3" />
</shape>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/bg_item_red.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/redRadio"/>
<corners android:radius="16dp"/>
</shape>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/bg_item_white.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/whiteRadio" />
<corners android:radius="16dp" />
</shape>
8 changes: 8 additions & 0 deletions app/src/main/res/drawable/circle_gray.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/gray1" />
<size
android:width="20dp"
android:height="20dp" />
</shape>
8 changes: 8 additions & 0 deletions app/src/main/res/drawable/circle_orange.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/orange" />
<size
android:width="20dp"
android:height="20dp" />
</shape>
Loading

0 comments on commit fc85fae

Please sign in to comment.