Skip to content

Commit

Permalink
Implement ShareCompat in Shareable blog
Browse files Browse the repository at this point in the history
  • Loading branch information
naufalprakoso committed May 25, 2020
1 parent f5b6e93 commit 5f2a4a8
Showing 1 changed file with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

package dev.shreyaspatil.foodium.ui.details

import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.core.app.ShareCompat
import androidx.lifecycle.Observer
import coil.api.load
import dev.shreyaspatil.foodium.R
Expand All @@ -41,7 +41,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
@ExperimentalCoroutinesApi
class PostDetailsActivity : BaseActivity<PostDetailsViewModel, ActivityPostDetailsBinding>() {

private var getPost: Post? = null
private lateinit var post: Post

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -59,7 +59,7 @@ class PostDetailsActivity : BaseActivity<PostDetailsViewModel, ActivityPostDetai
private fun initPost(postId: Int) {
mViewModel.getPost(postId).observe(this, Observer { post ->
mViewBinding.postContent.apply {
getPost = post
this@PostDetailsActivity.post = post

postTitle.text = post.title
postAuthor.text = post.author
Expand Down Expand Up @@ -91,17 +91,17 @@ class PostDetailsActivity : BaseActivity<PostDetailsViewModel, ActivityPostDetai
}

R.id.action_share -> {
if (getPost != null) {
val post = getPost!!
val shareMsg = """
"${post.title}" by ${post.author} on Foodium App
""".trimIndent()

val intent = Intent()
intent.action = Intent.ACTION_SEND
intent.putExtra(Intent.EXTRA_TEXT, shareMsg)
intent.type = "text/plain"
startActivity(Intent.createChooser(intent, "Share to:"))
val shareMsg = """
"${post.title}" by ${post.author} on Foodium App
""".trimIndent()

val intent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain")
.setText(shareMsg)
.intent

if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
return true
}
Expand Down

0 comments on commit 5f2a4a8

Please sign in to comment.