Skip to content

Commit

Permalink
[FIX/#143] cacheDir File Output 뷰모델 로직 이동
Browse files Browse the repository at this point in the history
  • Loading branch information
Marchbreeze committed Oct 10, 2024
1 parent 44bbe9c commit b8f6e13
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 49 deletions.
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.BitmapFactory
import android.net.Uri
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.Spanned
Expand All @@ -21,13 +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.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
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 @@ -40,15 +37,14 @@ import kr.genti.domain.enums.PictureType
import kr.genti.presentation.R
import kr.genti.presentation.databinding.ActivityFinishedBinding
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
import java.net.URL

@AndroidEntryPoint
class FinishedActivity : BaseActivity<ActivityFinishedBinding>(R.layout.activity_finished) {
Expand All @@ -58,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 @@ -69,6 +67,7 @@ class FinishedActivity : BaseActivity<ActivityFinishedBinding>(R.layout.activity
initUnwantedBtnListener()
getIntentInfo()
setStatusBarTransparent()
observeDownloadCacheImage()
}

private fun initView() {
Expand Down Expand Up @@ -117,46 +116,8 @@ class FinishedActivity : BaseActivity<ActivityFinishedBinding>(R.layout.activity
)
plusIntProperties("user_share")
}
getTemporaryUri()
}
}

private fun getTemporaryUri() {
val tempFile = File(cacheDir, TEMP_FILE_NAME)

lifecycleScope.launch {
runCatching {
withContext(Dispatchers.IO) {
val url = URL(viewModel.finishedImage.url)
val connection = url.openConnection()
connection.connect()
connection.getInputStream()?.use { inputStream ->
FileOutputStream(tempFile).use { out ->
val bitmap = BitmapFactory.decodeStream(inputStream)
bitmap?.compress(Bitmap.CompressFormat.PNG, 100, out)
}
}
}
FileProvider.getUriForFile(
this@FinishedActivity,
FILE_PROVIDER_AUTORITY,
tempFile,
)
}.onSuccess { uri ->
shareImage(uri)
}.onFailure {
toast(stringOf(R.string.error_msg))
}
}
}

private fun shareImage(uri: Uri) {
Intent().apply {
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))
tempFile = File(cacheDir, TEMP_FILE_NAME)
viewModel.downloadImageToCache(tempFile)
}
}

Expand All @@ -183,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 @@ -206,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

0 comments on commit b8f6e13

Please sign in to comment.