Make tutorial swipeable

But this still needs lots of improvement ^^

Closes #30
This commit is contained in:
Finn M Glas 2020-06-21 14:33:49 +02:00
parent b01ddf2ae6
commit d0201f02a0
No known key found for this signature in database
GPG key ID: 902A30146014DFBF
4 changed files with 148 additions and 109 deletions

View file

@ -2,10 +2,14 @@ package com.finnmglas.launcher.tutorial
import android.content.Context import android.content.Context
import android.os.Bundle import android.os.Bundle
import android.util.TypedValue
import android.view.* import android.view.*
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.finnmglas.launcher.* import com.finnmglas.launcher.*
import com.finnmglas.launcher.tutorial.tab.TutorialFragmentTab
import kotlinx.android.synthetic.main.tutorial.* import kotlinx.android.synthetic.main.tutorial.*
/** /**
@ -17,7 +21,6 @@ import kotlinx.android.synthetic.main.tutorial.*
*/ */
class TutorialActivity: AppCompatActivity(), UIObject { class TutorialActivity: AppCompatActivity(), UIObject {
private var menuNumber = 0
private var defaultApps = mutableListOf<String>() private var defaultApps = mutableListOf<String>()
private var isFirstTime = false private var isFirstTime = false
@ -26,13 +29,19 @@ class TutorialActivity: AppCompatActivity(), UIObject {
// Initialise layout // Initialise layout
setContentView(R.layout.tutorial) setContentView(R.layout.tutorial)
loadMenu(this)
// Check if it is the first time starting the app // Check if it is the first time starting the app
isFirstTime = !launcherPreferences.getBoolean("startedBefore", false) isFirstTime = !launcherPreferences.getBoolean("startedBefore", false)
if (isFirstTime) if (isFirstTime)
defaultApps = resetSettings(this) // UP, DOWN, RIGHT, LEFT, VOLUME_UP, VOLUME_DOWN defaultApps = resetSettings(this) // UP, DOWN, RIGHT, LEFT, VOLUME_UP, VOLUME_DOWN
else tutorial_appbar.visibility = View.VISIBLE else tutorial_appbar.visibility = View.VISIBLE
// set up tabs and swiping in settings
val sectionsPagerAdapter = TutorialSectionsPagerAdapter(this, supportFragmentManager, defaultApps, isFirstTime)
val viewPager: ViewPager = findViewById(R.id.tutorial_viewpager)
viewPager.adapter = sectionsPagerAdapter
//val tabs: TabLayout = findViewById(R.id.tutorial_tabs)
//tabs.setupWithViewPager(viewPager)
} }
override fun onStart() { override fun onStart() {
@ -40,64 +49,32 @@ class TutorialActivity: AppCompatActivity(), UIObject {
super<UIObject>.onStart() super<UIObject>.onStart()
} }
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP){
menuNumber++
loadMenu(this)
}
else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_BACK){
menuNumber--
// prevent negative indices
if (menuNumber < 0) menuNumber = 0
loadMenu(this)
}
return true
}
fun clickAnywhere(view: View){
menuNumber++
loadMenu(this)
}
private fun loadMenu(context: Context) { // Context needed for packageManager
val intro = resources.getStringArray(R.array.intro)
if (menuNumber < intro.size){
val entry = intro[menuNumber].split("|").toTypedArray() //heading|infoText|hintText|size
tutorial_page_heading.text = entry[0]
if (entry[4] == "1" && isFirstTime)
tutorial_page_text.text = String.format(entry[1],
defaultApps[0], defaultApps[1], defaultApps[2], defaultApps[3], defaultApps[4], defaultApps[5])
else if (entry[4] == "1" && !isFirstTime)
tutorial_page_text.text = String.format(entry[1],
"-", "-", "-", "-", "-", "-")
else tutorial_page_text.text = entry[1]
tutorial_page_hint.text = entry[2]
tutorial_page_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, entry[3].toFloat())
} else { // End intro
if (isFirstTime){
launcherPreferences.edit()
.putBoolean("startedBefore", true) // never auto run this again
.putLong("firstStartup", System.currentTimeMillis() / 1000L) // record first startup timestamp
.apply()
}
finish()
}
}
override fun applyTheme() { override fun applyTheme() {
tutorial_appbar.setBackgroundColor(dominantColor) tutorial_appbar.setBackgroundColor(dominantColor)
tutorial_container.setBackgroundColor(dominantColor) tutorial_container.setBackgroundColor(dominantColor)
tutorial_close.setTextColor(vibrantColor) tutorial_close.setTextColor(vibrantColor)
tutorial_page_hint.blink() // animate
} }
override fun setOnClicks() { override fun setOnClicks() {
tutorial_close.setOnClickListener() { finish() } tutorial_close.setOnClickListener() { finish() }
} }
} }
class TutorialSectionsPagerAdapter(private val context: Context, fm: FragmentManager,
val defaultApps: MutableList<String>, val isFirstTime: Boolean)
: FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
override fun getItem(position: Int): Fragment {
return when (position){
0 -> TutorialFragmentTab(defaultApps, isFirstTime, position)
1 -> TutorialFragmentTab(defaultApps, isFirstTime, position)
else -> TutorialFragmentTab(defaultApps, isFirstTime, position)
}
}
override fun getPageTitle(position: Int): CharSequence? {
return position.toString()
}
override fun getCount(): Int { return 20 }
}

View file

@ -0,0 +1,67 @@
package com.finnmglas.launcher.tutorial.tab
import android.content.Context
import android.os.Bundle
import android.util.TypedValue
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.finnmglas.launcher.*
import kotlinx.android.synthetic.main.tutorial_tab.*
/**
* The [TutorialFragmentTab] is a used as a tab in the TutorialActivity.
*
* It is used to display info in the tutorial
*/
class TutorialFragmentTab(var defaultApps: MutableList<String>, val isFirstTime: Boolean, val n: Int): Fragment(), UIObject {
private var menuNumber = 0
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.tutorial_tab, container, false)
}
override fun onStart(){
menuNumber = n
loadMenu(context!!)
super<Fragment>.onStart()
super<UIObject>.onStart()
}
override fun applyTheme() {
tutorial_tab_container.setBackgroundColor(dominantColor)
}
private fun loadMenu(context: Context) { // Context needed for packageManager
val intro = resources.getStringArray(R.array.intro)
if (menuNumber < intro.size){
val entry = intro[menuNumber].split("|").toTypedArray() //heading|infoText|hintText|size
tutorial_tab_heading.text = entry[0]
if (entry[4] == "1" && isFirstTime)
tutorial_tab_text.text = String.format(entry[1],
defaultApps[0], defaultApps[1], defaultApps[2], defaultApps[3], defaultApps[4], defaultApps[5])
else if (entry[4] == "1" && !isFirstTime)
tutorial_tab_text.text = String.format(entry[1],
"-", "-", "-", "-", "-", "-")
else tutorial_tab_text.text = entry[1]
tutorial_tab_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, entry[3].toFloat())
} else if (menuNumber > intro.size) { // End intro
if (isFirstTime){
launcherPreferences.edit()
.putBoolean("startedBefore", true) // never auto run this again
.putLong("firstStartup", System.currentTimeMillis() / 1000L) // record first startup timestamp
.apply()
}
activity!!.finish()
}
}
}

View file

@ -8,7 +8,6 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="?attr/colorPrimary" android:background="?attr/colorPrimary"
android:onClick="clickAnywhere"
tools:context=".tutorial.TutorialActivity"> tools:context=".tutorial.TutorialActivity">
<com.google.android.material.appbar.AppBarLayout <com.google.android.material.appbar.AppBarLayout
@ -16,7 +15,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center" android:gravity="center"
android:visibility="invisible" android:visibility="gone"
custom:layout_constraintEnd_toEndOf="parent" custom:layout_constraintEnd_toEndOf="parent"
custom:layout_constraintStart_toStartOf="parent" custom:layout_constraintStart_toStartOf="parent"
custom:layout_constraintTop_toTopOf="parent"> custom:layout_constraintTop_toTopOf="parent">
@ -56,59 +55,13 @@
custom:layout_constraintTop_toTopOf="parent" custom:layout_constraintTop_toTopOf="parent"
custom:type="solid" /> custom:type="solid" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout> </com.google.android.material.appbar.AppBarLayout>
<TextView <androidx.viewpager.widget.ViewPager
android:id="@+id/tutorial_page_heading" android:id="@+id/tutorial_viewpager"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:gravity="center" android:layout_marginTop="64dp"
android:onClick="clickAnywhere" app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:textSize="64sp" app:layout_constraintTop_toBottomOf="@id/tutorial_appbar" />
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.100000024" />
<TextView
android:id="@+id/tutorial_page_text"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
android:layout_marginBottom="100dp"
android:gravity="center"
android:onClick="clickAnywhere"
android:textColor="#fff"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tutorial_page_heading" />
<TextView
android:id="@+id/tutorial_page_hint"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
android:layout_marginBottom="8dp"
android:gravity="center"
android:onClick="clickAnywhere"
android:textColor="#999"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tutorial_page_text" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tutorial_tab_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/colorPrimary"
tools:context=".tutorial.tab.TutorialFragmentTab">
<TextView
android:id="@+id/tutorial_tab_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="64sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.100000024" />
<TextView
android:id="@+id/tutorial_tab_text"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
android:layout_marginBottom="32dp"
android:gravity="center"
android:textColor="#fff"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tutorial_tab_heading" />
</androidx.constraintlayout.widget.ConstraintLayout>