-
Notifications
You must be signed in to change notification settings - Fork 0
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
Add coroutine #10
base: develop/view
Are you sure you want to change the base?
Add coroutine #10
Changes from all commits
5fb2360
3d5c336
5442efb
90f1b91
805fd78
d6414db
3c4ea18
f04be46
758d7f2
e19b474
e299a92
d7efda5
afb77ea
ebe660c
9d196cb
da75466
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,33 @@ | ||
package org.sopt.sample | ||
|
||
import org.sopt.sample.adapter.rvAdapter | ||
import org.sopt.sample.databinding.FragmentHomeBinding | ||
import android.os.Bundle | ||
import android.util.Log | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.fragment.app.Fragment | ||
import com.google.android.material.snackbar.Snackbar | ||
import androidx.lifecycle.Observer | ||
import androidx.lifecycle.ViewModelProvider | ||
import org.sopt.sample.adapter.rvAdapter | ||
import org.sopt.sample.base.BindingFragment | ||
import org.sopt.sample.databinding.FragmentSearchBinding | ||
import org.sopt.sample.remote.* | ||
import retrofit2.Call | ||
import retrofit2.Callback | ||
import retrofit2.Response | ||
import org.sopt.sample.databinding.FragmentHomeBinding | ||
import org.sopt.sample.user.UserRepository | ||
import org.sopt.sample.user.UserViewModel | ||
import org.sopt.sample.user.UserViewModelFactory | ||
|
||
class HomeFragment: BindingFragment<FragmentHomeBinding>(R.layout.fragment_home) { | ||
private var _binding : FragmentHomeBinding? = null | ||
get() = requireNotNull(_binding) | ||
private val userService = UserServicePool.userService | ||
class HomeFragment : BindingFragment<FragmentHomeBinding>(R.layout.fragment_home) { | ||
private lateinit var viewModel: UserViewModel | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
viewModel = ViewModelProvider(this, UserViewModelFactory(UserRepository())) | ||
.get(UserViewModel::class.java) | ||
binding.viewModel = viewModel | ||
binding.lifecycleOwner = this | ||
|
||
val adapter = rvAdapter(requireContext()) | ||
binding.rvRepos.adapter = adapter | ||
userService.user().enqueue(object : Callback<ResponseUserDTO> { | ||
override fun onResponse( | ||
call: Call<ResponseUserDTO>, | ||
response: Response<ResponseUserDTO> | ||
) { | ||
Log.d("유저 정보", "${response.body()}") | ||
if (response.isSuccessful) { | ||
response.body()?.let { | ||
adapter.setRepoList(it.data) | ||
} | ||
} | ||
} | ||
|
||
override fun onFailure(call: Call<ResponseUserDTO>, t: Throwable) { | ||
Snackbar.make(binding.root, "서버통신 실패", Snackbar.LENGTH_LONG).show() | ||
} | ||
viewModel.fatchUser() | ||
|
||
viewModel.userInfo.observe(viewLifecycleOwner, Observer { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 위에 adapter 설정해주는 부분은 initLayout()으로, 이 부분은 addObservers함수로 로직 분리해주면 좋을 거같아용! |
||
adapter.setRepoList(it) | ||
}) | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
package org.sopt.sample.adapter | ||
|
||
import androidx.recyclerview.widget.RecyclerView | ||
import org.sopt.sample.data.Repo | ||
import org.sopt.sample.databinding.LayoutHeaderBinding | ||
import org.sopt.sample.databinding.ItemHeaderBinding | ||
|
||
class TitleViewHolder ( | ||
private val binding: LayoutHeaderBinding | ||
private val binding: ItemHeaderBinding | ||
): RecyclerView.ViewHolder(binding.root){ | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,9 @@ import android.content.Context | |
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.RecyclerView | ||
import org.sopt.sample.databinding.LayoutGithubRepoBinding | ||
import org.sopt.sample.databinding.LayoutHeaderBinding | ||
import org.sopt.sample.data.UserData | ||
import org.sopt.sample.databinding.ItemHeaderBinding | ||
import org.sopt.sample.databinding.ItemUserInfoBinding | ||
import org.sopt.sample.remote.ResponseUserDTO | ||
|
||
class rvAdapter(context : Context):RecyclerView.Adapter<RecyclerView.ViewHolder>() { | ||
|
@@ -15,11 +16,11 @@ class rvAdapter(context : Context):RecyclerView.Adapter<RecyclerView.ViewHolder> | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { | ||
return when(viewType){ | ||
REPO_TYPE ->{ | ||
val binding = LayoutGithubRepoBinding.inflate(inflater, parent,false) | ||
val binding = ItemUserInfoBinding.inflate(inflater, parent,false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네이밍 좋습니다 :) |
||
RepoViewHolder(binding) | ||
} | ||
HEADER_TYPE ->{ | ||
val binding = LayoutHeaderBinding.inflate(inflater, parent,false) | ||
val binding = ItemHeaderBinding.inflate(inflater, parent,false) | ||
TitleViewHolder(binding) | ||
} | ||
else -> throw RuntimeException("Error") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.sopt.sample.data.datasource | ||
|
||
import org.sopt.sample.remote.RequestLoginDTO | ||
import org.sopt.sample.remote.ResponseLoginDTO | ||
import org.sopt.sample.remote.ServicePool | ||
|
||
class LoginDataSource(private val service: ServicePool) { | ||
suspend fun login(requestLoginDTO: RequestLoginDTO): ResponseLoginDTO = | ||
service.loginService.login(requestLoginDTO) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저도 DataSource클래스 만들어서 다시 코드 정리해볼게요!! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.sopt.sample.data.repositoryImpl | ||
|
||
import org.sopt.sample.data.datasource.LoginDataSource | ||
import org.sopt.sample.domain.LoginRepository | ||
import org.sopt.sample.remote.RequestLoginDTO | ||
import timber.log.Timber | ||
|
||
class LoginRepositoryImpl( | ||
private val loginDataSource: LoginDataSource | ||
): LoginRepository { | ||
override suspend fun login(email: String, password: String) : Boolean = | ||
kotlin.runCatching { | ||
loginDataSource.login(RequestLoginDTO(email, password)) | ||
}.fold({ | ||
Timber.d("로그인 서버통신 성공") | ||
true | ||
}, { | ||
Timber.d("로그인 서버통신 실패") | ||
false | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.sopt.sample.domain | ||
|
||
interface LoginRepository { | ||
suspend fun login(email: String, password: String) : Boolean | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
패키지 나눈거 좋아용!!