-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from victorvicari/add_coroutines_to_repo_layer
#52 code cleanUp
- Loading branch information
Showing
15 changed files
with
238 additions
and
221 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
app/src/main/java/com/android254/droidconke19/datastates/Result.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,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>() | ||
} |
14 changes: 14 additions & 0 deletions
14
app/src/main/java/com/android254/droidconke19/datastates/RunCatching.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.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) | ||
} | ||
} |
25 changes: 11 additions & 14 deletions
25
app/src/main/java/com/android254/droidconke19/repository/AboutDetailsRepo.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 |
---|---|---|
@@ -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>() | ||
} | ||
} |
24 changes: 12 additions & 12 deletions
24
app/src/main/java/com/android254/droidconke19/repository/AgendaRepo.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 |
---|---|---|
@@ -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) | ||
} | ||
|
||
} | ||
} |
22 changes: 9 additions & 13 deletions
22
app/src/main/java/com/android254/droidconke19/repository/AnnouncementRepo.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 |
---|---|---|
@@ -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) | ||
} | ||
|
||
} | ||
} | ||
} |
21 changes: 9 additions & 12 deletions
21
app/src/main/java/com/android254/droidconke19/repository/EventFeedbackRepo.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 |
---|---|---|
@@ -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) | ||
} | ||
} | ||
} | ||
} |
27 changes: 13 additions & 14 deletions
27
app/src/main/java/com/android254/droidconke19/repository/EventTypeRepo.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 |
---|---|---|
@@ -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>() | ||
|
||
} | ||
|
||
} |
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
15 changes: 6 additions & 9 deletions
15
app/src/main/java/com/android254/droidconke19/repository/RoomRepo.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
Oops, something went wrong.