Skip to content

Commit

Permalink
Add AuthorScreen and corresponding components
Browse files Browse the repository at this point in the history
Change-Id: Iee5e8bd0932d1b68770aaa1e23fa660451109601
  • Loading branch information
tunjid authored and dturner committed May 11, 2022
1 parent dc4af7e commit d945a00
Show file tree
Hide file tree
Showing 20 changed files with 1,034 additions and 16 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ android {
}

dependencies {
implementation(project(":feature-author"))
implementation(project(":feature-interests"))
implementation(project(":feature-foryou"))
implementation(project(":feature-topic"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import androidx.navigation.compose.composable
import androidx.navigation.compose.navigation
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.google.samples.apps.nowinandroid.feature.author.AuthorDestinations
import com.google.samples.apps.nowinandroid.feature.author.AuthorDestinationsArgs
import com.google.samples.apps.nowinandroid.feature.author.AuthorRoute
import com.google.samples.apps.nowinandroid.feature.author.InterestsScreens.AUTHOR_SCREEN
import com.google.samples.apps.nowinandroid.feature.foryou.ForYouRoute
import com.google.samples.apps.nowinandroid.feature.interests.InterestsRoute
import com.google.samples.apps.nowinandroid.feature.topic.InterestsDestinations
Expand Down Expand Up @@ -69,7 +73,7 @@ fun NiaNavGraph(
composable(InterestsDestinations.INTERESTS_DESTINATION) {
InterestsRoute(
navigateToTopic = { navController.navigate("$TOPIC_SCREEN/$it") },
navigateToAuthor = { /* TO IMPLEMENT */ },
navigateToAuthor = { navController.navigate("$AUTHOR_SCREEN/$it") },
)
}
composable(
Expand All @@ -82,6 +86,16 @@ fun NiaNavGraph(
) {
TopicRoute(onBackClick = { navController.popBackStack() })
}
composable(
AuthorDestinations.AUTHOR_ROUTE,
arguments = listOf(
navArgument(AuthorDestinationsArgs.AUTHOR_ID_ARG) {
type = NavType.StringType
}
)
) {
AuthorRoute(onBackClick = { navController.popBackStack() })
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ interface AuthorsRepository : Syncable {
*/
fun getAuthorsStream(): Flow<List<Author>>

/**
* Gets data for a specific author
*/
fun getAuthorStream(id: String): Flow<Author>

/**
* Sets the user's currently followed authors
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class OfflineFirstAuthorsRepository @Inject constructor(
private val niaPreferences: NiaPreferences,
) : AuthorsRepository {

override fun getAuthorStream(id: String): Flow<Author> =
authorDao.getAuthorEntityStream(id).map {
it.asExternalModel()
}

override fun getAuthorsStream(): Flow<List<Author>> =
authorDao.getAuthorEntitiesStream()
.map { it.map(AuthorEntity::asExternalModel) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json

Expand Down Expand Up @@ -60,6 +61,10 @@ class FakeAuthorsRepository @Inject constructor(
}
.flowOn(ioDispatcher)

override fun getAuthorStream(id: String): Flow<Author> {
return getAuthorsStream().map { it.first { author -> author.id == id } }
}

override suspend fun setFollowedAuthorIds(followedAuthorIds: Set<String>) {
niaPreferences.setFollowedAuthorIds(followedAuthorIds)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class TestAuthorDao : AuthorDao {
override fun getAuthorEntitiesStream(): Flow<List<AuthorEntity>> =
entitiesStateFlow

override fun getAuthorEntityStream(authorId: String): Flow<AuthorEntity> {
throw NotImplementedError("Unused in tests")
}

override suspend fun insertOrIgnoreAuthors(authorEntities: List<AuthorEntity>): List<Long> {
entitiesStateFlow.value = authorEntities
// Assume no conflicts on insert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ import kotlinx.coroutines.flow.Flow
*/
@Dao
interface AuthorDao {
@Query(
value = """
SELECT * FROM authors
WHERE id = :authorId
"""
)
fun getAuthorEntityStream(authorId: String): Flow<AuthorEntity>

@Query(value = "SELECT * FROM authors")
fun getAuthorEntitiesStream(): Flow<List<AuthorEntity>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.google.samples.apps.nowinandroid.core.model.data.Author
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.map

class TestAuthorsRepository : AuthorsRepository {
/**
Expand All @@ -38,6 +39,10 @@ class TestAuthorsRepository : AuthorsRepository {

override fun getAuthorsStream(): Flow<List<Author>> = authorsFlow

override fun getAuthorStream(id: String): Flow<Author> {
return authorsFlow.map { authors -> authors.find { it.id == id }!! }
}

override fun getFollowedAuthorIdsStream(): Flow<Set<String>> = _followedAuthorIds

override suspend fun setFollowedAuthorIds(followedAuthorIds: Set<String>) {
Expand All @@ -56,14 +61,14 @@ class TestAuthorsRepository : AuthorsRepository {
override suspend fun syncWith(synchronizer: Synchronizer) = true

/**
* A test-only API to allow controlling the list of topics from tests.
* A test-only API to allow controlling the list of authors from tests.
*/
fun sendAuthors(authors: List<Author>) {
authorsFlow.tryEmit(authors)
}

/**
* A test-only API to allow querying the current followed topics.
* A test-only API to allow querying the current followed authors.
*/
fun getCurrentFollowedAuthors(): Set<String>? = _followedAuthorIds.replayCache.firstOrNull()
}
1 change: 1 addition & 0 deletions feature-author/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
64 changes: 64 additions & 0 deletions feature-author/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2022 The Android Open Source Project
*
* 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
*
* https://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.
*/
plugins {
id("nowinandroid.android.library")
id("nowinandroid.android.library.compose")
id("nowinandroid.android.library.jacoco")
kotlin("kapt")
id("dagger.hilt.android.plugin")
id("nowinandroid.spotless")
}

android {
defaultConfig {
testInstrumentationRunner = "com.google.samples.apps.nowinandroid.core.testing.NiaTestRunner"
}
}

dependencies {
implementation(project(":core-model"))
implementation(project(":core-ui"))
implementation(project(":core-data"))
implementation(project(":core-common"))

testImplementation(project(":core-testing"))
androidTestImplementation(project(":core-testing"))

implementation(libs.coil.kt)
implementation(libs.coil.kt.compose)

implementation(libs.kotlinx.coroutines.android)
implementation(libs.kotlinx.datetime)

implementation(libs.androidx.hilt.navigation.compose)
implementation(libs.androidx.lifecycle.viewModelCompose)

implementation(libs.hilt.android)
kapt(libs.hilt.compiler)

// TODO : Remove this dependency once we upgrade to Android Studio Dolphin b/228889042
// These dependencies are currently necessary to render Compose previews
debugImplementation(libs.androidx.customview.poolingcontainer)

// androidx.test is forcing JUnit, 4.12. This forces it to use 4.13
configurations.configureEach {
resolutionStrategy {
force(libs.junit4)
// Temporary workaround for https://issuetracker.google.com/174733673
force("org.objenesis:objenesis:2.6")
}
}
}
Loading

0 comments on commit d945a00

Please sign in to comment.