Skip to content

Commit

Permalink
Merge pull request #55 from victorvicari/add_coroutines_to_repo_layer
Browse files Browse the repository at this point in the history
#52 code cleanUp
  • Loading branch information
wangerekaharun authored Oct 15, 2019
2 parents ad33d2e + fa81494 commit adeabbc
Show file tree
Hide file tree
Showing 15 changed files with 238 additions and 221 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.android254.droidconke19.datastates

sealed class Result<out T : Any>{
data class Success<out T : Any>(val data: T) : Result<T>()
data class Error(val exception: String?) : Result<Nothing>()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.android254.droidconke19.datastates

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

suspend fun <T, R : Any> T.runCatching(
block: suspend T.() -> R
): Result<R> = withContext(Dispatchers.IO) {
return@withContext try {
Result.Success(block())
} catch (e: Exception) {
Result.Error(e.message)
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
package com.android254.droidconke19.repository

import com.android254.droidconke19.datastates.FirebaseResult
import com.android254.droidconke19.datastates.Result
import com.android254.droidconke19.datastates.runCatching
import com.android254.droidconke19.models.AboutDetailsModel
import com.android254.droidconke19.utils.await
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.Query
import com.google.firebase.firestore.ktx.toObjects

interface AboutDetailsRepo {
suspend fun getAboutDetails(aboutType: String): FirebaseResult<List<AboutDetailsModel>>
suspend fun getAboutDetails(aboutType: String): Result<List<AboutDetailsModel>>
}


class AboutDetailsRepoImpl(val firestore: FirebaseFirestore) : AboutDetailsRepo {

override suspend fun getAboutDetails(aboutType: String): FirebaseResult<List<AboutDetailsModel>> {
return try {
val snapshot = firestore.collection(aboutType)
.orderBy("id", Query.Direction.ASCENDING)
.get()
.await()
val aboutDetailsModelList = snapshot.toObjects<AboutDetailsModel>()
FirebaseResult.Success(aboutDetailsModelList)
} catch (e: Exception) {
FirebaseResult.Error( e.message)
}
}
override suspend fun getAboutDetails(aboutType: String): Result<List<AboutDetailsModel>> =
runCatching {
val snapshot = firestore.collection(aboutType)
.orderBy("id", Query.Direction.ASCENDING)
.get()
.await()
snapshot.toObjects<AboutDetailsModel>()
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package com.android254.droidconke19.repository

import com.android254.droidconke19.datastates.FirebaseResult
import com.android254.droidconke19.datastates.Result
import com.android254.droidconke19.datastates.runCatching
import com.android254.droidconke19.models.AgendaModel
import com.android254.droidconke19.utils.await
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.FirebaseFirestoreException
import com.google.firebase.firestore.Query
import com.google.firebase.firestore.ktx.toObjects

interface AgendaRepo {
suspend fun agendaData(): FirebaseResult<List<AgendaModel>>
suspend fun agendaData(): Result<List<AgendaModel>>
}

class AgendaRepoImpl(val firestore: FirebaseFirestore) : AgendaRepo {

override suspend fun agendaData(): FirebaseResult<List<AgendaModel>> {
return try {
val snapshot = firestore.collection("agenda_2019").orderBy("id", Query.Direction.ASCENDING).get().await()
return FirebaseResult.Success(snapshot.toObjects())
override suspend fun agendaData(): Result<List<AgendaModel>> =
runCatching {
val snapshot = firestore
.collection("agenda_2019")
.orderBy("id", Query.Direction.ASCENDING)
.get()
.await()
snapshot.toObjects<AgendaModel>()
}

} catch (e: FirebaseFirestoreException) {
FirebaseResult.Error(e.message)
}

}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
package com.android254.droidconke19.repository

import com.android254.droidconke19.datastates.FirebaseResult
import com.android254.droidconke19.datastates.Result
import com.android254.droidconke19.datastates.runCatching
import com.android254.droidconke19.models.Announcement
import com.android254.droidconke19.utils.await
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.FirebaseFirestoreException
import com.google.firebase.firestore.ktx.toObjects

interface AnnouncementRepo {
suspend fun getAnnouncements(): FirebaseResult<List<Announcement>>
suspend fun getAnnouncements(): Result<List<Announcement>>
}

class AnnouncementRepoImpl(val firestore: FirebaseFirestore) : AnnouncementRepo {

override suspend fun getAnnouncements(): FirebaseResult<List<Announcement>> {
return try {
val snapshot = firestore.collection("announcements").get().await()
return FirebaseResult.Success(snapshot.toObjects())
override suspend fun getAnnouncements(): Result<List<Announcement>> =
runCatching {
val snapshot = firestore.collection("announcements").get().await()
snapshot.toObjects<Announcement>()
}

} catch (e: FirebaseFirestoreException) {
FirebaseResult.Error(e.message)
}

}
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
package com.android254.droidconke19.repository

import com.android254.droidconke19.datastates.FirebaseResult
import com.android254.droidconke19.datastates.Result
import com.android254.droidconke19.datastates.runCatching
import com.android254.droidconke19.models.UserEventFeedback
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.FirebaseFirestoreException
import kotlinx.coroutines.tasks.await

interface EventFeedbackRepo {
suspend fun sendFeedBack(userEventFeedback: UserEventFeedback): FirebaseResult<String>
suspend fun sendFeedBack(userEventFeedback: UserEventFeedback): Result<String>
}

class EventFeedbackRepoImpl(val firestore: FirebaseFirestore) : EventFeedbackRepo {

override suspend fun sendFeedBack(userEventFeedback: UserEventFeedback): FirebaseResult<String> {
return try {
firestore.collection("event_feedback_2019").add(userEventFeedback).await()
FirebaseResult.Success("Thank you for your feedback")
override suspend fun sendFeedBack(userEventFeedback: UserEventFeedback): Result<String> =
runCatching {
firestore.collection("event_feedback_2019").add(userEventFeedback).await()
"Thank you for your feedback"
}

} catch (e: FirebaseFirestoreException) {
FirebaseResult.Error(e.message)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
package com.android254.droidconke19.repository

import com.android254.droidconke19.datastates.FirebaseResult
import com.android254.droidconke19.datastates.Result
import com.android254.droidconke19.datastates.runCatching
import com.android254.droidconke19.models.EventTypeModel
import com.android254.droidconke19.utils.await
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.FirebaseFirestoreException
import com.google.firebase.firestore.Query
import com.google.firebase.firestore.ktx.toObjects

interface EventTypeRepo {
suspend fun getSessionData(): FirebaseResult<List<EventTypeModel>>
suspend fun getSessionData(): Result<List<EventTypeModel>>
}

class EventTypeRepoImpl(val firestore: FirebaseFirestore) : EventTypeRepo {

override suspend fun getSessionData(): FirebaseResult<List<EventTypeModel>> {
return try {
val snapshots = firestore.collection("event_types")
.orderBy("id", Query.Direction.ASCENDING)
.get()
.await()
FirebaseResult.Success(snapshots.toObjects())
} catch (e: FirebaseFirestoreException) {
FirebaseResult.Error(e.message)
}
}
override suspend fun getSessionData(): Result<List<EventTypeModel>> =
runCatching {
val snapshots = firestore.collection("event_types")
.orderBy("id", Query.Direction.ASCENDING)
.get()
.await()
snapshots.toObjects<EventTypeModel>()

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ import com.android254.droidconke19.models.ReserveSeatModel
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.FirebaseFirestoreException
import kotlinx.coroutines.tasks.await
import com.android254.droidconke19.datastates.Result
import com.android254.droidconke19.datastates.runCatching

interface ReserveSeatRepo {
suspend fun reserveSeat(reserveSeatModel: ReserveSeatModel): FirebaseResult<String>
suspend fun reserveSeat(reserveSeatModel: ReserveSeatModel): Result<String>

suspend fun unReserveSeat(reserveSeatModel: ReserveSeatModel): FirebaseResult<String>
suspend fun unReserveSeat(reserveSeatModel: ReserveSeatModel): Result<String>
}

class ReserveSeatRepoImpl(val firestore: FirebaseFirestore) : ReserveSeatRepo {


override suspend fun reserveSeat(reserveSeatModel: ReserveSeatModel): FirebaseResult<String> {
if (!isSeatReserved(reserveSeatModel)) {
return try {
override suspend fun reserveSeat(reserveSeatModel: ReserveSeatModel): Result<String> {
return if (!isSeatReserved(reserveSeatModel)) {
runCatching {
firestore.collection("reserved_seats").add(reserveSeatModel).await()
FirebaseResult.Success("Seat successfully reserved")

} catch (e: FirebaseFirestoreException) {
FirebaseResult.Error(e.message)
"Seat successfully reserved"
}
} else {
return Result.Error("Seat already reserved")
}
return FirebaseResult.Error("Seat already reserved")
}

private suspend fun isSeatReserved(reserveSeatModel: ReserveSeatModel): Boolean {
Expand All @@ -42,8 +42,9 @@ class ReserveSeatRepoImpl(val firestore: FirebaseFirestore) : ReserveSeatRepo {
}
}

override suspend fun unReserveSeat(reserveSeatModel: ReserveSeatModel): FirebaseResult<String> {
return try {

override suspend fun unReserveSeat(reserveSeatModel: ReserveSeatModel): Result<String> {
return runCatching {
val snapshot = firestore.collection("reserved_seats")
.whereEqualTo("day_number", reserveSeatModel.day_number)
.whereEqualTo("session_id", reserveSeatModel.session_id)
Expand All @@ -53,9 +54,7 @@ class ReserveSeatRepoImpl(val firestore: FirebaseFirestore) : ReserveSeatRepo {
if (!snapshot.isEmpty) {
snapshot.documents.first().reference.delete().await()
}
FirebaseResult.Success("Seat un-reserved")
} catch (e: FirebaseFirestoreException) {
FirebaseResult.Error(e.message)
"Seat un-reserved"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
package com.android254.droidconke19.repository

import com.android254.droidconke19.datastates.FirebaseResult
import com.android254.droidconke19.datastates.Result
import com.android254.droidconke19.datastates.runCatching
import com.android254.droidconke19.models.RoomModel
import com.android254.droidconke19.utils.await
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.FirebaseFirestoreException

interface RoomRepo {
suspend fun getRoomDetails(roomId: Int): FirebaseResult<RoomModel>
suspend fun getRoomDetails(roomId: Int): Result<RoomModel>
}

class RoomRepoImpl(private val firestore: FirebaseFirestore) : RoomRepo {

override suspend fun getRoomDetails(roomId: Int): FirebaseResult<RoomModel> {
return try {
override suspend fun getRoomDetails(roomId: Int): Result<RoomModel> {
return runCatching {
val snapshot = firestore.collection("rooms")
.whereEqualTo("id", roomId)
.get()
.await()
val doc = snapshot.documents.first()
val roomModel = doc.toObject(RoomModel::class.java)
FirebaseResult.Success(roomModel!!)

} catch (e: FirebaseFirestoreException) {
FirebaseResult.Error(e.message)
roomModel!!
}

}
Expand Down
Loading

0 comments on commit adeabbc

Please sign in to comment.