Skip to content

Commit fcbf9db

Browse files
committed
Snackbar Sample
1 parent 982e1e1 commit fcbf9db

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1300
-0
lines changed

android/SnackSample/.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx

android/SnackSample/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

android/SnackSample/app/build.gradle

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
apply plugin: 'com.android.application'
2+
3+
apply plugin: 'kotlin-android'
4+
5+
apply plugin: 'kotlin-android-extensions'
6+
7+
android {
8+
compileSdkVersion 29
9+
buildToolsVersion "29.0.2"
10+
defaultConfig {
11+
applicationId "br.com.nglauber.snacksample"
12+
minSdkVersion 21
13+
targetSdkVersion 29
14+
versionCode 1
15+
versionName "1.0"
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
}
18+
buildTypes {
19+
release {
20+
minifyEnabled false
21+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
22+
}
23+
}
24+
}
25+
26+
dependencies {
27+
implementation fileTree(dir: 'libs', include: ['*.jar'])
28+
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
29+
implementation 'androidx.appcompat:appcompat:1.0.2'
30+
implementation 'androidx.core:core-ktx:1.0.2'
31+
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
32+
implementation 'com.google.android.material:material:1.1.0-alpha09'
33+
testImplementation 'junit:junit:4.12'
34+
androidTestImplementation 'androidx.test:runner:1.2.0'
35+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
36+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package br.com.nglauber.snacksample
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("br.com.nglauber.snacksample", appContext.packageName)
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="br.com.nglauber.snacksample">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/AppTheme">
12+
<activity
13+
android:name=".MainActivity"
14+
android:label="@string/app_name"
15+
android:theme="@style/AppTheme">
16+
<intent-filter>
17+
<action android:name="android.intent.action.MAIN" />
18+
19+
<category android:name="android.intent.category.LAUNCHER" />
20+
</intent-filter>
21+
</activity>
22+
</application>
23+
24+
</manifest>
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package br.com.nglauber.snacksample
2+
3+
import android.content.Context
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import android.view.ViewTreeObserver
8+
import android.widget.Button
9+
import android.widget.TextView
10+
import androidx.annotation.LayoutRes
11+
12+
class FakeSnackBar private constructor() {
13+
lateinit var swipeView: SwipeView
14+
private set
15+
private var viewReady = false
16+
private var showRequested = false
17+
18+
fun show() {
19+
showRequested = true
20+
if (viewReady) {
21+
swipeView.show()
22+
}
23+
}
24+
25+
fun dismiss() {
26+
showRequested = false
27+
swipeView.dismiss()
28+
}
29+
30+
companion object {
31+
fun make(
32+
parent: ViewGroup,
33+
text: String,
34+
actionText: String? = null,
35+
action: (() -> Unit)? = null
36+
): FakeSnackBar {
37+
val snack = FakeSnackBar()
38+
val context = parent.context
39+
val swipeView = buildSwipeView(context, snack)
40+
val content = initDefaultContent(context, parent, text, actionText) {
41+
action?.invoke()
42+
snack.dismiss()
43+
}
44+
setupViews(swipeView, content, snack, parent)
45+
return snack
46+
}
47+
48+
fun make(
49+
parent: ViewGroup,
50+
@LayoutRes layout: Int
51+
): FakeSnackBar {
52+
val snack = FakeSnackBar()
53+
val context = parent.context
54+
val swipeView = buildSwipeView(context, snack)
55+
val content = LayoutInflater.from(context)
56+
.inflate(layout, parent, false)
57+
setupViews(swipeView, content, snack, parent)
58+
return snack
59+
}
60+
61+
private fun setupViews(
62+
swipeView: SwipeView,
63+
content: View?,
64+
snack: FakeSnackBar,
65+
parent: ViewGroup
66+
) {
67+
swipeView.addView(content)
68+
snack.swipeView = swipeView
69+
parent.addView(swipeView)
70+
}
71+
72+
private fun buildSwipeView(context: Context, snack: FakeSnackBar): SwipeView {
73+
val swipeView = SwipeView(context).apply {
74+
visibility = View.INVISIBLE
75+
layoutParams = ViewGroup.LayoutParams(
76+
ViewGroup.LayoutParams.MATCH_PARENT,
77+
ViewGroup.LayoutParams.WRAP_CONTENT
78+
)
79+
}
80+
swipeView.viewTreeObserver.addOnGlobalLayoutListener(object :
81+
ViewTreeObserver.OnGlobalLayoutListener {
82+
override fun onGlobalLayout() {
83+
swipeView.viewTreeObserver.removeOnGlobalLayoutListener(this)
84+
snack.viewReady = true
85+
if (snack.showRequested) {
86+
snack.show()
87+
}
88+
}
89+
})
90+
return swipeView
91+
}
92+
93+
private fun initDefaultContent(
94+
context: Context,
95+
parent: ViewGroup,
96+
message: String,
97+
textAction: String?,
98+
action: () -> Unit
99+
) = LayoutInflater
100+
.from(context)
101+
.inflate(R.layout.mysnack, parent, false).apply {
102+
findViewById<TextView>(R.id.txtMessage).text = message
103+
if (textAction != null) {
104+
findViewById<Button>(R.id.btnAction).apply {
105+
text = textAction
106+
setOnClickListener {
107+
action()
108+
}
109+
}
110+
}
111+
}
112+
113+
}
114+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package br.com.nglauber.snacksample
2+
3+
import android.content.Context
4+
import android.util.AttributeSet
5+
import android.view.View
6+
import androidx.coordinatorlayout.widget.CoordinatorLayout
7+
import com.google.android.material.floatingactionbutton.FloatingActionButton
8+
9+
10+
class FloatingActionButtonBehavior(context: Context, attrs: AttributeSet?) :
11+
CoordinatorLayout.Behavior<FloatingActionButton>(context, attrs) {
12+
13+
override fun layoutDependsOn(
14+
parent: CoordinatorLayout,
15+
child: FloatingActionButton,
16+
dependency: View
17+
): Boolean {
18+
return dependency is SwipeView // here is key idea
19+
}
20+
21+
override fun onDependentViewChanged(
22+
parent: CoordinatorLayout,
23+
child: FloatingActionButton,
24+
dependency: View
25+
): Boolean {
26+
child.translationY = (parent.height - dependency.translationY) * -1
27+
return true
28+
}
29+
30+
override fun onDependentViewRemoved(
31+
parent: CoordinatorLayout,
32+
child: FloatingActionButton,
33+
dependency: View
34+
) {
35+
super.onDependentViewRemoved(parent, child, dependency)
36+
child.animate()
37+
.translationY(0f)
38+
.setDuration(200)
39+
.start()
40+
}
41+
}

0 commit comments

Comments
 (0)