Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions app/src/main/java/com/example/roomie/data/NoteRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.example.roomie.data

import com.example.roomie.data.api.CreateNoteBody
import com.example.roomie.data.api.NoteApiService
import com.example.roomie.data.api.NoteBoardResponse
import com.example.roomie.data.api.NoteColorDto
import com.example.roomie.data.api.NoteDto
import com.example.roomie.data.api.PinNoteBody
import com.example.roomie.data.api.ReportNoteBody
import com.example.roomie.data.api.RetrofitInstance
import com.example.roomie.data.api.RoomieApiResponse
import com.example.roomie.data.api.UpdateNoteBody
import com.example.roomie.data.api.apiCall
import com.example.roomie.data.api.ResponseFlow
import com.example.roomie.data.api.repositoryScope
import kotlinx.coroutines.flow.StateFlow
import java.time.Instant
import javax.inject.Inject
import javax.inject.Singleton

/** The backend doesn't serve the Note Board yet; see [NoteApiService]. */
@Singleton
class NoteRepository @Inject constructor(private val retrofitInstance: RetrofitInstance) {

private val scope = repositoryScope()
private val api get() = retrofitInstance.noteApi

private val _noteBoard = ResponseFlow<NoteBoardResponse>(scope)

/** The first page of the board. Refreshed after every change below succeeds. */
val noteBoard: StateFlow<RoomieApiResponse<NoteBoardResponse>> = _noteBoard.state

/** Pipelines the first page into [noteBoard]. */
fun fetchNoteBoard(houseId: Int, refresh: Boolean = false) {
_noteBoard.fetch(refresh) { api.getNotes(houseId, before = null) }
}

/** Older notes. Pass the previous page's `nextCursor` as [before]. Doesn't touch [noteBoard]. */
suspend fun getOlderNotes(houseId: Int, before: Int): RoomieApiResponse<NoteBoardResponse> =
apiCall { api.getNotes(houseId, before) }

suspend fun createNote(
houseId: Int,
body: String,
color: NoteColorDto? = null,
pinnedUntil: Instant? = null,
): RoomieApiResponse<NoteDto> =
changing(houseId) { api.createNote(houseId, CreateNoteBody(body.trim(), color, pinnedUntil)).note }

suspend fun updateNote(houseId: Int, noteId: Int, body: String? = null, color: NoteColorDto? = null): RoomieApiResponse<NoteDto> =
changing(houseId) { api.updateNote(houseId, noteId, UpdateNoteBody(body?.trim(), color)).note }

suspend fun deleteNote(houseId: Int, noteId: Int): RoomieApiResponse<Unit> =
changing(houseId) { api.deleteNote(houseId, noteId) }

/** Leave [until] null to pin until someone unpins it. */
suspend fun pinNote(houseId: Int, noteId: Int, until: Instant? = null): RoomieApiResponse<NoteDto> =
changing(houseId) { api.pinNote(houseId, noteId, PinNoteBody(until)).note }

suspend fun unpinNote(houseId: Int, noteId: Int): RoomieApiResponse<NoteDto> =
changing(houseId) { api.unpinNote(houseId, noteId).note }

suspend fun reportNote(houseId: Int, noteId: Int, reason: String? = null): RoomieApiResponse<Unit> =
apiCall { api.reportNote(houseId, noteId, ReportNoteBody(reason)) }

private suspend fun <T> changing(houseId: Int, call: suspend () -> T): RoomieApiResponse<T> =
apiCall(call).also { it.ifSuccess { fetchNoteBoard(houseId, refresh = true) } }
}
18 changes: 18 additions & 0 deletions app/src/main/java/com/example/roomie/data/api/ApiModels.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ enum class ShoppingCategory { GROCERIES, HOUSEHOLD, OTHER }

enum class ShoppingStatus { NEEDED, BOUGHT }

/** Proposed; the Note Board isn't built on the backend yet. */
enum class NoteColorDto { YELLOW, PINK, BLUE, GREEN, PURPLE }

/** The public view of a person: members, rotation, assignee, claimedBy, … */
data class UserDto(
val id: Int,
Expand Down Expand Up @@ -131,3 +134,18 @@ data class ShoppingItemDto(
val createdAt: Instant,
val updatedAt: Instant,
)

data class NoteDto(
val id: Int,
/** 1 to 500 characters. */
val body: String,
val color: NoteColorDto,
/** Compare `author.id` with your own ID to show Edit and Delete. */
val author: UserDto,
val pinned: Boolean,
/** When the pin ends. Null on a pinned note means until someone unpins it. */
val pinnedUntil: Instant?,
val createdAt: Instant,
/** Later than [createdAt] means it was edited. */
val updatedAt: Instant,
)
92 changes: 92 additions & 0 deletions app/src/main/java/com/example/roomie/data/api/NoteApiService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.example.roomie.data.api

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
import java.time.Instant

/**
* The Note Board. This is the proposed contract; the backend and the mock server don't serve
* it yet, so every call 404s for now.
*/
interface NoteApiService {

/**
* Pinned notes, then the rest newest first. Pass the previous page's `nextCursor` as [before]
* to load older notes. [limit] defaults to 30 (max 100).
*/
@GET("houses/{houseId}/notes")
suspend fun getNotes(
@Path("houseId") houseId: Int,
@Query("before") before: Int? = null,
@Query("limit") limit: Int? = null,
): NoteBoardResponse

@POST("houses/{houseId}/notes")
suspend fun createNote(@Path("houseId") houseId: Int, @Body body: CreateNoteBody): NoteResponse

@GET("houses/{houseId}/notes/{noteId}")
suspend fun getNote(@Path("houseId") houseId: Int, @Path("noteId") noteId: Int): NoteResponse

/** Your own notes only. */
@PATCH("houses/{houseId}/notes/{noteId}")
suspend fun updateNote(
@Path("houseId") houseId: Int,
@Path("noteId") noteId: Int,
@Body body: UpdateNoteBody,
): NoteResponse

/** The author or the house owner. */
@DELETE("houses/{houseId}/notes/{noteId}")
suspend fun deleteNote(@Path("houseId") houseId: Int, @Path("noteId") noteId: Int)

/** Pinning again replaces the end time. */
@POST("houses/{houseId}/notes/{noteId}/pin")
suspend fun pinNote(
@Path("houseId") houseId: Int,
@Path("noteId") noteId: Int,
@Body body: PinNoteBody,
): NoteResponse

@DELETE("houses/{houseId}/notes/{noteId}/pin")
suspend fun unpinNote(@Path("houseId") houseId: Int, @Path("noteId") noteId: Int): NoteResponse

/** Hides the note from you. You can't report your own. */
@POST("houses/{houseId}/notes/{noteId}/report")
suspend fun reportNote(
@Path("houseId") houseId: Int,
@Path("noteId") noteId: Int,
@Body body: ReportNoteBody,
)
}

data class NoteBoardResponse(
/** Only filled on the first page. */
val pinned: List<NoteDto>,
val notes: List<NoteDto>,
/** The `before` value for the next page, or null at the end. */
val nextCursor: Int?,
)

data class NoteResponse(val note: NoteDto)

data class CreateNoteBody(
val body: String,
val color: NoteColorDto? = null,
val pinnedUntil: Instant? = null,
)

data class UpdateNoteBody(
val body: String? = null,
val color: NoteColorDto? = null,
)

/** Leave [until] null to pin until someone unpins it. */
data class PinNoteBody(val until: Instant? = null)

/** Up to 200 characters. */
data class ReportNoteBody(val reason: String? = null)
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class RetrofitInstance internal constructor(retrofit: Retrofit) {
val houseApi: HouseApiService by lazy { retrofit.create(HouseApiService::class.java) }
val choreApi: ChoreApiService by lazy { retrofit.create(ChoreApiService::class.java) }
val shoppingApi: ShoppingApiService by lazy { retrofit.create(ShoppingApiService::class.java) }
val noteApi: NoteApiService by lazy { retrofit.create(NoteApiService::class.java) }
}

/** Builds the client. Separate from [RetrofitInstance] so tests can point it at a fake server. */
Expand Down