aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/ca/allanwang/kau/swipe/RelativeSlider.kt
blob: e3baa94bbe5beaf27940eadb65f82815d34a5b17 (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
76
/*
 * Copyright 2018 Allan Wang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ca.allanwang.kau.swipe

import kotlin.math.max
import kotlin.math.min

/**
 * Created by Mr.Jude on 2015/8/26.
 *
 * Updated by Allan Wang on 2017/07/05
 *
 * Helper class to give the previous activity an offset as the main page is pulled
 */
internal class RelativeSlider(var curPage: SwipeBackPage) : SwipeListener {

    var offset = 0f

    var enabled: Boolean
        get() = curPage.hasListener(this)
        set(value) {
            if (value) curPage.addListener(this)
            else curPage.removeListener(this)
        }

    /**
     * Set offset of previous page based on the edge flag and percentage scrolled
     */
    override fun onScroll(percent: Float, px: Int, edgeFlag: Int) {
        if (offset == 0f) return // relative slider is not enabled
        val page = SwipeBackHelper.getPrePage(curPage) ?: return
        if (percent == 0f) {
            page.swipeBackLayout.x = 0f
            page.swipeBackLayout.y = 0f
            return
        }
        when (edgeFlag) {
            SWIPE_EDGE_LEFT -> page.swipeBackLayout.x =
                min(-offset * max(1 - percent, 0f) + DEFAULT_OFFSET, 0f)
            SWIPE_EDGE_RIGHT -> page.swipeBackLayout.x =
                min(offset * max(1 - percent, 0f) - DEFAULT_OFFSET, 0f)
            SWIPE_EDGE_TOP -> page.swipeBackLayout.y =
                min(-offset * max(1 - percent, 0f) + DEFAULT_OFFSET, 0f)
            SWIPE_EDGE_BOTTOM -> page.swipeBackLayout.y =
                min(offset * max(1 - percent, 0f) - DEFAULT_OFFSET, 0f)
        }
    }

    override fun onEdgeTouch() {}

    /**
     * Reset offsets for previous page
     */
    override fun onScrollToClose(edgeFlag: Int) {
        val prePage = SwipeBackHelper.getPrePage(curPage) ?: return
        prePage.swipeBackLayout.x = 0f
        prePage.swipeBackLayout.y = 0f
    }

    companion object {
        private const val DEFAULT_OFFSET = 40
    }
}