This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @AndroidEntryPoint | |
| class MainActivity : ComponentActivity() { | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| enableEdgeToEdge() | |
| setContent { | |
| PagingLib3SampleTheme { | |
| val viewModel: NewsViewModel = hiltViewModel() | |
| val context = LocalContext.current |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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). | |
| * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class NewsRepository @Inject constructor( | |
| private val apiService: NewsApiService | |
| ) { | |
| suspend fun fetchNews( | |
| query: String, | |
| nextPage: Long | |
| ): NewsApiResponse { | |
| return try { | |
| apiService.fetchFeed( | |
| q = query, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, |