Skip to content
Permalink
Browse files

Create new HomeNavigationFragment

  • Loading branch information...
chrisbanes committed Mar 9, 2019
1 parent 09eb33a commit 32a7f8679d1ed60726906738924d724d2dcdff35
@@ -5,26 +5,37 @@
<option name="myDefaultNotNull" value="android.support.annotation.NonNull" />
<option name="myNullables">
<value>
<list size="4">
<list size="10">
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.Nullable" />
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nullable" />
<item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.Nullable" />
<item index="3" class="java.lang.String" itemvalue="android.support.annotation.Nullable" />
<item index="4" class="java.lang.String" itemvalue="javax.annotation.CheckForNull" />
<item index="5" class="java.lang.String" itemvalue="androidx.annotation.Nullable" />
<item index="6" class="java.lang.String" itemvalue="androidx.annotation.RecentlyNullable" />
<item index="7" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.qual.Nullable" />
<item index="8" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NullableDecl" />
<item index="9" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NullableType" />
</list>
</value>
</option>
<option name="myNotNulls">
<value>
<list size="4">
<list size="9">
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.NotNull" />
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nonnull" />
<item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.NonNull" />
<item index="3" class="java.lang.String" itemvalue="android.support.annotation.NonNull" />
<item index="4" class="java.lang.String" itemvalue="androidx.annotation.NonNull" />
<item index="5" class="java.lang.String" itemvalue="androidx.annotation.RecentlyNonNull" />
<item index="6" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.qual.NonNull" />
<item index="7" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NonNullDecl" />
<item index="8" class="java.lang.String" itemvalue="org.checkerframework.checker.nullness.compatqual.NonNullType" />
</list>
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="JDK" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
@@ -18,157 +18,32 @@ package app.tivi.home

import android.content.Intent
import android.os.Bundle
import android.view.ViewGroup
import androidx.core.view.forEach
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.ViewModelProviders
import app.tivi.R
import app.tivi.SharedElementHelper
import app.tivi.TiviActivity
import app.tivi.extensions.observeK
import app.tivi.home.HomeActivityViewModel.NavigationItem.DISCOVER
import app.tivi.home.HomeActivityViewModel.NavigationItem.LIBRARY
import app.tivi.home.discover.DiscoverFragment
import app.tivi.home.library.LibraryFragment
import app.tivi.home.popular.PopularShowsFragment
import app.tivi.home.trending.TrendingShowsFragment
import app.tivi.home.main.HomeNavigationFragment
import app.tivi.trakt.TraktConstants
import kotlinx.android.synthetic.main.activity_home.*
import net.openid.appauth.AuthorizationException
import net.openid.appauth.AuthorizationResponse
import net.openid.appauth.AuthorizationService

class HomeActivity : TiviActivity() {
companion object {
const val ROOT_FRAGMENT = "root"
}

private lateinit var viewModel: HomeActivityViewModel
private lateinit var navigatorViewModel: HomeNavigatorViewModel

val authService by lazy(LazyThreadSafetyMode.NONE) {
AuthorizationService(this)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)

home_content.setOnApplyWindowInsetsListener { view, insets ->
var consumed = false

(view as ViewGroup).forEach { child ->
if (child.dispatchApplyWindowInsets(insets).isConsumed) {
consumed = true
}
}

if (consumed) insets.consumeSystemWindowInsets() else insets
}

viewModel = ViewModelProviders.of(this, viewModelFactory)
.get(HomeActivityViewModel::class.java)

navigatorViewModel = ViewModelProviders.of(this, viewModelFactory)
.get(HomeNavigatorViewModel::class.java)

home_bottom_nav.setOnNavigationItemSelectedListener {
when (it.itemId) {
home_bottom_nav.selectedItemId -> {
if (supportFragmentManager.backStackEntryCount > 0) {
for (i in 0 until supportFragmentManager.backStackEntryCount) {
supportFragmentManager.popBackStack()
}
} else {
val fragment = supportFragmentManager.findFragmentById(R.id.home_content)
when (fragment) {
is DiscoverFragment -> fragment.scrollToTop()
is LibraryFragment -> fragment.scrollToTop()
}
}
true
}
R.id.home_nav_collection -> {
viewModel.onNavigationItemClicked(LIBRARY)
true
}
R.id.home_nav_discover -> {
viewModel.onNavigationItemClicked(DISCOVER)
true
}
else -> false
}
}

viewModel.navigationLiveData.observeK(this, this::showNavigationItem)

navigatorViewModel.showPopularCall.observeK(this, this::showPopular)
navigatorViewModel.showTrendingCall.observeK(this, this::showTrending)
navigatorViewModel.upClickedCall.observeK(this) { this.onUpClicked() }
}

private fun showNavigationItem(item: HomeActivityViewModel.NavigationItem?) {
if (item == null) {
return
}

val newFragment: Fragment
val newItemId: Int

when (item) {
DISCOVER -> {
newFragment = DiscoverFragment()
newItemId = R.id.home_nav_discover
}
LIBRARY -> {
newFragment = LibraryFragment()
newItemId = R.id.home_nav_collection
}
}

supportFragmentManager.popBackStackImmediate(ROOT_FRAGMENT, 0)

supportFragmentManager
.beginTransaction()
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.replace(R.id.home_content, newFragment, ROOT_FRAGMENT)
.commit()

// Now make the bottom nav show the correct item
if (home_bottom_nav.selectedItemId != newItemId) {
home_bottom_nav.menu.findItem(newItemId)?.isChecked = true
}
}

private fun showPopular(sharedElements: SharedElementHelper?) {
showStackFragment(PopularShowsFragment(), sharedElements)
}

private fun showTrending(sharedElements: SharedElementHelper?) {
showStackFragment(TrendingShowsFragment(), sharedElements)
}

private fun showStackFragment(fragment: Fragment, sharedElements: SharedElementHelper? = null) {
supportFragmentManager.beginTransaction()
.setReorderingAllowed(true)
.replace(R.id.home_content, fragment)
.addToBackStack(null)
.apply {
if (sharedElements != null && !sharedElements.isEmpty()) {
sharedElements.applyToTransaction(this)
} else {
setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
}
}
.replace(android.R.id.content, HomeNavigationFragment())
.commit()
}

private fun onUpClicked() {
// TODO can probably do something better here
supportFragmentManager.popBackStack()
}

override fun handleIntent(intent: Intent) {
when (intent.action) {
TraktConstants.INTENT_ACTION_HANDLE_AUTH_RESPONSE -> {
@@ -16,9 +16,7 @@

package app.tivi.home

import androidx.lifecycle.LiveData
import app.tivi.trakt.TraktManager
import app.tivi.util.SingleLiveEvent
import app.tivi.util.TiviViewModel
import net.openid.appauth.AuthorizationException
import net.openid.appauth.AuthorizationResponse
@@ -28,28 +26,6 @@ import javax.inject.Inject
internal class HomeActivityViewModel @Inject constructor(
private val traktManager: TraktManager
) : TiviViewModel() {

enum class NavigationItem {
DISCOVER, LIBRARY
}

private val mutableNavLiveData = SingleLiveEvent<NavigationItem>()

/**
* Facade so that we don't leak the fact that its mutable
*/
val navigationLiveData: LiveData<NavigationItem>
get() = mutableNavLiveData

init {
// Set default value
mutableNavLiveData.value = NavigationItem.DISCOVER
}

fun onNavigationItemClicked(item: NavigationItem) {
mutableNavLiveData.value = item
}

fun onAuthResponse(
authService: AuthorizationService,
response: AuthorizationResponse?,
@@ -16,15 +16,19 @@

package app.tivi.home

import android.content.Context
import androidx.lifecycle.ViewModel
import app.tivi.home.discover.DiscoverBuilder
import app.tivi.home.library.LibraryBuilder
import app.tivi.home.popular.PopularBuilder
import app.tivi.home.trending.TrendingBuilder
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import app.tivi.AppNavigator
import app.tivi.TiviAppActivityNavigator
import app.tivi.home.main.HomeNavigationBuilder
import app.tivi.inject.PerActivity
import app.tivi.inject.ViewModelBuilder
import app.tivi.inject.ViewModelKey
import dagger.Binds
import dagger.Module
import dagger.Provides
import dagger.android.ContributesAndroidInjector
import dagger.multibindings.IntoMap

@@ -33,10 +37,7 @@ internal abstract class HomeBuilder {
@ContributesAndroidInjector(modules = [
ViewModelBuilder::class,
HomeModule::class,
DiscoverBuilder::class,
TrendingBuilder::class,
PopularBuilder::class,
LibraryBuilder::class
HomeNavigationBuilder::class
])
internal abstract fun homeActivity(): HomeActivity

@@ -49,4 +50,27 @@ internal abstract class HomeBuilder {
@IntoMap
@ViewModelKey(HomeNavigatorViewModel::class)
abstract fun bindHomeNavigatorViewModel(viewModel: HomeNavigatorViewModel): ViewModel
}

@Module(includes = [HomeModuleBinds::class])
class HomeModule {
@Provides
fun provideHomeNavigator(
activity: HomeActivity,
factory: ViewModelProvider.Factory
): HomeNavigator {
return ViewModelProviders.of(activity, factory).get(HomeNavigatorViewModel::class.java)
}

@Provides
fun provideAppNavigator(activity: HomeActivity): AppNavigator {
return TiviAppActivityNavigator(activity)
}
}

@Module
abstract class HomeModuleBinds {
@Binds
@PerActivity
abstract fun provideContext(activity: HomeActivity): Context
}

This file was deleted.

Oops, something went wrong.
@@ -23,7 +23,6 @@ import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.browser.customtabs.CustomTabsIntent
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.net.toUri
import androidx.fragment.app.commit
import app.tivi.R
@@ -0,0 +1,35 @@
/*
* Copyright 2019 Google LLC
*
* 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
*
* http://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 app.tivi.home.main

import app.tivi.home.discover.DiscoverBuilder
import app.tivi.home.library.LibraryBuilder
import app.tivi.home.popular.PopularBuilder
import app.tivi.home.trending.TrendingBuilder
import dagger.Module
import dagger.android.ContributesAndroidInjector

@Module
internal abstract class HomeNavigationBuilder {
@ContributesAndroidInjector(modules = [
DiscoverBuilder::class,
TrendingBuilder::class,
PopularBuilder::class,
LibraryBuilder::class
])
internal abstract fun homeNavigationFragment(): HomeNavigationFragment
}
Oops, something went wrong.

0 comments on commit 32a7f86

Please sign in to comment.
You can’t perform that action at this time.