aboutsummaryrefslogtreecommitdiff
path: root/sample/src/main/kotlin/ca/allanwang/kau/sample/SwipeActivity.kt
blob: cba9ccd121e82c7c81b08f6d44f1891fbc0f3d12 (plain)
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
package ca.allanwang.kau.sample

import android.app.Activity
import android.os.Bundle
import android.support.v7.widget.Toolbar
import android.view.ViewGroup
import android.widget.Button
import ca.allanwang.kau.internal.KauBaseActivity
import ca.allanwang.kau.swipe.*
import ca.allanwang.kau.utils.*

/**
 * Created by Allan Wang on 2017-08-05.
 */
private const val SWIPE_EDGE = "swipe_edge"

fun Activity.startActivityWithEdge(flag: Int) {
    startActivity(SwipeActivity::class.java) {
        putExtra(SWIPE_EDGE, flag)
    }
}

class SwipeActivity : KauBaseActivity() {

    val toolbar: Toolbar by bindView(R.id.swipe_toolbar)
    val container: ViewGroup by bindView(R.id.swipe_container)
    val directions: List<Button> by bindViews(R.id.swipe_from_left, R.id.swipe_from_right, R.id.swipe_from_top, R.id.swipe_from_bottom)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_swipe)
        directions.forEach {
            val swipeEdge = when (it.id) {
                R.id.swipe_from_left -> SWIPE_EDGE_LEFT
                R.id.swipe_from_right -> SWIPE_EDGE_RIGHT
                R.id.swipe_from_top -> SWIPE_EDGE_TOP
                R.id.swipe_from_bottom -> SWIPE_EDGE_BOTTOM
                else -> -1
            }
            it.setOnClickListener { startActivityWithEdge(swipeEdge) }
        }
        val flag = intent.getIntExtra(SWIPE_EDGE, -1)
        kauSwipeOnCreate {
            edgeFlag = flag
        }
        toolbar.title = when (flag) {
            SWIPE_EDGE_LEFT -> "Left Edge Swipe"
            SWIPE_EDGE_RIGHT -> "Right Edge Swipe"
            SWIPE_EDGE_TOP -> "Top Edge Swipe"
            SWIPE_EDGE_BOTTOM -> "Bottom Edge Swipe"
            else -> "Invalid Edge Swipe"
        }
        setSupportActionBar(toolbar)
        val headerColor = rndColor.darken(0.6f)
        toolbar.setBackgroundColor(headerColor)
        statusBarColor = headerColor
        val bg = headerColor.darken(0.2f)
        container.setBackgroundColor(bg)
        navigationBarColor = bg
    }

    override fun onPostCreate(savedInstanceState: Bundle?) {
        super.onPostCreate(savedInstanceState)
        kauSwipeOnPostCreate()
    }

    override fun onDestroy() {
        super.onDestroy()
        kauSwipeOnDestroy()
    }

    override fun onBackPressed() {
        kauSwipeFinish()
    }
}