Commit 3557ff11 authored by Habib Kazemi's avatar Habib Kazemi Committed by Nicolas Pomepuy

Use ViewPager2 for Onboarding to support RTL

Fixes #1578 problem 1
parent b8184465
......@@ -164,6 +164,7 @@ dependencies {
api "com.google.android.material:material:$rootProject.ext.androidxMaterialVersion"
api "androidx.annotation:annotation:$rootProject.ext.androidxAnnotationVersion"
api "androidx.constraintlayout:constraintlayout:$rootProject.ext.constraintLayoutVersion"
api "androidx.viewpager2:viewpager2:$rootProject.ext.viewPager2Version"
api 'androidx.multidex:multidex:2.0.1'
api "androidx.lifecycle:lifecycle-extensions:$rootProject.ext.lifecycleVersion"
api "androidx.lifecycle:lifecycle-viewmodel-ktx:$rootProject.ext.lifecycleVersion"
......@@ -174,6 +175,7 @@ dependencies {
kapt "androidx.room:room-compiler:$rootProject.ext.roomVersion"
api "androidx.paging:paging-runtime-ktx:$rootProject.ext.pagingVersion"
//TV
api "androidx.leanback:leanback:$rootProject.ext.androidxLeanbackVersion"
api "androidx.leanback:leanback-preference:$rootProject.ext.androidxLeanbackVersion"
......
......@@ -7,7 +7,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="org.videolan.vlc.gui.onboarding.OnboardingActivity">
<org.videolan.vlc.gui.view.NonSwipeableViewPager
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/pager"
android:layout_width="0dp"
android:layout_height="0dp"
......
......@@ -37,12 +37,13 @@
app:layout_scrollFlags="scroll|enterAlways" />
<FrameLayout
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_placeholder"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="48dp"
android:keyboardNavigationCluster="true"
android:name="org.videolan.vlc.gui.browser.StorageBrowserFragment"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
......
......@@ -13,7 +13,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.content.edit
import androidx.lifecycle.lifecycleScope
import androidx.viewpager.widget.ViewPager
import androidx.viewpager2.widget.ViewPager2
import kotlinx.android.synthetic.main.activity_onboarding.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
......@@ -28,16 +28,15 @@ import org.videolan.vlc.gui.MainActivity
import org.videolan.vlc.gui.helpers.hf.PermissionViewmodel
import org.videolan.vlc.gui.helpers.hf.StoragePermissionsDelegate.Companion.getStoragePermission
import org.videolan.vlc.gui.helpers.hf.StoragePermissionsDelegate.Companion.resumePermissionRequest
import org.videolan.vlc.gui.view.NonSwipeableViewPager
import org.videolan.vlc.util.Permissions
const val ONBOARDING_DONE_KEY = "app_onboarding_done"
@ObsoleteCoroutinesApi
@ExperimentalCoroutinesApi
class OnboardingActivity : AppCompatActivity(), ViewPager.OnPageChangeListener, IOnScanningCustomizeChangedListener {
class OnboardingActivity : AppCompatActivity(), IOnScanningCustomizeChangedListener {
private lateinit var viewPager: NonSwipeableViewPager
private lateinit var viewPager: ViewPager2
private val indicators by lazy(LazyThreadSafetyMode.NONE) { arrayOf(
findViewById<View>(R.id.indicator0),
......@@ -66,10 +65,10 @@ class OnboardingActivity : AppCompatActivity(), ViewPager.OnPageChangeListener,
val count = viewModel.adapterCount
onboardingPagerAdapter = OnboardingFragmentPagerAdapter(supportFragmentManager, count)
onboardingPagerAdapter = OnboardingFragmentPagerAdapter(this, count)
viewPager.adapter = onboardingPagerAdapter
viewPager.addOnPageChangeListener(this)
viewPager.scrollEnabled = viewModel.permissionGranted
viewPager.registerOnPageChangeCallback(viewPager2PageChangeCallback)
viewPager.isUserInputEnabled = viewModel.permissionGranted
selectPage(0)
......@@ -92,10 +91,10 @@ class OnboardingActivity : AppCompatActivity(), ViewPager.OnPageChangeListener,
if (!viewModel.permissionGranted) {
return@launch
} else {
viewPager.scrollEnabled = true
viewPager.isUserInputEnabled = true
}
}
if (viewPager.currentItem < viewPager.adapter!!.count) {
if (viewPager.currentItem < viewPager.adapter!!.itemCount) {
viewPager.currentItem++
}
}
......@@ -107,6 +106,7 @@ class OnboardingActivity : AppCompatActivity(), ViewPager.OnPageChangeListener,
override fun onDestroy() {
super.onDestroy()
viewPager.unregisterOnPageChangeCallback(viewPager2PageChangeCallback)
Permissions.sAlertDialog?.run { dismiss() }
}
......@@ -133,7 +133,7 @@ class OnboardingActivity : AppCompatActivity(), ViewPager.OnPageChangeListener,
}
private fun selectPage(index: Int) {
if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, "Selecting page $index pager nb of item: ${onboardingPagerAdapter.count}")
if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, "Selecting page $index pager nb of item: ${onboardingPagerAdapter.itemCount}")
//Navigation button states
if (index == 0) {
previous.animate().scaleY(0f).scaleX(0f).alpha(0f)
......@@ -141,7 +141,7 @@ class OnboardingActivity : AppCompatActivity(), ViewPager.OnPageChangeListener,
previous.animate().scaleY(1f).scaleX(1f).alpha(0.6f)
}
if (index == onboardingPagerAdapter.count - 1) {
if (index == onboardingPagerAdapter.itemCount - 1) {
next.animate().scaleY(0f).scaleX(0f).alpha(0f)
doneButton.animate().cancel()
doneButton.visibility = View.VISIBLE
......@@ -171,12 +171,11 @@ class OnboardingActivity : AppCompatActivity(), ViewPager.OnPageChangeListener,
}
}
override fun onPageScrollStateChanged(state: Int) {}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
private val viewPager2PageChangeCallback = object :ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
selectPage(position)
}
override fun onPageSelected(position: Int) {
selectPage(position)
}
override fun onCustomizedChanged(customizeEnabled: Boolean) {
......
......@@ -7,18 +7,9 @@ import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import org.videolan.vlc.R
import org.videolan.vlc.gui.browser.StorageBrowserFragment
class OnboardingFoldersFragment : Fragment() {
private lateinit var storageBrowserFragment: StorageBrowserFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
storageBrowserFragment = StorageBrowserFragment()
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.onboarding_folders, container, false)
}
......@@ -34,14 +25,6 @@ class OnboardingFoldersFragment : Fragment() {
else -> title.visibility = View.VISIBLE
}
}
requireActivity().supportFragmentManager.beginTransaction()
.add(R.id.fragment_placeholder, storageBrowserFragment)
.commit()
}
......
package org.videolan.vlc.gui.onboarding
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.PagerAdapter
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
class OnboardingFragmentPagerAdapter(private val fragmentManager: FragmentManager, private var count: Int) : FragmentStatePagerAdapter(fragmentManager) {
class OnboardingFragmentPagerAdapter(fragmentActivity: FragmentActivity, private var count: Int) : FragmentStateAdapter(fragmentActivity) {
private var fragments = ArrayList<Fragment>()
private var folderFragment: OnboardingFoldersFragment = OnboardingFoldersFragment.newInstance()
init {
fragments.add(OnboardingWelcomeFragment.newInstance())
fragments.add(OnboardingScanningFragment.newInstance())
fragments.add(OnboardingThemeFragment.newInstance())
}
private var fragmentList: MutableList<FragmentName> = mutableListOf(
FragmentName.WELCOME,
FragmentName.SCAN,
FragmentName.THEME
)
fun onCustomizedChanged(customizeEnabled: Boolean) {
count = if (customizeEnabled) {
fragments.add(2, folderFragment)
fragmentList.add(2, FragmentName.FOLDERS)
4
} else {
fragments.remove(folderFragment)
fragmentList.remove(FragmentName.FOLDERS)
3
}
notifyDataSetChanged()
}
override fun getItemPosition(obj: Any)= PagerAdapter.POSITION_NONE
override fun getItemCount(): Int = count
override fun getItem(position: Int) = fragments[position]
override fun getItemId(position: Int): Long {
return fragmentList[position].ordinal.toLong()
}
override fun containsItem(itemId: Long): Boolean {
val fragment = FragmentName.values()[itemId.toInt()]
return fragmentList.contains(fragment)
}
override fun getCount() = count
override fun createFragment(position: Int): Fragment {
return when (fragmentList[position]) {
FragmentName.WELCOME -> OnboardingWelcomeFragment.newInstance()
FragmentName.SCAN -> OnboardingScanningFragment.newInstance()
FragmentName.FOLDERS -> OnboardingFoldersFragment.newInstance()
FragmentName.THEME -> OnboardingThemeFragment.newInstance()
}
}
enum class FragmentName {
WELCOME,
SCAN,
FOLDERS,
THEME
}
}
package org.videolan.vlc.gui.view
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.viewpager.widget.ViewPager
class NonSwipeableViewPager : ViewPager {
var scrollEnabled = true
constructor(context: Context) : super(context) {}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}
override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
if (!scrollEnabled) {
return false
}
return super.onInterceptTouchEvent(event)
}
override fun onTouchEvent(event: MotionEvent): Boolean {
if (!scrollEnabled) {
return false
}
return super.onTouchEvent(event)
}
}
\ No newline at end of file
......@@ -57,6 +57,7 @@ ext {
// if so, revert application/vlc-android/res/values[-v21]/styles.xml -> AppModalStyle
androidxMaterialVersion = '1.1.0'
constraintLayoutVersion = '1.1.3'
viewPager2Version = "1.0.0-alpha02"
archVersion = '2.1.0'
roomVersion = '2.2.5'
pagingVersion = '2.1.2'
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment