/
WelcomeActivity.kt
183 lines (152 loc) 路 6.75 KB
/
WelcomeActivity.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
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
/*
* Copyright 2016-2020 Open Food Facts
*
* 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
*
* https://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 openfoodfacts.github.scrachx.openfood.features.welcome
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.ActivityInfo
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.Window
import android.view.WindowManager
import android.widget.TextView
import androidx.core.content.edit
import androidx.core.content.res.ResourcesCompat
import androidx.core.text.HtmlCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import dagger.hilt.android.AndroidEntryPoint
import openfoodfacts.github.scrachx.openfood.R
import openfoodfacts.github.scrachx.openfood.analytics.MatomoAnalytics
import openfoodfacts.github.scrachx.openfood.databinding.ActivityWelcomeBinding
import openfoodfacts.github.scrachx.openfood.features.MainActivity
import openfoodfacts.github.scrachx.openfood.features.shared.BaseActivity
import openfoodfacts.github.scrachx.openfood.utils.PrefManager
import openfoodfacts.github.scrachx.openfood.utils.darken
import openfoodfacts.github.scrachx.openfood.utils.lighten
import javax.inject.Inject
/**
* This is the on boarding activity shown on first-run.
*/
/*
* TODO: redesign it & change the content
* TODO: explain the 3 scores
* TODO: be honest about offline until we implement offline scan (nobody cares about offline edit)
* TODO: perhaps highlight ingredient analysis
*/
@AndroidEntryPoint
class WelcomeActivity : BaseActivity() {
private var _binding: ActivityWelcomeBinding? = null
private val binding get() = _binding!!
private lateinit var prefManager: PrefManager
private val screens = WelcomeScreen.values()
@Inject
lateinit var matomoAnalytics: MatomoAnalytics
@Inject
lateinit var sharedPreferences: SharedPreferences
private val viewPagerPageChangeListener = object : OnPageChangeListener {
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) = Unit
override fun onPageScrollStateChanged(state: Int) = Unit
override fun onPageSelected(position: Int) {
refreshBottomBar(position)
when (WelcomeScreen[position]) {
WelcomeScreen.MATOMO -> {
binding.btnNext.setText(R.string.preference_analytics_bottom_sheet_grant_button)
binding.btnSkip.setText(R.string.preference_analytics_bottom_sheet_decline_button)
binding.btnNext.setTextColor(ResourcesCompat.getColor(resources, android.R.color.black, theme))
binding.btnSkip.setTextColor(ResourcesCompat.getColor(resources, android.R.color.black, theme))
binding.btnNext.setOnClickListener { saveThenLaunchHome(true) }
binding.btnSkip.setOnClickListener { saveThenLaunchHome(false) }
}
else -> {
binding.btnNext.setTextColor(ResourcesCompat.getColor(resources, android.R.color.white, theme))
binding.btnSkip.setTextColor(ResourcesCompat.getColor(resources, android.R.color.white, theme))
binding.btnNext.setText(R.string.next)
binding.btnSkip.setText(R.string.skip)
setOnClicks()
}
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
requestWindowFeature(Window.FEATURE_NO_TITLE)
super.onCreate(savedInstanceState)
if (resources.getBoolean(R.bool.portrait_only)) {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
_binding = ActivityWelcomeBinding.inflate(layoutInflater)
setContentView(binding.root)
prefManager = PrefManager(this)
if (!prefManager.isFirstTimeLaunch) {
launchHome()
finish()
}
WindowCompat.setDecorFitsSystemWindows(window, false)
WindowInsetsControllerCompat(window, binding.root).hide(WindowInsetsCompat.Type.statusBars())
refreshBottomBar(0)
changeStatusBarColor()
binding.viewPager.adapter = WelcomePageAdapter(layoutInflater)
binding.viewPager.addOnPageChangeListener(viewPagerPageChangeListener)
setOnClicks()
}
override fun onDestroy() {
_binding = null
super.onDestroy()
}
private fun setOnClicks() {
binding.btnSkip.setOnClickListener { binding.viewPager.currentItem = screens.size - 1 }
binding.btnNext.setOnClickListener { binding.viewPager.currentItem = nextItem }
}
private fun saveThenLaunchHome(grant: Boolean) {
saveAnalyticsReportingPref(grant)
matomoAnalytics.onAnalyticsEnabledToggled(grant)
launchHome()
}
private fun saveAnalyticsReportingPref(value: Boolean) {
sharedPreferences.edit {
putBoolean(getString(R.string.pref_analytics_reporting_key), value)
}
}
private fun launchHome() {
prefManager.isFirstTimeLaunch = false
startActivity(Intent(this@WelcomeActivity, MainActivity::class.java))
finish()
}
private fun refreshBottomBar(currentPage: Int) {
binding.layoutDots.removeAllViews()
val dots = (0..screens.lastIndex).map {
TextView(this).apply {
text = HtmlCompat.fromHtml("•", HtmlCompat.FROM_HTML_MODE_COMPACT)
textSize = 35f
setTextColor(WelcomeScreen[currentPage].color.lighten(0.85f))
binding.layoutDots.addView(this)
}
}
dots[currentPage].setTextColor(WelcomeScreen[currentPage].color.darken(0.1f))
}
private val nextItem get() = binding.viewPager.currentItem + 1
private fun changeStatusBarColor() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.statusBarColor = Color.TRANSPARENT
}
companion object {
fun start(context: Context) = context.startActivity(Intent(context, WelcomeActivity::class.java))
}
}