Skip to content

Commit

Permalink
Make ChromaView external again
Browse files Browse the repository at this point in the history
  • Loading branch information
suphon-t committed Jul 30, 2018
1 parent 5353dd6 commit f213ee2
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 32 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ new ChromaDialog.Builder()
.show(getSupportFragmentManager(), "ChromaDialog");
```

Don't want a dialog? Use `ChromaView` directly:
```
<me.priyesh.chroma.ChromaView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:initialColor="@android:color/red"
app:colorMode="rgb"/>
ChromaView chromaView = ...;
chromaView.getCurrentColor();
```

Check out the [sample project](chroma-sample) for more details.

License
Expand Down
27 changes: 13 additions & 14 deletions chroma/src/main/kotlin/me/priyesh/chroma/ChromaDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ import android.os.Bundle
import android.support.annotation.ColorInt
import android.support.v4.app.DialogFragment
import android.view.WindowManager
import me.priyesh.chroma.internal.ChromaView
import kotlin.properties.Delegates

class ChromaDialog constructor() : DialogFragment() {
class ChromaDialog : DialogFragment() {

companion object {
private val ArgInitialColor = "arg_initial_color"
Expand Down Expand Up @@ -76,20 +75,20 @@ class ChromaDialog constructor() : DialogFragment() {
}

private var listener: ColorSelectListener? = null
private var chromaView: ChromaView by Delegates.notNull<ChromaView>()
private var chromaView: ChromaView by Delegates.notNull()

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
chromaView = if (savedInstanceState == null) {
ChromaView(
arguments.getInt(ArgInitialColor),
ColorMode.fromName(arguments.getString(ArgColorModeName)),
context)
ChromaView(
arguments!!.getInt(ArgInitialColor),
ColorMode.fromName(arguments!!.getString(ArgColorModeName)),
context!!)
} else {
ChromaView(
savedInstanceState.getInt(ArgInitialColor, ChromaView.DefaultColor),
ColorMode.fromName(savedInstanceState.getString(ArgColorModeName)),
context
)
ChromaView(
savedInstanceState.getInt(ArgInitialColor, ChromaView.DefaultColor),
ColorMode.fromName(savedInstanceState.getString(ArgColorModeName)),
context!!
)
}

chromaView.enableButtonBar(object : ChromaView.ButtonBarListener {
Expand All @@ -115,8 +114,8 @@ class ChromaDialog constructor() : DialogFragment() {
}
}

override fun onSaveInstanceState(outState: Bundle?) {
outState?.putAll(makeArgs(chromaView.currentColor, chromaView.colorMode))
override fun onSaveInstanceState(outState: Bundle) {
outState.putAll(makeArgs(chromaView.currentColor, chromaView.colorMode))
super.onSaveInstanceState(outState)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@
* limitations under the License.
*/

package me.priyesh.chroma.internal
package me.priyesh.chroma

import android.content.Context
import android.graphics.Color
import android.support.annotation.ColorInt
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.RelativeLayout
import me.priyesh.chroma.ColorMode
import me.priyesh.chroma.R
import me.priyesh.chroma.internal.ChannelView

internal class ChromaView : RelativeLayout {
class ChromaView : RelativeLayout {

companion object {
val DefaultColor = Color.GRAY
Expand All @@ -45,11 +44,11 @@ internal class ChromaView : RelativeLayout {
init()
}

private fun init(): Unit {
private fun init() {
inflate(context, R.layout.chroma_view, this)
clipToPadding = false

val colorView = findViewById(R.id.color_view)
val colorView = findViewById<View>(R.id.color_view)
colorView.setBackgroundColor(currentColor)

val channelViews = colorMode.channels.map { ChannelView(it, currentColor, context) }
Expand All @@ -59,7 +58,7 @@ internal class ChromaView : RelativeLayout {
colorView.setBackgroundColor(currentColor)
}

val channelContainer = findViewById(R.id.channel_container) as ViewGroup
val channelContainer = findViewById<ViewGroup>(R.id.channel_container)
channelViews.forEach { it ->
channelContainer.addView(it)

Expand All @@ -71,15 +70,15 @@ internal class ChromaView : RelativeLayout {
}
}

internal interface ButtonBarListener {
interface ButtonBarListener {
fun onPositiveButtonClick(color: Int)
fun onNegativeButtonClick()
}

internal fun enableButtonBar(listener: ButtonBarListener?): Unit {
with(findViewById(R.id.button_bar)) {
val positiveButton = findViewById(R.id.positive_button)
val negativeButton = findViewById(R.id.negative_button)
fun enableButtonBar(listener: ButtonBarListener?) {
with(findViewById<View>(R.id.button_bar)) {
val positiveButton = findViewById<View>(R.id.positive_button)
val negativeButton = findViewById<View>(R.id.negative_button)

if (listener != null) {
visibility = VISIBLE
Expand Down
6 changes: 3 additions & 3 deletions chroma/src/main/kotlin/me/priyesh/chroma/chroma.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import android.graphics.Color
import android.util.DisplayMetrics
import android.view.WindowManager

internal fun screenDimensions(context: Context): DisplayMetrics {
fun screenDimensions(context: Context): DisplayMetrics {
val manager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val metrics = DisplayMetrics()
manager.defaultDisplay.getMetrics(metrics)
return metrics
}

internal fun orientation(context: Context) = context.resources.configuration.orientation
fun orientation(context: Context) = context.resources.configuration.orientation

internal infix fun Int.percentOf(n: Int): Int = (n * (this / 100.0)).toInt()
infix fun Int.percentOf(n: Int): Int = (n * (this / 100.0)).toInt()

fun hue(color: Int): Int = hsv(color, 0)
fun saturation(color: Int): Int = hsv(color, 1, 100)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal class ChannelView(
bindViews(rootView)
}

private fun bindViews(root: View): Unit {
private fun bindViews(root: View) {
(root.findViewById(R.id.label) as TextView).text = context.getString(channel.nameResourceId)

val progressView = root.findViewById(R.id.progress_text) as TextView
Expand All @@ -66,7 +66,7 @@ internal class ChannelView(
})
}

fun registerListener(listener: () -> Unit): Unit {
fun registerListener(listener: () -> Unit) {
this.listener = listener
}

Expand Down

0 comments on commit f213ee2

Please sign in to comment.