Skip to content

Commit

Permalink
Motion: Support predictive back in Container transform
Browse files Browse the repository at this point in the history
Change-Id: I9adb4a48be47761dc725f67e50be872a8865587a
  • Loading branch information
yaraki committed Feb 16, 2024
1 parent 1f1c48b commit 6f362dc
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 24 deletions.
34 changes: 17 additions & 17 deletions Motion/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ apply plugin: 'kotlin-android'
apply plugin: "androidx.navigation.safeargs.kotlin"

android {
compileSdk 31
compileSdk 34

defaultConfig {
applicationId 'com.example.android.motion'
minSdk 14
targetSdk 31
namespace 'com.example.android.motion'
minSdk 19
targetSdk 34
versionCode 1
versionName '1.0'
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
Expand All @@ -51,33 +51,33 @@ android {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.fragment:fragment-ktx:1.4.1'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'androidx.core:core-ktx:1.12.0'
implementation 'androidx.fragment:fragment-ktx:1.6.2'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.transition:transition:1.4.1'
implementation 'androidx.dynamicanimation:dynamicanimation:1.1.0-alpha03'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.recyclerview:recyclerview:1.3.2'

implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

def lifecycle_version = '2.4.1'
def lifecycle_version = '2.7.0'
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"

implementation 'androidx.paging:paging-runtime-ktx:3.1.1'
implementation 'androidx.paging:paging-runtime-ktx:3.2.1'

implementation 'com.google.android.material:material:1.6.0'
implementation 'com.google.android.material:material:1.11.0'

implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"

implementation 'com.github.bumptech.glide:glide:4.10.0'
implementation 'com.github.bumptech.glide:glide:4.15.1'

testImplementation 'junit:junit:4.13.2'
testImplementation 'com.google.truth:truth:1.1.3'

testImplementation 'androidx.test:core:1.4.0'
androidTestImplementation 'androidx.test.ext:truth:1.4.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
testImplementation 'androidx.test:core:1.5.0'
androidTestImplementation 'androidx.test.ext:truth:1.5.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
4 changes: 3 additions & 1 deletion Motion/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@

<application
android:allowBackup="false"
android:enableOnBackInvokedCallback="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Motion"
tools:ignore="GoogleAppIndexingWarning">
tools:ignore="GoogleAppIndexingWarning"
tools:targetApi="34">

<activity
android:name=".MainActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.activity.BackEventCompat
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.widget.Toolbar
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.ViewCompat
Expand All @@ -35,7 +37,10 @@ import androidx.core.widget.NestedScrollView
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.findNavController
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import androidx.transition.ChangeTransform
import androidx.transition.TransitionManager
import com.example.android.motion.R
import com.google.android.material.appbar.CollapsingToolbarLayout
import com.google.android.material.transition.MaterialContainerTransform
Expand All @@ -50,6 +55,8 @@ class CheeseArticleFragment : Fragment() {

private val viewModel: CheeseArticleViewModel by viewModels()

private val cancelTransition = ChangeTransform()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand Down Expand Up @@ -111,5 +118,45 @@ class CheeseArticleFragment : Fragment() {
toolbar.setNavigationOnClickListener { v ->
v.findNavController().popBackStack()
}

requireActivity().onBackPressedDispatcher.addCallback(
viewLifecycleOwner,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
// This invokes the sharedElementReturnTransition, which is
// MaterialContainerTransform.
// TODO: It pops the scaleX/Y back to 1f. It would look nicer to retain the
// scale. How should we handle it?
findNavController().popBackStack()
}

override fun handleOnBackProgressed(backEvent: BackEventCompat) {
val progress = backEvent.progress
// Translate as far as 20% of finger movement.
val translation = progress * background.width * 0.2f
background.translationX =
if (backEvent.swipeEdge == BackEventCompat.EDGE_LEFT) {
translation
} else {
-translation
}
// TODO: Consider handling backEvent.touchY to reflect the vertical movement.

// Scale down from 100% to 50%.
val scale = (2f - progress) * 0.5f
background.scaleX = scale
background.scaleY = scale
}

override fun handleOnBackCancelled() {
TransitionManager.beginDelayedTransition(background, cancelTransition)
background.run {
translationX = 0f
scaleX = 1f
scaleY = 1f
}
}
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class MirrorView @JvmOverloads constructor(
setWillNotDraw(value == null)
}

override fun onDraw(canvas: Canvas?) {
override fun onDraw(canvas: Canvas) {
_substance?.draw(canvas)
}
}
6 changes: 3 additions & 3 deletions Motion/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
*/

buildscript {
ext.kotlin_version = '1.6.21'
ext.navigation_version = '2.4.2'
ext.kotlin_version = '1.9.22'
ext.navigation_version = '2.7.7'
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.3.0'
classpath 'com.android.tools.build:gradle:8.2.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$navigation_version"
}
Expand Down
4 changes: 2 additions & 2 deletions Motion/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Oct 28 16:25:10 JST 2020
#Fri Feb 16 14:02:28 JST 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip

0 comments on commit 6f362dc

Please sign in to comment.