This repository has been archived by the owner on Jan 7, 2025. It is now read-only.
-
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.
- Loading branch information
Showing
14 changed files
with
356 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
app/src/main/java/com/fictadvisor/android/data/dto/schedule/DetailedEventResponse.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,10 @@ | ||
package com.fictadvisor.android.data.dto.schedule | ||
|
||
data class DetailedEventResponse( | ||
val url: String, | ||
val eventInfo: String, | ||
val disciplineType: TDiscipline, | ||
val disciplineInfo: String, | ||
val period: TEventPeriod, | ||
val teachers: List<Teacher> | ||
) |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/fictadvisor/android/data/dto/schedule/DisciplineTypeDTO.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,7 @@ | ||
package com.fictadvisor.android.data.dto.schedule | ||
|
||
data class DisciplineTypeDTO( | ||
val id: String, | ||
val disciplineId: String, | ||
val name: TDiscipline | ||
) |
11 changes: 11 additions & 0 deletions
11
app/src/main/java/com/fictadvisor/android/data/dto/schedule/EventDTO.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.fictadvisor.android.data.dto.schedule | ||
|
||
data class EventDTO( | ||
val id: String, | ||
val name: String, | ||
val startTime: String, | ||
val endTime: String, | ||
val disciplineType: DisciplineTypeDTO? | ||
) | ||
|
||
|
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/fictadvisor/android/data/dto/schedule/GetEventResponse.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,7 @@ | ||
package com.fictadvisor.android.data.dto.schedule | ||
|
||
data class GetEventResponse ( | ||
val week: String, | ||
val events: List<EventDTO>, | ||
val startTime: String, | ||
) |
14 changes: 14 additions & 0 deletions
14
app/src/main/java/com/fictadvisor/android/data/dto/schedule/PatchEventDTO.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,14 @@ | ||
package com.fictadvisor.android.data.dto.schedule | ||
|
||
data class PatchEventDTO( | ||
val week: Int, | ||
val changeStartDate: Boolean, | ||
val changeEndDate: Boolean, | ||
val disciplineType: String? = null, | ||
val url: String, | ||
val eventInfo: String, | ||
val disciplineInfo: String, | ||
val period: TEventPeriod, | ||
val teachers: List<String>, | ||
val disciplineId: String | ||
) |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/fictadvisor/android/data/dto/schedule/PostEventDTO.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,12 @@ | ||
package com.fictadvisor.android.data.dto.schedule | ||
|
||
data class PostEventDTO ( | ||
val groupId: String, | ||
val teachers: List<String>, | ||
val disciplineId: String, | ||
val url: String, | ||
val eventInfo: String, | ||
val disciplineType: TDiscipline, | ||
val disciplineInfo: String, | ||
val period: TEventPeriod, | ||
) |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/com/fictadvisor/android/data/dto/schedule/TDiscipline.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,10 @@ | ||
package com.fictadvisor.android.data.dto.schedule | ||
|
||
enum class TDiscipline { | ||
LECTURE, | ||
PRACTICE, | ||
LABORATORY, | ||
CONSULTATION, | ||
WORKOUT, | ||
EXAM | ||
} |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/fictadvisor/android/data/dto/schedule/TEventPeriod.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,7 @@ | ||
package com.fictadvisor.android.data.dto.schedule | ||
|
||
enum class TEventPeriod { | ||
NO_PERIOD, | ||
EVERY_WEEK, | ||
EVERY_FORTNIGHT | ||
} |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/fictadvisor/android/data/dto/schedule/Teacher.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,8 @@ | ||
package com.fictadvisor.android.data.dto.schedule | ||
|
||
data class Teacher( | ||
val id: String, | ||
val firstName: String, | ||
val middleName: String, | ||
val lastName: String | ||
) |
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
61 changes: 61 additions & 0 deletions
61
app/src/main/java/com/fictadvisor/android/data/remote/api/ScheduleApi.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,61 @@ | ||
package com.fictadvisor.android.data.remote.api | ||
|
||
import com.fictadvisor.android.data.dto.schedule.DetailedEventResponse | ||
import com.fictadvisor.android.data.dto.schedule.GetEventResponse | ||
import com.fictadvisor.android.data.dto.schedule.PatchEventDTO | ||
import com.fictadvisor.android.data.dto.schedule.PostEventDTO | ||
import retrofit2.Response | ||
import retrofit2.http.Body | ||
import retrofit2.http.DELETE | ||
import retrofit2.http.GET | ||
import retrofit2.http.PATCH | ||
import retrofit2.http.POST | ||
import retrofit2.http.Path | ||
import retrofit2.http.Query | ||
|
||
interface ScheduleApi { | ||
@GET("/v2/schedule/groups/{groupId}/general") | ||
suspend fun getEvents( | ||
@Path("groupId") groupId: String, | ||
@Query("week") week: Int, | ||
@Query("addLecture") addLecture: Boolean = true, | ||
@Query("addLaboratory") addLaboratory: Boolean = true, | ||
@Query("addPractice") addPractice: Boolean = true | ||
): Response<GetEventResponse> | ||
|
||
@GET("/v2/schedule/groups/{groupId}/events") | ||
suspend fun getEventsAuthorized( | ||
@Path("groupId") groupId: String, | ||
@Query("week") week: Int, | ||
@Query("showOwnSelective") showOwnSelective: Boolean, | ||
@Query("addLecture") addLecture: Boolean = true, | ||
@Query("addLaboratory") addLaboratory: Boolean = true, | ||
@Query("addPractice") addPractice: Boolean = true, | ||
@Query("otherEvents") otherEvents: Boolean = true | ||
): Response<GetEventResponse> | ||
|
||
@GET("/v2/schedule/events/{eventId}") | ||
suspend fun getEventInfo( | ||
@Path("eventId") eventId: String, | ||
@Query("week") week: Any // You can define the type accordingly | ||
): Response<DetailedEventResponse> | ||
|
||
@DELETE("/v2/schedule/groups/{groupId}/events/{eventId}") | ||
suspend fun deleteEventById( | ||
@Path("groupId") groupId: String, | ||
@Path("eventId") eventId: String | ||
): Response<DetailedEventResponse> | ||
|
||
@POST("/v2/schedule/events") | ||
suspend fun addEvent( | ||
@Body body: PostEventDTO, | ||
@Query("groupId") groupId: String | ||
): Response<DetailedEventResponse> | ||
|
||
@PATCH("/v2/schedule/groups/{groupId}/events/{eventId}") | ||
suspend fun editEvent( | ||
@Body body: PatchEventDTO, | ||
@Path("groupId") groupId: String, | ||
@Path("eventId") eventId: String | ||
): Response<DetailedEventResponse> | ||
} |
37 changes: 37 additions & 0 deletions
37
app/src/main/java/com/fictadvisor/android/repository/ScheduleRepository.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,37 @@ | ||
package com.fictadvisor.android.repository | ||
|
||
import com.fictadvisor.android.data.dto.schedule.DetailedEventResponse | ||
import com.fictadvisor.android.data.dto.schedule.GetEventResponse | ||
import com.fictadvisor.android.data.dto.schedule.PatchEventDTO | ||
import com.fictadvisor.android.data.dto.schedule.PostEventDTO | ||
import com.fictadvisor.android.data.remote.RetrofitClient | ||
import retrofit2.Response | ||
|
||
class ScheduleRepository { | ||
private val scheduleService = RetrofitClient.scheduleApi | ||
|
||
suspend fun getEvents(groupId: String, week: Int): Response<GetEventResponse> { | ||
return scheduleService.getEvents(groupId, week) | ||
} | ||
|
||
suspend fun getEventsAuthorized(groupId: String, week: Int, showOwnSelective: Boolean): Response<GetEventResponse> { | ||
return scheduleService.getEventsAuthorized(groupId, week, showOwnSelective) | ||
} | ||
|
||
suspend fun getEventInfo(eventId: String, week: Any): Response<DetailedEventResponse> { | ||
return scheduleService.getEventInfo(eventId, week) | ||
} | ||
|
||
suspend fun deleteEventById(groupId: String, eventId: String): Response<DetailedEventResponse> { | ||
return scheduleService.deleteEventById(groupId, eventId) | ||
} | ||
|
||
suspend fun addEvent(body: PostEventDTO, groupId: String): Response<DetailedEventResponse> { | ||
return scheduleService.addEvent(body, groupId) | ||
} | ||
|
||
suspend fun editEvent(body: PatchEventDTO, groupId: String, eventId: String): Response<DetailedEventResponse> { | ||
return scheduleService.editEvent(body, groupId, eventId) | ||
} | ||
} | ||
|
150 changes: 150 additions & 0 deletions
150
app/src/main/java/com/fictadvisor/android/viewmodel/ScheduleViewModel.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,150 @@ | ||
package com.fictadvisor.android.viewmodel | ||
|
||
import android.util.Log | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import com.fictadvisor.android.data.dto.BaseResponse | ||
import com.fictadvisor.android.data.dto.ErrorResponse | ||
import com.fictadvisor.android.data.dto.schedule.DetailedEventResponse | ||
import com.fictadvisor.android.data.dto.schedule.GetEventResponse | ||
import com.fictadvisor.android.data.dto.schedule.PatchEventDTO | ||
import com.fictadvisor.android.data.dto.schedule.PostEventDTO | ||
import com.fictadvisor.android.repository.ScheduleRepository | ||
import com.google.gson.Gson | ||
import com.google.gson.reflect.TypeToken | ||
import kotlinx.coroutines.CoroutineExceptionHandler | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
|
||
class ScheduleViewModel(private val mainRepository: ScheduleRepository) : ViewModel() { | ||
var job: Job? = null | ||
val mainDispatcher = Dispatchers.Main | ||
|
||
private val getEventsResponseMutable = MutableLiveData<BaseResponse<GetEventResponse>>() | ||
val getEventsResponse: LiveData<BaseResponse<GetEventResponse>> = getEventsResponseMutable | ||
|
||
private val getEventsAuthorizedResponseMutable = MutableLiveData<BaseResponse<GetEventResponse>>() | ||
val getEventsAuthorizedResponse: LiveData<BaseResponse<GetEventResponse>> = getEventsAuthorizedResponseMutable | ||
|
||
private val getEventInfoResponseMutable = MutableLiveData<BaseResponse<DetailedEventResponse>>() | ||
val getEventInfoResponse: LiveData<BaseResponse<DetailedEventResponse>> = getEventInfoResponseMutable | ||
|
||
private val deleteEventByIdResponseMutable = MutableLiveData<BaseResponse<DetailedEventResponse>>() | ||
val deleteEventByIdResponse: LiveData<BaseResponse<DetailedEventResponse>> = deleteEventByIdResponseMutable | ||
|
||
private val addEventResponseMutable = MutableLiveData<BaseResponse<DetailedEventResponse>>() | ||
val addEventResponse: LiveData<BaseResponse<DetailedEventResponse>> = addEventResponseMutable | ||
|
||
private val editEventResponseMutable = MutableLiveData<BaseResponse<DetailedEventResponse>>() | ||
val editEventResponse: LiveData<BaseResponse<DetailedEventResponse>> = editEventResponseMutable | ||
|
||
val exceptionHandler = CoroutineExceptionHandler { _, throwable -> | ||
Log.e("ScheduleViewModel", "Exception handled: ${throwable.localizedMessage}") | ||
} | ||
|
||
fun getEvents(groupId: String, week: Int){ | ||
job = CoroutineScope(Dispatchers.IO + exceptionHandler).launch { | ||
val response = mainRepository.getEvents(groupId, week) | ||
withContext(mainDispatcher) { | ||
if (response.isSuccessful) { | ||
getEventsResponseMutable.postValue(BaseResponse.Success(response.body())) | ||
} else { | ||
val gson = Gson() | ||
val type = object : TypeToken<ErrorResponse>() {}.type | ||
val errorResponse: ErrorResponse? = gson.fromJson(response.errorBody()!!.charStream(), type) | ||
|
||
getEventsResponseMutable.postValue(BaseResponse.Error(errorResponse)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun getEventsAuthorized(groupId: String, week: Int, showOwnSelective: Boolean){ | ||
job = CoroutineScope(Dispatchers.IO + exceptionHandler).launch { | ||
val response = mainRepository.getEventsAuthorized(groupId, week, showOwnSelective) | ||
withContext(mainDispatcher) { | ||
if (response.isSuccessful) { | ||
getEventsAuthorizedResponseMutable.postValue(BaseResponse.Success(response.body())) | ||
} else { | ||
val gson = Gson() | ||
val type = object : TypeToken<ErrorResponse>() {}.type | ||
val errorResponse: ErrorResponse? = gson.fromJson(response.errorBody()!!.charStream(), type) | ||
|
||
getEventsAuthorizedResponseMutable.postValue(BaseResponse.Error(errorResponse)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun getEventInfo(eventId: String, week: Any){ | ||
job = CoroutineScope(Dispatchers.IO + exceptionHandler).launch { | ||
val response = mainRepository.getEventInfo(eventId, week) | ||
withContext(mainDispatcher) { | ||
if (response.isSuccessful) { | ||
getEventInfoResponseMutable.postValue(BaseResponse.Success(response.body())) | ||
} else { | ||
val gson = Gson() | ||
val type = object : TypeToken<ErrorResponse>() {}.type | ||
val errorResponse: ErrorResponse? = gson.fromJson(response.errorBody()!!.charStream(), type) | ||
|
||
getEventInfoResponseMutable.postValue(BaseResponse.Error(errorResponse)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun deleteEventById(groupId: String, eventId: String){ | ||
job = CoroutineScope(Dispatchers.IO + exceptionHandler).launch { | ||
val response = mainRepository.deleteEventById(groupId, eventId) | ||
withContext(mainDispatcher) { | ||
if (response.isSuccessful) { | ||
deleteEventByIdResponseMutable.postValue(BaseResponse.Success(response.body())) | ||
} else { | ||
val gson = Gson() | ||
val type = object : TypeToken<ErrorResponse>() {}.type | ||
val errorResponse: ErrorResponse? = gson.fromJson(response.errorBody()!!.charStream(), type) | ||
|
||
deleteEventByIdResponseMutable.postValue(BaseResponse.Error(errorResponse)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun addEvent(body: PostEventDTO, groupId: String){ | ||
job = CoroutineScope(Dispatchers.IO + exceptionHandler).launch { | ||
val response = mainRepository.addEvent(body, groupId) | ||
withContext(mainDispatcher) { | ||
if (response.isSuccessful) { | ||
addEventResponseMutable.postValue(BaseResponse.Success(response.body())) | ||
} else { | ||
val gson = Gson() | ||
val type = object : TypeToken<ErrorResponse>() {}.type | ||
val errorResponse: ErrorResponse? = gson.fromJson(response.errorBody()!!.charStream(), type) | ||
|
||
addEventResponseMutable.postValue(BaseResponse.Error(errorResponse)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun editEvent(body: PatchEventDTO, groupId: String, eventId: String){ | ||
job = CoroutineScope(Dispatchers.IO + exceptionHandler).launch { | ||
val response = mainRepository.editEvent(body, groupId, eventId) | ||
withContext(mainDispatcher) { | ||
if (response.isSuccessful) { | ||
editEventResponseMutable.postValue(BaseResponse.Success(response.body())) | ||
} else { | ||
val gson = Gson() | ||
val type = object : TypeToken<ErrorResponse>() {}.type | ||
val errorResponse: ErrorResponse? = gson.fromJson(response.errorBody()!!.charStream(), type) | ||
|
||
editEventResponseMutable.postValue(BaseResponse.Error(errorResponse)) | ||
} | ||
} | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
app/src/main/java/com/fictadvisor/android/viewmodel/ScheduleViewModelFactory.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,15 @@ | ||
package com.fictadvisor.android.viewmodel | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import com.fictadvisor.android.repository.ScheduleRepository | ||
|
||
class ScheduleViewModelFactory(private val scheduleRepository: ScheduleRepository) : ViewModelProvider.Factory { | ||
|
||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
if (modelClass.isAssignableFrom(ScheduleViewModel::class.java)) { | ||
return ScheduleViewModel(scheduleRepository) as T | ||
} | ||
throw IllegalArgumentException("Unknown ViewModel class") | ||
} | ||
} |