Repository navigation
retrofit networking layer #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.example.roomie | ||
|
|
||
| import android.app.Application | ||
| import dagger.hilt.android.HiltAndroidApp | ||
|
|
||
| @HiltAndroidApp | ||
| class RoomieApplication : Application() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package com.example.roomie.data | ||
|
|
||
| import com.example.roomie.data.api.DevTokenProvider | ||
| import com.example.roomie.data.api.LoginResponse | ||
| import com.example.roomie.data.api.MeResponse | ||
| import com.example.roomie.data.api.RetrofitInstance | ||
| import com.example.roomie.data.api.RoomieApiResponse | ||
| import com.example.roomie.data.api.UpdateMeBody | ||
| import com.example.roomie.data.api.apiCall | ||
| import com.example.roomie.data.api.fetchInto | ||
| import com.example.roomie.data.api.repositoryScope | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| class AuthRepository @Inject constructor( | ||
| private val retrofitInstance: RetrofitInstance, | ||
| private val devTokenProvider: DevTokenProvider, | ||
| ) { | ||
| private val scope = repositoryScope() | ||
|
|
||
| private val _me = MutableStateFlow<RoomieApiResponse<MeResponse>>(RoomieApiResponse.Pending) | ||
|
|
||
| /** You, your house, and invites you've received. */ | ||
| val me: StateFlow<RoomieApiResponse<MeResponse>> = _me.asStateFlow() | ||
|
|
||
| /** Signs in to the local backend or mock server as [netId], with no Firebase involved. */ | ||
| suspend fun devLogin(netId: String): RoomieApiResponse<LoginResponse> { | ||
| devTokenProvider.netId = netId.trim().lowercase() | ||
| return login() | ||
| } | ||
|
|
||
| /** Call right after sign-in, once the [com.example.roomie.data.api.TokenProvider] has a token. */ | ||
| suspend fun login(): RoomieApiResponse<LoginResponse> = apiCall { retrofitInstance.userApi.login() } | ||
|
|
||
| /** Pipelines the response into [me]. */ | ||
| fun fetchMe(refresh: Boolean = false) { | ||
| scope.fetchInto(_me, refresh) { retrofitInstance.userApi.getMe() } | ||
| } | ||
|
|
||
| suspend fun updateMe(name: String? = null, major: String? = null): RoomieApiResponse<MeResponse> = | ||
| apiCall { retrofitInstance.userApi.updateMe(UpdateMeBody(name, major)) }.also { result -> | ||
| result.ifSuccess { _me.value = RoomieApiResponse.Success(it) } | ||
| } | ||
|
|
||
| fun signOut() { | ||
| devTokenProvider.netId = null | ||
| _me.value = RoomieApiResponse.Pending | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package com.example.roomie.data | ||
|
|
||
| import com.example.roomie.data.api.ChoreDto | ||
| import com.example.roomie.data.api.ChoreStatsResponse | ||
| import com.example.roomie.data.api.CreateChoreBody | ||
| import com.example.roomie.data.api.RetrofitInstance | ||
| import com.example.roomie.data.api.RoomieApiResponse | ||
| import com.example.roomie.data.api.SwapBody | ||
| import com.example.roomie.data.api.TurnDto | ||
| import com.example.roomie.data.api.UpdateChoreBody | ||
| import com.example.roomie.data.api.apiCall | ||
| import com.example.roomie.data.api.fetchInto | ||
| import com.example.roomie.data.api.repositoryScope | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import java.time.LocalDate | ||
| import java.time.YearMonth | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| class ChoreRepository @Inject constructor(private val retrofitInstance: RetrofitInstance) { | ||
|
|
||
| private val scope = repositoryScope() | ||
| private val api get() = retrofitInstance.choreApi | ||
|
|
||
| private val _chores = MutableStateFlow<RoomieApiResponse<List<ChoreDto>>>(RoomieApiResponse.Pending) | ||
|
|
||
| /** Every chore in the house. */ | ||
| val chores: StateFlow<RoomieApiResponse<List<ChoreDto>>> = _chores.asStateFlow() | ||
|
|
||
| private val _myTurnsToday = MutableStateFlow<RoomieApiResponse<List<TurnDto>>>(RoomieApiResponse.Pending) | ||
|
|
||
| /** Your turns due today, plus any you've left overdue. What Home shows. */ | ||
| val myTurnsToday: StateFlow<RoomieApiResponse<List<TurnDto>>> = _myTurnsToday.asStateFlow() | ||
|
|
||
| private val _turns = MutableStateFlow<RoomieApiResponse<List<TurnDto>>>(RoomieApiResponse.Pending) | ||
|
|
||
| /** Everyone's turns over the range last passed to [fetchTurns], plus overdue ones. */ | ||
| val turns: StateFlow<RoomieApiResponse<List<TurnDto>>> = _turns.asStateFlow() | ||
|
|
||
| private val _stats = MutableStateFlow<RoomieApiResponse<ChoreStatsResponse>>(RoomieApiResponse.Pending) | ||
|
|
||
| /** How many turns each member finished in the month last passed to [fetchStats]. */ | ||
| val stats: StateFlow<RoomieApiResponse<ChoreStatsResponse>> = _stats.asStateFlow() | ||
|
|
||
| /** Pipelines the response into [chores]. */ | ||
| fun fetchChores(houseId: Int, refresh: Boolean = false) { | ||
| scope.fetchInto(_chores, refresh) { api.getChores(houseId).chores } | ||
| } | ||
|
|
||
| /** Pipelines the response into [myTurnsToday]. */ | ||
| fun fetchMyTurnsToday(houseId: Int, today: LocalDate, refresh: Boolean = false) { | ||
| scope.fetchInto(_myTurnsToday, refresh) { | ||
| api.getAssignments(houseId, today, today, assignee = "me", includeOverdue = true).assignments | ||
| } | ||
| } | ||
|
|
||
| /** Pipelines [from] to [to] (at most 62 days) into [turns]. */ | ||
| fun fetchTurns(houseId: Int, from: LocalDate, to: LocalDate, refresh: Boolean = false) { | ||
| scope.fetchInto(_turns, refresh) { | ||
| api.getAssignments(houseId, from, to, includeOverdue = true).assignments | ||
| } | ||
| } | ||
|
|
||
| /** Pipelines the response into [stats]. Leave [month] null for this month. */ | ||
| fun fetchStats(houseId: Int, month: YearMonth? = null, refresh: Boolean = false) { | ||
| scope.fetchInto(_stats, refresh) { api.getChoreStats(houseId, month) } | ||
| } | ||
|
|
||
| suspend fun getChore(houseId: Int, choreId: Int): RoomieApiResponse<ChoreDto> = | ||
| apiCall { api.getChore(houseId, choreId).chore } | ||
|
|
||
| suspend fun createChore(houseId: Int, body: CreateChoreBody): RoomieApiResponse<ChoreDto> = | ||
| changingChores(houseId) { api.createChore(houseId, body).chore } | ||
|
|
||
| suspend fun updateChore(houseId: Int, choreId: Int, body: UpdateChoreBody): RoomieApiResponse<ChoreDto> = | ||
| changingChores(houseId) { api.updateChore(houseId, choreId, body).chore } | ||
|
|
||
| suspend fun deleteChore(houseId: Int, choreId: Int): RoomieApiResponse<Unit> = | ||
| changingChores(houseId) { api.deleteChore(houseId, choreId) } | ||
|
|
||
| suspend fun completeTurn(houseId: Int, turnId: Int): RoomieApiResponse<TurnDto> = | ||
| apiCall { api.completeAssignment(houseId, turnId).assignment }.also { it.ifSuccess { turn -> replaceTurns(listOf(turn)) } } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove completed overdue turns from If a cached turn was due yesterday, Refetch
|
||
|
|
||
| suspend fun uncompleteTurn(houseId: Int, turnId: Int): RoomieApiResponse<TurnDto> = | ||
| apiCall { api.uncompleteAssignment(houseId, turnId).assignment }.also { it.ifSuccess { turn -> replaceTurns(listOf(turn)) } } | ||
|
|
||
| /** Swapping can move a turn on or off your list, so [myTurnsToday] is refetched. */ | ||
| suspend fun swapTurns(houseId: Int, turnId: Int, withTurnId: Int, today: LocalDate): RoomieApiResponse<List<TurnDto>> = | ||
| apiCall { api.swapAssignment(houseId, turnId, SwapBody(withTurnId)).assignments }.also { | ||
| it.ifSuccess { turns -> | ||
| replaceTurns(turns) | ||
| fetchMyTurnsToday(houseId, today, refresh = true) | ||
| } | ||
| } | ||
|
|
||
| /** A chore change can rewrite its upcoming turns, so refetch the chores. Turn lists refresh on their next fetch. */ | ||
| private suspend fun <T> changingChores(houseId: Int, call: suspend () -> T): RoomieApiResponse<T> = | ||
| apiCall(call).also { it.ifSuccess { fetchChores(houseId, refresh = true) } } | ||
|
|
||
| /** Swaps the changed turns into the cached lists that already have them. */ | ||
| private fun replaceTurns(changed: List<TurnDto>) { | ||
| val byId = changed.associateBy { it.id } | ||
| for (flow in listOf(_turns, _myTurnsToday)) { | ||
| flow.value = flow.value.map { turns -> turns.map { byId[it.id] ?: it } } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Serialize turn-cache updates before connecting callers.
No screen calls these methods yet. Address this before a ViewModel connects to
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package com.example.roomie.data | ||
|
|
||
| import dagger.Module | ||
| import dagger.Provides | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.components.SingletonComponent | ||
|
|
||
| /** Lets ViewModels inject the in-memory data until each screen moves to its real repository. */ | ||
| @Module | ||
| @InstallIn(SingletonComponent::class) | ||
| object FakeDataModule { | ||
|
|
||
| @Provides | ||
| fun provideFakeRoomieRepository(): FakeRoomieRepository = FakeRoomieRepository | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package com.example.roomie.data | ||
|
|
||
| import com.example.roomie.data.api.CreateHouseBody | ||
| import com.example.roomie.data.api.HouseDto | ||
| import com.example.roomie.data.api.InviteBody | ||
| import com.example.roomie.data.api.JoinHouseBody | ||
| import com.example.roomie.data.api.RetrofitInstance | ||
| import com.example.roomie.data.api.RoomieApiResponse | ||
| import com.example.roomie.data.api.UpdateHouseBody | ||
| import com.example.roomie.data.api.apiCall | ||
| import com.example.roomie.data.api.fetchInto | ||
| import com.example.roomie.data.api.repositoryScope | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| class HouseRepository @Inject constructor(private val retrofitInstance: RetrofitInstance) { | ||
|
|
||
| private val scope = repositoryScope() | ||
| private val api get() = retrofitInstance.houseApi | ||
|
|
||
| private val _house = MutableStateFlow<RoomieApiResponse<HouseDto>>(RoomieApiResponse.Pending) | ||
|
|
||
| /** Your house. Every call below that returns the house updates this too. */ | ||
| val house: StateFlow<RoomieApiResponse<HouseDto>> = _house.asStateFlow() | ||
|
|
||
| /** Pipelines the response into [house]. */ | ||
| fun fetchHouse(houseId: Int, refresh: Boolean = false) { | ||
| scope.fetchInto(_house, refresh) { api.getHouse(houseId).house } | ||
| } | ||
|
|
||
| suspend fun createHouse(body: CreateHouseBody): RoomieApiResponse<HouseDto> = | ||
| updatingHouse { api.createHouse(body).house } | ||
|
|
||
| suspend fun updateHouse(houseId: Int, body: UpdateHouseBody): RoomieApiResponse<HouseDto> = | ||
| updatingHouse { api.updateHouse(houseId, body).house } | ||
|
|
||
| suspend fun invite(houseId: Int, netIds: List<String>): RoomieApiResponse<HouseDto> = | ||
| updatingHouse { api.invite(houseId, InviteBody(netIds)).house } | ||
|
|
||
| suspend fun cancelInvite(houseId: Int, inviteId: Int): RoomieApiResponse<Unit> = | ||
| apiCall { api.cancelInvite(houseId, inviteId) }.also { it.ifSuccess { fetchHouse(houseId, refresh = true) } } | ||
|
|
||
| suspend fun regenerateInviteCode(houseId: Int): RoomieApiResponse<HouseDto> = | ||
| updatingHouse { api.regenerateInviteCode(houseId).house } | ||
|
|
||
| suspend fun joinHouse(inviteCode: String): RoomieApiResponse<HouseDto> = | ||
| updatingHouse { api.joinHouse(JoinHouseBody(inviteCode.trim())).house } | ||
|
|
||
| suspend fun acceptInvite(inviteId: Int): RoomieApiResponse<HouseDto> = | ||
| updatingHouse { api.acceptInvite(inviteId).house } | ||
|
|
||
| suspend fun declineInvite(inviteId: Int): RoomieApiResponse<Unit> = | ||
| apiCall { api.declineInvite(inviteId) } | ||
|
|
||
| suspend fun leaveHouse(houseId: Int): RoomieApiResponse<Unit> = | ||
| apiCall { api.leaveHouse(houseId) }.also { it.ifSuccess { _house.value = RoomieApiResponse.Pending } } | ||
|
|
||
| suspend fun removeMember(houseId: Int, userId: Int): RoomieApiResponse<Unit> = | ||
| apiCall { api.removeMember(houseId, userId) }.also { it.ifSuccess { fetchHouse(houseId, refresh = true) } } | ||
|
|
||
| private suspend fun updatingHouse(call: suspend () -> HouseDto): RoomieApiResponse<HouseDto> = | ||
| apiCall(call).also { result -> result.ifSuccess { _house.value = RoomieApiResponse.Success(it) } } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reject results from superseded fetches.
Call
fetchTurnsfor January, then February. If February finishes first, the later January response replaces it.turnsthen violates its documented “range last passed” contract.Track a request generation or query key before publishing results. Apply the same protection to the other
fetchIntocallers. The shared helper currently accepts every completion.