Skip to content

Commit

Permalink
Created new search result item composable and improved search experience
Browse files Browse the repository at this point in the history
  • Loading branch information
rob729 committed Nov 30, 2023
1 parent 427cb3b commit 156809a
Show file tree
Hide file tree
Showing 29 changed files with 859 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.rob729.newsfeed.database

import android.content.Context
import androidx.room.AutoMigration
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.rob729.newsfeed.model.database.NewsSourceDbData

@TypeConverters(DataConverter::class)
@Database(entities = [NewsSourceDbData::class], version = 1, exportSchema = false)
@Database(entities = [NewsSourceDbData::class], version = 2, autoMigrations = [AutoMigration(from = 1, to = 2)])
abstract class NewsDatabase : RoomDatabase() {

abstract fun newsDao(): NewsDao
Expand All @@ -29,7 +30,7 @@ abstract class NewsDatabase : RoomDatabase() {
context.applicationContext,
NewsDatabase::class.java,
"news_database"
).build()
).fallbackToDestructiveMigration().build()

INSTANCE = instance
return instance
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.rob729.newsfeed.initalizers

import android.content.Context
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
import androidx.datastore.preferences.preferencesDataStore
import androidx.datastore.preferences.preferencesDataStoreFile
import androidx.startup.Initializer
import com.pluto.plugins.network.okhttp.addPlutoOkhttpInterceptor
import com.rob729.newsfeed.database.NewsDBDataSource
Expand All @@ -9,6 +12,7 @@ import com.rob729.newsfeed.network.NewsApi
import com.rob729.newsfeed.network.NewsApiDataSource
import com.rob729.newsfeed.network.NewsApiDataSourceImpl
import com.rob729.newsfeed.utils.Constants
import com.rob729.newsfeed.utils.SearchHistoryHelper
import com.rob729.newsfeed.vm.HomeViewModel
import com.rob729.newsfeed.vm.NewsRepository
import com.rob729.newsfeed.vm.SearchViewModel
Expand Down Expand Up @@ -54,6 +58,12 @@ class KoinInitializer : Initializer<KoinApplication> {
retrofitInstance.create(NewsApi::class.java)
}
},
module {
single {
val dataStore = PreferenceDataStoreFactory.create(produceFile = { context.preferencesDataStoreFile(Constants.PREFS_NAME) })
SearchHistoryHelper(dataStore)
}
},
module {
singleOf(::NewsApiDataSourceImpl) { bind<NewsApiDataSource>() }
singleOf(::NewsDBDataSource)
Expand Down
12 changes: 10 additions & 2 deletions app/src/main/java/com/rob729/newsfeed/model/api/NetworkArticle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.rob729.newsfeed.model.api
import androidx.annotation.Keep
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import java.io.Serializable

@Keep
@JsonClass(generateAdapter = true)
Expand All @@ -11,5 +12,12 @@ data class NetworkArticle(
@Json(name = "url") val url: String,
@Json(name = "urlToImage") val imageUrl: String?,
@Json(name = "description") val description: String?,
@Json(name = "publishedAt") val publishedAt: String
)
@Json(name = "publishedAt") val publishedAt: String,
@Json(name = "source") val source: ArticleSource? = null
): Serializable

@Keep
@JsonClass(generateAdapter = true)
data class ArticleSource(
@Json(name = "name") val name: String? = null
): Serializable
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ data class ArticleDbData(
@ColumnInfo(name = "url") val url: String,
@ColumnInfo(name = "urlToImage") val imageUrl: String?,
@ColumnInfo(name = "description") val description: String?,
@ColumnInfo(name = "publishedAt") val publishedAt: String
@ColumnInfo(name = "publishedAt") val publishedAt: String,
@ColumnInfo(name = "source") val source: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ fun mapNetworkArticleToArticleDbData(networkArticle: NetworkArticle): ArticleDbD
networkArticle.url,
networkArticle.imageUrl,
networkArticle.description,
networkArticle.publishedAt
networkArticle.publishedAt,
networkArticle.source?.name
)
}

Expand All @@ -22,7 +23,8 @@ fun mapArticleDbDataToNewsArticleUiData(articleDbData: ArticleDbData): NewsArtic
articleDbData.description,
articleDbData.imageUrl,
articleDbData.url,
articleDbData.publishedAt
articleDbData.publishedAt,
articleDbData.source.orEmpty()
)
}

Expand All @@ -34,6 +36,7 @@ fun mapNetworkArticleToNewsArticleUiData(networkArticle: NetworkArticle): NewsAr
networkArticle.description,
networkArticle.imageUrl,
networkArticle.url,
networkArticle.publishedAt
networkArticle.publishedAt,
networkArticle.source?.name ?: ""
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ import com.rob729.newsfeed.model.state.UiStatus

data class SearchState(
val uiStatus: UiStatus = UiStatus.EmptyScreen,
val editTextInput: String = "",
val searchQuery: String = "",
val searchHistoryList: List<String> = listOf()
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ data class NewsArticleUiData(
val description: String,
val imageUrl: String,
val url: String,
val publishedAt: String
val publishedAt: String,
val source: String
)
3 changes: 2 additions & 1 deletion app/src/main/java/com/rob729/newsfeed/network/NewsApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface NewsApi {
@Query("apiKey") apiKey: String,
@Query("from") startDate: String,
@Query("sortBy") sortBy: String,
@Query("language") language: String
@Query("language") language: String,
@Query("pageSize") pageSize: Int,
): Response<NetworkNews>
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class NewsApiDataSourceImpl(
BuildConfig.NEWS_FEED_API_KEY,
startDate,
Constants.SORT_RESULT_FILTER_PUBLISHED_AT,
Constants.API_RESULT_LANGUAGE
Constants.API_RESULT_LANGUAGE,
Constants.SEARCH_RESPONSE_PAGE_SIZE
)
)
}
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/rob729/newsfeed/ui/NewsActivity.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.rob729.newsfeed.ui

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
Expand All @@ -18,6 +19,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.testTagsAsResourceId
import androidx.core.content.ContextCompat
import androidx.datastore.preferences.preferencesDataStore
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
Expand All @@ -28,6 +30,8 @@ import com.rob729.newsfeed.ui.theme.NewsFeedTheme
import com.rob729.newsfeed.utils.Constants
import com.rob729.newsfeed.utils.NotificationHelper


private val Context.dataStore by preferencesDataStore(name = Constants.PREFS_NAME)
@OptIn(ExperimentalComposeUiApi::class)
class NewsActivity : ComponentActivity() {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,9 @@
package com.rob729.newsfeed.ui.components

import androidx.compose.animation.core.FastOutLinearInEasing
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.runtime.Composable
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color

@Composable
fun LoadingShimmer() {

val gradient = listOf(
Color.LightGray.copy(alpha = 0.7f), //darker grey (60% opacity)
Color.LightGray.copy(alpha = 0.3f), //lighter grey (20% opacity)
Color.LightGray.copy(alpha = 0.7f)
)

val transition = rememberInfiniteTransition() // animate infinite times

val translateAnimation = transition.animateFloat( //animate the transition
initialValue = 0f,
targetValue = 1000f,
animationSpec = infiniteRepeatable(
animation = tween(
durationMillis = 1000, // duration for the animation
easing = FastOutLinearInEasing
)
)
)
val brush = Brush.linearGradient(
colors = gradient,
start = Offset(200f, 200f),
end = Offset(
x = translateAnimation.value,
y = translateAnimation.value
)
)
ShimmerListItem(brush = brush)
fun LoadingShimmer(brush: Brush) {
NewsFeedItemShimmer(brush = brush)
}
62 changes: 57 additions & 5 deletions app/src/main/java/com/rob729/newsfeed/ui/components/LoadingView.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,66 @@
package com.rob729.newsfeed.ui.components

import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import com.rob729.newsfeed.utils.ScreenType

@Composable
fun LoadingView() {
LazyColumn {
repeat(4) {
item {
LoadingShimmer()
fun LoadingView(screenType: ScreenType = ScreenType.HOME) {
val gradient = listOf(
Color.LightGray.copy(alpha = 0.8f), //darker grey (60% opacity)
Color.LightGray.copy(alpha = 0.3f), //lighter grey (20% opacity)
Color.LightGray.copy(alpha = 0.8f)
)

val transition = rememberInfiniteTransition(label = "loading_shimmer_transition") // animate infinite times

val translateAnimation by transition.animateFloat( //animate the transition
initialValue = 0f,
targetValue = 1000f,
animationSpec = infiniteRepeatable(
animation = tween(
durationMillis = 1000, // duration for the animation
easing = LinearEasing
)
), label = "loading_shimmer_translate_anim"
)

val brush = Brush.linearGradient(
colors = gradient,
start = Offset(translateAnimation, translateAnimation),
end = Offset(
x = translateAnimation + 200f,
y = translateAnimation + 200f
)
)

LazyColumn(modifier = Modifier.fillMaxSize()) {
when(screenType) {
ScreenType.HOME -> {
repeat(4) {
item {
NewsFeedItemShimmer(brush)
}
}
}
ScreenType.SEARCH -> {
repeat(7) {
item {
SearchResultShimmer(brush)
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.unit.dp

@Composable
fun ShimmerListItem(brush: Brush) {
fun NewsFeedItemShimmer(brush: Brush) {

Card(
modifier = Modifier.padding(start = 8.dp, end = 8.dp, top = 8.dp),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.rob729.newsfeed.R
Expand Down Expand Up @@ -54,7 +56,8 @@ fun NoInternetView(onTryAgainClicked: () -> Unit) {
fontSize = 32.sp,
color = Color.White,
fontWeight = FontWeight.SemiBold,
fontFamily = lexendDecaFontFamily
fontFamily = lexendDecaFontFamily,
textAlign = TextAlign.Center
)

Image(
Expand All @@ -81,4 +84,12 @@ fun NoInternetView(onTryAgainClicked: () -> Unit) {
)
}
}
}

@Preview
@Composable
fun NoInternetViewPreview() {
NoInternetView {

}
}
Loading

0 comments on commit 156809a

Please sign in to comment.