Skip to content

Commit

Permalink
chore(ci): run ktlint format
Browse files Browse the repository at this point in the history
  • Loading branch information
octera committed Jul 30, 2024
1 parent d99ad18 commit a99590d
Show file tree
Hide file tree
Showing 63 changed files with 726 additions and 795 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ plugins {
ktlint {
android.set(true)
outputColorName.set("RED")
ignoreFailures.set(true)
}

android {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package info.octera.droidstorybox

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
Expand All @@ -21,4 +19,4 @@ class ExampleInstrumentedTest {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.example.newsapp", appContext.packageName)
}
}
}
7 changes: 3 additions & 4 deletions app/src/main/java/info/octera/droidstorybox/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.material3.MaterialTheme
import androidx.compose.ui.Modifier
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import dagger.hilt.android.AndroidEntryPoint
import info.octera.droidstorybox.presentation.navgraph.NavGraph
import info.octera.droidstorybox.ui.theme.NewsAppTheme
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
Expand All @@ -22,17 +22,16 @@ class MainActivity : ComponentActivity() {
super.onCreate(savedInstanceState)
enableEdgeToEdge()


installSplashScreen().apply {
setKeepOnScreenCondition {
viewModel.splashCondition
}
}
actionBar?.hide();
actionBar?.hide()
setContent {
NewsAppTheme {
Box(
modifier = Modifier.background(MaterialTheme.colorScheme.background)
modifier = Modifier.background(MaterialTheme.colorScheme.background),
) {
val startDestination = viewModel.startDestination
NavGraph(startDestination = startDestination)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class MainApplication : Application() {
}
class MainApplication : Application()
41 changes: 21 additions & 20 deletions app/src/main/java/info/octera/droidstorybox/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,36 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import info.octera.droidstorybox.domain.usecases.app_entry.AppEntryUseCases
import info.octera.droidstorybox.presentation.navgraph.Route
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import javax.inject.Inject

@HiltViewModel
class MainViewModel @Inject constructor(
private val appEntryUseCases: AppEntryUseCases
) :
class MainViewModel
@Inject
constructor(
private val appEntryUseCases: AppEntryUseCases,
) :
ViewModel() {
var splashCondition by mutableStateOf(true)
private set

var splashCondition by mutableStateOf(true)
private set

var startDestination by mutableStateOf(Route.AppStartNavigation.route)
private set
var startDestination by mutableStateOf(Route.AppStartNavigation.route)
private set

init {
appEntryUseCases.readAppEntry().onEach { shouldStartFromHomeScreen ->
if (shouldStartFromHomeScreen) {
startDestination = Route.NewsNavigation.route
} else {
startDestination = Route.AppStartNavigation.route
}
delay(300)
splashCondition = false
}.launchIn(viewModelScope)
init {
appEntryUseCases.readAppEntry().onEach { shouldStartFromHomeScreen ->
if (shouldStartFromHomeScreen) {
startDestination = Route.NewsNavigation.route
} else {
startDestination = Route.AppStartNavigation.route
}
delay(300)
splashCondition = false
}.launchIn(viewModelScope)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import androidx.room.Query
import info.octera.droidstorybox.domain.model.Article
import kotlinx.coroutines.flow.Flow


@Dao
interface NewsDao {

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun upsert(article: Article)

Expand All @@ -23,6 +21,4 @@ interface NewsDao {

@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 @@ -5,10 +5,8 @@ import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import info.octera.droidstorybox.domain.model.Article

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

abstract val newsDao: NewsDao

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@ import androidx.room.ProvidedTypeConverter
import androidx.room.TypeConverter
import info.octera.droidstorybox.domain.model.Source


@ProvidedTypeConverter
class NewsTypeConverter {


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

@TypeConverter
fun stringToSource(source: String): Source{
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
Expand Up @@ -12,7 +12,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

class LocalUserManagerImpl(
private val context: Context
private val context: Context,
) : LocalUserManager {
override suspend fun saveAppEntry() {
context.dataStore.edit { setting ->
Expand All @@ -26,8 +26,9 @@ class LocalUserManagerImpl(
}
}
}
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = Constants.USER_SETTINGS)

private object PreferencesKeys {
val APP_ENTRY = booleanPreferencesKey(name = Constants.APP_ENTRY)
}
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = Constants.USER_SETTINGS)

private object PreferencesKeys {
val APP_ENTRY = booleanPreferencesKey(name = Constants.APP_ENTRY)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ import retrofit2.http.GET
import retrofit2.http.Query

interface NewsApi {

@GET("everything")
suspend fun getNews(
@Query("page") page: Int,
@Query("sources") sources: String,
@Query("apiKey") apiKey: String = API_KEY
@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("apiKey") apiKey: String = API_KEY
@Query("apiKey") apiKey: String = API_KEY,
): NewsResponse
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import info.octera.droidstorybox.domain.model.Article

class NewsPagingSource(
private val newsApi: NewsApi,
private val sources: String
private val sources: String,
) : PagingSource<Int, Article>() {
private var totalNewsCount = 0

Expand All @@ -15,33 +15,27 @@ class NewsPagingSource(
return try {
val newsResponse = newsApi.getNews(sources = sources, page = page)
totalNewsCount += newsResponse.articles.size
val articles = newsResponse.articles.distinctBy {
it.title
}
val articles =
newsResponse.articles.distinctBy {
it.title
}
LoadResult.Page(
data = articles,
prevKey = if (page == 1) null else page - 1,
nextKey = if (totalNewsCount >= newsResponse.totalResults) null else page + 1
nextKey = if (totalNewsCount >= newsResponse.totalResults) null else page + 1,
)
} catch (e: Exception) {
e.printStackTrace()
LoadResult.Error(
throwable = e
throwable = e,
)
}


}

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)
}




}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ import info.octera.droidstorybox.domain.model.Article
class SearchNewsPagingSource(
private val searchQuery: String,
private val newsApi: NewsApi,
private val sources: String
): PagingSource<Int, Article>() {

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


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)
Expand All @@ -24,21 +21,22 @@ class SearchNewsPagingSource(
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 = newsApi.searchNews(searchQuery = searchQuery, sources = sources, page = page)
totalNewsCount += newsResponse.articles.size
val articles = newsResponse.articles.distinctBy {
it.title
}
val articles =
newsResponse.articles.distinctBy {
it.title
}
LoadResult.Page(
data = articles,
prevKey = if (page == 1) null else page - 1,
nextKey = if (totalNewsCount >= newsResponse.totalResults) null else page + 1
nextKey = if (totalNewsCount >= newsResponse.totalResults) null else page + 1,
)
} catch (e: Exception) {
e.printStackTrace()
LoadResult.Error(
throwable = e
throwable = e,
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import info.octera.droidstorybox.domain.model.Article
data class NewsResponse(
val articles: List<Article>,
val status: String,
val totalResults: Int
)
val totalResults: Int,
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,35 @@ import kotlinx.coroutines.flow.Flow

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

override fun searchNews(searchQuery: 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 = searchQuery,
newsApi = newsApi,
sources = sources.joinToString(",")
sources = sources.joinToString(","),
)
}
},
).flow
}

}

override suspend fun upsertArticle(article: Article) {
newsDao.upsert(article)
Expand All @@ -56,4 +58,4 @@ class NewsRepositoryImpl(
override suspend fun getArticle(url: String): Article? {
return newsDao.getArticle(url = url)
}
}
}
Loading

0 comments on commit a99590d

Please sign in to comment.