Skip to content

Commit

Permalink
♻️ ktFormating
Browse files Browse the repository at this point in the history
  • Loading branch information
ii2001 committed Aug 1, 2024
1 parent 76af5c4 commit 8111f66
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

class DefaultMakingRecipeRemoteDataSource(
private val makingRecipeService: MakingRecipeService
private val makingRecipeService: MakingRecipeService,
) : MakingRecipeRemoteDataSource {

override suspend fun fetchImageUri(keyName: String): String {
val response = makingRecipeService.fetchImageUri(keyName)
if (response.isSuccessful) {
Expand All @@ -18,7 +17,10 @@ class DefaultMakingRecipeRemoteDataSource(
}
}

override suspend fun uploadImageToS3(presignedUrl: String, file: File) {
override suspend fun uploadImageToS3(
presignedUrl: String,
file: File,
) {
val requestFile = file.asRequestBody("image/jpeg".toMediaTypeOrNull())
val response = makingRecipeService.uploadImageToS3(presignedUrl, requestFile)
if (!response.isSuccessful) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@ import java.io.File

interface MakingRecipeRemoteDataSource {
suspend fun fetchImageUri(keyName: String): String
suspend fun uploadImageToS3(presignedUrl: String, file: File)

suspend fun uploadImageToS3(
presignedUrl: String,
file: File,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@ import retrofit2.http.Query
import retrofit2.http.Url

interface MakingRecipeService {

@GET("/image")
suspend fun fetchImageUri(
@Query("keyName") keyName: String
@Query("keyName") keyName: String,
): Response<PresignedUrlResponse>

@PUT
suspend fun uploadImageToS3(
@Url url: String,
@Body image: RequestBody
@Body image: RequestBody,
): Response<Unit>
}

data class PresignedUrlResponse(
val url: String
val url: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import net.pengcook.android.data.datasource.makingrecipe.MakingRecipeRemoteDataS
import java.io.File

class MakingRecipeRepository(private val remoteDataSource: MakingRecipeRemoteDataSource) {

suspend fun fetchImageUri(keyName: String): String {
return remoteDataSource.fetchImageUri(keyName)
}

suspend fun uploadImageToS3(presignedUrl: String, file: File) {
suspend fun uploadImageToS3(
presignedUrl: String,
file: File,
) {
remoteDataSource.uploadImageToS3(presignedUrl, file)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import net.pengcook.android.R
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// Security.insertProviderAt(Conscrypt.newProvider(), 1)

setContentView(R.layout.activity_main)
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host) as NavHostFragment
Expand All @@ -21,7 +24,10 @@ class MainActivity : AppCompatActivity() {

navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.homeFragment, R.id.searchFragment, R.id.profileFragment, R.id.categoryFragment -> bottomNav.visibility = View.VISIBLE
R.id.homeFragment, R.id.searchFragment, R.id.profileFragment, R.id.categoryFragment ->
bottomNav.visibility =
View.VISIBLE

else -> bottomNav.visibility = View.GONE
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import android.net.Uri
import android.provider.MediaStore

object FileUtils {
fun getPathFromUri(context: Context, uri: Uri): String? {
fun getPathFromUri(
context: Context,
uri: Uri,
): String? {
val projection = arrayOf(MediaStore.Images.Media.DATA)
val cursor: Cursor? = context.contentResolver.query(uri, projection, null, null, null)
cursor?.use {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@ import retrofit2.converter.gson.GsonConverterFactory
import java.io.File
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.*
import java.util.Date

class RecipeMakingFragment : Fragment() {
private var _binding: FragmentRecipeMakingBinding? = null
private val binding: FragmentRecipeMakingBinding
get() = _binding!!

private val viewModel: RecipeMakingViewModel by viewModels {
val retrofit = Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
val retrofit =
Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()

val makingRecipeService = retrofit.create(MakingRecipeService::class.java)
val remoteDataSource = DefaultMakingRecipeRemoteDataSource(makingRecipeService)
Expand All @@ -50,46 +51,49 @@ class RecipeMakingFragment : Fragment() {

private val permissionArray =
arrayOf(
Manifest.permission.CAMERA
Manifest.permission.CAMERA,
)

private val requestPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
if (isGranted) {
Toast.makeText(requireContext(), "카메라 권한이 허용되어 있습니다.", Toast.LENGTH_SHORT).show()
showImageSourceDialog()
} else {
Toast.makeText(requireContext(), "카메라 권한이 필요합니다.", Toast.LENGTH_SHORT).show()
private val requestPermissionLauncher =
registerForActivityResult(
ActivityResultContracts.RequestPermission(),
) { isGranted: Boolean ->
if (isGranted) {
Toast.makeText(requireContext(), "카메라 권한이 허용되어 있습니다.", Toast.LENGTH_SHORT).show()
showImageSourceDialog()
} else {
Toast.makeText(requireContext(), "카메라 권한이 필요합니다.", Toast.LENGTH_SHORT).show()
}
}
}

private val getContentLauncher = registerForActivityResult(
ActivityResultContracts.GetContent()
) { uri: Uri? ->
uri?.let {
photoUri = it
currentPhotoPath = FileUtils.getPathFromUri(requireContext(), it)
if (currentPhotoPath != null) {
viewModel.fetchImageUri(File(currentPhotoPath!!).name)
} else {
private val getContentLauncher =
registerForActivityResult(
ActivityResultContracts.GetContent(),
) { uri: Uri? ->
uri?.let {
photoUri = it
currentPhotoPath = FileUtils.getPathFromUri(requireContext(), it)
if (currentPhotoPath != null) {
viewModel.fetchImageUri(File(currentPhotoPath!!).name)
} else {
Toast.makeText(requireContext(), "이미지 선택에 실패했습니다.", Toast.LENGTH_SHORT).show()
}
} ?: run {
Toast.makeText(requireContext(), "이미지 선택에 실패했습니다.", Toast.LENGTH_SHORT).show()
}
} ?: run {
Toast.makeText(requireContext(), "이미지 선택에 실패했습니다.", Toast.LENGTH_SHORT).show()
}
}

// 사진 촬영 ActivityResultLauncher
private val takePictureLauncher = registerForActivityResult(
ActivityResultContracts.TakePicture()
) { success ->
if (success) {
viewModel.fetchImageUri(File(currentPhotoPath!!).name)
} else {
Toast.makeText(requireContext(), "사진을 찍는데 실패했습니다.", Toast.LENGTH_SHORT).show()
private val takePictureLauncher =
registerForActivityResult(
ActivityResultContracts.TakePicture(),
) { success ->
if (success) {
viewModel.fetchImageUri(File(currentPhotoPath!!).name)
} else {
Toast.makeText(requireContext(), "사진을 찍는데 실패했습니다.", Toast.LENGTH_SHORT).show()
}
}
}

override fun onCreateView(
inflater: LayoutInflater,
Expand All @@ -115,36 +119,46 @@ class RecipeMakingFragment : Fragment() {
}

// Observer to handle the pre-signed URL response
viewModel.imageUri.observe(viewLifecycleOwner, Observer { uri ->
if (uri != null) {
uploadImageToS3(uri)
}
})
viewModel.imageUri.observe(
viewLifecycleOwner,
Observer { uri ->
if (uri != null) {
uploadImageToS3(uri)
}
},
)

// Observer to handle the upload success
viewModel.uploadSuccess.observe(viewLifecycleOwner, Observer { success ->
if (success == true) {
Toast.makeText(requireContext(), "이미지 업로드 성공!", Toast.LENGTH_SHORT).show()
} else if (success == false) {
Toast.makeText(requireContext(), "이미지 업로드 실패!", Toast.LENGTH_SHORT).show()
}
})
viewModel.uploadSuccess.observe(
viewLifecycleOwner,
Observer { success ->
if (success == true) {
Toast.makeText(requireContext(), "이미지 업로드 성공!", Toast.LENGTH_SHORT).show()
} else if (success == false) {
Toast.makeText(requireContext(), "이미지 업로드 실패!", Toast.LENGTH_SHORT).show()
}
},
)

// Observer to handle upload error
viewModel.uploadError.observe(viewLifecycleOwner, Observer { errorMessage ->
if (errorMessage != null) {
Toast.makeText(requireContext(), errorMessage, Toast.LENGTH_SHORT).show()
}
})
viewModel.uploadError.observe(
viewLifecycleOwner,
Observer { errorMessage ->
if (errorMessage != null) {
Toast.makeText(requireContext(), errorMessage, Toast.LENGTH_SHORT).show()
}
},
)
}

private fun onAddImageClicked() {
if (permissionArray.all {
ContextCompat.checkSelfPermission(
requireContext(),
it
it,
) == PackageManager.PERMISSION_GRANTED
}) {
}
) {
showImageSourceDialog()
} else {
requestPermissionLauncher.launch(Manifest.permission.CAMERA)
Expand All @@ -166,11 +180,12 @@ class RecipeMakingFragment : Fragment() {

private fun takePicture() {
val photoFile: File = createImageFile()
photoUri = FileProvider.getUriForFile(
requireContext(),
"net.pengcook.android.fileprovider",
photoFile
)
photoUri =
FileProvider.getUriForFile(
requireContext(),
"net.pengcook.android.fileprovider",
photoFile,
)
takePictureLauncher.launch(photoUri)
}

Expand All @@ -185,7 +200,7 @@ class RecipeMakingFragment : Fragment() {
return File.createTempFile(
"JPEG_${timeStamp}_",
".jpg",
storageDir
storageDir,
).apply {
currentPhotoPath = absolutePath
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ class RecipeMakingViewModel(private val makingRecipeRepository: MakingRecipeRepo
}

// Function to upload image to S3
fun uploadImageToS3(presignedUrl: String, file: File) {
fun uploadImageToS3(
presignedUrl: String,
file: File,
) {
viewModelScope.launch {
try {
makingRecipeRepository.uploadImageToS3(presignedUrl, file)
Expand All @@ -71,5 +74,6 @@ class RecipeMakingViewModel(private val makingRecipeRepository: MakingRecipeRepo

sealed interface MakingEvent {
data object NavigateToMakingStep : MakingEvent

data object AddImage : MakingEvent
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class RecipeMakingViewModelFactory(private val makingRecipeRepository: MakingRec
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(RecipeMakingViewModel::class.java)) {
return RecipeMakingViewModel(
makingRecipeRepository = makingRecipeRepository
makingRecipeRepository = makingRecipeRepository,
) as T
} else {
throw IllegalArgumentException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package net.pengcook.android.presentation.making.listener

interface RecipeMakingEventListener {
fun onNavigateToMakingStep()

fun onAddImage()
}
2 changes: 1 addition & 1 deletion android/app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/recipeMakingFragment">
app:startDestination="@id/homeFragment">

<fragment
android:id="@+id/homeFragment"
Expand Down
Loading

0 comments on commit 8111f66

Please sign in to comment.