diff --git a/app/src/main/java/com/dnd/sixth/lmsservice/data/network/service/SubjectService.kt b/app/src/main/java/com/dnd/sixth/lmsservice/data/network/service/SubjectService.kt index 16867a3..eb7a866 100644 --- a/app/src/main/java/com/dnd/sixth/lmsservice/data/network/service/SubjectService.kt +++ b/app/src/main/java/com/dnd/sixth/lmsservice/data/network/service/SubjectService.kt @@ -3,10 +3,7 @@ package com.dnd.sixth.lmsservice.data.network.service import com.dnd.sixth.lmsservice.data.model.generalclass.GeneralSubjectModel import com.dnd.sixth.lmsservice.data.model.subject.SubjectModel import retrofit2.Call -import retrofit2.http.Body -import retrofit2.http.GET -import retrofit2.http.POST -import retrofit2.http.Query +import retrofit2.http.* interface SubjectService { @POST("subject") @@ -14,6 +11,11 @@ interface SubjectService { @Body subjectModel: SubjectModel ): Call + @DELETE("subject") + fun deleteSubject( + @Query("id") subjectId: Int + ): Call + @GET("subject") fun getGeneralClassList( @Query("id") uid: Int diff --git a/app/src/main/java/com/dnd/sixth/lmsservice/data/repository/subject/SubjectRepositoryImpl.kt b/app/src/main/java/com/dnd/sixth/lmsservice/data/repository/subject/SubjectRepositoryImpl.kt index 1cac336..436053d 100644 --- a/app/src/main/java/com/dnd/sixth/lmsservice/data/repository/subject/SubjectRepositoryImpl.kt +++ b/app/src/main/java/com/dnd/sixth/lmsservice/data/repository/subject/SubjectRepositoryImpl.kt @@ -21,4 +21,8 @@ class SubjectRepositoryImpl( it.toEntity() } } + + override suspend fun deleteSubject(subjectId: Int): SubjectEntity? { + return subjectRemoteDataSource.deleteSubject(remoteErrorEmitter, subjectId)?.toEntity() + } } \ No newline at end of file diff --git a/app/src/main/java/com/dnd/sixth/lmsservice/data/repository/subject/datasource/remote/SubjectRemoteDataSource.kt b/app/src/main/java/com/dnd/sixth/lmsservice/data/repository/subject/datasource/remote/SubjectRemoteDataSource.kt index 5621e5c..1ba07ad 100644 --- a/app/src/main/java/com/dnd/sixth/lmsservice/data/repository/subject/datasource/remote/SubjectRemoteDataSource.kt +++ b/app/src/main/java/com/dnd/sixth/lmsservice/data/repository/subject/datasource/remote/SubjectRemoteDataSource.kt @@ -9,4 +9,5 @@ import com.dnd.sixth.lmsservice.domain.entity.SubjectEntity interface SubjectRemoteDataSource { suspend fun createSubject(remoteErrorEmitter: RemoteErrorEmitter, subjectEntity: SubjectEntity): SubjectModel? suspend fun getGeneralClassList(remoteErrorEmitter: RemoteErrorEmitter, uid: Int): List? + suspend fun deleteSubject(remoteErrorEmitter: RemoteErrorEmitter, subjectId: Int): SubjectModel? } \ No newline at end of file diff --git a/app/src/main/java/com/dnd/sixth/lmsservice/data/repository/subject/datasource/remote/SubjectRemoteDataSourceImpl.kt b/app/src/main/java/com/dnd/sixth/lmsservice/data/repository/subject/datasource/remote/SubjectRemoteDataSourceImpl.kt index fa22e12..cd1fc39 100644 --- a/app/src/main/java/com/dnd/sixth/lmsservice/data/repository/subject/datasource/remote/SubjectRemoteDataSourceImpl.kt +++ b/app/src/main/java/com/dnd/sixth/lmsservice/data/repository/subject/datasource/remote/SubjectRemoteDataSourceImpl.kt @@ -40,14 +40,14 @@ class SubjectRemoteDataSourceImpl(private val subjectApi: SubjectApi) : SubjectR // Http 통신 결과 (200 코드대인 경우) if (response.isSuccessful) { // 서버 DB로부터 받은 DTO객체 - val responseObject = response.body() + val subjectModel = response.body() as SubjectModel if (BuildConfig.DEBUG) { Log.d(TAG, response.code().toString()) - Log.d(TAG, responseObject.toString()) + Log.d(TAG, subjectModel.toString()) } // 코루틴 재게 - cont.resumeWith(Result.success(responseObject)) + cont.resumeWith(Result.success(subjectModel)) } else { // 서버로부터 에러 반환 Log.d(TAG, response.errorBody().toString()) } @@ -79,15 +79,15 @@ class SubjectRemoteDataSourceImpl(private val subjectApi: SubjectApi) : SubjectR // Http 통신 결과 (200 코드대인 경우) if (response.isSuccessful) { // 서버 DB로부터 받은 DTO객체 - val responseObject = response.body() as List + val generalSubjectList = response.body() as List if (BuildConfig.DEBUG) { Log.d(TAG, response.code().toString()) - Log.d(TAG, responseObject.toString()) + Log.d(TAG, generalSubjectList.toString()) } - // 코루틴 재게 - cont.resumeWith(Result.success(responseObject)) + // 코루틴 재게 (성공 데이터 반환) + cont.resumeWith(Result.success(generalSubjectList)) } else { // 서버로부터 에러 반환 Timber.d(response.errorBody().toString()) } @@ -101,4 +101,35 @@ class SubjectRemoteDataSourceImpl(private val subjectApi: SubjectApi) : SubjectR } }) } + + override suspend fun deleteSubject( + remoteErrorEmitter: RemoteErrorEmitter, + subjectId: Int + ) : SubjectModel? = suspendCancellableCoroutine { cont -> + subjectApi.api.deleteSubject(subjectId).enqueue(object : Callback { + @SuppressLint("LongLogTag") + override fun onResponse(call: Call, response: Response) { + if (response.isSuccessful) { + // 삭제한 수업 Subject 모델을 받아온다. + val deletedSubjectModel = response.body() as SubjectModel + + if (BuildConfig.DEBUG) { + Log.d(TAG, response.code().toString()) + Log.d(TAG, deletedSubjectModel.toString()) + } + + // 삭제한 수업 Subject Model 반환 + cont.resumeWith(Result.success(deletedSubjectModel)) + } else { // 서버로부터 에러 반환 + Timber.d(response.errorBody().toString()) + } + } + + @SuppressLint("LongLogTag") + override fun onFailure(call: Call, cause: Throwable) { + Log.e(TAG, cause.message.toString()) + cont.resumeWithException(cause) + } + }) + } } diff --git a/app/src/main/java/com/dnd/sixth/lmsservice/di/modules/UseCaseModules.kt b/app/src/main/java/com/dnd/sixth/lmsservice/di/modules/UseCaseModules.kt index 84efb72..5d2c4e3 100644 --- a/app/src/main/java/com/dnd/sixth/lmsservice/di/modules/UseCaseModules.kt +++ b/app/src/main/java/com/dnd/sixth/lmsservice/di/modules/UseCaseModules.kt @@ -1,6 +1,7 @@ package com.dnd.sixth.lmsservice.di.modules import com.dnd.sixth.lmsservice.domain.useCase.CreateSubjectUseCase +import com.dnd.sixth.lmsservice.domain.useCase.DeleteSubjectUseCase import com.dnd.sixth.lmsservice.domain.useCase.GetGeneralSubjectListUseCase import org.koin.dsl.module @@ -12,4 +13,7 @@ val useCaseModules = module { factory { GetGeneralSubjectListUseCase(get()) } + factory { + DeleteSubjectUseCase(get()) + } } \ No newline at end of file diff --git a/app/src/main/java/com/dnd/sixth/lmsservice/di/modules/ViewModelModules.kt b/app/src/main/java/com/dnd/sixth/lmsservice/di/modules/ViewModelModules.kt index a6caf74..ec87c51 100644 --- a/app/src/main/java/com/dnd/sixth/lmsservice/di/modules/ViewModelModules.kt +++ b/app/src/main/java/com/dnd/sixth/lmsservice/di/modules/ViewModelModules.kt @@ -63,7 +63,7 @@ val viewModelModules = module { MainViewModel() } viewModel { - SubjectViewModel(get(), get()) + SubjectViewModel(get(), get(), get()) } viewModel { EditResponseViewModel() diff --git a/app/src/main/java/com/dnd/sixth/lmsservice/domain/repository/SubjectRepository.kt b/app/src/main/java/com/dnd/sixth/lmsservice/domain/repository/SubjectRepository.kt index 65cb930..0758e16 100644 --- a/app/src/main/java/com/dnd/sixth/lmsservice/domain/repository/SubjectRepository.kt +++ b/app/src/main/java/com/dnd/sixth/lmsservice/domain/repository/SubjectRepository.kt @@ -15,4 +15,9 @@ interface SubjectRepository { * @return : List * */ suspend fun getGeneralClassList(uid: Int): List? + + /* + * 수업 지우기 + * */ + suspend fun deleteSubject(subjectId: Int): SubjectEntity? } \ No newline at end of file diff --git a/app/src/main/java/com/dnd/sixth/lmsservice/domain/useCase/DeleteSubjectUseCase.kt b/app/src/main/java/com/dnd/sixth/lmsservice/domain/useCase/DeleteSubjectUseCase.kt new file mode 100644 index 0000000..120b16a --- /dev/null +++ b/app/src/main/java/com/dnd/sixth/lmsservice/domain/useCase/DeleteSubjectUseCase.kt @@ -0,0 +1,10 @@ +package com.dnd.sixth.lmsservice.domain.useCase + +import com.dnd.sixth.lmsservice.domain.entity.SubjectEntity +import com.dnd.sixth.lmsservice.domain.repository.SubjectRepository + +class DeleteSubjectUseCase(private val subjectRepository: SubjectRepository) { + suspend operator fun invoke(subjectId: Int): SubjectEntity? { + return subjectRepository.deleteSubject(subjectId) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/dnd/sixth/lmsservice/presentation/base/BaseFragment.kt b/app/src/main/java/com/dnd/sixth/lmsservice/presentation/base/BaseFragment.kt index 4d75b19..d45f14c 100644 --- a/app/src/main/java/com/dnd/sixth/lmsservice/presentation/base/BaseFragment.kt +++ b/app/src/main/java/com/dnd/sixth/lmsservice/presentation/base/BaseFragment.kt @@ -9,6 +9,7 @@ import android.widget.Toast import androidx.databinding.DataBindingUtil import androidx.databinding.ViewDataBinding import androidx.fragment.app.Fragment +import com.google.android.material.snackbar.Snackbar abstract class BaseFragment : Fragment() { @@ -55,4 +56,9 @@ abstract class BaseFragment : Fragment() fun showToast(msg: String) { Toast.makeText(requireContext(), msg, Toast.LENGTH_SHORT).show() } + + // 스낵바 띄우기 + fun showSnackBar(msg: String) { + Snackbar.make(binding.root, msg, Snackbar.LENGTH_LONG).show() + } } \ No newline at end of file diff --git a/app/src/main/java/com/dnd/sixth/lmsservice/presentation/main/classmanage/subject/SubjectFragment.kt b/app/src/main/java/com/dnd/sixth/lmsservice/presentation/main/classmanage/subject/SubjectFragment.kt index f01101f..555731b 100644 --- a/app/src/main/java/com/dnd/sixth/lmsservice/presentation/main/classmanage/subject/SubjectFragment.kt +++ b/app/src/main/java/com/dnd/sixth/lmsservice/presentation/main/classmanage/subject/SubjectFragment.kt @@ -22,6 +22,10 @@ import com.dnd.sixth.lmsservice.presentation.main.classmanage.subject.edit.Subje import com.dnd.sixth.lmsservice.presentation.utility.UnitConverter import com.google.android.material.bottomsheet.BottomSheetDialog import kotlinx.android.synthetic.main.layout_edit_delete_bottom_sheet.view.* +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import org.koin.androidx.viewmodel.ext.android.viewModel import timber.log.Timber @@ -67,7 +71,7 @@ class SubjectFragment : BaseFragment(), } // 서버로부터 수업 리스틀를 가져와 업데이트한다. - viewModel?.updateGeneralSubjectList() + //viewModel?.updateGeneralSubjectList() // 수업 리스트가 변경됨에 따라 화면 크기 조절을 하기 위한 Observer viewModel?.generalSubjectDataList?.observe(this@SubjectFragment) { @@ -201,7 +205,7 @@ class SubjectFragment : BaseFragment(), // 삭제 버튼 클릭시, 하단 Dialog 종료와 함께, 삭제 여부를 묻는 Dialog를 보여준다. deleteBtn.setOnClickListener { dialog.dismiss() // 하단 Dialog 종료 - showDeleteClassDialog() // 삭제 여부를 묻는 Dialog show + showDeleteClassDialog(position) // 삭제 여부를 묻는 Dialog show } // 수정 버튼 클릭시, 수업 정보를 Edit할 수 있는 Activity로 이동 editBtn.setOnClickListener { @@ -229,13 +233,27 @@ class SubjectFragment : BaseFragment(), // 클래스 삭제 여부를 묻는 다이얼로그를 보여준다. - private fun showDeleteClassDialog() { + private fun showDeleteClassDialog(position: Int) { // 삭제를 묻는 Dialog Builder 생성 val builder = AlertDialog.Builder(requireContext()).setMessage("클래스를 삭제하시겠어요?") .setPositiveButton( "삭제" ) { _, _ -> // 수업 삭제 로직 수행 + CoroutineScope(Dispatchers.IO).launch { + val isSuccess = viewModel.deleteSubject(position) // 수업 삭제 + launch(Dispatchers.Main) { + if(isSuccess) { + showSnackBar(getString(R.string.success_delete_subject)) + + // viewModel의 List에서 해당 수업 삭제 로직 구현 예정 + // + // + } else { + showSnackBar(getString(R.string.failed_delete_subject)) + } + } + } }.setNegativeButton("취소") { dialog, _ -> dialog.dismiss() diff --git a/app/src/main/java/com/dnd/sixth/lmsservice/presentation/main/classmanage/subject/SubjectViewModel.kt b/app/src/main/java/com/dnd/sixth/lmsservice/presentation/main/classmanage/subject/SubjectViewModel.kt index 3361f9f..a495ba1 100644 --- a/app/src/main/java/com/dnd/sixth/lmsservice/presentation/main/classmanage/subject/SubjectViewModel.kt +++ b/app/src/main/java/com/dnd/sixth/lmsservice/presentation/main/classmanage/subject/SubjectViewModel.kt @@ -5,13 +5,16 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope import com.dnd.sixth.lmsservice.data.preference.PreferenceManager import com.dnd.sixth.lmsservice.domain.entity.GeneralSubjectEntity +import com.dnd.sixth.lmsservice.domain.useCase.DeleteSubjectUseCase import com.dnd.sixth.lmsservice.domain.useCase.GetGeneralSubjectListUseCase import com.dnd.sixth.lmsservice.presentation.base.BaseViewModel import com.dnd.sixth.lmsservice.presentation.utility.SAVED_UID_KEY import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext class SubjectViewModel( private val getGeneralSubjectListUseCase: GetGeneralSubjectListUseCase, + private val deleteSubjectUseCase: DeleteSubjectUseCase, private val preferenceManager: PreferenceManager ) : BaseViewModel() { /* @@ -28,14 +31,26 @@ class SubjectViewModel( return _generalSubjectList.value?.get(position) ?: throw Exception("Exist no item") } - /* - * General SubjectList를 서버 DB로부터 갱신 + + /* General SubjectList를 서버 DB로부터 갱신 * */ fun updateGeneralSubjectList() { viewModelScope.launch { - val generalSubjectList = getGeneralSubjectListUseCase(preferenceManager.getString(SAVED_UID_KEY)?.toInt()!!) + val generalSubjectList = + getGeneralSubjectListUseCase(preferenceManager.getString(SAVED_UID_KEY)?.toInt()!!) _generalSubjectList.value = generalSubjectList } } + + /* 선택한 수업 삭제 + * */ + suspend fun deleteSubject(position: Int): Boolean = + withContext(viewModelScope.coroutineContext) { + val deleteSubjectId = _generalSubjectList.value?.get(position)?.classId?.toInt() + val deletedSubjectEntity = deleteSubjectId?.let { deleteSubjectUseCase(it) } + (deletedSubjectEntity == null) + } + + } \ No newline at end of file diff --git a/app/src/main/java/com/dnd/sixth/lmsservice/presentation/main/classmanage/subject/create/SubjectCreateActivity.kt b/app/src/main/java/com/dnd/sixth/lmsservice/presentation/main/classmanage/subject/create/SubjectCreateActivity.kt index 1786599..59f1325 100644 --- a/app/src/main/java/com/dnd/sixth/lmsservice/presentation/main/classmanage/subject/create/SubjectCreateActivity.kt +++ b/app/src/main/java/com/dnd/sixth/lmsservice/presentation/main/classmanage/subject/create/SubjectCreateActivity.kt @@ -79,7 +79,7 @@ class SubjectCreateActivity : BaseActivity아직 클래스가 없어요😯 \n
수업이 생성될 때까지 조금만 기다려주세요! + 클래스 만들기 - 알 수 없는 이유로 수업에 실패했어요! + + 해당 수업이 삭제되었어요. + + + 알 수 없는 이유로 수업에 실패했어요! + 알 수 없는 이유로 수업 삭제에 실패했어요! \ No newline at end of file