Skip to content

Instantly share code, notes, and snippets.

View anitaa1990's full-sized avatar
🎯
Focusing

Anitaa Murthy anitaa1990

🎯
Focusing
  • Chennai
View GitHub Profile
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.jetbrains.kotlin.android)
alias(libs.plugins.compose.compiler)
alias(libs.plugins.ksp)
alias(libs.plugins.hilt)
}
android {
namespace = "com.an.notesapp"
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.jetbrains.kotlin.android) apply false
alias(libs.plugins.compose.compiler) apply false
alias(libs.plugins.ksp) apply false
alias(libs.plugins.hilt) apply false
}
[versions]
agp = "8.7.2"
kotlin = "2.0.21"
coreKtx = "1.15.0"
junit = "4.13.2"
junitVersion = "1.2.1"
espressoCore = "3.6.1"
lifecycleRuntimeKtx = "2.8.7"
activityCompose = "1.9.3"
composeBom = "2024.11.00"
/**
* Unit tests for the NewsDataSource class to ensure correct behavior
* when loading paginated data from the repository.
*/
class NewsDataSourceTest {
// Mocked repository to simulate API responses.
private val repository: NewsRepository = mock()
private val query = "movies" // Query used for testing.
private val pageSize = 20 // Number of items per page.
@Composable
fun HomeScreen(
viewModel: NewsViewModel,
onItemClicked: (url: String) -> Unit,
onShareButtonClicked: (url: String) -> Unit
) {
val news = viewModel.news.collectAsLazyPagingItems()
val inputText = viewModel.inputText.collectAsState()
val searchWidgetState by viewModel.searchWidgetState
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
PagingLib3SampleTheme {
val viewModel: NewsViewModel = hiltViewModel()
val context = LocalContext.current
/**
* ViewModel for managing UI state and business logic related to fetching and displaying paginated news.
* Uses Paging 3 library and Compose-friendly state management.
*/
@HiltViewModel
class NewsViewModel @Inject constructor(
private val repository: NewsRepository // Injected repository for fetching news data.
) : ViewModel() {
// Mutable state flow for managing the search input text.
private val _inputText: MutableStateFlow<String> = MutableStateFlow("movies") // Default query is "movies".
class NewsDataSource @Inject constructor (
private val repository: NewsRepository,
private val query: String
): PagingSource<Long, Article>() {
/**
* Returns the key for the next page to be loaded when refreshing.
*
* This method is used by the Paging library to determine the starting point
* for loading data when the user performs a refresh action (e.g., swipe-to-refresh).
*
class NewsRepository @Inject constructor(
private val apiService: NewsApiService
) {
suspend fun fetchNews(
query: String,
nextPage: Long
): NewsApiResponse {
return try {
apiService.fetchFeed(
q = query,
interface NewsApiService {
/**
* We would be using the below url:
* https://newsapi.org/v2/everything?q={query}&apiKey={api}&pageSize={pageSize}&page={page}
* It has four query parameters: query, apiKey, page & pageSize
*/
@GET("/v2/everything")
suspend fun fetchFeed(
@Query("q") q: String,
@Query("apiKey") apiKey: String,