Skip to content

Commit

Permalink
Adds feature to color notes according to their color.
Browse files Browse the repository at this point in the history
  • Loading branch information
coderPaddyS committed Nov 4, 2023
1 parent ae0f26a commit 7c7daa8
Show file tree
Hide file tree
Showing 10 changed files with 265 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ interface CategoryDao {

@Query("SELECT name FROM categories WHERE _id=:thisCategoryId ")
fun categoryNameFromId(thisCategoryId: Integer): LiveData<String?>

@Query("SELECT color FROM categories WHERE _id=:category ")
fun getCategoryColor(category: Int): String?
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package org.secuso.privacyfriendlynotes.ui.adapter

import android.content.res.Configuration
import android.graphics.Color
import android.preference.PreferenceManager
import android.text.Html
import android.util.Log
Expand All @@ -33,7 +35,7 @@ import org.secuso.privacyfriendlynotes.ui.main.MainActivityViewModel
*
* @see org.secuso.privacyfriendlynotes.ui.RecycleActivity
*/
class NoteAdapter(private val mainActivityViewModel: MainActivityViewModel) : RecyclerView.Adapter<NoteAdapter.NoteHolder>() {
class NoteAdapter(private val mainActivityViewModel: MainActivityViewModel, ) : RecyclerView.Adapter<NoteAdapter.NoteHolder>() {
private var notes: MutableList<Note> = ArrayList()
private var listener: ((Note) -> Unit)? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoteHolder {
Expand All @@ -59,6 +61,21 @@ class NoteAdapter(private val mainActivityViewModel: MainActivityViewModel) : Re
holder.textViewExtraText.text = null
holder.imageViewcategory.visibility = View.GONE
holder.imageViewcategory.setImageResource(0)

mainActivityViewModel.categoryColor(currentNote.category) {
if (it != null) {
when(holder.textViewTitle.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
Configuration.UI_MODE_NIGHT_YES -> {
holder.textViewTitle.setTextColor(Color.parseColor(it))
holder.textViewExtraText.setTextColor(Color.parseColor(it))
}
else -> {
holder.viewNoteItem.setBackgroundColor(Color.parseColor(it))
}
}
}
}

when (currentNote.type) {
DbContract.NoteEntry.TYPE_TEXT -> {
holder.textViewDescription.text = Html.fromHtml(currentNote.content)
Expand Down Expand Up @@ -112,12 +129,14 @@ class NoteAdapter(private val mainActivityViewModel: MainActivityViewModel) : Re
val textViewDescription: TextView
val imageViewcategory: ImageView
val textViewExtraText: TextView
val viewNoteItem: View

init {
textViewTitle = itemView.findViewById(R.id.text_view_title)
textViewDescription = itemView.findViewById(R.id.text_view_description)
imageViewcategory = itemView.findViewById(R.id.imageView_category)
textViewExtraText = itemView.findViewById(R.id.note_text_extra)
viewNoteItem = itemView.findViewById(R.id.note_item)
itemView.setOnClickListener {
val position = adapterPosition
if (listener != null && position != RecyclerView.NO_POSITION) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.text.Html
import androidx.core.graphics.drawable.toBitmap
import androidx.core.util.Consumer
import androidx.lifecycle.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -76,6 +77,12 @@ class MainActivityViewModel(application: Application) : AndroidViewModel(applica
}
}

fun categoryColor(category: Int, consumer: Consumer<String?>) {
viewModelScope.launch(Dispatchers.Default) {
consumer.accept(repository.categoryDao().getCategoryColor(category))
}
}

private fun filterNoteFlow (filter: String, notes: Flow<List<Note?>?>): Flow<List<Note?>> {
return notes.map {
it.orEmpty().filter { note ->
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
This file is part of the application Privacy Friendly Notes.
Privacy Friendly Notes is free software:
you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation,
either version 3 of the License, or any later version.
Privacy Friendly Notes is distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Privacy Friendly Notes. If not, see <http://www.gnu.org/licenses/>.
*/
package org.secuso.privacyfriendlynotes.ui.manageCategories

import android.content.DialogInterface
import android.os.Bundle
import android.preference.PreferenceManager
import android.view.View
import android.widget.EditText
import android.widget.ImageButton
import android.widget.LinearLayout
import androidx.annotation.ColorInt
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.button.MaterialButton
import eltos.simpledialogfragment.SimpleDialog.OnDialogResultListener
import eltos.simpledialogfragment.color.SimpleColorDialog
import org.secuso.privacyfriendlynotes.R
import org.secuso.privacyfriendlynotes.room.model.Category
import org.secuso.privacyfriendlynotes.ui.SettingsActivity
import org.secuso.privacyfriendlynotes.ui.adapter.CategoryAdapter

/**
* Activity provides possibility to add, delete categories.
* Data is provided by the ManageCategoriesViewModel
* @see ManageCategoriesViewModel
*/
class ManageCategoriesActivity : AppCompatActivity(), View.OnClickListener, OnDialogResultListener {
var manageCategoriesViewModel: ManageCategoriesViewModel? = null
var allCategories: List<Category>? = null
private val etName: EditText by lazy { findViewById(R.id.etName) }
private val recyclerList: RecyclerView by lazy { findViewById(R.id.recyclerview_category) }
private val btnResetColor: ImageButton by lazy { findViewById(R.id.category_menu_color_reset) }
private val btnColorSelector: MaterialButton by lazy { findViewById(R.id.btn_color_selector) }
private val btnExpandMenu: ImageButton by lazy { findViewById(R.id.category_expand_menu_button) }
private val expandMenu: LinearLayout by lazy { findViewById(R.id.category_expand_menu) }
private var catColor: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_manage_categories)
findViewById<View>(R.id.btn_add).setOnClickListener(this)

this.recyclerList.layoutManager = LinearLayoutManager(this)
this.recyclerList.setHasFixedSize(true)
val adapter = CategoryAdapter()
this.recyclerList.adapter = adapter
manageCategoriesViewModel = ViewModelProvider(this).get(ManageCategoriesViewModel::class.java)
manageCategoriesViewModel!!.allCategoriesLive.observe(this) { categories ->
adapter.setCategories(categories)
allCategories = categories
}
adapter.setOnItemClickListener { currentCategory ->
AlertDialog.Builder(this@ManageCategoriesActivity)
.setTitle(String.format(getString(R.string.dialog_delete_title), currentCategory.name))
.setMessage(String.format(getString(R.string.dialog_delete_message), currentCategory.name))
.setNegativeButton(android.R.string.no) { dialog, which ->
//do nothing
}
.setPositiveButton(R.string.dialog_ok) { dialog, which -> deleteCategory(currentCategory) }
.setIcon(android.R.drawable.ic_dialog_alert)
.show()
}

btnColorSelector.setBackgroundColor(resources.getColor(R.color.transparent))
btnExpandMenu.setOnClickListener { expandMenu.visibility = if (expandMenu.visibility == View.GONE) { View.VISIBLE } else { View.GONE } }
btnResetColor.setOnClickListener {
btnColorSelector.setBackgroundColor(resources.getColor(R.color.transparent))
catColor = null
}
btnColorSelector.setOnClickListener { displayColorDialog() }
}

override fun onClick(v: View) {
if (v.id == R.id.btn_add) {
if (etName.text.isNotEmpty()) {
val category = Category(etName.text.toString(), catColor)
if (allCategories!!.firstOrNull { it.name == category.name } == null) {
manageCategoriesViewModel!!.insert(category)
}
}
etName.setText("")
}
}

private fun deleteCategory(cat: Category) {

// Delete all notes from category if the option is set
val sp = PreferenceManager.getDefaultSharedPreferences(this)
if (sp.getBoolean(SettingsActivity.PREF_DEL_NOTES, false)) {
manageCategoriesViewModel!!.allNotesLiveData.observe(this) {
notes -> notes.filter { it.category == cat._id }.forEach { manageCategoriesViewModel!!.delete(it) }
}
}
manageCategoriesViewModel!!.delete(cat)
}

private fun displayColorDialog() {
SimpleColorDialog.build()
.title("")
.allowCustom(true)
.cancelable(true) //allows close by tapping outside of dialog
.colors(this, R.array.mdcolor_500)
.choiceMode(SimpleColorDialog.SINGLE_CHOICE_DIRECT) //auto-close on selection
.show(this, TAG_COLORDIALOG)
}

override fun onResult(dialogTag: String, which: Int, extras: Bundle): Boolean {
if (dialogTag == TAG_COLORDIALOG && which == DialogInterface.BUTTON_POSITIVE) {
@ColorInt val color = extras.getInt(SimpleColorDialog.COLOR)
btnColorSelector.setBackgroundColor(color)
catColor = "#${Integer.toHexString(color)}"
return true
}
return false
}

companion object {
private const val TAG_COLORDIALOG = "org.secuso.privacyfriendlynotes.COLORDIALOG"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,8l-6,6 1.41,1.41L12,10.83l4.59,4.58L18,14z"/>
</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M16.59,8.59L12,13.17 7.41,8.59 6,10l6,6 6,-6z"/>
</vector>
Loading

0 comments on commit 7c7daa8

Please sign in to comment.