Skip to content

Commit

Permalink
[Merge] #99 -> develop
Browse files Browse the repository at this point in the history
[Feat/#93] image download ๊ธฐ๋Šฅ ๊ตฌํ˜„
  • Loading branch information
chattymin authored Jan 14, 2024
2 parents f3de42f + 9fa3b93 commit 4f81ad3
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 7 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="32"
tools:ignore="ScopedStorage" />

<application
android:name=".MyApp"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package com.going.presentation.tendency.result

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.media.MediaScannerConnection
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.text.SpannableString
import android.text.Spanned
import android.text.style.BulletSpan
import androidx.activity.viewModels
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.going.presentation.R
Expand All @@ -19,6 +29,8 @@ import com.going.ui.extension.toast
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import java.io.File
import java.io.FileOutputStream

@AndroidEntryPoint
class TendencyResultActivity :
Expand All @@ -27,14 +39,14 @@ class TendencyResultActivity :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

getuserInfo()
getUserInfo()
observeUserInfoState()
initRestartBtnClickLitener()
initRestartBtnClickListener()
initSaveImgBtnClickListener()
initFinishBtnClickListener()
}

private fun getuserInfo() {
private fun getUserInfo() {
viewModel.getUserInfoState()
}

Expand Down Expand Up @@ -97,15 +109,15 @@ class TendencyResultActivity :
return string
}

private fun initRestartBtnClickLitener() {
private fun initRestartBtnClickListener() {
binding.btnTendencyTestRestart.setOnSingleClickListener {
navigateToTendencyTestScreen()
}
}

private fun initSaveImgBtnClickListener() {
binding.btnTendencyResultDownload.setOnSingleClickListener {
toast("์ถ”ํ›„ ์—…๋ฐ์ดํŠธ ์˜ˆ์ •")
startImageDownload()
}
}

Expand All @@ -129,4 +141,75 @@ class TendencyResultActivity :
startActivity(this)
}
}

private fun startImageDownload() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU && ContextCompat.checkSelfPermission(
this,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
PERMISSION_REQUEST_CODE,
)
} else {
saveImageToGallery(resources)
}
}

private fun saveImageToGallery(resources: Resources) {
val imageBitmap: Bitmap = BitmapFactory.decodeResource(
resources,
R.drawable.img_tendency_result_ari,
)
val imageFileName = DOWNLOAD_IMAGE_NAME.replace("%s", viewModel.tendencyId.value.toString())
val path = DOWNLOAD_PATH

val uploadFolder = Environment.getExternalStoragePublicDirectory(path)
if (!uploadFolder.exists()) {
uploadFolder.mkdirs()
}

val imageFile = File(uploadFolder, imageFileName)

val outputStream = FileOutputStream(imageFile)
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
outputStream.flush()
outputStream.close()

scanFile(imageFile, "image/jpeg")

toast(getString(R.string.profile_image_download_success))
}

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray,
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == PERMISSION_REQUEST_CODE) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startImageDownload()
} else {
toast(getString(R.string.profile_image_download_error))
}
}
}

private fun scanFile(file: File, mimeType: String) {
MediaScannerConnection.scanFile(
this,
arrayOf(file.absolutePath),
arrayOf(mimeType),
null,
)
}

companion object {
const val PERMISSION_REQUEST_CODE = 200
const val DOWNLOAD_PATH = "/Download/"
const val DOWNLOAD_IMAGE_NAME = "img_tendency_result%s.png"
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.going.presentation.tendency.result

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.going.domain.entity.ProfileMock
Expand All @@ -20,10 +21,13 @@ class TendencyResultViewModel @Inject constructor(
private val _userInfoState = MutableStateFlow<UiState<UserProfileRequestModel>>(UiState.Empty)
val userInfoState: StateFlow<UiState<UserProfileRequestModel>> = _userInfoState

val tendencyId = MutableLiveData(0)

fun getUserInfoState() {
viewModelScope.launch {
_userInfoState.value = UiState.Loading
profileRepository.getUserProfile().onSuccess {
tendencyId.value = it.result
_userInfoState.value = UiState.Success(it)
}.onFailure {
_userInfoState.value = UiState.Failure(it.message.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ class TendencyTestActivity :
override fun onAnimationStart(animation: Animator) {
viewModel.clearAllChecked()
setProgressAnimate(binding.pbTendencyTest, viewModel.step.value)
fadeOutList.map {
it.start()
for (i in 1 until fadeOutList.size) {
fadeOutList[i].start()
}
}

Expand Down
2 changes: 2 additions & 0 deletions presentation/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@
<string name="profile_tv_name">๋‘๋ฆ…์ด</string>
<string name="profile_tv_one_line">๋‚˜๋Š” ๋‘๋ฆ…์ด ์ข‹๋‹ค.</string>
<string name="profile_tv_restart_button"><u>๋‹ค์‹œ ํ•ด๋ณผ๋ž˜์š”</u></string>
<string name="profile_image_download_success">์ด๋ฏธ์ง€๊ฐ€ ์ €์žฅ๋˜์—ˆ์–ด์š”\n์นœ๊ตฌ์—๊ฒŒ ๋‚ด ์บ๋ฆญํ„ฐ๋ฅผ ๊ณต์œ ํ•ด ๋ณด์„ธ์š”</string>
<string name="profile_image_download_error">์ €์žฅํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค\ndoorip์— ์‚ฌ์ง„์—๋Œ€ํ•œ ์—‘์„ธ์Šค ๊ถŒํ•œ์ด ์—†์Šต๋‹ˆ๋‹ค</string>

<!--enter_trip-->
<string name="enter_trip_tv_layout">์—ฌํ–‰ ์ž…์žฅํ•˜๊ธฐ</string>
Expand Down

0 comments on commit 4f81ad3

Please sign in to comment.