Skip to content

Commit 569e5fa

Browse files
Merge pull request #12118 from nextcloud/refactor/convert-WhatsNewActivity-to-kt
Convert WhatsNewActivity to Kotlin
2 parents b1664af + d87ac3b commit 569e5fa

2 files changed

Lines changed: 169 additions & 159 deletions

File tree

app/src/main/java/com/nextcloud/client/onboarding/WhatsNewActivity.java

Lines changed: 0 additions & 159 deletions
This file was deleted.
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
* Nextcloud Android client application
3+
*
4+
* @author Bartosz Przybylski
5+
* @author Chris Narkiewicz
6+
* Copyright (C) 2015 Bartosz Przybylski
7+
* Copyright (C) 2015 ownCloud Inc.
8+
* Copyright (C) 2016 Nextcloud.
9+
* Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
10+
*
11+
* This program is free software; you can redistribute it and/or
12+
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
13+
* License as published by the Free Software Foundation; either
14+
* version 3 of the License, or any later version.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public
22+
* License along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*/
24+
package com.nextcloud.client.onboarding
25+
26+
import android.os.Bundle
27+
import android.view.View
28+
import androidx.activity.OnBackPressedCallback
29+
import androidx.fragment.app.FragmentActivity
30+
import androidx.viewpager.widget.ViewPager
31+
import com.nextcloud.android.common.ui.theme.utils.ColorRole
32+
import com.nextcloud.client.appinfo.AppInfo
33+
import com.nextcloud.client.di.Injectable
34+
import com.nextcloud.client.preferences.AppPreferences
35+
import com.owncloud.android.BuildConfig
36+
import com.owncloud.android.R
37+
import com.owncloud.android.databinding.WhatsNewActivityBinding
38+
import com.owncloud.android.ui.adapter.FeaturesViewAdapter
39+
import com.owncloud.android.ui.adapter.FeaturesWebViewAdapter
40+
import com.owncloud.android.utils.theme.ViewThemeUtils
41+
import javax.inject.Inject
42+
43+
/**
44+
* Activity displaying new features after an update.
45+
*/
46+
class WhatsNewActivity : FragmentActivity(), ViewPager.OnPageChangeListener, Injectable {
47+
48+
@JvmField
49+
@Inject
50+
var preferences: AppPreferences? = null
51+
52+
@JvmField
53+
@Inject
54+
var appInfo: AppInfo? = null
55+
56+
@JvmField
57+
@Inject
58+
var onboarding: OnboardingService? = null
59+
60+
@JvmField
61+
@Inject
62+
var viewThemeUtilsFactory: ViewThemeUtils.Factory? = null
63+
64+
private var viewThemeUtils: ViewThemeUtils? = null
65+
66+
private lateinit var binding: WhatsNewActivityBinding
67+
68+
override fun onCreate(savedInstanceState: Bundle?) {
69+
super.onCreate(savedInstanceState)
70+
71+
binding = WhatsNewActivityBinding.inflate(layoutInflater)
72+
setContentView(binding.root)
73+
74+
viewThemeUtils = viewThemeUtilsFactory?.withPrimaryAsBackground()
75+
viewThemeUtils?.platform?.themeStatusBar(this, ColorRole.PRIMARY)
76+
77+
val urls = resources.getStringArray(R.array.whatsnew_urls)
78+
val showWebView = urls.isNotEmpty()
79+
80+
setupFeatureViewAdapter(showWebView, urls)
81+
binding.contentPanel.addOnPageChangeListener(this)
82+
setupForwardImageButton()
83+
setupSkipImageButton()
84+
setupWelcomeText(showWebView)
85+
updateNextButtonIfNeeded()
86+
handleOnBackPressed()
87+
}
88+
89+
@Suppress("SpreadOperator")
90+
private fun setupFeatureViewAdapter(showWebView: Boolean, urls: Array<String>) {
91+
val adapter = if (showWebView) {
92+
FeaturesWebViewAdapter(supportFragmentManager, *urls)
93+
} else {
94+
onboarding?.let {
95+
FeaturesViewAdapter(supportFragmentManager, *it.whatsNew)
96+
}
97+
}
98+
99+
adapter?.let {
100+
binding.progressIndicator.setNumberOfSteps(it.count)
101+
binding.contentPanel.adapter = it
102+
}
103+
}
104+
105+
private fun setupForwardImageButton() {
106+
viewThemeUtils?.platform?.colorImageView(binding.forward, ColorRole.ON_PRIMARY)
107+
binding.forward.setOnClickListener {
108+
if (binding.progressIndicator.hasNextStep()) {
109+
binding.contentPanel.setCurrentItem(binding.contentPanel.currentItem + 1, true)
110+
binding.progressIndicator.animateToStep(binding.contentPanel.currentItem + 1)
111+
} else {
112+
onFinish()
113+
finish()
114+
}
115+
updateNextButtonIfNeeded()
116+
}
117+
binding.forward.background = null
118+
}
119+
120+
private fun setupSkipImageButton() {
121+
viewThemeUtils?.platform?.colorTextView(binding.skip, ColorRole.ON_PRIMARY)
122+
binding.skip.setOnClickListener {
123+
onFinish()
124+
finish()
125+
}
126+
}
127+
128+
private fun setupWelcomeText(showWebView: Boolean) {
129+
viewThemeUtils?.platform?.colorTextView(binding.welcomeText, ColorRole.ON_PRIMARY)
130+
binding.welcomeText.text = if (showWebView) {
131+
getString(R.string.app_name)
132+
} else {
133+
String.format(getString(R.string.whats_new_title), appInfo?.versionName)
134+
}
135+
}
136+
137+
private fun handleOnBackPressed() {
138+
onBackPressedDispatcher.addCallback(
139+
this,
140+
object : OnBackPressedCallback(true) {
141+
override fun handleOnBackPressed() {
142+
onFinish()
143+
onBackPressedDispatcher.onBackPressed()
144+
}
145+
}
146+
)
147+
}
148+
149+
private fun updateNextButtonIfNeeded() {
150+
val hasNextStep = binding.progressIndicator.hasNextStep()
151+
binding.forward.setImageResource(if (hasNextStep) R.drawable.arrow_right else R.drawable.ic_ok)
152+
binding.skip.visibility = if (hasNextStep) View.VISIBLE else View.INVISIBLE
153+
}
154+
155+
private fun onFinish() {
156+
preferences?.lastSeenVersionCode = BuildConfig.VERSION_CODE
157+
}
158+
159+
override fun onPageSelected(position: Int) {
160+
binding.progressIndicator.animateToStep(position + 1)
161+
updateNextButtonIfNeeded()
162+
}
163+
164+
@Suppress("EmptyFunctionBlock")
165+
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
166+
167+
@Suppress("EmptyFunctionBlock")
168+
override fun onPageScrollStateChanged(state: Int) {}
169+
}

0 commit comments

Comments
 (0)