Skip to content

Commit

Permalink
Implement missing dismiss automatically when lifecycle owner is onDes…
Browse files Browse the repository at this point in the history
…troy
  • Loading branch information
skydoves committed Aug 9, 2019
1 parent 58b328d commit 5b0d4c4
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 9 deletions.
2 changes: 0 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
19 changes: 19 additions & 0 deletions app/src/main/java/com/skydoves/balloondemo/BalloonUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ object BalloonUtils {
.build()
}

fun getProfileBalloonForViewHolder(baseContext: Context, lifecycleOwner: LifecycleOwner): Balloon {
return Balloon.Builder(baseContext)
.setText("You can edit your profile now!")
.setArrowSize(10)
.setWidthRatio(0.75f)
.setHeight(63)
.setTextSize(15f)
.setCornerRadius(8f)
.setArrowOrientation(ArrowOrientation.TOP)
.setTextColor(ContextCompat.getColor(baseContext, R.color.white_87))
.setIconDrawable(ContextCompat.getDrawable(baseContext, R.drawable.ic_edit))
.setBackgroundColor(ContextCompat.getColor(baseContext, R.color.skyBlue))
.setOnBalloonDismissListener { Toast.makeText(baseContext, "dismissed", Toast.LENGTH_SHORT).show() }
.setDismissWhenTouchOutside(true)
.setBalloonAnimation(BalloonAnimation.ELASTIC)
.setLifecycleOwner(lifecycleOwner)
.build()
}

fun getNavigationBalloon(
baseContext: Context,
onBalloonClickListener: OnBalloonClickListener,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CustomActivity : AppCompatActivity(),
SampleViewHolder.Delegate,
CustomViewHolder.Delegate {

private val adapter by lazy { SampleAdapter(this) }
private val adapter by lazy { SampleAdapter(this, this) }
private val customAdapter by lazy { CustomAdapter(this) }
private val customListBalloon by lazy { BalloonUtils.getCustomListBalloon(this, this) }
private val customProfileBalloon by lazy { BalloonUtils.getCustomProfileBalloon(this, this) }
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/skydoves/balloondemo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(), SampleViewHolder.Delegate, OnBalloonClickListener {

private val adapter by lazy { SampleAdapter(this) }
private val adapter by lazy { SampleAdapter(this, this) }
private val profileBalloon by lazy { BalloonUtils.getProfileBalloon(this, this) }
private val navigationBalloon by lazy { BalloonUtils.getNavigationBalloon(this, this, this) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
package com.skydoves.balloondemo.recycler

import android.view.View
import androidx.lifecycle.LifecycleOwner
import com.skydoves.balloondemo.R
import com.skydoves.baserecyclerviewadapter.BaseAdapter
import com.skydoves.baserecyclerviewadapter.BaseViewHolder
import com.skydoves.baserecyclerviewadapter.SectionRow

@Suppress("PrivatePropertyName")
class SampleAdapter(private val delegate: SampleViewHolder.Delegate)
: BaseAdapter() {
class SampleAdapter(
private val delegate: SampleViewHolder.Delegate,
private val lifecycleOwner: LifecycleOwner
) : BaseAdapter() {

private val section_item = 0

Expand All @@ -42,6 +45,6 @@ class SampleAdapter(private val delegate: SampleViewHolder.Delegate)
}

override fun viewHolder(layout: Int, view: View): BaseViewHolder {
return SampleViewHolder(view, delegate)
return SampleViewHolder(view, delegate, lifecycleOwner)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,23 @@
package com.skydoves.balloondemo.recycler

import android.view.View
import androidx.lifecycle.LifecycleOwner
import com.skydoves.balloondemo.BalloonUtils
import com.skydoves.baserecyclerviewadapter.BaseViewHolder
import kotlinx.android.synthetic.main.item_sample.view.*

@Suppress("CanBeParameter")
class SampleViewHolder(
private val view: View,
private val delegate: Delegate
private val delegate: Delegate,
private val lifecycleOwner: LifecycleOwner
) : BaseViewHolder(view) {

interface Delegate {
fun onItemClick(sampleItem: SampleItem)
}

private val balloon by lazy { BalloonUtils.getProfileBalloonForViewHolder(context(), lifecycleOwner) }
private lateinit var sampleItem: SampleItem

override fun bindData(data: Any) {
Expand All @@ -49,6 +53,7 @@ class SampleViewHolder(

override fun onClick(v: View?) {
delegate.onItemClick(this.sampleItem)
balloon.showAlignBottom(v!!)
}

override fun onLongClick(v: View?) = false
Expand Down
8 changes: 8 additions & 0 deletions balloon/src/main/java/com/skydoves/balloon/Balloon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ import androidx.annotation.FloatRange
import androidx.annotation.LayoutRes
import androidx.core.content.ContextCompat
import androidx.core.widget.ImageViewCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
import kotlinx.android.synthetic.main.layout_balloon.view.*

@DslMarker
Expand Down Expand Up @@ -341,6 +343,12 @@ class Balloon(
return bodyView.balloon_detail
}

/** dismiss automatically when lifecycle owner is destroyed. */
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy() {
dismiss()
}

/** Builder class for creating [Balloon]. */
@BalloonDsl
class Builder(private val context: Context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,4 @@ inline fun Fragment.balloon(
crossinline balloonProducer: (() -> Balloon)
): Lazy<Balloon> {
return lazy { balloonProducer() }
}
}

0 comments on commit 5b0d4c4

Please sign in to comment.