Commit cde66e44 authored by nihar's avatar nihar
Browse files

Automatically reload previously selected fragment on orientation change,...

Automatically reload previously selected fragment on orientation change, dynamically set number of category columns on orientation change
parent 26829813
......@@ -18,11 +18,12 @@ import io.eelo.appinstaller.search.SearchFragment
import io.eelo.appinstaller.settings.SettingsFragment
import io.eelo.appinstaller.updates.UpdatesFragment
import io.eelo.appinstaller.utils.Common
import io.eelo.appinstaller.utils.Constants.CURRENTLY_SELECTED_FRAGMENT_KEY
import io.eelo.appinstaller.utils.Constants.STORAGE_PERMISSION_REQUEST_CODE
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener {
private var currentFragment: Fragment? = null
private var currentFragmentId = 0
private val homeFragment = HomeFragment()
private val categoriesFragment = CategoriesFragment()
private val searchFragment = SearchFragment()
......@@ -38,7 +39,15 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS
val installManager = installManagerGetter.connectAndGet(this)
initialiseFragments(installManager)
// Show the home fragment by default
showFragment(homeFragment)
if (savedInstanceState != null) {
if (selectFragment(savedInstanceState.getInt(CURRENTLY_SELECTED_FRAGMENT_KEY))) {
currentFragmentId = savedInstanceState.getInt(CURRENTLY_SELECTED_FRAGMENT_KEY)
}
} else {
if (selectFragment(R.id.menu_home)) {
currentFragmentId = R.id.menu_home
}
}
}
bottom_navigation_view.setOnNavigationItemSelectedListener(this)
......@@ -52,24 +61,32 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when {
item.itemId == R.id.menu_home -> {
if (selectFragment(item.itemId)) {
currentFragmentId = item.itemId
return true
}
return false
}
private fun selectFragment(fragmentId: Int): Boolean {
when (fragmentId) {
R.id.menu_home -> {
showFragment(homeFragment)
return true
}
item.itemId == R.id.menu_categories -> {
R.id.menu_categories -> {
showFragment(categoriesFragment)
return true
}
item.itemId == R.id.menu_search -> {
R.id.menu_search -> {
showFragment(searchFragment)
return true
}
item.itemId == R.id.menu_updates -> {
R.id.menu_updates -> {
showFragment(updatesFragment)
return true
}
item.itemId == R.id.menu_settings -> {
R.id.menu_settings -> {
showFragment(settingsFragment)
return true
}
......@@ -82,7 +99,6 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS
.beginTransaction()
.replace(R.id.frame_layout, fragment)
.commit()
currentFragment = fragment
}
@SuppressLint("RestrictedApi")
......@@ -112,6 +128,11 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS
}
}
override fun onSaveInstanceState(outState: Bundle?) {
super.onSaveInstanceState(outState)
outState?.putInt(CURRENTLY_SELECTED_FRAGMENT_KEY, currentFragmentId)
}
override fun onDestroy() {
super.onDestroy()
installManagerGetter.disconnect(this)
......
......@@ -2,6 +2,7 @@ package io.eelo.appinstaller.categories
import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.content.res.Configuration
import android.os.Bundle
import android.support.v4.app.Fragment
import android.text.TextUtils
......@@ -60,6 +61,18 @@ class CategoriesFragment : Fragment() {
itemParams.marginStart = itemPadding
itemParams.marginEnd = itemPadding
// Check device orientation and increase/decrease number of columns
val orientation = resources.configuration.orientation
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
// In landscape
applicationsCategoriesList.columnCount = 3
gamesCategoriesList.columnCount = 3
} else {
// In portrait
applicationsCategoriesList.columnCount = 2
gamesCategoriesList.columnCount = 2
}
// Bind to the list of applications categories
categoriesViewModel.getApplicationsCategories().observe(this, Observer {
showApplicationsCategories()
......
......@@ -17,4 +17,7 @@ object Constants {
// Categories
const val CATEGORY_KEY = "category_key"
// Home
const val CURRENTLY_SELECTED_FRAGMENT_KEY = "currently_selected_fragment"
}
......@@ -37,7 +37,6 @@
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/layout_margin_medium"
android:columnCount="2"
app:layout_constraintTop_toBottomOf="@id/applications_categories_title" />
<TextView
......@@ -59,7 +58,6 @@
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/layout_margin_medium"
android:columnCount="2"
app:layout_constraintTop_toBottomOf="@id/games_categories_title" />
</LinearLayout>
......
Supports Markdown
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