Skip to content

Commit bae9119

Browse files
committed
draw new line when firebase change
1 parent 932047a commit bae9119

File tree

2 files changed

+48
-43
lines changed

2 files changed

+48
-43
lines changed

app/src/main/kotlin/com/trydroid/coboard/CoBoardActivity.kt

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.trydroid.coboard
22

3+
import android.graphics.Point
34
import android.os.Bundle
45
import android.support.v7.app.AppCompatActivity
56
import android.util.Log
67
import android.view.Menu
78
import android.view.MenuItem
89
import com.google.firebase.database.*
9-
import com.trydroid.coboard.views.SimpleDrawView
1010
import kotlinx.android.synthetic.main.activity_main.*
1111

1212
class CoBoardActivity : AppCompatActivity() {
@@ -26,16 +26,16 @@ class CoBoardActivity : AppCompatActivity() {
2626

2727
override fun onOptionsItemSelected(item: MenuItem?) =
2828
when (item?.itemId) {
29-
R.id.action_clear -> consumeMenuSelected { clearDrawView() }
29+
R.id.action_clear -> consumeMenuSelected { removeFirebaseChild() }
3030
else -> super.onOptionsItemSelected(item)
3131
}
3232

3333

3434
override fun onStart() {
3535
super.onStart()
3636
mLinesReference.addChildEventListener(mLinesReferenceListener)
37-
drawView.drawListener = { pointList ->
38-
pointList?.let { sendToFirebase(pointList) }
37+
drawView.drawListener = { lineList ->
38+
lineList?.let { sendToFirebase(lineList) }
3939
}
4040
}
4141

@@ -45,35 +45,42 @@ class CoBoardActivity : AppCompatActivity() {
4545
drawView.drawListener = null
4646
}
4747

48-
private fun sendToFirebase(pointList: List<SimpleDrawView.Point>) {
49-
mLinesReference.push().setValue(pointList)
48+
private fun sendToFirebase(lineList: List<Point>) {
49+
mLinesReference.push().setValue(lineList)
5050
}
5151

52-
private fun clearFirebase() {
53-
mLinesReference.push().removeValue()
52+
private fun removeFirebaseChild() {
53+
mLinesReference.removeValue()
5454
}
5555

5656
private fun clearDrawView() {
5757
drawView.clear()
58-
clearFirebase()
58+
}
59+
60+
private fun drawLine(lineList: List<Point>) {
61+
drawView.drawLine(lineList)
5962
}
6063

6164
private val mLinesReferenceListener = object : ChildEventListener {
62-
override fun onChildMoved(dataSnapshot: DataSnapshot?, p1: String?) {
65+
override fun onChildAdded(dataSnapshot: DataSnapshot?, p1: String?) {
66+
Log.e(TAG, "onChildAdded")
67+
dataSnapshot?.children
68+
?.map { children -> children.getValue<Point>(Point::class.java) }
69+
?.let { lineList -> drawLine(lineList) }
6370
}
6471

65-
override fun onChildChanged(dataSnapshot: DataSnapshot?, p1: String?) {
72+
override fun onChildRemoved(dataSnapshot: DataSnapshot?) {
73+
Log.e(TAG, "onChildRemoved")
74+
clearDrawView()
6675
}
6776

68-
override fun onChildAdded(dataSnapshot: DataSnapshot?, p1: String?) {
69-
Log.e(TAG, "onDataChange")
77+
override fun onChildMoved(dataSnapshot: DataSnapshot?, p1: String?) {
7078
}
7179

72-
override fun onChildRemoved(dataSnapshot: DataSnapshot?) {
80+
override fun onChildChanged(dataSnapshot: DataSnapshot?, p1: String?) {
7381
}
7482

7583
override fun onCancelled(databaseError: DatabaseError) {
76-
Log.w(TAG, "Lines Reference onCancelled", databaseError.toException())
7784
}
7885
}
7986

app/src/main/kotlin/com/trydroid/coboard/views/SimpleDrawView.kt

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
package com.trydroid.coboard.views
22

33
import android.content.Context
4-
import android.graphics.Canvas
5-
import android.graphics.Color
6-
import android.graphics.Paint
7-
import android.graphics.Path
4+
import android.graphics.*
85
import android.util.AttributeSet
96
import android.view.MotionEvent
107
import android.view.View
118

12-
typealias OnDrawListener = (List<SimpleDrawView.Point>?) -> Unit
9+
typealias OnDrawListener = (List<Point>?) -> Unit
1310

1411
/**
1512
* Customize form original source
1613
* - https://github.com/johncarl81/androiddraw/blob/master/src/main/java/org/example/androiddraw/SimpleDrawView.java
1714
* - https://github.com/ByoxCode/DrawView/blob/master/drawview/src/main/java/com/byox/drawview/views/DrawView.java
1815
* */
1916
class SimpleDrawView(context: Context, attributeSet: AttributeSet) : View(context, attributeSet), View.OnTouchListener {
20-
private val mPointList: MutableList<MutableList<Point>> = mutableListOf()
17+
private val mLineHistoryList: MutableList<MutableList<Point>> = mutableListOf()
2118
private val mPaint: Paint by lazy {
2219
Paint(Paint.ANTI_ALIAS_FLAG).apply {
2320
strokeWidth = STROKE_WIDTH
@@ -36,24 +33,21 @@ class SimpleDrawView(context: Context, attributeSet: AttributeSet) : View(contex
3633

3734
override fun onDraw(canvas: Canvas) {
3835
val path = Path()
39-
mPointList.forEach { pointHistory ->
40-
pointHistory.forEachIndexed { index, point ->
36+
mLineHistoryList.forEach { line ->
37+
line.forEachIndexed { index, point ->
38+
val x = point.x.toFloat()
39+
val y = point.y.toFloat()
4140
if (index == 0) {
42-
path.moveTo(point.x, point.y)
41+
path.moveTo(x, y)
4342
}
4443
else {
45-
path.lineTo(point.x, point.y)
44+
path.lineTo(x, y)
4645
}
4746
}
4847
canvas.drawPath(path, mPaint)
4948
}
5049
}
5150

52-
fun clear() {
53-
mPointList.clear()
54-
invalidate()
55-
}
56-
5751
override fun onTouch(view: View, event: MotionEvent): Boolean {
5852
when (event.action) {
5953
MotionEvent.ACTION_DOWN -> {
@@ -70,27 +64,31 @@ class SimpleDrawView(context: Context, attributeSet: AttributeSet) : View(contex
7064
return true
7165
}
7266

73-
private fun onTouchStart() {
74-
mPointList.add(mutableListOf())
67+
fun clear() {
68+
mLineHistoryList.clear()
69+
invalidate()
7570
}
7671

77-
private fun onTouchMove(event: MotionEvent) {
78-
mPointList.last()
79-
.add(Point(x = event.x,
80-
y = event.y))
72+
fun drawLine(lineList: List<Point>) {
73+
mLineHistoryList.add(lineList.toMutableList())
8174
invalidate()
8275
}
8376

84-
private fun onTouchEnd() {
85-
drawListener?.invoke(mPointList.lastOrNull())
77+
private fun onTouchStart() {
78+
mLineHistoryList.add(mutableListOf())
8679
}
8780

88-
inner class Point(val x: Float = 0f,
89-
val y: Float = 0f) {
90-
91-
override fun toString(): String {
92-
return "$x,$y"
81+
private fun onTouchMove(event: MotionEvent) {
82+
val point = Point().apply {
83+
x = event.x.toInt()
84+
y = event.y.toInt()
9385
}
86+
mLineHistoryList.last().add(point)
87+
invalidate()
88+
}
89+
90+
private fun onTouchEnd() {
91+
drawListener?.invoke(mLineHistoryList.lastOrNull())
9492
}
9593

9694
companion object {

0 commit comments

Comments
 (0)