aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/activities/IntroActivity.kt
blob: 8fd45ab8a50752630808136af46107386cdfe01e (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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * Copyright 2018 Allan Wang
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.pitchedapps.frost.activities

import android.animation.ValueAnimator
import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import ca.allanwang.kau.internal.KauBaseActivity
import ca.allanwang.kau.utils.blendWith
import ca.allanwang.kau.utils.color
import ca.allanwang.kau.utils.fadeScaleTransition
import ca.allanwang.kau.utils.navigationBarColor
import ca.allanwang.kau.utils.postDelayed
import ca.allanwang.kau.utils.scaleXY
import ca.allanwang.kau.utils.setIcon
import ca.allanwang.kau.utils.statusBarColor
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
import com.pitchedapps.frost.R
import com.pitchedapps.frost.databinding.ActivityIntroBinding
import com.pitchedapps.frost.injectors.ThemeProvider
import com.pitchedapps.frost.intro.BaseIntroFragment
import com.pitchedapps.frost.intro.IntroAccountFragment
import com.pitchedapps.frost.intro.IntroFragmentEnd
import com.pitchedapps.frost.intro.IntroFragmentTheme
import com.pitchedapps.frost.intro.IntroFragmentWelcome
import com.pitchedapps.frost.intro.IntroTabContextFragment
import com.pitchedapps.frost.intro.IntroTabTouchFragment
import com.pitchedapps.frost.prefs.Prefs
import com.pitchedapps.frost.utils.ActivityThemer
import com.pitchedapps.frost.utils.cookies
import com.pitchedapps.frost.utils.launchNewTask
import com.pitchedapps.frost.utils.loadAssets
import com.pitchedapps.frost.widgets.NotificationWidget
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.launch
import org.koin.android.ext.android.inject
import javax.inject.Inject

/**
 * Created by Allan Wang on 2017-07-25.
 *
 * A beautiful intro activity
 * Phone showcases are drawn via layers
 */
@AndroidEntryPoint
class IntroActivity :
    KauBaseActivity(),
    ViewPager.PageTransformer,
    ViewPager.OnPageChangeListener {

    private val prefs: Prefs by inject()
    private val themeProvider: ThemeProvider by inject()

    @Inject
    lateinit var activityThemer: ActivityThemer

    lateinit var binding: ActivityIntroBinding
    private var barHasNext = true

    val fragments = listOf(
        IntroFragmentWelcome(),
        IntroFragmentTheme(),
        IntroAccountFragment(),
        IntroTabTouchFragment(),
        IntroTabContextFragment(),
        IntroFragmentEnd()
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityIntroBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.init()
    }

    private fun ActivityIntroBinding.init() {
        viewpager.apply {
            setPageTransformer(true, this@IntroActivity)
            addOnPageChangeListener(this@IntroActivity)
            adapter = IntroPageAdapter(supportFragmentManager, fragments)
        }
        indicator.setViewPager(viewpager)
        next.setIcon(GoogleMaterial.Icon.gmd_navigate_next)
        next.setOnClickListener {
            if (barHasNext) viewpager.setCurrentItem(viewpager.currentItem + 1, true)
            else finish(next.x + next.pivotX, next.y + next.pivotY)
        }
        skip.setOnClickListener { finish() }
        ripple.set(themeProvider.bgColor)
        theme()
    }

    fun theme() {
        statusBarColor = themeProvider.headerColor
        navigationBarColor = themeProvider.headerColor
        with(binding) {
            skip.setTextColor(themeProvider.textColor)
            next.imageTintList = ColorStateList.valueOf(themeProvider.textColor)
            indicator.setColour(themeProvider.textColor)
            indicator.invalidate()
        }
        fragments.forEach { it.themeFragment() }
        activityThemer.setFrostTheme(forceTransparent = true)
    }

    /**
     * Transformations are mainly handled on a per view basis
     * This sifies it by making the first fragment fade out as the second fragment comes in
     * All fragments are locked in position
     */
    override fun transformPage(page: View, position: Float) {
        // only apply to adjacent pages
        if ((position < 0 && position > -1) || (position > 0 && position < 1)) {
            val pageWidth = page.width
            val translateValue = position * -pageWidth
            page.translationX = (if (translateValue > -pageWidth) translateValue else 0f)
            page.alpha = if (position < 0) 1 + position else 1f
        } else {
            page.alpha = 1f
            page.translationX = 0f
        }
    }

    fun finish(x: Float, y: Float) {
        val blue = color(R.color.facebook_blue)
        window.setFlags(
            WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
            WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
        )
        binding.ripple.ripple(blue, x, y, 600) {
            postDelayed(1000) { finish() }
        }
        val lastView: View? = fragments.last().view
        arrayOf<View?>(
            binding.skip, binding.indicator, binding.next,
            lastView?.findViewById(R.id.intro_title),
            lastView?.findViewById(R.id.intro_desc)
        ).forEach {
            it?.animate()?.alpha(0f)?.setDuration(600)?.start()
        }
        if (themeProvider.textColor != Color.WHITE) {
            val f = lastView?.findViewById<ImageView>(R.id.intro_image)?.drawable
            if (f != null)
                ValueAnimator.ofFloat(0f, 1f).apply {
                    addUpdateListener {
                        f.setTint(
                            themeProvider.textColor.blendWith(
                                Color.WHITE,
                                it.animatedValue as Float
                            )
                        )
                    }
                    duration = 600
                    start()
                }
        }
        if (themeProvider.headerColor != blue) {
            ValueAnimator.ofFloat(0f, 1f).apply {
                addUpdateListener {
                    val c = themeProvider.headerColor.blendWith(blue, it.animatedValue as Float)
                    statusBarColor = c
                    navigationBarColor = c
                }
                duration = 600
                start()
            }
        }
    }

    override fun finish() {
        launch(NonCancellable) {
            loadAssets(themeProvider)
            NotificationWidget.forceUpdate(this@IntroActivity)
            launchNewTask<MainActivity>(cookies(), false)
            super.finish()
        }
    }

    override fun onBackPressed() {
        with(binding) {
            if (viewpager.currentItem > 0) viewpager.setCurrentItem(viewpager.currentItem - 1, true)
            else finish()
        }
    }

    override fun onPageScrollStateChanged(state: Int) {
    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
        fragments[position].onPageScrolled(positionOffset)
        if (position + 1 < fragments.size)
            fragments[position + 1].onPageScrolled(positionOffset - 1)
    }

    override fun onPageSelected(position: Int) {
        fragments[position].onPageSelected()
        val hasNext = position != fragments.size - 1
        if (barHasNext == hasNext) return
        barHasNext = hasNext
        binding.next.fadeScaleTransition {
            setIcon(
                if (barHasNext) GoogleMaterial.Icon.gmd_navigate_next else GoogleMaterial.Icon.gmd_done,
                color = themeProvider.textColor
            )
        }
        binding.skip.animate().scaleXY(if (barHasNext) 1f else 0f)
    }

    class IntroPageAdapter(fm: FragmentManager, private val fragments: List<BaseIntroFragment>) :
        FragmentPagerAdapter(fm) {

        override fun getItem(position: Int): Fragment = fragments[position]

        override fun getCount(): Int = fragments.size
    }
}