Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from Irineu333/release/v1.0.5
Browse files Browse the repository at this point in the history
Release/v1.0.5

fix : correcting crash when digites "rules" in path dialog
feat : highlighting everywhere
  • Loading branch information
Irineu333 committed Jan 25, 2022
2 parents 56a215f + 389afcf commit a0b70f8
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 106 deletions.
8 changes: 6 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ android {
applicationId "com.neo.fbrules"
minSdk 21
targetSdk 31
versionCode 5
versionName "1.0.4"
versionCode 6
versionName "1.0.5"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down Expand Up @@ -124,6 +124,10 @@ dependencies {
//hilt unit tests
testImplementation 'com.google.dagger:hilt-android-testing:2.40'
kaptTest 'com.google.dagger:hilt-compiler:2.40'

//mockk
testImplementation "io.mockk:mockk:1.12.2"
testImplementation "io.mockk:mockk-agent-jvm:1.12.2"
}

kapt {
Expand Down
34 changes: 34 additions & 0 deletions app/src/main/java/com/neo/fbrules/core/constants/RulesHelp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.neo.fbrules.core.constants

fun setupConditions(getIsPortuguese : () -> Boolean) = lazy {
if (getIsPortuguese()) {
arrayListOf(
"Nenhum" to "false",
"Apenas o usuário" to "auth.uid == \$uid",
"Apenas autenticado" to "auth != null",
"Todos" to "true"
)
} else {
arrayListOf(
"None" to "false",
"Only user" to "auth.uid == \$uid",
"Just authenticated" to "auth != null",
"All" to "true"
)

}
}

fun setupProperties(getIsPortuguese : () -> Boolean) = lazy {
if (getIsPortuguese()) {
arrayListOf(
"Leitura" to ".read",
"Escrita" to ".write"
)
} else {
arrayListOf(
"Read" to ".read",
"Write" to ".write"
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.neo.fbrules.main.presenter.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -13,17 +14,19 @@ import com.neo.fbrules.main.presenter.model.PathModel
import com.neo.fbrules.util.dp
import com.neo.fbrules.util.requestColor
import com.neo.highlight.core.Highlight
import com.neo.highlight.util.scheme.BackgroundScheme
import com.neo.highlight.util.scheme.ColorScheme
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONObject
import java.util.regex.Pattern

private typealias PathRulesView = ItemPathBinding

class PathsAdapter(
private val pathListener: RulesPathListener? = null
private val pathListener: RulesPathListener
) : RecyclerView.Adapter<PathsAdapter.Holder>() {

private var paths: MutableList<PathModel> = mutableListOf()
Expand All @@ -48,15 +51,15 @@ class PathsAdapter(
holder.setupHighlight()

holder.addConditionBtn.setOnClickListener {
pathListener?.onAddRule(position)
pathListener.onAddRule(position)
}

holder.itemView.setOnClickListener {
pathListener?.onEditPath(rule, position)
pathListener.onEditPath(rule, position)
}

holder.itemView.setOnLongClickListener {
pathListener?.onRemovePath(position)
pathListener.onRemovePath(position)
true
}
}
Expand Down Expand Up @@ -131,23 +134,40 @@ class PathsAdapter(

class Holder(
private val binding: PathRulesView,
private var onRulePathListener: RulesPathListener? = null
private var onRulePathListener: RulesPathListener
) : RecyclerView.ViewHolder(binding.root) {

val addConditionBtn = binding.mbAddRuleBtn

private val context get() = itemView.context

val addConditionBtn get() = binding.mbAddRuleBtn
private val codeBtn get() = binding.ibCodeBtn

private val ruleConditionAdapter: RulesAdapter
by setupRulesConditionAdapter()

lateinit var pathModel: PathModel

private val pathHighlight = Highlight().apply {
addScheme(
ColorScheme(
Expression.variableInProperty,
context.requestColor(R.color.syntax_variable)
)
)

addScheme(
BackgroundScheme(
Pattern.compile("\\b(TODOS|ALL)\\b"),
Color.GRAY
)
)
}

//setup

private fun setupRulesConditionAdapter() = lazy {
RulesAdapter(onRulePathListener?.let {
object : RulesAdapter.OnRuleClickListener{
val onRuleClickListener = onRulePathListener.let {
object : RulesAdapter.OnRuleClickListener {
override fun onRuleEdit(rule: RuleModel, position: Int) {
it.onEditRule(rule, adapterPosition, position)
}
Expand All @@ -156,7 +176,12 @@ class PathsAdapter(
it.onRemoveRule(adapterPosition, position)
}
}
}) { pathModel }.apply {
}

RulesAdapter(
onRuleClickListener = onRuleClickListener,
getPath = { pathModel }
).apply {
binding.rvRules.adapter = this
}
}
Expand All @@ -167,24 +192,35 @@ class PathsAdapter(
this.pathModel = path

ruleConditionAdapter.updateAll()
binding.tvPath.text = path.rootPath.replaceFirst("rules/", "/")
binding.tvPath.text = getPath()

setupListeners()
configBottomMargin(isLastItem)
}

fun setupHighlight() {
Highlight().apply {
addScheme(
ColorScheme(
Expression.variableInProperty,
context.requestColor(R.color.syntax_variable)
)
)
private fun getPath(): CharSequence {
return if (pathModel.showCode || pathModel.rootPath != "rules") {
pathModel.rootPath.replaceFirst("rules/", "/")
} else if (context.resources.getBoolean(R.bool.portuguese)) {
"TODOS"
} else {
"ALL"
}
}

setSpan(binding.tvPath)
private fun setupListeners() {
codeBtn.setOnClickListener {
pathModel.showCode = !pathModel.showCode
ruleConditionAdapter.updateAll()
binding.tvPath.text = getPath()
setupHighlight()
}
}

fun setupHighlight() {
pathHighlight.setSpan(binding.tvPath)
}

private fun configBottomMargin(lastItem: Boolean) =
with(itemView.layoutParams as ViewGroup.MarginLayoutParams) {
bottomMargin = context.dp(if (lastItem) 6 else 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
package com.neo.fbrules.main.presenter.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.neo.fbrules.R
import com.neo.fbrules.core.Expression
import com.neo.fbrules.core.constants.Highlighting
import com.neo.fbrules.core.constants.setupConditions
import com.neo.fbrules.core.constants.setupProperties
import com.neo.fbrules.databinding.ItemRuleBinding
import com.neo.fbrules.main.presenter.model.RuleModel
import com.neo.fbrules.main.presenter.model.PathModel
import com.neo.fbrules.util.dp
import com.neo.fbrules.util.requestColor
import com.neo.highlight.core.Highlight
import com.neo.highlight.util.scheme.BackgroundScheme
import com.neo.highlight.util.scheme.ColorScheme
import java.util.regex.Pattern

private typealias RuleConditionView = ItemRuleBinding

class RulesAdapter(
private val onRuleClickListener: OnRuleClickListener? = null,
private val getPath: () -> PathModel
private val onRuleClickListener: OnRuleClickListener,
private val getPath: () -> PathModel,
) : RecyclerView.Adapter<RulesAdapter.Holder>() {

private val rule get() = getPath()
Expand All @@ -31,12 +35,13 @@ class RulesAdapter(

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
return Holder(
RuleConditionView
binding = RuleConditionView
.inflate(
LayoutInflater.from(
parent.context
), parent, false
)
),
getShowCode = { rule.showCode }
)
}

Expand All @@ -59,41 +64,103 @@ class RulesAdapter(
holder.itemView.setOnClickListener {
val position = holder.adapterPosition
val rule = conditions[position]
onRuleClickListener?.onRuleEdit(rule, position)
onRuleClickListener.onRuleEdit(rule, position)
}

holder.itemView.setOnLongClickListener {
val position = holder.adapterPosition
val rule = conditions[position]
onRuleClickListener?.onRuleRemove(rule, position)?.let { true } ?: false

onRuleClickListener.onRuleRemove(rule, position)
true
}
}

class Holder(private val binding: RuleConditionView) : RecyclerView.ViewHolder(binding.root) {
class Holder(
private val binding: RuleConditionView,
private val getShowCode: () -> Boolean
) : RecyclerView.ViewHolder(binding.root) {

private val context get() = itemView.context

private val properties by setupProperties { context.resources.getBoolean(R.bool.portuguese) }
private val conditions by setupConditions { context.resources.getBoolean(R.bool.portuguese) }

private lateinit var rule: RuleModel

fun bind(rule: RuleModel, isLastItem: Boolean) {
binding.tvProperty.text = rule.property
binding.tvCondition.text = rule.condition
this.rule = rule

binding.tvProperty.text = getProperty()
binding.tvCondition.text = geCondition()

configBottomMargin(isLastItem)
}

private fun getProperty(): CharSequence {
return if (getShowCode()) {
rule.property
} else {
properties.find {
it.second == rule.property.trim()
}?.first ?: rule.property
}
}

private fun geCondition(): CharSequence {
return if (getShowCode()) {
rule.condition
} else {
conditions.find {
it.second.removeSpaces() == rule.condition.removeSpaces()
}?.first ?: rule.condition
}
}

fun setupHighlight(path: String) {
val highlighting = Highlighting(context)

Highlight().apply {

schemes = highlighting.propertySyntax

properties.forEach { _ ->
addScheme(
BackgroundScheme(
Pattern.compile(
properties.joinToString(
prefix = "(",
separator = ")|(",
postfix = ")"
) { it.first }
),
Color.GRAY
)
)
}

setSpan(binding.tvProperty)
}

Highlight().apply {

schemes = highlighting.conditionSyntax

conditions.forEach { _ ->
addScheme(
BackgroundScheme(
Pattern.compile(
conditions.joinToString(
prefix = "(",
separator = ")|(",
postfix = ")"
) { it.first }
),
Color.GRAY
)
)
}

val matcher = Expression.variableInProperty.matcher(path)
while (matcher.find()) {
val variable = matcher.group()
Expand All @@ -114,6 +181,8 @@ class RulesAdapter(
bottomMargin = context.dp(if (lastItem) 6 else 0)
itemView.layoutParams = this
}

private fun String.removeSpaces() = this.replace(" ", "")
}

interface OnRuleClickListener {
Expand Down
Loading

0 comments on commit a0b70f8

Please sign in to comment.