Skip to content

Commit

Permalink
[ADD/#9] Music 프래그먼트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
b1urrrr committed Dec 8, 2022
1 parent be84fa7 commit cc25560
Show file tree
Hide file tree
Showing 21 changed files with 233 additions and 98 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ android {

buildConfigField "String", "BASE_URL", properties['base_url']
buildConfigField "String", "REQRES_URL", properties['reqres_url']
buildConfigField "String", "MUSIC_URL", properties['music_url']
}

buildTypes {
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/java/org/sopt/sample/api/MusicService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.sopt.sample.api

import okhttp3.MultipartBody
import org.sopt.sample.data.dto.request.RequestRegisterMusicDto
import org.sopt.sample.data.dto.response.ResponseGetMusicListDto
import org.sopt.sample.data.dto.response.ResponseRegisterMusicDto
import retrofit2.http.* // ktlint-disable no-wildcard-imports

interface MusicService {
// 음악 리스트 조회 API
@GET("music/list")
suspend fun getMusicList(): ResponseGetMusicListDto

// 음악 생성 API
@Multipart
@POST("music")
suspend fun registerMusic(
@Part image: MultipartBody.Part?,
@Body requestRegisterMusic: RequestRegisterMusicDto
): ResponseRegisterMusicDto
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.sopt.sample.data.dto.request

import kotlinx.serialization.Serializable

@Serializable
data class RequestRegisterMusicDto(
val singer: String,
val title: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.sopt.sample.data.dto.response

import kotlinx.serialization.Serializable

@Serializable
data class ResponseGetMusicListDto(
val statusCode: Int,
val success: Boolean,
val message: String,
val data: List<Music>
) {
@Serializable
data class Music(
val id: Int,
val image: String,
val title: String,
val singer: String
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.sopt.sample.data.dto.response

import kotlinx.serialization.Serializable

@Serializable
data class ResponseRegisterMusicDto(
val statusCode: Int,
val success: Boolean,
val message: String,
val data: ResponseGetMusicListDto.Music
)
3 changes: 2 additions & 1 deletion app/src/main/java/org/sopt/sample/data/local/BaseUrlType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package org.sopt.sample.data.local

enum class BaseUrlType {
IN_SOPT,
REQRES
REQRES,
MUSIC
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.sopt.sample.data.repository

import okhttp3.MultipartBody
import org.sopt.sample.data.dto.request.RequestRegisterMusicDto
import org.sopt.sample.data.dto.response.ResponseGetMusicListDto
import org.sopt.sample.data.dto.response.ResponseRegisterMusicDto

interface MusicRepository {
suspend fun getMusicList(): Result<ResponseGetMusicListDto>

suspend fun registerMusic(
image: MultipartBody.Part,
requestRegisterMusicDto: RequestRegisterMusicDto
): Result<ResponseRegisterMusicDto>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.sopt.sample.data.repository

import okhttp3.MultipartBody
import org.sopt.sample.data.dto.request.RequestRegisterMusicDto
import org.sopt.sample.data.dto.response.ResponseGetMusicListDto
import org.sopt.sample.data.dto.response.ResponseRegisterMusicDto
import org.sopt.sample.data.source.MusicDataSource
import javax.inject.Inject

class MusicRepositoryImpl @Inject constructor(
private val musicDataSource: MusicDataSource
) : MusicRepository {
override suspend fun getMusicList(): Result<ResponseGetMusicListDto> =
kotlin.runCatching { musicDataSource.getMusicList() }

override suspend fun registerMusic(
image: MultipartBody.Part,
requestRegisterMusicDto: RequestRegisterMusicDto
): Result<ResponseRegisterMusicDto> =
kotlin.runCatching { musicDataSource.registerMusic(image, requestRegisterMusicDto) }
}
18 changes: 18 additions & 0 deletions app/src/main/java/org/sopt/sample/data/source/MusicDataSource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.sopt.sample.data.source

import okhttp3.MultipartBody
import org.sopt.sample.api.MusicService
import org.sopt.sample.data.dto.request.RequestRegisterMusicDto
import org.sopt.sample.data.dto.response.ResponseGetMusicListDto
import org.sopt.sample.data.dto.response.ResponseRegisterMusicDto
import javax.inject.Inject

class MusicDataSource @Inject constructor(
private val musicService: MusicService
) {
suspend fun getMusicList(): ResponseGetMusicListDto =
musicService.getMusicList()

suspend fun registerMusic(image: MultipartBody.Part, requestRegisterMusic: RequestRegisterMusicDto): ResponseRegisterMusicDto =
musicService.registerMusic(image, requestRegisterMusic)
}
14 changes: 12 additions & 2 deletions app/src/main/java/org/sopt/sample/di/RetrofitModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.sopt.sample.BuildConfig.BASE_URL
import org.sopt.sample.BuildConfig.REQRES_URL
import org.sopt.sample.BuildConfig.* // ktlint-disable no-wildcard-imports
import org.sopt.sample.data.local.BaseUrlType
import retrofit2.Retrofit
import javax.inject.Qualifier
Expand Down Expand Up @@ -54,6 +53,17 @@ object RetrofitModule {
.client(client)
.build()

@Provides
@Singleton
@Retrofit2(BaseUrlType.MUSIC)
fun providesMusicRetrofit(
client: OkHttpClient
): Retrofit = Retrofit.Builder()
.baseUrl(MUSIC_URL)
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
.client(client)
.build()

@Qualifier
annotation class Retrofit2(val baseUrlType: BaseUrlType)
}
5 changes: 5 additions & 0 deletions app/src/main/java/org/sopt/sample/di/RetrofitServiceModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import org.sopt.sample.api.AuthService
import org.sopt.sample.api.FollowerService
import org.sopt.sample.api.MusicService
import org.sopt.sample.data.local.BaseUrlType
import retrofit2.Retrofit

Expand All @@ -19,4 +20,8 @@ object RetrofitServiceModule {
@Provides
fun providesFollowerService(@RetrofitModule.Retrofit2(BaseUrlType.REQRES) retrofit: Retrofit): FollowerService =
retrofit.create(FollowerService::class.java)

@Provides
fun providesMusicService(@RetrofitModule.Retrofit2(BaseUrlType.MUSIC) retrofit: Retrofit): MusicService =
retrofit.create(MusicService::class.java)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import dagger.hilt.android.AndroidEntryPoint
import org.sopt.sample.R
import org.sopt.sample.databinding.ActivityMainBinding
import org.sopt.sample.presentation.main.home.HomeFragment
import org.sopt.sample.presentation.main.music.MusicFragment
import org.sopt.sample.presentation.main.setting.SettingFragment
import org.sopt.sample.presentation.user.GalleryFragment
import org.sopt.sample.presentation.main.user.GalleryFragment
import org.sopt.sample.util.binding.BindingActivity

@AndroidEntryPoint
Expand All @@ -30,6 +31,7 @@ class MainActivity : BindingActivity<ActivityMainBinding>(R.layout.activity_main
binding.bnvHome.setOnItemSelectedListener {
when (it.itemId) {
R.id.menu_home -> setCurrentFragment<HomeFragment>()
R.id.menu_music -> setCurrentFragment<MusicFragment>()
R.id.menu_gallery -> setCurrentFragment<GalleryFragment>()
R.id.menu_setting -> setCurrentFragment<SettingFragment>()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class FollowerAdapter(context: Context) : RecyclerView.Adapter<RecyclerView.View
false
)
)
else -> throw ClassCastException("Unknown View BaseUrlType : $viewType")
else -> throw ClassCastException("Unknown View Type : $viewType")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class HomeFragment : BindingFragment<FragmentHomeBinding>(R.layout.fragment_home
VIEW_TYPE_HEADER -> 2
VIEW_TYPE_ITEM -> 1
else -> throw ClassCastException(
"Unknown View BaseUrlType : ${followerAdapter.getItemViewType(position)}"
"Unknown View Type : ${followerAdapter.getItemViewType(position)}"
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.sopt.sample.presentation.main.music

import android.os.Bundle
import android.view.View
import org.sopt.sample.R
import org.sopt.sample.databinding.FragmentMusicBinding
import org.sopt.sample.util.binding.BindingFragment

class MusicFragment : BindingFragment<FragmentMusicBinding>(R.layout.fragment_music) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}

companion object {
fun newInstance(): MusicFragment {
return MusicFragment()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,22 @@
package org.sopt.sample.presentation.user
package org.sopt.sample.presentation.main.user

import android.Manifest
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment
import coil.load
import org.sopt.sample.R
import org.sopt.sample.databinding.FragmentGalleryBinding
import org.sopt.sample.util.binding.BindingFragment

class GalleryFragment : Fragment() {
private var _binding: FragmentGalleryBinding? = null
private val binding: FragmentGalleryBinding
get() = requireNotNull(_binding) { "binding value was null." }

class GalleryFragment : BindingFragment<FragmentGalleryBinding>(R.layout.fragment_gallery) {
private val launcher =
registerForActivityResult(ActivityResultContracts.PickMultipleVisualMedia()) {
binding.imgSample.load(it.get(0))
binding.imgSample2.load(it.get(1))
binding.imgSample.load(it[0])
binding.imgSample2.load(it[1])
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentGalleryBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val permissionLauncher =
Expand All @@ -42,11 +28,6 @@ class GalleryFragment : Fragment() {
}
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

companion object {
fun newInstance(): GalleryFragment {
return GalleryFragment()
Expand Down

This file was deleted.

5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_music.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M20,2L8,2c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2L22,4c0,-1.1 -0.9,-2 -2,-2zM18,7h-3v5.5c0,1.38 -1.12,2.5 -2.5,2.5S10,13.88 10,12.5s1.12,-2.5 2.5,-2.5c0.57,0 1.08,0.19 1.5,0.51L14,5h4v2zM4,6L2,6v14c0,1.1 0.9,2 2,2h14v-2L4,20L4,6z"/>
</vector>
Loading

0 comments on commit cc25560

Please sign in to comment.