Skip to content

Commit

Permalink
Merge pull request #267 from TeamDon-tBe/feature/#261-multipart-spree…
Browse files Browse the repository at this point in the history
…nt-3

[Feature/#261] 사진 업로드 - 3차 스프린트
  • Loading branch information
chanubc authored May 23, 2024
2 parents 9d9620d + 22df2b7 commit a133135
Show file tree
Hide file tree
Showing 41 changed files with 918 additions and 114 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.teamdontbe.core_ui.util.context

import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.content.res.Resources
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.net.Uri
import android.provider.Settings
import android.util.TypedValue
import android.view.View
import android.view.WindowManager
Expand Down Expand Up @@ -83,3 +87,24 @@ fun Context.statusBarColorOf(
window?.statusBarColor = colorOf(resId)
}
}

fun Context.showPermissionAppSettingsDialog() {
AlertDialog.Builder(this)
.setTitle("권한이 필요해요")
.setMessage("이 앱은 파일 및 미디어 접근 권한이 필요해요.\n앱 세팅으로 이동해서 권한을 부여 할 수 있어요.")
.setPositiveButton("이동하기") { dialog, _ ->
navigateToAppSettings()
dialog.dismiss()
}
.setNegativeButton("취소하기") { dialog, _ ->
dialog.dismiss()
}
.show()
}

fun Context.navigateToAppSettings() {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
val uri = Uri.fromParts("package", packageName, null)
intent.data = uri
startActivity(intent)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.teamdontbe.data_local.di

import android.content.ContentResolver
import android.content.Context
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object ContentResolverModule {
@Provides
@Singleton
fun providesContentResolver(
@ApplicationContext context: Context,
): ContentResolver = context.contentResolver
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ package com.teamdontbe.data_remote.api

import com.teamdontbe.data.dto.BaseResponse
import com.teamdontbe.data.dto.request.RequestCommentLikedDto
import com.teamdontbe.data.dto.request.RequestCommentPostingDto
import com.teamdontbe.data.dto.request.RequestFeedLikedDto
import com.teamdontbe.data.dto.request.RequestTransparentDto
import com.teamdontbe.data.dto.response.ResponseCommentDto
import com.teamdontbe.data.dto.response.ResponseFeedDto
import com.teamdontbe.data_remote.api.LoginApiService.Companion.API
import com.teamdontbe.data_remote.api.LoginApiService.Companion.V1
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.http.Body
import retrofit2.http.DELETE
import retrofit2.http.GET
import retrofit2.http.Multipart
import retrofit2.http.POST
import retrofit2.http.Part
import retrofit2.http.Path
import retrofit2.http.Query

Expand All @@ -23,7 +26,6 @@ interface HomeApiService {
const val CURSOR = "cursor"
const val COMMENT = "comment"
const val COMMENTS = "comments"
const val DETAIL = "detail"
const val CONTENT_ID = "contentId"
const val LIKED = "liked"
const val UNLIKED = "unliked"
Expand Down Expand Up @@ -64,10 +66,12 @@ interface HomeApiService {
@Path(value = CONTENT_ID) contentId: Int,
): BaseResponse<Unit>

@POST("/$API/$V1/$CONTENT/{$CONTENT_ID}/$COMMENT")
@Multipart
@POST("/$API/$V2/$CONTENT/{$CONTENT_ID}/$COMMENT")
suspend fun postCommentPosting(
@Path(value = CONTENT_ID) contentId: Int,
@Body request: RequestCommentPostingDto,
@Part("text") request: RequestBody,
@Part file: MultipartBody.Part?,
): BaseResponse<Unit>

@DELETE("/$API/$V1/$COMMENT/{$COMMENT_ID}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@ package com.teamdontbe.data_remote.api

import com.teamdontbe.data.dto.BaseResponse
import com.teamdontbe.data.dto.request.RequestPostingDto
import com.teamdontbe.data_remote.api.HomeApiService.Companion.V2
import com.teamdontbe.data_remote.api.LoginApiService.Companion.V1
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.http.Body
import retrofit2.http.Multipart
import retrofit2.http.POST
import retrofit2.http.Part

interface PostingApiService {
companion object {
const val API = "api"
const val V1 = "v1"
const val CONTENT = "content"
}

@POST("/$API/$V1/$CONTENT")
suspend fun posting(
@Body requestPostingDto: RequestPostingDto,
): BaseResponse<Unit>

@Multipart
@POST("/$API/$V2/$CONTENT")
suspend fun postingMultiPart(
@Part("text") text: RequestBody,
@Part image: MultipartBody.Part?,
): BaseResponse<Unit>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import androidx.paging.PagingData
import com.teamdontbe.data.datasource.HomeDataSource
import com.teamdontbe.data.dto.BaseResponse
import com.teamdontbe.data.dto.request.RequestCommentLikedDto
import com.teamdontbe.data.dto.request.RequestCommentPostingDto
import com.teamdontbe.data.dto.request.RequestTransparentDto
import com.teamdontbe.data.dto.response.ResponseFeedDto
import com.teamdontbe.data_remote.api.HomeApiService
Expand All @@ -15,6 +14,8 @@ import com.teamdontbe.data_remote.pagingsourceimpl.HomeFeedPagingSourceImpl
import com.teamdontbe.domain.entity.CommentEntity
import com.teamdontbe.domain.entity.FeedEntity
import kotlinx.coroutines.flow.Flow
import okhttp3.MultipartBody
import okhttp3.RequestBody
import javax.inject.Inject

class HomeDataSourceImpl
Expand Down Expand Up @@ -54,9 +55,10 @@ constructor(

override suspend fun postCommentPosting(
contentId: Int,
commentText: RequestCommentPostingDto,
commentText: RequestBody,
image: MultipartBody.Part?
): BaseResponse<Unit> {
return homeApiService.postCommentPosting(contentId, commentText)
return homeApiService.postCommentPosting(contentId, commentText, image)
}

override suspend fun deleteComment(commentId: Int): BaseResponse<Unit> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ import com.teamdontbe.data.datasource.PostingDataSource
import com.teamdontbe.data.dto.BaseResponse
import com.teamdontbe.data.dto.request.RequestPostingDto
import com.teamdontbe.data_remote.api.PostingApiService
import okhttp3.MultipartBody
import okhttp3.RequestBody
import javax.inject.Inject

class PostingDataSourceImpl
@Inject
constructor(private val postingApiService: PostingApiService) :
PostingDataSource {
override suspend fun posting(requestPosting: RequestPostingDto): BaseResponse<Unit> = postingApiService.posting(requestPosting)
class PostingDataSourceImpl @Inject constructor(
private val postingApiService: PostingApiService
) : PostingDataSource {
override suspend fun posting(requestPosting: RequestPostingDto): BaseResponse<Unit> =
postingApiService.posting(requestPosting)

override suspend fun postingMultiPart(
text: RequestBody,
image: MultipartBody.Part?
): BaseResponse<Unit> {
return postingApiService.postingMultiPart(text, image)
}
}
1 change: 1 addition & 0 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies {
// android
implementation(libs.bundles.room)
implementation(libs.paging)
implementation(libs.androidx.exifinterface)

// Third Party
implementation(libs.bundles.retrofit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package com.teamdontbe.data.datasource

import androidx.paging.PagingData
import com.teamdontbe.data.dto.BaseResponse
import com.teamdontbe.data.dto.request.RequestCommentPostingDto
import com.teamdontbe.data.dto.response.ResponseFeedDto
import com.teamdontbe.domain.entity.CommentEntity
import com.teamdontbe.domain.entity.FeedEntity
import kotlinx.coroutines.flow.Flow
import okhttp3.MultipartBody
import okhttp3.RequestBody

interface HomeDataSource {
fun getFeedList(): Flow<PagingData<FeedEntity>>
Expand All @@ -23,7 +24,8 @@ interface HomeDataSource {

suspend fun postCommentPosting(
contentId: Int,
commentText: RequestCommentPostingDto,
commentText: RequestBody,
image: MultipartBody.Part?
): BaseResponse<Unit>

suspend fun deleteComment(commentId: Int): BaseResponse<Unit>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ package com.teamdontbe.data.datasource

import com.teamdontbe.data.dto.BaseResponse
import com.teamdontbe.data.dto.request.RequestPostingDto
import okhttp3.MultipartBody
import okhttp3.RequestBody

interface PostingDataSource {
suspend fun posting(requestPostingDto: RequestPostingDto): BaseResponse<Unit>
suspend fun postingMultiPart(
text: RequestBody,
image: MultipartBody.Part?,
): BaseResponse<Unit>
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
package com.teamdontbe.data.repositoryimpl

import android.content.ContentResolver
import androidx.paging.PagingData
import com.teamdontbe.data.datasource.HomeDataSource
import com.teamdontbe.data.dto.request.RequestCommentPostingDto
import com.teamdontbe.data.repositoryimpl.utils.createImagePart
import com.teamdontbe.data.repositoryimpl.utils.extractErrorMessage
import com.teamdontbe.domain.entity.CommentEntity
import com.teamdontbe.domain.entity.FeedEntity
import com.teamdontbe.domain.repository.HomeRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.withContext
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import retrofit2.HttpException
import java.io.IOException
import javax.inject.Inject

class HomeRepositoryImpl
@Inject constructor(
private val contentResolver: ContentResolver,
private val homeDataSource: HomeDataSource,
) : HomeRepository {
override fun getFeedList(): Flow<PagingData<FeedEntity>> = homeDataSource.getFeedList()
Expand All @@ -21,7 +32,8 @@ class HomeRepositoryImpl
}
}

override fun getCommentList(contentId: Int): Flow<PagingData<CommentEntity>> = homeDataSource.getCommentList(contentId)
override fun getCommentList(contentId: Int): Flow<PagingData<CommentEntity>> =
homeDataSource.getCommentList(contentId)

override suspend fun deleteFeed(contentId: Int): Result<Boolean> {
return runCatching {
Expand All @@ -41,18 +53,6 @@ class HomeRepositoryImpl
}
}

override suspend fun postCommentPosting(
contentId: Int,
commentText: String,
): Result<Boolean> {
return runCatching {
homeDataSource.postCommentPosting(
contentId,
RequestCommentPostingDto(commentText, "comment"),
).success
}
}

override suspend fun deleteComment(commentId: Int): Result<Boolean> {
return runCatching {
homeDataSource.deleteComment(
Expand Down Expand Up @@ -88,4 +88,42 @@ class HomeRepositoryImpl
).success
}
}

override suspend fun postCommentPosting(
contentId: Int,
commentText: String,
uriString: String?
): Result<Boolean> {
return runCatching {

val infoRequestBody = createCommentRequestBody(commentText)
val imagePart = withContext(Dispatchers.IO) {
createImagePart(contentResolver, uriString)
}

homeDataSource.postCommentPosting(
contentId,
infoRequestBody,
imagePart
).success
}.onFailure { throwable ->
return when (throwable) {
is HttpException -> Result.failure(IOException(throwable.extractErrorMessage()))
else -> Result.failure(throwable)
}
}
}

private fun createCommentRequestBody(commentText: String): RequestBody {
val infoJson = JSONObject().apply {
put("commentText", commentText)
put("notificationTriggerType", COMMENT_VALUE)
}.toString()

return infoJson.toRequestBody("application/json".toMediaTypeOrNull())
}

companion object {
private const val COMMENT_VALUE = "comment"
}
}
Loading

0 comments on commit a133135

Please sign in to comment.