From 1855afc2b558ec40a4af8db1e40f5fe5a45d91c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sun, 14 Jun 2020 19:22:51 +0200 Subject: [PATCH] Merge rxSingle blocks in methods --- .../kotlin/io/github/wulkanowy/sdk/Sdk.kt | 200 +++++++++++------- 1 file changed, 121 insertions(+), 79 deletions(-) diff --git a/sdk/src/main/kotlin/io/github/wulkanowy/sdk/Sdk.kt b/sdk/src/main/kotlin/io/github/wulkanowy/sdk/Sdk.kt index d599ddeb..63975df9 100644 --- a/sdk/src/main/kotlin/io/github/wulkanowy/sdk/Sdk.kt +++ b/sdk/src/main/kotlin/io/github/wulkanowy/sdk/Sdk.kt @@ -194,7 +194,14 @@ class Sdk { } } - fun getStudentsHybrid(email: String, password: String, scrapperBaseUrl: String, firebaseToken: String, startSymbol: String = "Default", apiKey: String = ""): Single> { + fun getStudentsHybrid( + email: String, + password: String, + scrapperBaseUrl: String, + firebaseToken: String, + startSymbol: String = "Default", + apiKey: String = "" + ): Single> { return getStudentsFromScrapper(email, password, scrapperBaseUrl, startSymbol) .compose(ScrapperExceptionTransformer()) .map { students -> students.distinctBy { it.symbol } } @@ -223,17 +230,21 @@ class Sdk { } fun getSemesters(now: LocalDate = LocalDate.now()): Single> { - return when (mode) { - Mode.HYBRID, Mode.SCRAPPER -> rxSingle { scrapper.getSemesters().mapSemesters() }.compose(ScrapperExceptionTransformer()) - Mode.API -> rxSingle { mobile.getStudents().mapSemesters(studentId, now) } - } + return rxSingle { + when (mode) { + Mode.HYBRID, Mode.SCRAPPER -> scrapper.getSemesters().mapSemesters() + Mode.API -> mobile.getStudents().mapSemesters(studentId, now) + } + }.compose(ScrapperExceptionTransformer()) } fun getAttendance(startDate: LocalDate, endDate: LocalDate, semesterId: Int): Single> { - return when (mode) { - Mode.SCRAPPER -> rxSingle { scrapper.getAttendance(startDate, endDate).mapAttendance() }.compose(ScrapperExceptionTransformer()) - Mode.HYBRID, Mode.API -> rxSingle { mobile.getAttendance(startDate, endDate, semesterId).mapAttendance(mobile.getDictionaries()) } - } + return rxSingle { + when (mode) { + Mode.SCRAPPER -> scrapper.getAttendance(startDate, endDate).mapAttendance() + Mode.HYBRID, Mode.API -> mobile.getAttendance(startDate, endDate, semesterId).mapAttendance(mobile.getDictionaries()) + } + }.compose(ScrapperExceptionTransformer()) } fun getAttendanceSummary(subjectId: Int? = -1): Single> { @@ -251,38 +262,48 @@ class Sdk { } fun getSubjects(): Single> { - return when (mode) { - Mode.HYBRID, Mode.SCRAPPER -> rxSingle { scrapper.getSubjects().mapSubjects() }.compose(ScrapperExceptionTransformer()) - Mode.API -> rxSingle { mobile.getDictionaries().subjects.mapSubjects() } - } + return rxSingle { + when (mode) { + Mode.HYBRID, Mode.SCRAPPER -> scrapper.getSubjects().mapSubjects() + Mode.API -> mobile.getDictionaries().subjects.mapSubjects() + } + }.compose(ScrapperExceptionTransformer()) } fun getExams(start: LocalDate, end: LocalDate, semesterId: Int): Single> { - return when (mode) { - Mode.SCRAPPER -> rxSingle { scrapper.getExams(start, end).mapExams() }.compose(ScrapperExceptionTransformer()) - Mode.HYBRID, Mode.API -> rxSingle { mobile.getExams(start, end, semesterId).mapExams(mobile.getDictionaries()) } - } + return rxSingle { + when (mode) { + Mode.SCRAPPER -> scrapper.getExams(start, end).mapExams() + Mode.HYBRID, Mode.API -> mobile.getExams(start, end, semesterId).mapExams(mobile.getDictionaries()) + } + }.compose(ScrapperExceptionTransformer()) } fun getGrades(semesterId: Int): Single, List>> { - return when (mode) { - Mode.SCRAPPER -> rxSingle { scrapper.getGrades(semesterId).mapGrades() }.compose(ScrapperExceptionTransformer()) - Mode.HYBRID, Mode.API -> rxSingle { mobile.getGrades(semesterId).mapGrades(mobile.getDictionaries()) } - } + return rxSingle { + when (mode) { + Mode.SCRAPPER -> scrapper.getGrades(semesterId).mapGrades() + Mode.HYBRID, Mode.API -> mobile.getGrades(semesterId).mapGrades(mobile.getDictionaries()) + } + }.compose(ScrapperExceptionTransformer()) } fun getGradesDetails(semesterId: Int): Single> { - return when (mode) { - Mode.SCRAPPER -> rxSingle { scrapper.getGradesDetails(semesterId).mapGradesDetails() }.compose(ScrapperExceptionTransformer()) - Mode.HYBRID, Mode.API -> rxSingle { mobile.getGradesDetails(semesterId).mapGradesDetails(mobile.getDictionaries()) } - } + return rxSingle { + when (mode) { + Mode.SCRAPPER -> scrapper.getGradesDetails(semesterId).mapGradesDetails() + Mode.HYBRID, Mode.API -> mobile.getGradesDetails(semesterId).mapGradesDetails(mobile.getDictionaries()) + } + }.compose(ScrapperExceptionTransformer()) } fun getGradesSummary(semesterId: Int): Single> { - return when (mode) { - Mode.SCRAPPER -> rxSingle { scrapper.getGradesSummary(semesterId).mapGradesSummary() }.compose(ScrapperExceptionTransformer()) - Mode.HYBRID, Mode.API -> rxSingle { mobile.getGradesSummary(semesterId).mapGradesSummary(mobile.getDictionaries()) } - } + return rxSingle { + when (mode) { + Mode.SCRAPPER -> scrapper.getGradesSummary(semesterId).mapGradesSummary() + Mode.HYBRID, Mode.API -> mobile.getGradesSummary(semesterId).mapGradesSummary(mobile.getDictionaries()) + } + }.compose(ScrapperExceptionTransformer()) } fun getGradesAnnualStatistics(semesterId: Int): Single> { @@ -307,17 +328,21 @@ class Sdk { } fun getHomework(start: LocalDate, end: LocalDate, semesterId: Int = 0): Single> { - return when (mode) { - Mode.SCRAPPER -> rxSingle { scrapper.getHomework(start, end).mapHomework() }.compose(ScrapperExceptionTransformer()) - Mode.HYBRID, Mode.API -> rxSingle { mobile.getHomework(start, end, semesterId).mapHomework(mobile.getDictionaries()) } - } + return rxSingle { + when (mode) { + Mode.SCRAPPER -> scrapper.getHomework(start, end).mapHomework() + Mode.HYBRID, Mode.API -> mobile.getHomework(start, end, semesterId).mapHomework(mobile.getDictionaries()) + } + }.compose(ScrapperExceptionTransformer()) } fun getNotes(semesterId: Int): Single> { - return when (mode) { - Mode.SCRAPPER -> rxSingle { scrapper.getNotes().mapNotes() }.compose(ScrapperExceptionTransformer()) - Mode.HYBRID, Mode.API -> rxSingle { mobile.getNotes(semesterId).mapNotes(mobile.getDictionaries()) } - } + return rxSingle { + when (mode) { + Mode.SCRAPPER -> scrapper.getNotes().mapNotes() + Mode.HYBRID, Mode.API -> mobile.getNotes(semesterId).mapNotes(mobile.getDictionaries()) + } + }.compose(ScrapperExceptionTransformer()) } fun getRegisteredDevices(): Single> { @@ -342,10 +367,12 @@ class Sdk { } fun getTeachers(semesterId: Int): Single> { - return when (mode) { - Mode.SCRAPPER -> rxSingle { scrapper.getTeachers().mapTeachers() }.compose(ScrapperExceptionTransformer()) - Mode.HYBRID, Mode.API -> rxSingle { mobile.getTeachers(studentId, semesterId).mapTeachers(mobile.getDictionaries()) } - } + return rxSingle { + when (mode) { + Mode.SCRAPPER -> scrapper.getTeachers().mapTeachers() + Mode.HYBRID, Mode.API -> mobile.getTeachers(studentId, semesterId).mapTeachers(mobile.getDictionaries()) + } + }.compose(ScrapperExceptionTransformer()) } fun getSchool(): Single { @@ -356,24 +383,30 @@ class Sdk { } fun getStudentInfo(): Single { - return when (mode) { - Mode.HYBRID, Mode.SCRAPPER -> rxSingle { scrapper.getStudentInfo().mapStudent() }.compose(ScrapperExceptionTransformer()) - Mode.API -> throw FeatureNotAvailableException("Student info is not available in API mode") - } + return rxSingle { + when (mode) { + Mode.HYBRID, Mode.SCRAPPER -> scrapper.getStudentInfo().mapStudent() + Mode.API -> throw FeatureNotAvailableException("Student info is not available in API mode") + } + }.compose(ScrapperExceptionTransformer()) } fun getReportingUnits(): Single> { - return when (mode) { - Mode.HYBRID, Mode.SCRAPPER -> rxSingle { scrapper.getReportingUnits().mapReportingUnits() }.compose(ScrapperExceptionTransformer()) - Mode.API -> rxSingle { mobile.getStudents().mapReportingUnits(studentId) } - } + return rxSingle { + when (mode) { + Mode.HYBRID, Mode.SCRAPPER -> scrapper.getReportingUnits().mapReportingUnits() + Mode.API -> mobile.getStudents().mapReportingUnits(studentId) + } + }.compose(ScrapperExceptionTransformer()) } fun getRecipients(unitId: Int, role: Int = 2): Single> { - return when (mode) { - Mode.HYBRID, Mode.SCRAPPER -> rxSingle { scrapper.getRecipients(unitId, role).mapRecipients() }.compose(ScrapperExceptionTransformer()) - Mode.API -> rxSingle { mobile.getDictionaries().teachers.mapRecipients(unitId) } - } + return rxSingle { + when (mode) { + Mode.HYBRID, Mode.SCRAPPER -> scrapper.getRecipients(unitId, role).mapRecipients() + Mode.API -> mobile.getDictionaries().teachers.mapRecipients(unitId) + } + }.compose(ScrapperExceptionTransformer()) } fun getMessages(folder: Folder, start: LocalDateTime, end: LocalDateTime): Single> { @@ -385,24 +418,30 @@ class Sdk { } fun getReceivedMessages(start: LocalDateTime, end: LocalDateTime): Single> { - return when (mode) { - Mode.HYBRID, Mode.SCRAPPER -> rxSingle { scrapper.getReceivedMessages().mapMessages() }.compose(ScrapperExceptionTransformer()) // TODO - Mode.API -> rxSingle { mobile.getMessages(start, end).mapMessages(mobile.getDictionaries()) } - } + return rxSingle { + when (mode) { + Mode.HYBRID, Mode.SCRAPPER -> scrapper.getReceivedMessages().mapMessages() // TODO + Mode.API -> mobile.getMessages(start, end).mapMessages(mobile.getDictionaries()) + } + }.compose(ScrapperExceptionTransformer()) } fun getSentMessages(start: LocalDateTime, end: LocalDateTime): Single> { - return when (mode) { - Mode.HYBRID, Mode.SCRAPPER -> rxSingle { scrapper.getSentMessages().mapMessages() }.compose(ScrapperExceptionTransformer()) - Mode.API -> rxSingle { mobile.getMessagesSent(start, end).mapMessages(mobile.getDictionaries()) } - } + return rxSingle { + when (mode) { + Mode.HYBRID, Mode.SCRAPPER -> scrapper.getSentMessages().mapMessages() + Mode.API -> mobile.getMessagesSent(start, end).mapMessages(mobile.getDictionaries()) + } + }.compose(ScrapperExceptionTransformer()) } fun getDeletedMessages(start: LocalDateTime, end: LocalDateTime): Single> { - return when (mode) { - Mode.HYBRID, Mode.SCRAPPER -> rxSingle { scrapper.getDeletedMessages().mapMessages() }.compose(ScrapperExceptionTransformer()) - Mode.API -> rxSingle { mobile.getMessagesDeleted(start, end).mapMessages(mobile.getDictionaries()) } - } + return rxSingle { + when (mode) { + Mode.HYBRID, Mode.SCRAPPER -> scrapper.getDeletedMessages().mapMessages() + Mode.API -> mobile.getMessagesDeleted(start, end).mapMessages(mobile.getDictionaries()) + } + }.compose(ScrapperExceptionTransformer()) } fun getMessageRecipients(messageId: Int, senderId: Int): Single> { @@ -413,24 +452,25 @@ class Sdk { } fun getMessageDetails(messageId: Int, folderId: Int, read: Boolean = false, id: Int? = null): Single { - return when (mode) { - Mode.HYBRID, Mode.SCRAPPER -> rxSingle { scrapper.getMessageDetails(messageId, folderId, read, id).mapScrapperMessage() }.compose(ScrapperExceptionTransformer()) - Mode.API -> rxSingle { - mobile.changeMessageStatus(messageId, when (folderId) { + return rxSingle { + when (mode) { + Mode.HYBRID, Mode.SCRAPPER -> scrapper.getMessageDetails(messageId, folderId, read, id).mapScrapperMessage() + Mode.API -> mobile.changeMessageStatus(messageId, when (folderId) { 1 -> "Odebrane" 2 -> "Wysłane" else -> "Usunięte" }, "Widoczna").let { MessageDetails("", emptyList()) } } - } + }.compose(ScrapperExceptionTransformer()) } fun sendMessage(subject: String, content: String, recipients: List): Single { - return when (mode) { - Mode.HYBRID, Mode.SCRAPPER -> rxSingle { scrapper.sendMessage(subject, content, recipients.mapFromRecipientsToScraper()).mapSentMessage() } - .compose(ScrapperExceptionTransformer()) - Mode.API -> rxSingle { mobile.sendMessage(subject, content, recipients.mapFromRecipientsToMobile()).mapSentMessage(loginId) } - } + return rxSingle { + when (mode) { + Mode.HYBRID, Mode.SCRAPPER -> scrapper.sendMessage(subject, content, recipients.mapFromRecipientsToScraper()).mapSentMessage() + Mode.API -> mobile.sendMessage(subject, content, recipients.mapFromRecipientsToMobile()).mapSentMessage(loginId) + } + }.compose(ScrapperExceptionTransformer()) } fun deleteMessages(messages: List>): Single { @@ -449,10 +489,12 @@ class Sdk { } fun getTimetable(start: LocalDate, end: LocalDate): Single> { - return when (mode) { - Mode.SCRAPPER -> rxSingle { scrapper.getTimetable(start, end).mapTimetable() }.compose(ScrapperExceptionTransformer()) - Mode.HYBRID, Mode.API -> rxSingle { mobile.getTimetable(start, end, 0).mapTimetable(mobile.getDictionaries()) } - } + return rxSingle { + when (mode) { + Mode.SCRAPPER -> scrapper.getTimetable(start, end).mapTimetable() + Mode.HYBRID, Mode.API -> mobile.getTimetable(start, end, 0).mapTimetable(mobile.getDictionaries()) + } + }.compose(ScrapperExceptionTransformer()) } fun getCompletedLessons(start: LocalDate, end: LocalDate? = null, subjectId: Int = -1): Single> {