Skip to content

Commit

Permalink
Use view binding in favor of Kotlin synthetics (closes #267, closes #265
Browse files Browse the repository at this point in the history
)
  • Loading branch information
SUPERCILEX committed Feb 19, 2020
1 parent 94d932e commit 0c230af
Show file tree
Hide file tree
Showing 84 changed files with 480 additions and 378 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ import com.supercilex.robotscouter.core.ui.isInTabletMode
import com.supercilex.robotscouter.core.ui.showStoreListing
import com.supercilex.robotscouter.core.ui.transitionAnimationDuration
import com.supercilex.robotscouter.core.unsafeLazy
import com.supercilex.robotscouter.databinding.HomeActivityContentBinding
import com.supercilex.robotscouter.databinding.HomeActivityNormalBinding
import com.supercilex.robotscouter.shared.PermissionRequestHandler
import com.supercilex.robotscouter.shared.launchUrl
import kotlinx.android.synthetic.main.activity_home_base.*
import kotlinx.android.synthetic.main.activity_home_content.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

Expand All @@ -63,11 +63,17 @@ internal class HomeActivity : ActivityBase(), NavigationView.OnNavigationItemSel

private val updateManager by lazy { AppUpdateManagerFactory.create(this) }

private val binding by unsafeLazy {
HomeActivityNormalBinding.bind(findViewById(R.id.drawer_layout))
}
private val contentBinding by unsafeLazy {
HomeActivityContentBinding.bind(findViewById(R.id.root))
}
private val drawerToggle by unsafeLazy {
ActionBarDrawerToggle(
this,
drawerLayout,
toolbar,
binding.drawerLayout,
contentBinding.toolbar,
R.string.navigation_drawer_open_desc,
R.string.navigation_drawer_close_desc
)
Expand All @@ -85,7 +91,7 @@ internal class HomeActivity : ActivityBase(), NavigationView.OnNavigationItemSel
* [TabletScoutListFragmentCompanion] counterpart. The fragment will call [onTeamSelected]
* which will kick of a series of transactions.
* 1. The back stack will be popped to remove the integrated fragment.
* 1. The tablet fragment will replace anything in the [R.id.scoutList] container.
* 1. The tablet fragment will replace anything in the [R.id.scout_list] container.
* 1. The [R.id.content] container will be forced to be a [TeamListFragmentCompanion].
* 1. The bottom nav will attempt to be updated to point to the teams fragment, but it will be
* null b/c we're still in [onCreate] and haven't called [setContentView] yet.
Expand All @@ -106,7 +112,7 @@ internal class HomeActivity : ActivityBase(), NavigationView.OnNavigationItemSel
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.RobotScouter_NoActionBar_TransparentStatusBar)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
setContentView(R.layout.home_activity)
if (savedInstanceState == null) {
supportFragmentManager.commit {
add(R.id.content,
Expand All @@ -128,12 +134,14 @@ internal class HomeActivity : ActivityBase(), NavigationView.OnNavigationItemSel
) onBackPressedDispatcher.onBackPressed()
}

setSupportActionBar(toolbar)
drawerLayout.addDrawerListener(drawerToggle)
setSupportActionBar(contentBinding.toolbar)
binding.drawerLayout.addDrawerListener(drawerToggle)
drawerToggle.syncState()
drawer.setNavigationItemSelectedListener(this)
if (enableAutoScout) bottomNavigation.menu.findItem(R.id.autoScout).isVisible = true
bottomNavigation.setOnNavigationItemSelectedListener listener@{
binding.drawer.setNavigationItemSelectedListener(this)
if (enableAutoScout) {
binding.bottomNavigation.menu.findItem(R.id.auto_scout).isVisible = true
}
binding.bottomNavigation.setOnNavigationItemSelectedListener listener@{
if (bottomNavStatusNeedsUpdatingHack) return@listener true

val manager = supportFragmentManager
Expand All @@ -153,7 +161,7 @@ internal class HomeActivity : ActivityBase(), NavigationView.OnNavigationItemSel
return@listener true
}

appBar.setExpanded(true)
contentBinding.appBar.setExpanded(true)
manager.commitNow {
val newFragment = manager.destTagToFragment(newTag)

Expand All @@ -165,22 +173,28 @@ internal class HomeActivity : ActivityBase(), NavigationView.OnNavigationItemSel
true
}
prefs.asLiveData().observe(this) {
bottomNavigation.menu.findItem(R.id.templates).isEnabled = isTemplateEditingAllowed
binding.bottomNavigation.menu.findItem(R.id.templates).isEnabled =
isTemplateEditingAllowed
}

if (isInTabletMode() && Build.VERSION.SDK_INT >= 21) {
scoutList.outlineProvider = object : ViewOutlineProvider() {
findViewById<View>(R.id.scout_list).outlineProvider = object : ViewOutlineProvider() {
private val padding = resources.getDimensionPixelSize(R.dimen.spacing_mini)

override fun getOutline(view: View, outline: Outline) {
// Without negative starting values, the outline will show up on top of the
// toolbar.
outline.setRect(-bottomNavigation.width - 100, -100, padding, view.height)
outline.setRect(
-binding.bottomNavigation.width - 100,
-100,
padding,
view.height
)
}
}
}

handleModuleInstalls(moduleInstallProgress)
handleModuleInstalls(findViewById(R.id.module_install_progress))
authHelper.init().logFailures("authInit").addOnSuccessListener(this) {
handleIntent()
}
Expand All @@ -190,7 +204,7 @@ internal class HomeActivity : ActivityBase(), NavigationView.OnNavigationItemSel

private fun destIdToTag(id: Int) = when (id) {
R.id.teams -> TeamListFragmentCompanion.TAG
R.id.autoScout -> AutoScoutFragmentCompanion.TAG
R.id.auto_scout -> AutoScoutFragmentCompanion.TAG
R.id.templates -> TemplateListFragmentCompanion.TAG
else -> error("Unknown id: $id")
}
Expand Down Expand Up @@ -270,7 +284,7 @@ internal class HomeActivity : ActivityBase(), NavigationView.OnNavigationItemSel
}
}

drawerLayout.closeDrawer(GravityCompat.START)
binding.drawerLayout.closeDrawer(GravityCompat.START)
return false
}

Expand All @@ -283,14 +297,14 @@ internal class HomeActivity : ActivityBase(), NavigationView.OnNavigationItemSel
}

override fun onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START)
if (binding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
binding.drawerLayout.closeDrawer(GravityCompat.START)
} else {
val homeId = bottomNavigation.menu.children.first().itemId
if (bottomNavigation.selectedItemId == homeId) {
val homeId = binding.bottomNavigation.menu.children.first().itemId
if (binding.bottomNavigation.selectedItemId == homeId) {
super.onBackPressed()
} else {
bottomNavigation.selectedItemId = homeId
binding.bottomNavigation.selectedItemId = homeId
}
}
}
Expand All @@ -308,7 +322,7 @@ internal class HomeActivity : ActivityBase(), NavigationView.OnNavigationItemSel
if (isInTabletMode()) {
manager.commit {
setCustomAnimations(R.anim.pop_fade_in_right, R.anim.fade_out)
replace(R.id.scoutList,
replace(R.id.scout_list,
TabletScoutListFragmentCompanion().newInstance(args),
ScoutListFragmentCompanionBase.TAG)

Expand Down Expand Up @@ -360,7 +374,7 @@ internal class HomeActivity : ActivityBase(), NavigationView.OnNavigationItemSel
private fun updateBottomNavStatusAfterTeamSelection() {
bottomNavStatusNeedsUpdatingHack = true

val nav = bottomNavigation ?: return
val nav = binding.bottomNavigation
nav.post {
nav.selectedItemId = R.id.teams
bottomNavStatusNeedsUpdatingHack = false
Expand All @@ -380,8 +394,8 @@ internal class HomeActivity : ActivityBase(), NavigationView.OnNavigationItemSel
super.onTrimMemory(level)
if (level == TRIM_MEMORY_MODERATE || level == TRIM_MEMORY_RUNNING_LOW) {
supportFragmentManager.apply {
val currentDestId = bottomNavigation.selectedItemId
val unfocusedFragments = bottomNavigation.menu.children
val currentDestId = binding.bottomNavigation.selectedItemId
val unfocusedFragments = binding.bottomNavigation.menu.children
.filterNot { it.itemId == currentDestId }
.map(MenuItem::getItemId)
.map(::destIdToTag)
Expand Down Expand Up @@ -412,11 +426,11 @@ internal class HomeActivity : ActivityBase(), NavigationView.OnNavigationItemSel
onTeamSelected(checkNotNull(getBundle(SCOUT_ARGS_KEY)))
containsKey(TEMPLATE_ARGS_KEY) -> {
val args = checkNotNull(getBundle(TEMPLATE_ARGS_KEY))
if (bottomNavigation.selectedItemId == R.id.templates) {
if (binding.bottomNavigation.selectedItemId == R.id.templates) {
// Handles args
TemplateListFragmentCompanion().getInstance(supportFragmentManager, args)
} else {
bottomNavigation.selectedItemId = R.id.templates
binding.bottomNavigation.selectedItemId = R.id.templates
}
}
containsKey(DONATE_EXTRA) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import com.supercilex.robotscouter.core.logBreadcrumb
import com.supercilex.robotscouter.core.longToast
import com.supercilex.robotscouter.core.ui.ActivityBase
import com.supercilex.robotscouter.core.ui.addNewDocumentFlags
import com.supercilex.robotscouter.core.unsafeLazy
import com.supercilex.robotscouter.databinding.LinkReceiverActivityBinding
import com.supercilex.robotscouter.shared.client.onSignedIn
import kotlinx.android.synthetic.main.activity_link_receiver.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
Expand All @@ -34,11 +35,15 @@ import java.util.Date
internal class LinkReceiverActivity : ActivityBase(), CoroutineScope {
override val coroutineContext = Job()

private val binding by unsafeLazy {
LinkReceiverActivityBinding.bind(findViewById(R.id.progress))
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_link_receiver)
progress.show()
handleModuleInstalls(progress)
setContentView(R.layout.link_receiver_activity)
binding.progress.show()
handleModuleInstalls(binding.progress)

async {
val dynamicLink: PendingDynamicLinkData? =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<include layout="@layout/activity_home_base" />
<include layout="@layout/home_activity_large" />

</merge>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<include layout="@layout/activity_home_base" />
<include layout="@layout/home_activity_large" />

</merge>
9 changes: 9 additions & 0 deletions app/android-base/src/main/res/layout/home_activity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include layout="@layout/home_activity_normal" />

</merge>
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/guideline"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/bottomNavigation"
app:layout_constraintBottom_toTopOf="@id/bottom_navigation"
tools:ignore="UnusedAttribute">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBar"
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
Expand All @@ -35,7 +35,7 @@
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<ProgressBar
android:id="@+id/moduleInstallProgress"
android:id="@+id/module_install_progress"
style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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/drawerLayout"
android:id="@id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
Expand All @@ -15,10 +15,10 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<include layout="@layout/activity_home_content" />
<include layout="@layout/home_activity_content" />

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigation"
android:id="@+id/bottom_navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/bottom_app_bar"
Expand All @@ -29,7 +29,7 @@
app:menu="@menu/home_bottom_navigation" />

<FrameLayout
android:id="@+id/scoutList"
android:id="@+id/scout_list"
android:layout_width="0dp"
android:layout_height="0dp"
android:elevation="@dimen/card_elevation"
Expand All @@ -41,7 +41,7 @@
tools:ignore="UnusedAttribute" />

<TextView
android:id="@+id/noTeamSelectedHint"
android:id="@+id/no_team_selected_hint"
style="@style/RobotScouter.NoContentHint"
android:text="@string/tutorial_no_team_selected_hint"
android:visibility="visible"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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/drawerLayout"
android:id="@id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
Expand All @@ -14,7 +14,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<include layout="@layout/activity_home_content" />
<include layout="@layout/home_activity_content" />

<!-- Needed to match the tablet layout guideline -->
<androidx.constraintlayout.widget.Guideline
Expand All @@ -25,7 +25,7 @@
app:layout_constraintGuide_percent="1" />

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigation"
android:id="@+id/bottom_navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/bottom_app_bar"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
android:title="@string/home_teams_title" />

<item
android:id="@+id/autoScout"
android:id="@+id/auto_scout"
android:icon="@drawable/ic_auto_fix_colorable_24dp"
android:title="@string/home_autoscout_title"
android:visible="false" />
Expand Down
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import org.apache.commons.io.output.TeeOutputStream
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
import org.jetbrains.kotlin.gradle.internal.AndroidExtensionsExtension
import org.jetbrains.kotlin.gradle.internal.CacheImplementation

buildscript {
repositories.deps()
Expand Down Expand Up @@ -131,6 +130,8 @@ fun Project.configureAndroid() {
xmlOutput = file("$reportsDir/lint-results.xml")
}

buildFeatures.viewBinding = true

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
Expand All @@ -142,7 +143,6 @@ fun Project.configureAndroid() {
}

configure<AndroidExtensionsExtension> {
isExperimental = true
defaultCacheImplementation = CacheImplementation.SPARSE_ARRAY
features = setOf("parcelize")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.supercilex.robotscouter.Bridge
import com.supercilex.robotscouter.core.ui.FragmentBase

@Bridge
internal class AutoScoutFragment : FragmentBase(R.layout.fragment_auto_scout) {
internal class AutoScoutFragment : FragmentBase(R.layout.auto_scout_fragment) {
companion object : AutoScoutFragmentCompanion {
override fun getInstance(manager: FragmentManager) =
manager.findFragmentByTag(TAG) as AutoScoutFragment? ?: AutoScoutFragment()
Expand Down
1 change: 1 addition & 0 deletions feature/scouts/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ dependencies {
implementation(project(":library:shared-scouting"))

implementation(Config.Libs.Jetpack.palette)
implementation(Config.Libs.Misc.flexbox)
}
Loading

0 comments on commit 0c230af

Please sign in to comment.