Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement View binding, replacing synthetics #634

Merged
merged 11 commits into from
May 20, 2021
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
signingConfig signingConfigs.release
}
viewBinding {
enabled = true
}
buildTypes {
release {
minifyEnabled false
Expand Down
45 changes: 26 additions & 19 deletions app/src/main/java/com/automattic/loop/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.Navigation
import com.automattic.photoeditor.util.PermissionUtils
import com.automattic.loop.StoryComposerActivity.Companion.KEY_EXAMPLE_METADATA
import com.automattic.loop.StoryComposerActivity.Companion.KEY_STORY_INDEX
import com.automattic.loop.databinding.ActivityMainBinding
import com.automattic.loop.intro.IntroActivity
import com.automattic.loop.photopicker.PhotoPickerActivity
import com.automattic.photoeditor.util.PermissionUtils
import com.google.android.material.snackbar.Snackbar
import com.wordpress.stories.compose.frame.FrameSaveNotifier
import com.wordpress.stories.compose.frame.FrameSaveNotifier.Companion.getNotificationIdForError
Expand All @@ -19,23 +20,25 @@ import com.wordpress.stories.compose.frame.StorySaveEvents.StorySaveProcessStart
import com.wordpress.stories.compose.frame.StorySaveEvents.StorySaveResult
import com.wordpress.stories.compose.story.StoryRepository
import com.wordpress.stories.util.KEY_STORY_SAVE_RESULT
import kotlinx.android.synthetic.main.activity_main.*
import com.wordpress.stories.viewBinding
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MainActivity : AppCompatActivity(), MainFragment.OnFragmentInteractionListener {
private val binding by viewBinding(ActivityMainBinding::inflate)

override fun onFragmentInteraction(uri: Uri) {
// TODO: change OnFragmentInteractionListener for something relevant to our needs
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)

setSupportActionBar(binding.toolbar)
EventBus.getDefault().register(this)

activity_main.setOnApplyWindowInsetsListener { view, insets ->
binding.root.setOnApplyWindowInsetsListener { view, insets ->
// remember the insetTop as margin to all controls appearing at the top of the screen for full screen
// screens (i.e. ComposeLoopFrameActivity)
(application as Loop).setStatusBarHeight(insets.systemWindowInsetTop)
Expand All @@ -47,22 +50,24 @@ class MainActivity : AppCompatActivity(), MainFragment.OnFragmentInteractionList
finish()
}

fab.setOnClickListener { view ->
fab.isEnabled = false
// NOTE: we want to start with camera capture mode in this demo app, so we pass the
// bundle with the corresponding parameter.
// If we had URIs to start the composer already populated with them, we'd use
// EXTRA_MEDIA_URIS and a list of URIs for media items we want to use as Story frames.
val bundle = Bundle()
bundle.putBoolean(PhotoPickerActivity.EXTRA_LAUNCH_WPSTORIES_CAMERA_REQUESTED, true)
Navigation.findNavController(this, R.id.nav_host_fragment)
.navigate(R.id.action_mainFragment_to_composeLoopFrameActivity, bundle)
binding.fab.run {
setOnClickListener {
isEnabled = false
// NOTE: we want to start with camera capture mode in this demo app, so we pass the
// bundle with the corresponding parameter.
// If we had URIs to start the composer already populated with them, we'd use
// EXTRA_MEDIA_URIS and a list of URIs for media items we want to use as Story frames.
val bundle = Bundle()
bundle.putBoolean(PhotoPickerActivity.EXTRA_LAUNCH_WPSTORIES_CAMERA_REQUESTED, true)
Navigation.findNavController(this@MainActivity, R.id.nav_host_fragment)
.navigate(R.id.action_mainFragment_to_composeLoopFrameActivity, bundle)
}
}
}

override fun onResume() {
super.onResume()
fab.isEnabled = true
binding.fab.isEnabled = true
}

override fun onSupportNavigateUp() =
Expand All @@ -82,8 +87,9 @@ class MainActivity : AppCompatActivity(), MainFragment.OnFragmentInteractionList
val payloadString = it.getString(KEY_EXAMPLE_METADATA)
val storyIndex = it.getInt(KEY_STORY_INDEX)
Toast.makeText(
this, "Payload is: " + payloadString + " - index: " + storyIndex,
Toast.LENGTH_SHORT)
this, "Payload is: $payloadString - index: $storyIndex",
Toast.LENGTH_SHORT
)
.show()
}

Expand Down Expand Up @@ -116,7 +122,8 @@ class MainActivity : AppCompatActivity(), MainFragment.OnFragmentInteractionList
// from tapping on MANAGE on the snackbar (otherwise they'll be able to discard the
// errored story but the error notification will remain existing in the system dashboard)
intent.action = getNotificationIdForError(
StoryComposerActivity.BASE_FRAME_MEDIA_ERROR_NOTIFICATION_ID, event.storyIndex).toString() + ""
StoryComposerActivity.BASE_FRAME_MEDIA_ERROR_NOTIFICATION_ID, event.storyIndex
).toString() + ""

startActivity(intent)
}
Expand Down
12 changes: 2 additions & 10 deletions app/src/main/java/com/automattic/loop/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import android.content.Context
import android.net.Uri
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

// TODO: Rename parameter arguments, choose names that match

Expand All @@ -23,7 +20,7 @@ private const val ARG_PARAM2 = "param2"
* create an instance of this fragment.
*
*/
class MainFragment : Fragment() {
class MainFragment : Fragment(R.layout.fragment_main) {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
Expand All @@ -37,11 +34,6 @@ class MainFragment : Fragment() {
}
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false)
}

// TODO: Rename method, update argument and hook method into UI event
fun onButtonPressed(uri: Uri) {
listener?.onFragmentInteraction(uri)
Expand All @@ -52,7 +44,7 @@ class MainFragment : Fragment() {
if (context is OnFragmentInteractionListener) {
listener = context
} else {
throw RuntimeException(context.toString() + " must implement OnFragmentInteractionListener")
throw RuntimeException("$context must implement OnFragmentInteractionListener")
}
}

Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/com/automattic/loop/intro/IntroActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.navigation.Navigation
import com.automattic.photoeditor.util.PermissionUtils
import com.automattic.loop.AppPrefs
import com.automattic.loop.MainActivity
import com.automattic.loop.R
import com.automattic.loop.databinding.ActivityIntroBinding
import com.automattic.photoeditor.util.PermissionUtils
import com.wordpress.stories.viewBinding

class IntroActivity : AppCompatActivity(), IntroFragment.OnFragmentInteractionListener,
PermissionRequestFragment.OnFragmentInteractionListener {
private val binding by viewBinding(ActivityIntroBinding::inflate)

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

window.statusBarColor = ContextCompat.getColor(this, android.R.color.white)
showFragment(IntroFragment(), IntroFragment.TAG)
Expand Down
29 changes: 14 additions & 15 deletions app/src/main/java/com/automattic/loop/intro/IntroFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,34 @@ package com.automattic.loop.intro

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.automattic.loop.R.layout
import kotlinx.android.synthetic.main.fragment_intro.*
import com.automattic.loop.R
import com.automattic.loop.databinding.FragmentIntroBinding
import com.wordpress.stories.viewBinding

class IntroFragment : Fragment() {
class IntroFragment : Fragment(R.layout.fragment_intro) {
interface OnFragmentInteractionListener {
fun onGetStartedPressed()
}

private var listener: OnFragmentInteractionListener? = null
private val binding by viewBinding(FragmentIntroBinding::bind)

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(layout.fragment_intro, container, false)
}
private var listener: OnFragmentInteractionListener? = null

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

get_started_button.setOnClickListener {
listener?.onGetStartedPressed()
}
binding.run {
getStartedButton.setOnClickListener {
listener?.onGetStartedPressed()
}

intro_pager.adapter = IntroPagerAdapter(childFragmentManager)
introPager.adapter = IntroPagerAdapter(childFragmentManager)

// Using a TabLayout for simulating a page indicator strip
tab_layout_indicator.setupWithViewPager(intro_pager, true)
// Using a TabLayout for simulating a page indicator strip
tabLayoutIndicator.setupWithViewPager(binding.introPager, true)
}
}

override fun onAttach(context: Context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.automattic.loop.R

class IntroPagerAdapter(supportFragmentManager: FragmentManager) : FragmentPagerAdapter(supportFragmentManager) {
class IntroPagerAdapter(
supportFragmentManager: FragmentManager
) : FragmentPagerAdapter(supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
override fun getItem(position: Int): Fragment {
return if (position == 0) {
IntroPagerTitleFragment.newInstance(R.string.intro_title_text, PROMO_TEXTS[position],
Expand Down
18 changes: 8 additions & 10 deletions app/src/main/java/com/automattic/loop/intro/IntroPagerFragment.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.automattic.loop.intro

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.automattic.loop.R
import com.automattic.loop.databinding.IntroTemplateViewBinding
import com.automattic.loop.util.INVALID_RESOURCE_ID
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.intro_title_template_view.*
import com.wordpress.stories.viewBinding

class IntroPagerFragment : Fragment(R.layout.intro_template_view) {
private val binding by viewBinding(IntroTemplateViewBinding::bind)

class IntroPagerFragment : Fragment() {
private var promoText: Int = INVALID_RESOURCE_ID
private var backgroundImage: Int = INVALID_RESOURCE_ID

Expand All @@ -23,19 +24,16 @@ class IntroPagerFragment : Fragment() {
}
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.intro_template_view, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

with(view) {
Glide.with(context)
.load(backgroundImage)
.into(background_image)
.into(binding.backgroundImage)
}
promo_text.setText(promoText)

binding.promoText.setText(promoText)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.automattic.loop.intro

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.automattic.loop.R
import com.automattic.loop.databinding.IntroTitleTemplateViewBinding
import com.automattic.loop.util.INVALID_RESOURCE_ID
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.intro_title_template_view.*
import com.wordpress.stories.viewBinding

class IntroPagerTitleFragment : Fragment(R.layout.intro_title_template_view) {
private val binding by viewBinding(IntroTitleTemplateViewBinding::bind)

class IntroPagerTitleFragment : Fragment() {
private var titleText: Int = INVALID_RESOURCE_ID
private var promoText: Int = INVALID_RESOURCE_ID
private var backgroundImage: Int = INVALID_RESOURCE_ID
Expand All @@ -25,20 +26,17 @@ class IntroPagerTitleFragment : Fragment() {
}
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.intro_title_template_view, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

with(view) {
Glide.with(context)
.load(backgroundImage)
.into(background_image)
.into(binding.backgroundImage)
}
title_text.setText(titleText)
promo_text.setText(promoText)

binding.titleText.setText(titleText)
binding.promoText.setText(promoText)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,25 @@ package com.automattic.loop.intro

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.automattic.loop.R
import kotlinx.android.synthetic.main.fragment_permission.*
import com.automattic.loop.databinding.FragmentPermissionBinding
import com.wordpress.stories.viewBinding

class PermissionRequestFragment : Fragment() {
class PermissionRequestFragment : Fragment(R.layout.fragment_permission) {
interface OnFragmentInteractionListener {
fun onTurnOnPermissionsPressed()
}

private var listener: OnFragmentInteractionListener? = null
private val binding by viewBinding(FragmentPermissionBinding::bind)

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_permission, container, false)
}
private var listener: OnFragmentInteractionListener? = null

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

turn_on_permissions_button.setOnClickListener {
binding.turnOnPermissionsButton.setOnClickListener {
listener?.onTurnOnPermissionsPressed()
}
}
Expand Down
Loading