Skip to content

Commit

Permalink
Refactor instagram share feature
Browse files Browse the repository at this point in the history
  • Loading branch information
l2hyunwoo committed Feb 21, 2024
1 parent 4934aec commit f995260
Showing 1 changed file with 26 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@ import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Rect
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Size
import android.view.PixelCopy
import android.widget.Toast
import androidx.activity.addCallback
import androidx.activity.viewModels
Expand All @@ -25,6 +20,7 @@ import com.google.android.material.datepicker.DateValidatorPointBackward
import com.google.android.material.datepicker.MaterialDatePicker
import com.teampophory.pophory.BuildConfig
import com.teampophory.pophory.R
import com.teampophory.pophory.common.bitmap.capture
import com.teampophory.pophory.common.bitmap.saveToDisk
import com.teampophory.pophory.common.context.colorOf
import com.teampophory.pophory.common.context.snackBar
Expand Down Expand Up @@ -104,53 +100,31 @@ class AddPhotoActivity : AppCompatActivity() {
finish()
}
binding.btnShare.setOnSingleClickListener {
val bitmap = Bitmap.createBitmap(
binding.layoutImage.measuredWidth,
binding.layoutImage.measuredHeight,
Bitmap.Config.ARGB_8888,
)
val location = IntArray(2)
binding.layoutImage.getLocationInWindow(location)
PixelCopy.request(
window,
Rect(
location[0],
location[1],
location[0] + binding.layoutImage.width,
location[1] + binding.layoutImage.height,
),
bitmap,
{
if (it == PixelCopy.SUCCESS) {
lifecycleScope.launch {
val shareImageUri = bitmap.saveToDisk(this@AddPhotoActivity)
val intent = Intent("com.instagram.share.ADD_TO_STORY").apply {
putExtra("source_application", BuildConfig.FACEBOOK_APP_ID)
type = "image/png"
putExtra("interactive_asset_uri", shareImageUri)
putExtra("top_background_color", "#000000")
putExtra("bottom_background_color", "#000000")
}
grantUriPermission(
"com.instagram.android",
shareImageUri,
Intent.FLAG_GRANT_READ_URI_PERMISSION,
)
try {
startActivity(intent)
} catch (e: ActivityNotFoundException) {
Toast.makeText(
this@AddPhotoActivity,
"인스타그램 앱이 존재하지 않습니다.",
Toast.LENGTH_SHORT,
)
.show()
}
}
}
},
Handler(Looper.getMainLooper()),
)
lifecycleScope.launch {
val bitmap = binding.layoutImage.capture(this@AddPhotoActivity)
val shareImageUri = bitmap.saveToDisk(this@AddPhotoActivity)
val intent = Intent("com.instagram.share.ADD_TO_STORY").apply {
putExtra("source_application", BuildConfig.FACEBOOK_APP_ID)
type = "image/png"
putExtra("interactive_asset_uri", shareImageUri)
putExtra("top_background_color", "#000000")
putExtra("bottom_background_color", "#000000")
}
grantUriPermission(
"com.instagram.android",
shareImageUri,
Intent.FLAG_GRANT_READ_URI_PERMISSION,
)
try {
startActivity(intent)
} catch (e: ActivityNotFoundException) {
Toast.makeText(
this@AddPhotoActivity,
"인스타그램 앱이 존재하지 않습니다.",
Toast.LENGTH_SHORT,
).show()
}
}
}
}

Expand Down

0 comments on commit f995260

Please sign in to comment.