generated from GO-SOPT-ANDROID/android-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT/#8] get follower list 서버 통신 구현
- Loading branch information
Showing
11 changed files
with
217 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
app/src/main/java/org/android/go/sopt/presentation/main/follower/FollowerAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.android.go.sopt.presentation.main.follower | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.ListAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
import org.android.go.sopt.R | ||
import org.android.go.sopt.databinding.HeaderFollowerBinding | ||
import org.android.go.sopt.databinding.ItemFollowerBinding | ||
import org.android.go.sopt.domain.model.Follower | ||
import org.android.go.sopt.util.DiffCallback | ||
|
||
class FollowerAdapter : ListAdapter<Follower, RecyclerView.ViewHolder>(diffUtil) { | ||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { | ||
return when (viewType) { | ||
VIEW_TYPE_HEADER -> FollowerHeaderViewHolder( | ||
HeaderFollowerBinding.inflate( | ||
LayoutInflater.from(parent.context), | ||
parent, | ||
false, | ||
), | ||
) | ||
|
||
VIEW_TYPE_ITEM -> FollowerViewHolder( | ||
ItemFollowerBinding.inflate( | ||
LayoutInflater.from(parent.context), | ||
parent, | ||
false, | ||
), | ||
) | ||
|
||
else -> throw ClassCastException( | ||
parent.context.getString( | ||
R.string.view_type_error_msg, | ||
viewType, | ||
), | ||
) | ||
} | ||
} | ||
|
||
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { | ||
if (holder is FollowerViewHolder) holder.setFollower(getItem(position - HEADER_COUNT)) | ||
} | ||
|
||
override fun getItemCount(): Int { | ||
return super.getItemCount() + HEADER_COUNT | ||
} | ||
|
||
override fun getItemViewType(position: Int): Int { | ||
return when (position) { | ||
0 -> VIEW_TYPE_HEADER | ||
else -> VIEW_TYPE_ITEM | ||
} | ||
} | ||
|
||
class FollowerHeaderViewHolder(private val binding: HeaderFollowerBinding) : | ||
RecyclerView.ViewHolder(binding.root) | ||
|
||
class FollowerViewHolder(private val binding: ItemFollowerBinding) : | ||
RecyclerView.ViewHolder(binding.root) { | ||
fun setFollower(follower: Follower) { | ||
binding.data = follower | ||
} | ||
} | ||
|
||
companion object { | ||
private val diffUtil = DiffCallback<Follower>( | ||
onItemsTheSame = { old, new -> old.id == new.id }, | ||
onContentsTheSame = { old, new -> old == new }, | ||
) | ||
|
||
private const val HEADER_COUNT = 1 | ||
|
||
const val VIEW_TYPE_HEADER = 0 | ||
const val VIEW_TYPE_ITEM = 1 | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
app/src/main/java/org/android/go/sopt/presentation/main/follower/FollowerFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
app/src/main/java/org/android/go/sopt/presentation/main/follower/FollowerViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.android.go.sopt.presentation.main.follower | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.launch | ||
import org.android.go.sopt.domain.model.Follower | ||
import org.android.go.sopt.domain.repository.FollowerRepository | ||
import org.android.go.sopt.util.state.RemoteUiState | ||
import org.android.go.sopt.util.state.RemoteUiState.Error | ||
import org.android.go.sopt.util.state.RemoteUiState.Success | ||
import retrofit2.HttpException | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class FollowerViewModel @Inject constructor( | ||
private val followerRepository: FollowerRepository, | ||
) : ViewModel() { | ||
private val _followerList = MutableLiveData<List<Follower>>() | ||
val followerList: List<Follower>? | ||
get() = _followerList.value | ||
|
||
private val _getFollowerListState = MutableLiveData<RemoteUiState>() | ||
val getFollowerListState: LiveData<RemoteUiState> | ||
get() = _getFollowerListState | ||
|
||
init { | ||
getFollowerList() | ||
} | ||
|
||
private fun getFollowerList() { | ||
Timber.d("get follower list 시작") | ||
viewModelScope.launch { | ||
followerRepository.getFollowerList(1) | ||
.onSuccess { response -> | ||
// TODO: followerList null 처리 | ||
_followerList.value = response | ||
_getFollowerListState.value = Success | ||
Timber.d("GET FOLLOWER LIST SUCCESS : $response") | ||
} | ||
.onFailure { t -> | ||
if (t is HttpException) { | ||
// TODO : failure 예외 처리 | ||
_getFollowerListState.value = Error | ||
Timber.e("GET FOLLOWER LIST FAIL : ${t.message}") | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters