Skip to content

Commit

Permalink
feat: Added new search result item composable and improved search exp…
Browse files Browse the repository at this point in the history
…erience (#30)
  • Loading branch information
rob729 authored Jan 21, 2024
1 parent b2680d7 commit dbf19d6
Show file tree
Hide file tree
Showing 35 changed files with 986 additions and 167 deletions.
52 changes: 52 additions & 0 deletions app/schemas/com.rob729.newsfeed.database.NewsDatabase/2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"formatVersion": 1,
"database": {
"version": 2,
"identityHash": "20866458d27e519b9d62fabf4e9427b0",
"entities": [
{
"tableName": "news_source_table",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `news_source_domain` TEXT NOT NULL, `news_article` TEXT NOT NULL, `news_source_fetch_time` INTEGER NOT NULL)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "newsSourceDomain",
"columnName": "news_source_domain",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "newsArticle",
"columnName": "news_article",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "newsSourceFetchTimeInMillis",
"columnName": "news_source_fetch_time",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '20866458d27e519b9d62fabf4e9427b0')"
]
}
}
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,14 @@ 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 @@ -25,7 +26,8 @@ fun mapArticleDbDataToNewsArticleUiData(articleDbData: ArticleDbData): NewsArtic
articleDbData.description,
articleDbData.imageUrl,
articleDbData.url,
articleDbData.publishedAt
articleDbData.publishedAt,
articleDbData.source.orEmpty()
)
}

Expand All @@ -40,6 +42,7 @@ fun mapNetworkArticleToNewsArticleUiData(networkArticle: NetworkArticle): NewsAr
networkArticle.description,
networkArticle.imageUrl,
networkArticle.url,
networkArticle.publishedAt
networkArticle.publishedAt,
networkArticle.source?.name.orEmpty()
)
}
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 @@ -38,7 +38,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
2 changes: 2 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 Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.rob729.newsfeed.ui.components

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Schedule
import androidx.compose.material.icons.filled.Share
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp

@Composable
fun ShareIcon(modifier: Modifier = Modifier, shareUrl: String) {
val context = LocalContext.current
Icon(
imageVector = Icons.Filled.Share,
contentDescription = "share",
modifier
.size(14.dp)
.clickable { shareArticle(context, shareUrl) }
)
}

@Composable
fun TimeIcon(modifier: Modifier = Modifier) {
Icon(
imageVector = Icons.Default.Schedule, contentDescription = "time",
modifier
.size(14.dp)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ fun NewsSourceExtendedFab(modifier: Modifier, isExpanded: Boolean, onClick: () -
ExtendedFloatingActionButton(
modifier = modifier,
onClick = onClick,
text = { Text(text = Constants.FAB_TITLE, color = Color.White) },
text = { Text(text = Constants.FAB_TITLE, color = MaterialTheme.colorScheme.onPrimary) },
icon = {
Icon(
imageVector = Icons.Default.Newspaper,
tint = Color.White,
tint = MaterialTheme.colorScheme.onPrimary,
contentDescription = Constants.FAB_TITLE
)
},
elevation = FloatingActionButtonDefaults.elevation(8.dp),
expanded = isExpanded,
containerColor = MaterialTheme.colorScheme.secondaryContainer,
containerColor = MaterialTheme.colorScheme.primary,
shape = RoundedCornerShape(12.dp)
)
}
Expand All @@ -41,12 +41,12 @@ fun ScrollToTopFab(modifier: Modifier, onClick: () -> Unit) {
SmallFloatingActionButton(
modifier = modifier,
onClick = onClick,
containerColor = MaterialTheme.colorScheme.secondaryContainer,
containerColor = MaterialTheme.colorScheme.primary,
shape = RoundedCornerShape(12.dp)
) {
Icon(
imageVector = Icons.Default.KeyboardArrowUp,
tint = Color.White,
tint = MaterialTheme.colorScheme.onPrimary,
contentDescription = Constants.FAB_TITLE
)
}
Expand Down

This file was deleted.

Loading

0 comments on commit dbf19d6

Please sign in to comment.