generated from GO-SOPT-ANDROID/android-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
266 additions
and
25 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
app/src/main/java/org/android/go/sopt/data/repository/ImageRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.android.go.sopt.data.repository | ||
|
||
import okhttp3.MultipartBody | ||
import org.android.go.sopt.data.source.remote.ImageDataSource | ||
import org.android.go.sopt.domain.repository.ImageRepository | ||
import javax.inject.Inject | ||
|
||
class ImageRepositoryImpl @Inject constructor( | ||
private val imageDataSource: ImageDataSource, | ||
) : ImageRepository { | ||
override suspend fun uploadImage(file: MultipartBody.Part): Result<Unit> = kotlin.runCatching { | ||
imageDataSource.uploadImage(file) | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
app/src/main/java/org/android/go/sopt/data/service/ImageService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.android.go.sopt.data.service | ||
|
||
import okhttp3.MultipartBody | ||
import retrofit2.http.Multipart | ||
import retrofit2.http.POST | ||
import retrofit2.http.Part | ||
|
||
interface ImageService { | ||
@Multipart | ||
@POST("upload") | ||
suspend fun uploadImage( | ||
@Part file: MultipartBody.Part, | ||
) | ||
} |
11 changes: 11 additions & 0 deletions
11
app/src/main/java/org/android/go/sopt/data/source/remote/ImageDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.android.go.sopt.data.source.remote | ||
|
||
import okhttp3.MultipartBody | ||
import org.android.go.sopt.data.service.ImageService | ||
import javax.inject.Inject | ||
|
||
class ImageDataSource @Inject constructor( | ||
private val imageService: ImageService, | ||
) { | ||
suspend fun uploadImage(file: MultipartBody.Part) = imageService.uploadImage(file) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
app/src/main/java/org/android/go/sopt/domain/repository/ImageRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.android.go.sopt.domain.repository | ||
|
||
import okhttp3.MultipartBody | ||
|
||
interface ImageRepository { | ||
suspend fun uploadImage(file: MultipartBody.Part): Result<Unit> | ||
} |
67 changes: 51 additions & 16 deletions
67
app/src/main/java/org/android/go/sopt/presentation/main/gallery/GalleryFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
app/src/main/java/org/android/go/sopt/presentation/main/gallery/GalleryViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.android.go.sopt.presentation.main.gallery | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.launch | ||
import okhttp3.MultipartBody | ||
import org.android.go.sopt.domain.repository.ImageRepository | ||
import org.android.go.sopt.util.ContentUriRequestBody | ||
import org.android.go.sopt.util.state.RemoteUiState | ||
import org.android.go.sopt.util.state.RemoteUiState.Failure | ||
import org.android.go.sopt.util.state.RemoteUiState.Success | ||
import retrofit2.HttpException | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class GalleryViewModel @Inject constructor( | ||
private val imageRepository: ImageRepository, | ||
) : ViewModel() { | ||
private val _postImageState = MutableLiveData<RemoteUiState>() | ||
val postImageState: LiveData<RemoteUiState> | ||
get() = _postImageState | ||
|
||
private val _imageFile = MutableLiveData<ContentUriRequestBody>() | ||
val imageFile: LiveData<ContentUriRequestBody> | ||
get() = _imageFile | ||
|
||
fun setImageFile(requestBody: ContentUriRequestBody) { | ||
_imageFile.value = requestBody | ||
} | ||
|
||
fun postImage() { | ||
viewModelScope.launch { | ||
// TODO: null 처리 | ||
imageRepository.uploadImage(imageFile.value!!.toFormData()) | ||
.onSuccess { response -> | ||
Timber.d("POST IMAGE SUCCESS : $response") | ||
_postImageState.value = Success | ||
} | ||
.onFailure { throwable -> | ||
if (throwable is HttpException) { | ||
Timber.e("POST IMAGE FAILURE(${throwable.code()}) : ${throwable.message()}") | ||
_postImageState.value = Failure(null) | ||
} | ||
} | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
app/src/main/java/org/android/go/sopt/util/ContentUriRequestBody.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.android.go.sopt.util | ||
|
||
import android.content.Context | ||
import android.net.Uri | ||
import android.provider.MediaStore | ||
import okhttp3.MediaType | ||
import okhttp3.MediaType.Companion.toMediaTypeOrNull | ||
import okhttp3.MultipartBody | ||
import okhttp3.RequestBody | ||
import okio.BufferedSink | ||
import okio.source | ||
|
||
class ContentUriRequestBody( | ||
context: Context, | ||
private val uri: Uri, | ||
) : RequestBody() { | ||
private val contentResolver = context.contentResolver | ||
|
||
private var fileName = "" | ||
private var size = -1L | ||
|
||
init { | ||
contentResolver.query( | ||
uri, | ||
arrayOf(MediaStore.Images.Media.SIZE, MediaStore.Images.Media.DISPLAY_NAME), | ||
null, | ||
null, | ||
null, | ||
)?.use { cursor -> | ||
if (cursor.moveToFirst()) { | ||
size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE)) | ||
fileName = | ||
cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME)) | ||
} | ||
} | ||
} | ||
|
||
fun getFileName() = fileName | ||
|
||
override fun contentLength(): Long = size | ||
|
||
override fun contentType(): MediaType? = | ||
contentResolver.getType(uri)?.toMediaTypeOrNull() | ||
|
||
override fun writeTo(sink: BufferedSink) { | ||
contentResolver.openInputStream(uri)?.source()?.use { source -> | ||
sink.writeAll(source) | ||
} | ||
} | ||
|
||
fun toFormData() = MultipartBody.Part.createFormData("file", getFileName(), this) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters