-
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.
[nunu/#43] feat: HomeFragment API 연동
- RetrofitInstance 추가 - AlbumApi, Album Response, AlbumService 구현 - HomeFragment 연결
- Loading branch information
1 parent
7473c1e
commit b8d6929
Showing
6 changed files
with
113 additions
and
2 deletions.
There are no files selected for viewing
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,9 @@ | ||
package com.example.umc_6th | ||
|
||
import retrofit2.Call | ||
import retrofit2.http.GET | ||
|
||
interface AlbumApi { | ||
@GET("/albums") | ||
fun getAlbums(): Call<AlbumResponse> | ||
} |
20 changes: 20 additions & 0 deletions
20
UMC_6th/app/src/main/java/com/example/umc_6th/AlbumResponse.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,20 @@ | ||
package com.example.umc_6th | ||
|
||
import com.google.gson.annotations.SerializedName | ||
|
||
data class AlbumResponse( @SerializedName("isSuccess") val isSuccess: Boolean, | ||
@SerializedName("code") val code: Int, | ||
@SerializedName("message") val message: String, | ||
@SerializedName("result") val result: TodayAlbumResult) | ||
|
||
|
||
data class TodayAlbumResult( | ||
@SerializedName("albums") val albums: List<TodayAlbum> | ||
) | ||
|
||
data class TodayAlbum( | ||
@SerializedName("albumIdx") val albumIdx: Int, | ||
@SerializedName("singer") val singer: String, | ||
@SerializedName("title") val title:String, | ||
@SerializedName("coverImgUrl") val coverImgUrl : String | ||
) |
40 changes: 40 additions & 0 deletions
40
UMC_6th/app/src/main/java/com/example/umc_6th/AlbumService.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,40 @@ | ||
package com.example.umc_6th | ||
|
||
import android.util.Log | ||
import retrofit2.Call | ||
import retrofit2.Callback | ||
import retrofit2.Response | ||
|
||
class AlbumService { | ||
private lateinit var homeAlbumView: HomeAlbumView | ||
|
||
fun setHomeAlbumView(homeAlbumView: HomeAlbumView) { | ||
this.homeAlbumView = homeAlbumView | ||
} | ||
|
||
fun getAlbum() { | ||
homeAlbumView.onGetAlbumLoading() | ||
|
||
RetrofitInstance.albumApi.getAlbums().enqueue(object : Callback<AlbumResponse> { | ||
override fun onResponse(call: Call<AlbumResponse>, response: Response<AlbumResponse>) { | ||
if (response.isSuccessful && response.code() == 200) { | ||
val albumResponse: AlbumResponse = response.body()!! | ||
|
||
Log.d("ALBUM-RESPONSE", albumResponse.toString()) | ||
|
||
when (val code = albumResponse.code) { | ||
1000 -> { | ||
homeAlbumView.onGetAlbumSuccess(code, albumResponse.result!!) | ||
} | ||
|
||
else -> homeAlbumView.onGetAlbumFailure(code, albumResponse.message) | ||
} | ||
} | ||
} | ||
|
||
override fun onFailure(call: Call<AlbumResponse>, t: Throwable) { | ||
homeAlbumView.onGetAlbumFailure(400, "네트워크 오류가 발생했습니다.") | ||
} | ||
}) | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
UMC_6th/app/src/main/java/com/example/umc_6th/HomeAlbumView.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 com.example.umc_6th | ||
|
||
interface HomeAlbumView { | ||
fun onGetAlbumLoading() | ||
fun onGetAlbumSuccess(code: Int, result: TodayAlbumResult) | ||
fun onGetAlbumFailure(code: Int, message: String) | ||
} |
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