Skip to content

Commit

Permalink
Code Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Shreyassp002 committed Oct 20, 2024
1 parent a9a2712 commit 1229e5a
Show file tree
Hide file tree
Showing 62 changed files with 997 additions and 578 deletions.
22 changes: 22 additions & 0 deletions .idea/other.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
android:roundIcon="@drawable/ic_news_katta_circular"
android:supportsRtl="true"
android:theme="@style/Theme.NewsKatta"
tools:targetApi="31">
android:enableOnBackInvokedCallback="true"
tools:targetApi="34">
<activity
android:name=".MainActivity"
android:name=".presentation.mainactivity.MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.NewsKatta">
Expand Down
42 changes: 0 additions & 42 deletions app/src/main/java/com/rey/newskatta/MainViewModel.kt

This file was deleted.

4 changes: 4 additions & 0 deletions app/src/main/java/com/rey/newskatta/data/local/NewsDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ interface NewsDao {

@Query("SELECT * FROM Article")
fun getArticles(): Flow<List<Article>>

@Query("SELECT * FROM Article WHERE url=:url")
suspend fun getArticle(url: String): Article?

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import androidx.room.TypeConverters
import com.rey.newskatta.domain.model.Article


@Database(entities = [Article::class], version = 1)
@TypeConverters(NewsTypeConverter::class)
abstract class NewsDatabase: RoomDatabase() {
@Database(entities = [Article::class],version = 1,)
@TypeConverters(NewsTypeConvertor::class)
abstract class NewsDatabase : RoomDatabase() {

abstract val newsDao: NewsDao

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import androidx.room.TypeConverter
import com.rey.newskatta.domain.model.Source

@ProvidedTypeConverter
class NewsTypeConverter {
class NewsTypeConvertor {

@TypeConverter
fun sourceToString(source: Source): String {
return "${source.id}, ${source.name}"
fun sourceToString(source: Source): String{
return "${source.id},${source.name}"
}

@TypeConverter
fun stringToSource(source: String): Source {
return source.split(", ").let { sourceArray ->
Source(sourceArray[0], sourceArray[1])
fun stringToSource(source: String): Source{
return source.split(',').let { sourceArray ->
Source(id = sourceArray[0], name = sourceArray[1])
}
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
package com.rey.newskatta.data.manager

import android.app.Application
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.preferencesDataStore
import com.rey.newskatta.domain.manager.LocalUserManager

import com.rey.newskatta.domain.manager.LocalUserManger
import com.rey.newskatta.util.Constants
import com.rey.newskatta.util.Constants.USER_SETTINGS
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class LocalUserMangerImpl @Inject constructor(
private val application: Application
) : LocalUserManger {

class LocalUserManagerImpl(
private val context: Context
): LocalUserManager {
override suspend fun saveAppEntry() {
context.dataStore.edit { settings ->
settings[PrefencesKeys.APP_ENTRY] = true
application.dataStore.edit { settings ->
settings[PreferenceKeys.APP_ENTRY] = true
}
}

override fun readAppEntry(): Flow<Boolean> {
return context.dataStore.data.map { preferences ->
preferences[PrefencesKeys.APP_ENTRY] ?: false
return application.dataStore.data.map { preferences ->
preferences[PreferenceKeys.APP_ENTRY] ?: false
}
}
}

private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = USER_SETTINGS)
private val readOnlyProperty = preferencesDataStore(name = USER_SETTINGS)

val Context.dataStore: DataStore<Preferences> by readOnlyProperty

private object PrefencesKeys {
val APP_ENTRY = booleanPreferencesKey(name = Constants.APP_ENTRY)
private object PreferenceKeys {
val APP_ENTRY = booleanPreferencesKey(Constants.APP_ENTRY)
}
5 changes: 2 additions & 3 deletions app/src/main/java/com/rey/newskatta/data/remote/NewsApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ interface NewsApi {

@GET("everything")
suspend fun getNews(
@Query("page") page: Int,
@Query("sources") sources: String,
@Query("page") page: Int,
@Query("apiKey") apiKey: String = API_KEY
): NewsResponse


@GET("everything")
suspend fun searchNews(
@Query("q") searchQuery: String,
@Query("page") page: Int,
@Query("sources") sources: String,
@Query("page") page: Int,
@Query("apiKey") apiKey: String = API_KEY
): NewsResponse
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import com.rey.newskatta.util.Constants.API_KEY
class NewsPagingSource(
private val newsApi: NewsApi,
private val sources: String
): PagingSource<Int, Article>() {
) : PagingSource<Int, Article>() {

private var totalNewsCount = 0

override fun getRefreshKey(state: PagingState<Int, Article>): Int? {
return state.anchorPosition?.let { anchorPosition ->
Expand All @@ -19,25 +18,25 @@ class NewsPagingSource(
}
}

private var totalNewsCount = 0

override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Article> {
val page = params.key ?: 1

return try {
val newsResponse = newsApi.getNews(sources = sources, page = page)
totalNewsCount += newsResponse.articles.size
val articles = newsResponse.articles.distinctBy { it.title } // removes duplicates
val articles = newsResponse.articles.distinctBy { it.title } //Remove duplicates

LoadResult.Page(
data = articles,
nextKey = if (totalNewsCount == newsResponse.totalResults) null else page + 1,
prevKey = null
)

}catch (e:Exception){
} catch (e: Exception) {
e.printStackTrace()
LoadResult.Error(
throwable = e
)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,37 @@ import com.rey.newskatta.domain.model.Article
import retrofit2.http.Query

class SearchNewsPagingSource(
private val newsApi: NewsApi,
private val api: NewsApi,
private val searchQuery: String,
private val sources: String
): PagingSource<Int, Article>() {

private var totalNewsCount = 0
) : PagingSource<Int, Article>() {

override fun getRefreshKey(state: PagingState<Int, Article>): Int? {
return state.anchorPosition?.let { anchorPosition ->
val anchorPage = state.closestPageToPosition(anchorPosition)
anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1)
return state.anchorPosition?.let { anchorPage ->
val page = state.closestPageToPosition(anchorPage)
page?.nextKey?.minus(1) ?: page?.prevKey?.plus(1)
}
}

private var totalNewsCount = 0

override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Article> {
val page = params.key ?: 1

return try {
val newsResponse = newsApi.searchNews(searchQuery = searchQuery, sources = sources, page = page)
val newsResponse = api.searchNews(searchQuery = searchQuery, sources = sources, page = page)
totalNewsCount += newsResponse.articles.size
val articles = newsResponse.articles.distinctBy { it.title } // removes duplicates
val articles = newsResponse.articles.distinctBy { it.title } //Remove duplicates

LoadResult.Page(
data = articles,
nextKey = if (totalNewsCount == newsResponse.totalResults) null else page + 1,
prevKey = null
)

}catch (e:Exception){
} catch (e: Exception) {
e.printStackTrace()
LoadResult.Error(
throwable = e
)
} }
LoadResult.Error(throwable = e)
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,55 @@ package com.rey.newskatta.data.repository
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.rey.newskatta.data.local.NewsDao
import com.rey.newskatta.data.remote.NewsApi
import com.rey.newskatta.data.remote.NewsPagingSource
import com.rey.newskatta.data.remote.SearchNewsPagingSource
import com.rey.newskatta.domain.model.Article
import com.rey.newskatta.domain.repository.NewsRepository
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

class NewsRepositoryImpl @Inject constructor(
private val newsApi: NewsApi,
private val newsDao: NewsDao
) : NewsRepository {

class NewsRepositoryImpl(
private val newsApi: NewsApi
): NewsRepository {
override fun getNews(sources: List<String>): Flow<PagingData<Article>> {
return Pager(
config = PagingConfig(pageSize = 10),
pagingSourceFactory = {
NewsPagingSource(
newsApi = newsApi,
sources = sources.joinToString(separator = ",")
)
NewsPagingSource(newsApi = newsApi, sources = sources.joinToString(separator = ","))
}
).flow
}

override fun searchNews(seachQuery: String, sources: List<String>): Flow<PagingData<Article>> {
override fun searchNews(searchQuery: String, sources: List<String>): Flow<PagingData<Article>> {
return Pager(
config = PagingConfig(pageSize = 10),
pagingSourceFactory = {
SearchNewsPagingSource(
searchQuery = seachQuery,
newsApi = newsApi,
api = newsApi,
searchQuery = searchQuery,
sources = sources.joinToString(separator = ",")
)
}
).flow
}

override suspend fun upsertArticle(article: Article) {
newsDao.upsert(article)
}

override suspend fun deleteArticle(article: Article) {
newsDao.delete(article)
}

override fun getArticles(): Flow<List<Article>> {
return newsDao.getArticles()
}

override suspend fun getArticle(url: String): Article? {
return newsDao.getArticle(url = url)
}
}
Loading

0 comments on commit 1229e5a

Please sign in to comment.