-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: 학교 선택 화면 구현 * feat: 학교 조회 api 연결 * feat: 학교 선택 화면 이동 * refactor: 뷰모델 init 대신 액티비티에서 호출하도록 수정 * test: 학교 선택 화면 테스트 작성 * refactor: SchoolUiState 제거 * feat: 로딩, 에러 화면 처리 * refactor: 불필요한 프로퍼티 제거 * feat: 학교 선택 상단 제목 추가 * test: analyticsHelper relaxed true 설정 * refactor: 파라미터 순서 변경 * fix: 화면 회전 시 schoolSelected 값 유지되도록 수정
- Loading branch information
Showing
19 changed files
with
512 additions
and
12 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
17 changes: 17 additions & 0 deletions
17
android/festago/app/src/main/java/com/festago/festago/data/dto/SchoolResponse.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,17 @@ | ||
package com.festago.festago.data.dto | ||
|
||
import com.festago.festago.model.School | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class SchoolResponse( | ||
val id: Int, | ||
val domain: String, | ||
val name: String | ||
) { | ||
fun toDomain(): School = School( | ||
id = id.toLong(), | ||
domain = domain, | ||
name = name | ||
) | ||
} |
11 changes: 11 additions & 0 deletions
11
android/festago/app/src/main/java/com/festago/festago/data/dto/SchoolsResponse.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,11 @@ | ||
package com.festago.festago.data.dto | ||
|
||
import com.festago.festago.model.School | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class SchoolsResponse( | ||
val schools: List<SchoolResponse> | ||
) { | ||
fun toDomain(): List<School> = schools.map { it.toDomain() } | ||
} |
14 changes: 13 additions & 1 deletion
14
.../festago/app/src/main/java/com/festago/festago/data/repository/SchoolDefaultRepository.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
11 changes: 11 additions & 0 deletions
11
android/festago/app/src/main/java/com/festago/festago/data/service/SchoolRetrofitService.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,11 @@ | ||
package com.festago.festago.data.service | ||
|
||
import com.festago.festago.data.dto.SchoolsResponse | ||
import retrofit2.Response | ||
import retrofit2.http.GET | ||
|
||
interface SchoolRetrofitService { | ||
|
||
@GET("/schools") | ||
suspend fun getSchools(): Response<SchoolsResponse> | ||
} |
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
87 changes: 87 additions & 0 deletions
87
...pp/src/main/java/com/festago/festago/presentation/ui/selectschool/SelectSchoolActivity.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,87 @@ | ||
package com.festago.festago.presentation.ui.selectschool | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.widget.ArrayAdapter | ||
import androidx.activity.viewModels | ||
import androidx.appcompat.app.AppCompatActivity | ||
import com.festago.festago.R | ||
import com.festago.festago.databinding.ActivitySelectSchoolBinding | ||
import com.festago.festago.presentation.ui.studentverification.StudentVerificationActivity | ||
import com.festago.festago.presentation.util.repeatOnStarted | ||
import dagger.hilt.android.AndroidEntryPoint | ||
|
||
@AndroidEntryPoint | ||
class SelectSchoolActivity : AppCompatActivity() { | ||
|
||
private val binding: ActivitySelectSchoolBinding by lazy { | ||
ActivitySelectSchoolBinding.inflate(layoutInflater) | ||
} | ||
|
||
private val vm: SelectSchoolViewModel by viewModels() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
initBinding() | ||
initObserve() | ||
initView() | ||
} | ||
|
||
private fun initBinding() { | ||
setContentView(binding.root) | ||
binding.lifecycleOwner = this | ||
binding.vm = vm | ||
} | ||
|
||
private fun initObserve() { | ||
repeatOnStarted { | ||
vm.uiState.collect { uiState -> | ||
handleUiState(uiState) | ||
} | ||
} | ||
repeatOnStarted { | ||
vm.event.collect { event -> | ||
handleEvent(event) | ||
} | ||
} | ||
} | ||
|
||
private fun initView() { | ||
vm.loadSchools() | ||
} | ||
|
||
private fun handleUiState(uiState: SelectSchoolUiState) { | ||
binding.uiState = uiState | ||
when (uiState) { | ||
is SelectSchoolUiState.Loading, is SelectSchoolUiState.Error -> Unit | ||
is SelectSchoolUiState.Success -> handleSuccess(uiState) | ||
} | ||
} | ||
|
||
private fun handleSuccess(uiState: SelectSchoolUiState.Success) { | ||
val adapter = | ||
ArrayAdapter(this, R.layout.item_select_school, uiState.schools.map { it.name }) | ||
binding.actvSelectSchool.setAdapter(adapter) | ||
binding.actvSelectSchool.setOnItemClickListener { _, _, position, _ -> | ||
val selectedSchool = uiState.schools.firstOrNull { | ||
it.name == adapter.getItem(position) | ||
} | ||
selectedSchool?.let { vm.selectSchool(it.id) } | ||
} | ||
} | ||
|
||
private fun handleEvent(event: SelectSchoolEvent) { | ||
when (event) { | ||
is SelectSchoolEvent.ShowStudentVerification -> { | ||
startActivity(StudentVerificationActivity.getIntent(this, event.schoolId)) | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
fun getIntent(context: Context): Intent { | ||
return Intent(context, SelectSchoolActivity::class.java) | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...o/app/src/main/java/com/festago/festago/presentation/ui/selectschool/SelectSchoolEvent.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,5 @@ | ||
package com.festago.festago.presentation.ui.selectschool | ||
|
||
interface SelectSchoolEvent { | ||
class ShowStudentVerification(val schoolId: Long) : SelectSchoolEvent | ||
} |
21 changes: 21 additions & 0 deletions
21
...app/src/main/java/com/festago/festago/presentation/ui/selectschool/SelectSchoolUiState.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,21 @@ | ||
package com.festago.festago.presentation.ui.selectschool | ||
|
||
import com.festago.festago.model.School | ||
|
||
interface SelectSchoolUiState { | ||
object Loading : SelectSchoolUiState | ||
|
||
data class Success( | ||
val schools: List<School>, | ||
val selectedSchoolId: Long? = null | ||
) : SelectSchoolUiState { | ||
val schoolSelected = selectedSchoolId != null | ||
} | ||
|
||
object Error : SelectSchoolUiState | ||
|
||
val enableNext get() = (this is Success) && schoolSelected | ||
val shouldShowSuccess get() = this is Success | ||
val shouldShowLoading get() = this is Loading | ||
val shouldShowError get() = this is Error | ||
} |
71 changes: 71 additions & 0 deletions
71
...p/src/main/java/com/festago/festago/presentation/ui/selectschool/SelectSchoolViewModel.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,71 @@ | ||
package com.festago.festago.presentation.ui.selectschool | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.festago.festago.analytics.AnalyticsHelper | ||
import com.festago.festago.analytics.logNetworkFailure | ||
import com.festago.festago.repository.SchoolRepository | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.SharedFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class SelectSchoolViewModel @Inject constructor( | ||
private val schoolRepository: SchoolRepository, | ||
private val analyticsHelper: AnalyticsHelper | ||
) : ViewModel() { | ||
|
||
private val _uiState = MutableStateFlow<SelectSchoolUiState>(SelectSchoolUiState.Loading) | ||
val uiState: StateFlow<SelectSchoolUiState> = _uiState.asStateFlow() | ||
|
||
private val _event = MutableSharedFlow<SelectSchoolEvent>() | ||
val event: SharedFlow<SelectSchoolEvent> = _event.asSharedFlow() | ||
|
||
fun loadSchools() { | ||
viewModelScope.launch { | ||
schoolRepository.loadSchools() | ||
.onSuccess { schools -> | ||
if (uiState.value is SelectSchoolUiState.Success) { | ||
val successState = (uiState.value as SelectSchoolUiState.Success) | ||
_uiState.value = successState.copy(schools = schools) | ||
} else { | ||
_uiState.value = SelectSchoolUiState.Success(schools) | ||
} | ||
} | ||
.onFailure { | ||
_uiState.value = SelectSchoolUiState.Error | ||
analyticsHelper.logNetworkFailure(KEY_LOAD_SCHOOLS_LOG, it.message.toString()) | ||
} | ||
} | ||
} | ||
|
||
fun selectSchool(schoolId: Long) { | ||
if (uiState.value is SelectSchoolUiState.Success) { | ||
_uiState.value = | ||
(uiState.value as SelectSchoolUiState.Success).copy(selectedSchoolId = schoolId) | ||
} | ||
} | ||
|
||
fun showStudentVerification() { | ||
viewModelScope.launch { | ||
if (uiState.value is SelectSchoolUiState.Success) { | ||
val success = uiState.value as SelectSchoolUiState.Success | ||
success.selectedSchoolId?.let { schoolId -> | ||
_event.emit( | ||
SelectSchoolEvent.ShowStudentVerification(schoolId) | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
private const val KEY_LOAD_SCHOOLS_LOG = "load_schools" | ||
} | ||
} |
Oops, something went wrong.