Skip to content
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

Feed api implementation #125

Merged
merged 33 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f704a29
Adding Feed.kt file
Jacquigee Jun 4, 2023
c88bdd1
Adding FeedRepo.kt Interface file
Jacquigee Jun 4, 2023
68046aa
Renaming Feed.kt to FeedDTO.kt
Jacquigee Jun 4, 2023
5cee66f
Adding Feed ViewModel
Jacquigee Jun 4, 2023
db7ea8e
Adds feed implementation
Jacquigee Jun 6, 2023
765da0c
Changing API BaseURL Test
Jacquigee Jun 6, 2023
7596394
Adding tests in FeedScreenTest.kt file
Jacquigee Jun 6, 2023
65653bc
Renamed api -> FeedApi
Jacquigee Jun 7, 2023
94b61de
Changed ResourceResult<List<Feed>> -> List<Feed>
Jacquigee Jun 7, 2023
23d9d50
Referencing String directly from the composable
Jacquigee Jun 7, 2023
0d06003
Modifying FeedScreenTest.kt file
Jacquigee Jun 8, 2023
b1f0d05
Create droidcon_logo_dark.xml
Borwe Jun 5, 2023
edc97d5
check mode in code
Borwe Jun 5, 2023
bec52c4
Added codeAnalysis.bat for windows uers and ran it
Borwe Jun 5, 2023
949f1a8
set it for all AppBars'
Borwe Jun 5, 2023
45c4dbe
use if with id
Borwe Jun 6, 2023
5018874
contrib-readme-action has updated readme (#127)
github-actions[bot] Jun 7, 2023
e7d01a9
Update to AS Flamingo (#126)
wangerekaharun Jun 8, 2023
36a1db0
contrib-readme-action has updated readme (#130)
github-actions[bot] Jun 8, 2023
e735b19
contrib-readme-action has updated readme (#131)
github-actions[bot] Jun 8, 2023
eb9a234
contrib-readme-action has updated readme (#132)
github-actions[bot] Jun 8, 2023
f61f68c
Migrate some State and viewModel calls from their composables to thei…
yveskalume Jun 7, 2023
daeb947
contrib-readme-action has updated readme
github-actions[bot] Jun 8, 2023
685f9b1
contrib-readme-action has updated readme (#134)
github-actions[bot] Jun 8, 2023
3d1ec6f
Adds feed implementation
Jacquigee Jun 6, 2023
a797ae9
Handling state in the FeedScreen.kt file
Jacquigee Jun 9, 2023
d5c48f8
Merge branch 'main' into feed_api_implementation
Jacquigee Jun 14, 2023
505f092
Merge branch 'droidconKE:main' into feed_api_implementation
Jacquigee Jun 14, 2023
fdbf6ed
Refactoring results
Jacquigee Jun 14, 2023
d92441d
Merge remote-tracking branch 'origin/feed_api_implementation' into fe…
Jacquigee Jun 14, 2023
6d8495e
Fixes failing tests
Jacquigee Jun 14, 2023
c2a21c4
Merge remote-tracking branch 'origin/feed_api_implementation' into fe…
Jacquigee Jun 27, 2023
409fa1a
Fixes String resource merge conflicts
Jacquigee Jun 27, 2023
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
6 changes: 6 additions & 0 deletions data/src/main/java/com/android254/data/di/RepoModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
package com.android254.data.di

import com.android254.data.repos.AuthManager
import com.android254.data.repos.FeedManager
import com.android254.data.repos.HomeRepoImpl
import com.android254.data.repos.OrganizersSource
import com.android254.data.repos.SessionsManager
import com.android254.data.repos.SpeakersManager
import com.android254.domain.repos.AuthRepo
import com.android254.domain.repos.FeedRepo
import com.android254.domain.repos.HomeRepo
import com.android254.domain.repos.OrganizersRepository
import com.android254.domain.repos.SessionsRepo
Expand Down Expand Up @@ -54,4 +56,8 @@ abstract class RepoModule {
@Binds
@Singleton
abstract fun provideOrganizersRepo(source: OrganizersSource): OrganizersRepository

@Binds
@Singleton
abstract fun provideFeedRepo(manager: FeedManager): FeedRepo
}
14 changes: 7 additions & 7 deletions data/src/main/java/com/android254/data/network/apis/FeedApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
*/
package com.android254.data.network.apis

import com.android254.data.network.models.responses.Feed
import com.android254.data.network.models.responses.FeedDTO
import com.android254.data.network.models.responses.PaginatedResponse
import com.android254.data.network.util.dataResultSafeApiCall
import com.android254.data.network.util.provideEventBaseUrl
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.request.*
import com.android254.data.network.util.provideBaseUrl
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.get
import javax.inject.Inject

class FeedApi @Inject constructor(private val client: HttpClient) {

suspend fun fetchFeed(page: Int = 1, size: Int = 100) = dataResultSafeApiCall {
val response: PaginatedResponse<List<Feed>> =
client.get("${provideEventBaseUrl()}/feeds") {
val response: PaginatedResponse<List<FeedDTO>> =
client.get("${provideBaseUrl()}/feeds") {
url {
parameters.append("page", page.toString())
parameters.append("per_page", size.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

@Serializable
data class Feed(
data class FeedDTO(
val title: String,
val body: String,
val topic: String,
Expand Down
37 changes: 37 additions & 0 deletions data/src/main/java/com/android254/data/repos/FeedManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2023 DroidconKE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android254.data.repos

import com.android254.data.network.apis.FeedApi
import com.android254.data.repos.mappers.toDomain
import com.android254.domain.models.DataResult
import com.android254.domain.models.Feed
import com.android254.domain.models.ResourceResult
import com.android254.domain.repos.FeedRepo
import javax.inject.Inject

class FeedManager @Inject constructor(
private val FeedApi: FeedApi
) : FeedRepo {
override suspend fun fetchFeed(): ResourceResult<List<Feed>> {
return when (val result = FeedApi.fetchFeed(1, 100)) {
DataResult.Empty -> ResourceResult.Empty("Empty list ")
is DataResult.Error -> ResourceResult.Error(result.message)
is DataResult.Loading -> ResourceResult.Loading(true)
is DataResult.Success -> ResourceResult.Success(result.data.map { it.toDomain() })
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2023 DroidconKE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android254.data.repos.mappers

import com.android254.data.network.models.responses.FeedDTO
import com.android254.domain.models.Feed

fun FeedDTO.toDomain() = Feed(
title = title,
body = body,
topic = topic,
url = url,
image = image,
createdAt = createdAt.toString()
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@
*/
package com.android254.data.network.apis

import com.android254.data.network.models.responses.Feed
import com.android254.data.network.models.responses.FeedDTO
import com.android254.data.network.util.HttpClientFactory
import com.android254.data.network.util.MockTokenProvider
import com.android254.data.network.util.RemoteFeatureToggle
import com.android254.data.network.util.provideEventBaseUrl
import com.android254.data.network.util.provideBaseUrl
import com.android254.domain.models.DataResult
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import io.ktor.client.engine.mock.*
import io.ktor.http.*
import io.ktor.client.engine.mock.MockEngine
import io.ktor.client.engine.mock.respond
import io.ktor.client.engine.mock.respondOk
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpMethod
import io.ktor.http.headersOf
import io.mockk.mockk
import kotlinx.coroutines.test.runTest
import org.hamcrest.CoreMatchers.`is`
Expand Down Expand Up @@ -53,7 +57,7 @@ class FeedApiTest {

assertThat(mockEngine.requestHistory.size, `is`(1))
mockEngine.requestHistory.first().run {
val expectedUrl = "${provideEventBaseUrl()}/feeds?page=2&per_page=50"
val expectedUrl = "${provideBaseUrl()}/feeds?page=2&per_page=50"
assertThat(url.toString(), `is`(expectedUrl))
assertThat(method, `is`(HttpMethod.Get))
}
Expand Down Expand Up @@ -108,7 +112,7 @@ class FeedApiTest {
`is`(
DataResult.Success(
listOf(
Feed(
FeedDTO(
title = "Test",
body = "Good one",
topic = "droidconweb",
Expand All @@ -119,7 +123,7 @@ class FeedApiTest {
LocalTime.parse("18:45:49")
)
),
Feed(
FeedDTO(
title = "niko fine",
body = "this is a test",
topic = "droidconweb",
Expand Down
25 changes: 25 additions & 0 deletions domain/src/main/java/com/android254/domain/models/Feed.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2023 DroidconKE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android254.domain.models

class Feed(
val title: String,
val body: String,
val topic: String,
val url: String,
val image: String?,
val createdAt: String
)
23 changes: 23 additions & 0 deletions domain/src/main/java/com/android254/domain/repos/FeedRepo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2023 DroidconKE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android254.domain.repos

import com.android254.domain.models.Feed
import com.android254.domain.models.ResourceResult

interface FeedRepo {
suspend fun fetchFeed(): ResourceResult<List<Feed>>
chepsi marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2023 DroidconKE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android254.presentation.common.bottomsheet

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.ui.R
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext

@Immutable
@kotlin.jvm.JvmInline
value class Strings private constructor(@Suppress("unused") private val value: Int) {
companion object {
val NavigationMenu = Strings(0)
val CloseDrawer = Strings(1)
val CloseSheet = Strings(2)
val DefaultErrorMessage = Strings(3)
val ExposedDropdownMenu = Strings(4)
val SliderRangeStart = Strings(5)
val SliderRangeEnd = Strings(6)
}
}

@Composable
fun getString(string: Strings): String {
LocalConfiguration.current
val resources = LocalContext.current.resources
return when (string) {
Strings.NavigationMenu -> resources.getString(R.string.navigation_menu)
Strings.CloseDrawer -> resources.getString(R.string.close_drawer)
Strings.CloseSheet -> resources.getString(R.string.close_sheet)
Strings.DefaultErrorMessage -> resources.getString(R.string.default_error_message)
Strings.ExposedDropdownMenu -> resources.getString(R.string.dropdown_menu)
Strings.SliderRangeStart -> resources.getString(R.string.range_start)
Strings.SliderRangeEnd -> resources.getString(R.string.range_end)
else -> ""
wangerekaharun marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2023 DroidconKE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android254.presentation.feed

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.android254.domain.models.ResourceResult
import com.android254.domain.repos.FeedRepo
import com.android254.presentation.feed.view.FeedUIState
import com.android254.presentation.feed.view.toPresentation
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class FeedViewModel @Inject constructor(
private val feedRepo: FeedRepo
) : ViewModel() {
var viewState: FeedUIState by mutableStateOf(FeedUIState.Loading)
private set

fun fetchFeed() {
viewModelScope.launch {
viewState = when (val value = feedRepo.fetchFeed()) {
is ResourceResult.Empty -> FeedUIState.Empty
is ResourceResult.Error -> FeedUIState.Error(value.message)
is ResourceResult.Loading -> FeedUIState.Loading
is ResourceResult.Success -> FeedUIState.Success(
value.data?.map { it.toPresentation() }
?: emptyList()
)
}
}
}
}
Loading