-
Notifications
You must be signed in to change notification settings - Fork 0
/
TaskView.kt
109 lines (86 loc) · 3.41 KB
/
TaskView.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.example.santiago.kotlintests.queue_list.recycler.view
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.os.AsyncTask
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.commit451.nativestackblur.NativeStackBlur
import com.example.santiago.kotlintests.R
/**
* Created by santiago on 13/04/16.
*/
class TaskView : RelativeLayout {
private val task by lazy { findViewById(R.id.view_task_text) as TextView }
private val binImage by lazy { findViewById(R.id.view_task_bin) as ImageView }
private val requeueImage by lazy { findViewById(R.id.view_task_requeue) as ImageView }
private val blurImage by lazy { findViewById(R.id.view_task_blur) as ImageView }
private var active: Boolean = true
var listener: TaskCallback? = null
private lateinit var startPoint: Pair<Float, Float>
constructor(context: Context) : this(context, null)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
inflate(context, R.layout.view_task, this)
layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
}
fun setText(text: String) {
task.text = text
}
fun setBlur(blur: Boolean) {
if (blur) {
post {
var bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
draw(Canvas(bitmap));
blurImage.visibility = VISIBLE
blurImage.setImageBitmap(NativeStackBlur.process(bitmap, 15));
}
} else blurImage.visibility = GONE
active = !blur
}
override fun onTouchEvent(event: MotionEvent?): Boolean {
if(!active)
return true
fun move() {
if (event != null) {
val padding = (startPoint.first - event.x)
if (padding > 0) {
val params = requeueImage.layoutParams as RelativeLayout.LayoutParams
params.width = padding.toInt()
requeueImage.layoutParams = params
} else {
val params = binImage.layoutParams as RelativeLayout.LayoutParams
params.width = -padding.toInt()
binImage.layoutParams = params
}
}
}
fun finish() {
when {
requeueImage.width > width/2 -> listener?.onRequeue()
binImage.width > width/2 -> listener?.onRemove()
}
val binParams = binImage.layoutParams as RelativeLayout.LayoutParams
binParams.width = 0
binImage.layoutParams = binParams
val requeueParams = requeueImage.layoutParams as RelativeLayout.LayoutParams
requeueParams.width = 0
requeueImage.layoutParams = requeueParams
}
when (event?.action) {
MotionEvent.ACTION_DOWN -> startPoint = Pair(event?.x?:0f, event?.y?:0f)
MotionEvent.ACTION_MOVE -> move()
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> finish()
}
return true
}
interface TaskCallback {
fun onRequeue()
fun onRemove()
}
}