Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

week3 commit 이승철 #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
8 changes: 8 additions & 0 deletions local.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Thu Aug 11 16:45:35 KST 2022
sdk.dir=/Users/leeseungcheol/Library/Android/sdk
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 파일도 git ignore 에 추가시켜도 됩니다~

7 changes: 7 additions & 0 deletions summer_coding_android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.github.bumptech.glide:glide:4.13.2'


testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'


}
4 changes: 4 additions & 0 deletions summer_coding_android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kr.co.landvibe.summer_coding_android">

<!-- 승철 -->
<uses-permission android:name="android.permission.INTERNET"/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

퍼미션 관련 부분에는 굳이 주석을 넣지 않아도 되요~!


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,71 @@ package kr.co.landvibe.summer_coding_android.leesc0893

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.LinearLayout
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.snackbar.Snackbar
import kr.co.landvibe.summer_coding_android.R
import kr.co.landvibe.summer_coding_android.databinding.ActivityLeesc0893Binding
import kr.co.landvibe.summer_coding_android.leesc0893.adapter.BookAdapter
import kr.co.landvibe.summer_coding_android.leesc0893.api.BookService
import kr.co.landvibe.summer_coding_android.leesc0893.model.BestSellerDto
import retrofit2.*
import retrofit2.converter.gson.GsonConverterFactory

class Leesc0893Activity : AppCompatActivity() {
private lateinit var binding: ActivityLeesc0893Binding
private lateinit var adapter: BookAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_leesc0893)
binding = ActivityLeesc0893Binding.inflate(layoutInflater)
setContentView(binding.root)
initRecyclerView()



val retrofit = Retrofit.Builder()
.baseUrl("https://book.interpark.com") // http 는 x , https 는 허용
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요런 상수들은 따로 상수화를 진행합니다.

companion object 에
const val BASE_URL = "https://book.interpark.com"
요런식으로들 써요~

.addConverterFactory(GsonConverterFactory.create())
.build()

val bookService = retrofit.create(BookService::class.java)

bookService.getBestSellerBooks("2513DC43FA0D012F7E9EF24437004EC8AAFD621E944D018691E66C2874B46AF0")
.enqueue(object : Callback<BestSellerDto> {
override fun onResponse( // 성공
call: Call<BestSellerDto>,
response: Response<BestSellerDto>
) {
if (response.isSuccessful.not()) {
Log.d(TAG, "response.isSuccessful.not()")
return
}
response.body()?.let { BestSellerDto ->
Log.d(TAG, BestSellerDto.toString())
BestSellerDto.books.forEach { book ->
Log.d(TAG, book.toString())
}
adapter.submitList(BestSellerDto.books)
}
}

override fun onFailure(call: Call<BestSellerDto>, t: Throwable) { // 실패
Log.d(TAG, t.toString())
}

})
/* bookService.getBooksByName()*/
}

private fun initRecyclerView(){
adapter = BookAdapter()
binding.mainRecyclerView.layoutManager = LinearLayoutManager(this)
binding.mainRecyclerView.adapter = adapter
}

companion object {
private const val TAG: String = "Leesc0893Activity"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package kr.co.landvibe.summer_coding_android.leesc0893.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import kr.co.landvibe.summer_coding_android.databinding.ItemBookBinding
import kr.co.landvibe.summer_coding_android.leesc0893.model.Book


class BookAdapter : ListAdapter<Book, BookAdapter.BookItemViewHolder>(diffUtil) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inner class BookItemViewHolder(private val binding: ItemBookBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(bookModel: Book) {
binding.titleTextView.text = bookModel.title
binding.contentTextView.text = bookModel.description
Glide.with(binding.bookImageView.context).load(bookModel.coverSmallUrl)
.into(binding.bookImageView)
}

}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BookItemViewHolder {
return BookItemViewHolder(
ItemBookBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
)
}

override fun onBindViewHolder(holder: BookItemViewHolder, position: Int) {
holder.bind(currentList[position])
}

companion object {
val diffUtil = object : DiffUtil.ItemCallback<Book>() {
override fun areItemsTheSame(oldItem: Book, newItem: Book): Boolean {
return oldItem == newItem
}

override fun areContentsTheSame(oldItem: Book, newItem: Book): Boolean {
return oldItem.id == newItem.id
}

}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package kr.co.landvibe.summer_coding_android.leesc0893.api

import kr.co.landvibe.summer_coding_android.leesc0893.model.BestSellerDto
import kr.co.landvibe.summer_coding_android.leesc0893.model.SearchBookDto
import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Query

interface BookService {

@GET("/api/search.api?output=json") // json 으로 받아오는 것 고정
fun getBooksByName(
@Query("key") apiKey: String,
@Query("query") keyword: String,
):Call<SearchBookDto>

@GET("/api/bestSeller.api?output=json&categoryId=100") // json 으로 받아오는 것 고정
fun getBestSellerBooks(
@Query("key") apiKey: String,
):Call<BestSellerDto>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package kr.co.landvibe.summer_coding_android.leesc0893.model

import com.google.gson.annotations.SerializedName

data class BestSellerDto( // 이게 데이터를 한번에 가져오고 "Book" data class 의 정보들을 mapping 된 상태로 저장
@SerializedName("title") val title: String,
@SerializedName("item") val books: List<Book>

)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package kr.co.landvibe.summer_coding_android.leesc0893.model

import com.google.gson.annotations.SerializedName

data class Book(
@SerializedName("itemId") val id : Long,
@SerializedName("title") val title : String,
@SerializedName("description") val description : String,
@SerializedName("coverSmallUrl") val coverSmallUrl : String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package kr.co.landvibe.summer_coding_android.leesc0893.model

import com.google.gson.annotations.SerializedName

data class SearchBookDto(
@SerializedName("title") val title: String,
@SerializedName("item") val books: List<Book>
)
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,21 @@
android:layout_height="match_parent"
tools:context=".leesc0893.Leesc0893Activity">

<EditText
android:id="@+id/searchEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/mainRecyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/searchEditText" />

</androidx.constraintlayout.widget.ConstraintLayout>
51 changes: 51 additions & 0 deletions summer_coding_android/app/src/main/res/layout/item_book.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/bookImageView"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:background="@color/black" />

<TextView
android:id="@+id/titleTextView"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

titleTextView 라는 id들은 평이 하게 쓰일 수 있으니
itemBookTitleText 와 같이, 해당 xml 의 파일명을 접두사 혹은 접미사로 넣어주는걸 추천드립니다.

android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:ellipsize="end"
android:fontFamily="@font/bmhannaprooft"
android:lines="1"
android:textColor="@color/black"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/bookImageView"
app:layout_constraintTop_toTopOf="parent"
tools:text="제목 예시" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tools:text = "내용"
요런식도 좋지만, 좀 긴 평문들이 필요하거나, 랜덤한 부분들 까지 xml 에서 확인하고 싶다면
tools:text=@tools:sample/lorem/random
요렇게도 사용합니다, android lorem 찾아보면 재미있는 것들이 많아요~!


<TextView
android:id="@+id/contentTextView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="12dp"
android:ellipsize="end"
android:fontFamily="@font/bmhannaprooft"
android:lines="1"
android:maxLines="3"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/titleTextView"
app:layout_constraintStart_toStartOf="@+id/titleTextView"
app:layout_constraintTop_toBottomOf="@+id/titleTextView"
tools:text="내용 예시" />


</androidx.constraintlayout.widget.ConstraintLayout>