Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
Binary file removed server/.DS_Store
Binary file not shown.
10 changes: 1 addition & 9 deletions teawon/project/toyproejct/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,7 @@ dependencies {
implementation("io.ktor:ktor-client-serialization:1.4.0")
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.1"

// implementation("io.ktor:ktor-client-core:1.6.7")
// implementation("io.ktor:ktor-client-cio:1.6.7")
// implementation("io.ktor:ktor-client-android:1.6.0")
// implementation "io.ktor:ktor-client-logging:1.6.0"
// implementation("io.ktor:ktor-client-serialization:1.4.0")
// implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.1" //ktor

// implementation "io.ktor:ktor-client-core:1.6.1"
// implementation "io.ktor:ktor-client-cio:1.6.1"
implementation "androidx.compose.runtime:runtime-livedata:$compose_ui_version" //ui

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.toy_proejct.data

data class User(
val username: String,
val phone: String
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.toy_proejct.api.getDetail
package com.example.toy_proejct.data.product.detail

@kotlinx.serialization.Serializable
data class DetailDto(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.toy_proejct.api.getDetail
package com.example.toy_proejct.data.product.detail
@kotlinx.serialization.Serializable
data class MallDtoInfo(
val delivery: Int,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.toy_proejct.api.getSearchList
package com.example.toy_proejct.data.product.list

@kotlinx.serialization.Serializable
data class GetSearchList(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.toy_proejct.api.getSearchList
package com.example.toy_proejct.data.product.list

@kotlinx.serialization.Serializable
data class ProductListDto(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example.toy_proejct.di

import com.example.toy_proejct.domain.product.ProductRepository
import com.example.toy_proejct.domain.product.ProductRepositoryImpl
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.features.json.*
import io.ktor.client.features.json.serializer.*

object DataModule {
private val client: HttpClient = HttpClient(CIO) {
install(JsonFeature) {
serializer = KotlinxSerializer(
kotlinx.serialization.json.Json {
prettyPrint = true
isLenient = true
ignoreUnknownKeys = true
}
)
}
}

val productRepository: ProductRepository = ProductRepositoryImpl(client)

/** [의존성 주입(DI)과 이유]
* 1. client는 모든 api에서 사용된다. 따라서 매 호출마다 각각 부르는 것보다 하나의 모듈로 관리해야 한다. 보일러플레이트 문제의 방지
*
* 2. 인터페이스를 통한 도메인 구현
* 23번째 줄을 보면 인터페이스로 선언한 ProductRepository을 통해 실제 필요한 객체를 불러오고있다.
*
* 3. 각 인터페이스를 상속받아 대해 실제 데이터을 가져오는 도메인을 각 DB , 특성에 따라 구현
*
* 4. 각 ViewModel에서는 위의 도메인객체를 통해 값을 가져온다.
*
* -> 왜? 만약 DB를 바꾸게된다면 각각의 종속적인 DB에 대해 접근하는 메소드를 따로 다 만들어야한다.
* -> 하지만 23번째 줄에서 이미 구현한 도메인에 대해 사용하려는 도메인만 바꿔주면 전체 코드를 뜯어고치지 않고 바로 변경이 가능하다.
*
*
* **/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.toy_proejct.domain.product

import com.example.toy_proejct.data.product.detail.DetailDto
import com.example.toy_proejct.data.product.list.GetSearchList

interface ProductRepository {
suspend fun fetchProductList(keyword: String): GetSearchList
suspend fun fetchProductDetail(url: String): DetailDto
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.toy_proejct.domain.product

import com.example.toy_proejct.data.product.detail.DetailDto
import com.example.toy_proejct.data.product.list.GetSearchList
import io.ktor.client.*
import io.ktor.client.request.*

class ProductRepositoryImpl(private val client: HttpClient): ProductRepository {
override suspend fun fetchProductList(keyword: String): GetSearchList =
client.get("http://3.39.75.19:8080/api/v1/crawler/search/products?word=$keyword")

override suspend fun fetchProductDetail(url: String): DetailDto =
client.get("http://3.39.75.19:8080/api/v1/crawler/search/product?url=${url}")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.toy_proejct.domain.product

import com.example.toy_proejct.data.product.detail.DetailDto
import com.example.toy_proejct.data.product.list.GetSearchList
import io.ktor.client.*

class ProductRepositoryRetrofit(private val client: HttpClient): ProductRepository {
override suspend fun fetchProductList(keyword: String): GetSearchList {
TODO("Not yet implemented")
}

override suspend fun fetchProductDetail(url: String): DetailDto {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.toy_proejct.domain.user

import com.example.toy_proejct.data.User

interface UserRepository {
suspend fun getUserList(): List<User>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.toy_proejct.domain.user

import com.example.toy_proejct.data.User

class UserRepositoryImpl: UserRepository {
override suspend fun getUserList(): List<User> {
return emptyList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,23 @@ import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CornerSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment

import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat.startActivity
import coil.compose.AsyncImage
import com.example.toy_proejct.api.getDetail.DetailDto
import com.example.toy_proejct.api.getDetail.MallDtoInfo
import com.example.toy_proejct.api.getSearchList.ProductListDto
import com.example.toy_proejct.scenarios.detail.DetailActivity
import com.example.toy_proejct.scenarios.home.ItemRow
import com.example.toy_proejct.data.product.detail.DetailDto
import com.example.toy_proejct.data.product.detail.MallDtoInfo
import com.example.toy_proejct.ui.component.CommonComponent
import com.example.toy_proejct.utils.UnitHelper
import kotlinx.coroutines.launch
import java.util.*

@Composable
fun DetailScreen(viewModel: DetailViewModel, url: String, back:() -> Unit) {
Expand Down Expand Up @@ -130,7 +119,7 @@ fun MallList(item: MallDtoInfo) { //각 상품에 대한 설명
style = MaterialTheme.typography.h6
)
Text(
text = " ${item.price}원",
text = UnitHelper.getStringFromMoneyInteger(item.price),
style = MaterialTheme.typography.h6,
color = Color.Blue

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,34 @@ import androidx.compose.runtime.MutableState
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import com.example.toy_proejct.LogHelper
import com.example.toy_proejct.api.getDetail.DetailDto
import com.example.toy_proejct.api.getDetail.MallDtoInfo
import com.example.toy_proejct.api.getSearchList.GetSearchList
import com.example.toy_proejct.api.getSearchList.ProductListDto
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.features.json.*
import io.ktor.client.features.json.serializer.*
import io.ktor.client.request.*
import com.example.toy_proejct.di.DataModule
import com.example.toy_proejct.domain.product.ProductRepository
import com.example.toy_proejct.data.product.detail.DetailDto
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.Json


class DetailViewModel : ViewModel() {
class DetailViewModel(private val productRepository: ProductRepository = DataModule.productRepository) : ViewModel() {
private val _detailInfo: MutableState<DetailDto> = mutableStateOf<DetailDto>(
DetailDto(
"",
listOf(), 0, "", ""
)
)
val detailInfo: MutableState<DetailDto> = _detailInfo; //화면에 표현될 list

private val _detailInfo : MutableState<DetailDto> = mutableStateOf<DetailDto>(DetailDto("",
listOf(),0,"",""))
val detailInfo: MutableState<DetailDto> =_detailInfo; //화면에 표현될 list
private val _isLoading: MutableState<Boolean> = mutableStateOf(value = false)
val isLoading: State<Boolean> = _isLoading

private val _isLoading: MutableState<Boolean> = mutableStateOf(value = false)
val isLoading : State<Boolean> = _isLoading

private val client: HttpClient = HttpClient(CIO) {
install(JsonFeature) {
serializer = KotlinxSerializer(
kotlinx.serialization.json.Json {
prettyPrint = true
isLenient = true
ignoreUnknownKeys = true
}
)
}
}


suspend fun getDetailInfo(url:String) {
_isLoading.value = true
suspend fun getDetailInfo(url: String) {
withContext(Dispatchers.IO) {
_isLoading.value = true
kotlin.runCatching {
client.get<DetailDto>("http://3.39.75.19:8080/api/v1/crawler/search/product?url=${url}")
productRepository.fetchProductDetail(url)
}.onSuccess {
_detailInfo.value = it //성공시 데이터 갱신
_isLoading.value = false
LogHelper.print("success111: ${it}")
}.onFailure {
LogHelper.print("faaaailed: $it")
}
}
}

}
Loading