Skip to content

Commit

Permalink
[MERGE] #143 -> develop
Browse files Browse the repository at this point in the history
[FIX/#143] 공유하기 FileProvider cache 활용
  • Loading branch information
Marchbreeze authored Oct 10, 2024
2 parents cf58afd + 1b8743d commit 53d578f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 48 deletions.
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ object Constants {
const val compileSdk = 34
const val minSdk = 28
const val targetSdk = 34
const val versionCode = 14
const val versionName = "1.1.5"
const val versionCode = 15
const val versionName = "1.1.6"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package kr.genti.presentation.generate.finished

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.net.Uri
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.Spanned
Expand All @@ -21,9 +18,13 @@ import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import coil.load
import coil.transform.RoundedCornersTransformation
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kr.genti.core.base.BaseActivity
import kr.genti.core.extension.colorOf
import kr.genti.core.extension.dpToPx
Expand All @@ -35,14 +36,15 @@ import kr.genti.domain.enums.PictureRatio.Companion.toPictureRatio
import kr.genti.domain.enums.PictureType
import kr.genti.presentation.R
import kr.genti.presentation.databinding.ActivityFinishedBinding
import kr.genti.presentation.main.profile.ProfileImageDialog
import kr.genti.presentation.main.profile.ProfileImageDialog.Companion.FILE_PROVIDER_AUTORITY
import kr.genti.presentation.main.profile.ProfileImageDialog.Companion.IMAGE_TYPE
import kr.genti.presentation.main.profile.ProfileImageDialog.Companion.TEMP_FILE_NAME
import kr.genti.presentation.util.AmplitudeManager
import kr.genti.presentation.util.AmplitudeManager.EVENT_CLICK_BTN
import kr.genti.presentation.util.AmplitudeManager.PROPERTY_BTN
import kr.genti.presentation.util.AmplitudeManager.PROPERTY_PAGE
import kr.genti.presentation.util.downloadImage
import java.io.File
import java.io.FileOutputStream

@AndroidEntryPoint
class FinishedActivity : BaseActivity<ActivityFinishedBinding>(R.layout.activity_finished) {
Expand All @@ -52,6 +54,8 @@ class FinishedActivity : BaseActivity<ActivityFinishedBinding>(R.layout.activity
private var finishedReportDialog: FinishedReportDialog? = null
private var finishedRatingDialog: FinishedRatingDialog? = null

private lateinit var tempFile: File

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

Expand All @@ -63,6 +67,7 @@ class FinishedActivity : BaseActivity<ActivityFinishedBinding>(R.layout.activity
initUnwantedBtnListener()
getIntentInfo()
setStatusBarTransparent()
observeDownloadCacheImage()
}

private fun initView() {
Expand Down Expand Up @@ -103,49 +108,19 @@ class FinishedActivity : BaseActivity<ActivityFinishedBinding>(R.layout.activity

private fun initShareBtnListener() {
binding.btnShare.setOnSingleClickListener {
getTemporaryUri()?.let { uri ->
AmplitudeManager.apply {
trackEvent(
EVENT_CLICK_BTN,
mapOf(PROPERTY_PAGE to "picdone"),
mapOf(PROPERTY_BTN to "picshare"),
)
plusIntProperties("user_share")
}
Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_STREAM, uri)
type = ProfileImageDialog.IMAGE_TYPE
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivity(Intent.createChooser(this, SHARE_IMAGE_CHOOSER))
}
} ?: run {
toast(stringOf(R.string.error_msg))
AmplitudeManager.apply {
trackEvent(
EVENT_CLICK_BTN,
mapOf(PROPERTY_PAGE to "picdone"),
mapOf(PROPERTY_BTN to "picshare"),
)
plusIntProperties("user_share")
}
tempFile = File(cacheDir, TEMP_FILE_NAME)
viewModel.downloadImageToCache(tempFile)
}
}

private fun getTemporaryUri(): Uri? {
val tempFile = File(cacheDir, ProfileImageDialog.TEMP_FILE_NAME)
val imageView: ImageView =
if (viewModel.isRatioGaro) binding.ivFinishedImage32 else binding.ivFinishedImage23
if (imageView.drawable == null || imageView.drawable !is BitmapDrawable) {
return null
}
FileOutputStream(tempFile).use { out ->
(imageView.drawable as BitmapDrawable).bitmap.compress(
Bitmap.CompressFormat.PNG,
100,
out,
)
}
return FileProvider.getUriForFile(
this,
ProfileImageDialog.FILE_PROVIDER_AUTORITY,
tempFile,
)
}

private fun initReturnBtnListener() {
binding.btnReturnMain.setOnSingleClickListener {
AmplitudeManager.trackEvent(
Expand All @@ -169,7 +144,7 @@ class FinishedActivity : BaseActivity<ActivityFinishedBinding>(R.layout.activity
viewModel.finishedImage =
ImageModel(
intent.getLongExtra(EXTRA_RESPONSE_ID, -1),
intent.getStringExtra(EXTRA_URL) ?: "",
intent.getStringExtra(EXTRA_URL).orEmpty(),
"",
intent.getStringExtra(EXTRA_RATIO)?.toPictureRatio(),
PictureType.PictureCompleted,
Expand All @@ -192,6 +167,30 @@ class FinishedActivity : BaseActivity<ActivityFinishedBinding>(R.layout.activity
}
}

private fun observeDownloadCacheImage() {
viewModel.isImageDownloaded
.flowWithLifecycle(lifecycle)
.onEach { isDownloaded ->
if (isDownloaded) {
Intent().apply {
val uri =
FileProvider.getUriForFile(
this@FinishedActivity,
FILE_PROVIDER_AUTORITY,
tempFile,
)
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_STREAM, uri)
type = IMAGE_TYPE
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivity(Intent.createChooser(this, SHARE_IMAGE_CHOOSER))
}
} else {
toast(stringOf(R.string.error_msg))
}
}.launchIn(lifecycleScope)
}

private fun ImageView.loadImageToView() {
this.load(viewModel.finishedImage.url) {
transformations(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package kr.genti.presentation.generate.finished

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kr.genti.domain.entity.request.ReportRequestModel
import kr.genti.domain.entity.response.ImageModel
import kr.genti.domain.enums.PictureRatio
import kr.genti.domain.repository.GenerateRepository
import kr.genti.domain.repository.UserRepository
import java.io.File
import java.io.FileOutputStream
import java.net.URL
import javax.inject.Inject

@HiltViewModel
Expand All @@ -35,6 +42,9 @@ class FinishedViewModel

var isRatioGaro = true

private val _isImageDownloaded = MutableSharedFlow<Boolean>()
val isImageDownloaded: SharedFlow<Boolean> = _isImageDownloaded

private val _postReportResult = MutableSharedFlow<Boolean>()
val postReportResult: SharedFlow<Boolean> = _postReportResult

Expand All @@ -52,6 +62,27 @@ class FinishedViewModel
isRatioGaro = finishedImage.pictureRatio == PictureRatio.RATIO_GARO
}

fun downloadImageToCache(outputFile: File) {
viewModelScope.launch {
runCatching {
withContext(Dispatchers.IO) {
val connection = URL(finishedImage.url).openConnection()
connection.connect()
connection.getInputStream()?.use { inputStream ->
FileOutputStream(outputFile).use { out ->
val bitmap = BitmapFactory.decodeStream(inputStream)
bitmap?.compress(Bitmap.CompressFormat.PNG, 100, out)
}
}
}
}.onSuccess {
_isImageDownloaded.emit(true)
}.onFailure {
_isImageDownloaded.emit(false)
}
}
}

fun postGenerateRateToServer(star: Int) {
viewModelScope.launch {
generateRepository
Expand Down
7 changes: 6 additions & 1 deletion presentation/src/main/res/layout/activity_finished.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
android:layout_marginTop="28dp"
android:gravity="center"
android:text="@string/finished_tv_title"
android:textSize="19dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/iv_finished_logo_2_3" />
Expand Down Expand Up @@ -216,6 +217,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="14dp"
android:text="@string/finished_tv_saved"
android:textColor="@color/grey_3"
app:layout_constraintEnd_toEndOf="parent"
Expand All @@ -240,6 +242,7 @@
android:layout_height="wrap_content"
android:paddingVertical="10dp"
android:text="@string/finished_btn_share"
android:textSize="22dp"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Expand All @@ -263,6 +266,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:textSize="17dp"
android:padding="12dp"
android:text="@string/finished_btn_return_main"
android:textColor="@color/grey_3"
Expand All @@ -276,8 +280,9 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:padding="12dp"
android:textSize="14dp"
android:gravity="center"
android:padding="12dp"
android:text="@string/finished_btn_unwanted"
android:textColor="@color/error"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down

0 comments on commit 53d578f

Please sign in to comment.