Skip to content

Commit

Permalink
Merge pull request #34 from mkaomwakuni/Bookmarks-Integration
Browse files Browse the repository at this point in the history
Feature Bookmarks
  • Loading branch information
mkaomwakuni authored Jul 5, 2024
2 parents 873ba36 + 8aadd8a commit 51aee5c
Show file tree
Hide file tree
Showing 22 changed files with 447 additions and 283 deletions.
6 changes: 4 additions & 2 deletions app/src/main/java/dev/mkao/weaver/data/remote/NewsApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ import retrofit2.http.Query

interface NewsApi {
//base url for the news api
//https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey=2d8537ffd9a94e55b63b570ff2674a3a
//https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey={}
@GET("top-headlines")
suspend fun getTopHeadlines(
@Query("category") category: String,
@Query ("country") country : String = "us",
@Query("apikey") apiKey: String = API_KEY,

): NewsApiResponse

@GET("everything")
suspend fun searchRequest(
@Query("q") query: String,
@Query("apiKey") apiKey: String = API_KEY
): NewsApiResponse

companion object{
const val API_KEY = "2d8537ffd9a94e55b63b570ff2674a3a"
const val API_KEY = "ADD YOUR API KEY HERE"
const val BASE_URL = "https://newsapi.org/v2/"
}
}
6 changes: 4 additions & 2 deletions app/src/main/java/dev/mkao/weaver/data/remote/NewsDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ interface NewsDao {
suspend fun upsert(article: Article)

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

@Delete
suspend fun delete(article: Article)

}
@Query("SELECT * FROM Articles WHERE isBookedMarked = 1")
suspend fun getBookedArticles(): List<Article>
}
19 changes: 2 additions & 17 deletions app/src/main/java/dev/mkao/weaver/data/remote/NewsDatabase.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
package dev.mkao.weaver.data.remote

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import dev.mkao.weaver.domain.model.Article

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

companion object {
@Volatile private var instance: NewsDatabase? = null

fun getDatabase(context: Context): NewsDatabase =
instance ?: synchronized(this) {
instance ?: Room.databaseBuilder(
context.applicationContext,
NewsDatabase::class.java,
"articles"
).build().also { instance = it }
}
}
abstract class NewsDatabase : RoomDatabase() {
abstract fun articleDao(): NewsDao
}
47 changes: 36 additions & 11 deletions app/src/main/java/dev/mkao/weaver/data/repository/RepositoryImpl.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
package dev.mkao.weaver.data.repository

import android.util.Log
import dev.mkao.weaver.data.remote.NewsApi
import dev.mkao.weaver.data.remote.NewsDao
import dev.mkao.weaver.domain.model.Article
import dev.mkao.weaver.domain.repository.Repository
import dev.mkao.weaver.util.Assets
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext


class RepositoryImpl(
private val newsApi: NewsApi,
private val newsDao: NewsDao
): Repository {

override suspend fun getTopHeadlines(category: String): Assets<List<Article>> {
return try {
val response = newsApi.getTopHeadlines(category = category)
val articles = response.articles
//cache the articles
articles.forEach { newsDao.upsert(it) }

Assets.Success(filterRemovedArticles(articles))
} catch (e: Exception) {
Assets.Error(message = "404! Api Error")
//Retrieve articles from DB if API Error
val articles = newsDao.getArticles()
Assets.Success(filterRemovedArticles(articles))
}
}

Expand All @@ -28,6 +35,8 @@ class RepositoryImpl(
// Use the newsApi to fetch search results using the provided query
val response = newsApi.searchRequest(query = query)
val articles = response.articles
//Cache the searched articles
articles.forEach { newsDao.upsert(it) }

// Return the search results as Success after filtering
Assets.Success(filterRemovedArticles(articles))
Expand All @@ -37,23 +46,39 @@ class RepositoryImpl(
}
}

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

override suspend fun deleteArticle(article: Article) {
newsDao.delete(article)
val unBookedMarked = article.copy(isBookedMarked = false)
newsDao.upsert(unBookedMarked) // Update instead of delete
Log.d("RepositoryImpl", "Article deleted: ${article.title}")
}


override suspend fun getArticle(url: String): Article? {
TODO("Not yet implemented")
override suspend fun getArticles(): Assets<List<Article>> {
return try {
withContext(Dispatchers.IO) {
val articles = newsDao.getArticles()
Assets.Success(articles)
}
} catch (e: Exception) {
Assets.Error(message = "Error")
}
}

override suspend fun insertedArticle(article: Article) {
val bookedMarked = article.copy(isBookedMarked = true)
newsDao.upsert(bookedMarked)
Log.d("RepositoryImpl", "Article inserted/updated: ${article.title}")
}
override fun getArticles(): Assets<List<Article>> {


override suspend fun getBookedArticles(): Assets<List<Article>> {
return try {
val articles = newsDao.getArticles()
val articles = newsDao.getBookedArticles()
Log.d("RepositoryImpl", "Retrieved ${articles.size} bookmarked articles")
Assets.Success(articles)
} catch (e: Exception) {
} catch (e:Exception){
Log.e("RepositoryImpl", "Error retrieving bookmarked articles", e)
Assets.Error(message = "Error")
}
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/dev/mkao/weaver/domain/model/Article.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import kotlinx.android.parcel.Parcelize
@Parcelize
@Entity(tableName = "Articles")
data class Article(
@PrimaryKey val url: String,
val source: Source,
val author: String?,
val title: String,
val content: String?,
val description: String?,
@PrimaryKey val url: String,
var isBookedMarked: Boolean = false,
val urlToImage: String?,
val publishedAt: String,
): Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ interface Repository {

suspend fun searchRequest(query: String): Assets<List<Article>>

suspend fun upsertArticle(article: Article)

suspend fun deleteArticle(article: Article)

fun getArticles(): Assets<List<Article>>
suspend fun getArticles(): Assets<List<Article>>

suspend fun insertedArticle(article: Article)

suspend fun getArticle(url: String): Article?
suspend fun getBookedArticles(): Assets<List<Article>>
}
3 changes: 3 additions & 0 deletions app/src/main/java/dev/mkao/weaver/presentation/About/About.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
Expand All @@ -34,6 +36,7 @@ fun About() {
Box(
modifier = Modifier
.fillMaxWidth()
.clip(RoundedCornerShape(bottomStart = 10.dp, bottomEnd = 10.dp))
.height(260.dp)
) {
Image(
Expand Down
Loading

0 comments on commit 51aee5c

Please sign in to comment.