Skip to content

Commit

Permalink
[ADD] : network neighborhoodLife api #11
Browse files Browse the repository at this point in the history
  • Loading branch information
Jokwanhee committed Nov 29, 2023
1 parent 92ae33e commit c0dc703
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 14 deletions.
6 changes: 6 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ plugins {
id 'kotlin-parcelize'
}

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())

android {
namespace 'org.sopt.carrot'
compileSdk 34
Expand All @@ -17,6 +20,8 @@ android {
versionCode 1
versionName "1.0"

buildConfigField "String", "BASE_URL", properties["base.url"]

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand All @@ -36,6 +41,7 @@ android {
buildFeatures {
viewBinding true
dataBinding true
buildConfig true
}
}

Expand Down
20 changes: 20 additions & 0 deletions app/src/main/java/org/sopt/carrot/CarrotApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package org.sopt.carrot

import android.app.Application
import androidx.appcompat.app.AppCompatDelegate
import org.sopt.carrot.data.api.RetrofitServicePool
import org.sopt.carrot.data.datasource.remote.NeighborhoodLifeRemoteDatasource
import org.sopt.carrot.data.repo.NeighborhoodLifeRepository
import timber.log.Timber

class CarrotApp : Application() {
Expand All @@ -17,4 +20,21 @@ class CarrotApp : Application() {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}

companion object {
private lateinit var neighborhoodLifeRepository: NeighborhoodLifeRepository

@Synchronized
fun getNeighborhoodLifeRepositoryInstance(): NeighborhoodLifeRepository {
if (!::neighborhoodLifeRepository.isInitialized) {
try {
neighborhoodLifeRepository = NeighborhoodLifeRepository(
NeighborhoodLifeRemoteDatasource(RetrofitServicePool.carrotService)
)
} catch (e: ExceptionInInitializerError) {
e.printStackTrace()
}
}
return neighborhoodLifeRepository
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import org.sopt.carrot.data.model.neighborhoodlife.LivesResponse
import retrofit2.http.GET
import retrofit2.http.Query

interface NeighborhoodLifeService {
interface CarrotService {
@GET("api/lives")
fun getLives(
@Query("category") category: String
): LivesResponse

@GET("api/lives")
suspend fun getLives(): LivesResponse
}
33 changes: 33 additions & 0 deletions app/src/main/java/org/sopt/carrot/data/api/RetrofitManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.sopt.carrot.data.api

import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.sopt.carrot.BuildConfig
import retrofit2.Retrofit

object RetrofitManager {
private const val BASE_URL = BuildConfig.BASE_URL

private val httpLoggingInterceptor = HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY)

private val okHttpClient = OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor)
.build()

val retrofit: Retrofit =
Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
.build()

inline fun <reified T> create(): T = retrofit.create<T>(T::class.java)
}

object RetrofitServicePool {
val carrotService = RetrofitManager.create<CarrotService>()
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package org.sopt.carrot.data.datasource.remote

import org.sopt.carrot.data.api.NeighborhoodLifeService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.withContext
import org.sopt.carrot.data.api.CarrotService
import org.sopt.carrot.data.model.neighborhoodlife.LivesDataResponse
import org.sopt.carrot.data.model.neighborhoodlife.LivesResponse
import retrofit2.Response

class NeighborhoodLifeRemoteDatasource(
private val neighborhoodLifeService: NeighborhoodLifeService
private val carrotService: CarrotService
) {
suspend fun getLives(category: String): List<LivesDataResponse> {
return neighborhoodLifeService.getLives(category).data
suspend fun getLives(): List<LivesDataResponse> {
return withContext(Dispatchers.IO) {
async {
carrotService.getLives().data
}.await()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.sopt.carrot.data.datasource.remote.NeighborhoodLifeRemoteDatasource
import org.sopt.carrot.data.model.neighborhoodlife.LivesDataResponse
import kotlin.coroutines.coroutineContext

class NeighborhoodLifeRepository(
private val neighborhoodLifeRemoteDatasource: NeighborhoodLifeRemoteDatasource
) {
suspend fun getLives(category: String): List<LivesDataResponse> =
suspend fun getLives(): List<LivesDataResponse> =
withContext(Dispatchers.IO) {
neighborhoodLifeRemoteDatasource.getLives(category)
neighborhoodLifeRemoteDatasource.getLives()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.RecyclerView.VERTICAL
import org.sopt.carrot.CarrotApp
import org.sopt.carrot.R
import org.sopt.carrot.core.ui.base.BindingFragment
import org.sopt.carrot.core.ui.fragment.snackBar
Expand All @@ -18,14 +20,15 @@ import org.sopt.carrot.presentation.profile.ProfileActivity

class NeighborhoodLifeFragment :
BindingFragment<FragmentNeighborhoodLifeBinding>(R.layout.fragment_neighborhood_life) {
private val neighborhoodViewModel: NeighborhoodViewModel by activityViewModels()
private lateinit var neighborhoodViewModel: NeighborhoodViewModel

private lateinit var neighborhoodLifeAdapter: NeighborhoodLifeAdapter
private lateinit var carouselTextAdapter: CarouselTextAdapter
private lateinit var carouselTagAdapter: CarouselTagAdapter
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

initNeighborhoodLifeViewModel()
initCarouselTextDummyAdapter()
initCarouselTagDummyAdapter()
initNeighborhoodLifeAdapter()
Expand All @@ -34,6 +37,12 @@ class NeighborhoodLifeFragment :
observeData()
}

private fun initNeighborhoodLifeViewModel() {
neighborhoodViewModel = NeighborhoodLifeViewModelProvider(
CarrotApp.getNeighborhoodLifeRepositoryInstance()
).create(NeighborhoodViewModel::class.java)
}

private fun initLives() {
neighborhoodViewModel.getLives()
}
Expand All @@ -59,6 +68,7 @@ class NeighborhoodLifeFragment :
private fun initNeighborhoodLifeAdapter() {
neighborhoodLifeAdapter = NeighborhoodLifeAdapter()
binding.rcvContents.adapter = neighborhoodLifeAdapter
binding.rcvContents.addItemDecoration(DividerItemDecoration(requireContext(), VERTICAL))
}

private fun observeData() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.sopt.carrot.presentation.neighborhoodlife

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import org.sopt.carrot.data.repo.NeighborhoodLifeRepository

class NeighborhoodLifeViewModelProvider(
private val neighborhoodLifeRepository: NeighborhoodLifeRepository
) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return modelClass.getConstructor(NeighborhoodLifeRepository::class.java)
.newInstance(neighborhoodLifeRepository)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class NeighborhoodViewModel(
private val _livesList = MutableLiveData<List<LivesDataResponse>>()
val livesList: LiveData<List<LivesDataResponse>> get() = _livesList

fun getLives(category: String = "") {
fun getLives() {
viewModelScope.launch {
kotlin.runCatching {
neighborhoodLifeRepository.getLives(category)
neighborhoodLifeRepository.getLives()
}.onSuccess {
_livesList.value = it
}.onFailure {
Expand Down

0 comments on commit c0dc703

Please sign in to comment.