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

영화 목록 출력 #22

Open
wants to merge 17 commits into
base: handnew04
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions ToyProject/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ android {
dataBinding {
enabled = true
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.activity:activity-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.toyproject.adapter

import android.widget.ImageView
import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.toyproject.data.entity.SearchMovieData
import com.example.toyproject.ui.main.MainMoviePostingRecyclerAdapter

@BindingAdapter("bind:replace")
fun RecyclerView.replaceAll(item: List<SearchMovieData>?) {
if (!item.isNullOrEmpty()) (adapter as? MainMoviePostingRecyclerAdapter)!!.setItemList(item as ArrayList<SearchMovieData>)
Copy link
Collaborator

Choose a reason for hiding this comment

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

as?는 타입 캐스팅에 실패했을 경우 null을 반환하기 때문에, 만약 캐스팅이 실패한다면 setItemList가 호출될 때 NPE가 발생하게 됩니다. !! 대신 ?를 사용하는 것이 좋아 보입니다 ㅎㅎ

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

앗 감사합니다!
8ab4e5e

}

@BindingAdapter("bind:bindImage")
fun bindImage(imageView: ImageView, imageUri: String) {
if (imageUri.isNotEmpty())
Glide.with(imageView.context).load(imageUri).into(imageView)
Copy link
Collaborator

Choose a reason for hiding this comment

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

kotlin style guide에 따르면, 실행문이 단문이더라도 괄호로 감싸주는 것이 권장사항입니다.
관련 링크 : https://developer.android.com/kotlin/style-guide/#braces

Copy link
Collaborator

Choose a reason for hiding this comment

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

RecyclerView와 마찬가지로 확잠 하수로 만들면 어떨까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

혹시 이렇게 바꾸면 말씀하신 확장함수일까요 ? 검색해보니 이런 형태인것 같아서요
8ab4e5e

Copy link
Collaborator

Choose a reason for hiding this comment

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

@handnew04 네 맞습니다 ㅎㅎ

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.toyproject.ui.main

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.toyproject.data.entity.SearchMovieData
import com.example.toyproject.data.repository.MovieRepositoryImpl

class MainViewModel() : ViewModel() {
val TAG = this.javaClass.name
val movieData = MutableLiveData<List<SearchMovieData>>()
val query = MutableLiveData<String>()

fun searchMovie() {
MovieRepositoryImpl.getMovieData(query.toString(), success = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

liveData를 사용하셨으므로 query.value를 사용하는 것 이 좋을 것 같네요

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

3b18433
늦어서 죄송해요. 감사합니다 ! ㅜㅜ

movieData.value = it.searchMovieResults
}, fail = {
Log.e(TAG, it.message)
})
}
}
10 changes: 7 additions & 3 deletions ToyProject/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
xmlns:tools="http://schemas.android.com/tools"
xmlns:bind="http://schemas.android.com/tools">

<data>

<variable
name="vm"
type="com.example.toyproject.ui.main.MainViewModel" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
Expand All @@ -13,9 +16,10 @@
tools:context=".ui.main.MainActivity">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rcv_poster"
android:layout_width="0dp"
android:layout_height="0dp"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
bind:replace="@{vm.movieData}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down
13 changes: 9 additions & 4 deletions ToyProject/app/src/main/res/layout/item_main.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:bind="http://schemas.android.com/tools">

<data>

<variable
name="movieData"
type="com.example.toyproject.data.entity.SearchMovieData" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_height="150dp"
android:orientation="vertical"
android:layout_marginBottom="4dp">

<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
bind:bindImage="@{movieData.posterUrl}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down