-
Notifications
You must be signed in to change notification settings - Fork 27
Mani: QuotifyApp - Remote call #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
e8604a5
5610a3e
f7963cd
8e7d1d2
03d7158
627d6ec
8658fcc
721faa2
da5aa16
67800aa
2a8e134
62d5604
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.mani.quotify007.data.remote | ||
|
|
||
| import com.mani.quotify007.domain.model.QuoteResult | ||
| import retrofit2.http.GET | ||
|
|
||
| interface QuoteApiService { | ||
| @GET("quotes") | ||
| suspend fun getQuoteResult(): QuoteResult | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package com.mani.quotify007.data.remote | ||
|
|
||
| import com.mani.quotify007.QuotifyApp | ||
| import com.mani.quotify007.R | ||
| import okhttp3.OkHttpClient | ||
| import java.security.KeyStore | ||
| import javax.net.ssl.SSLContext | ||
| import javax.net.ssl.TrustManagerFactory | ||
| import javax.net.ssl.X509TrustManager | ||
|
|
||
| fun getSafeOkHttpClient(quotifyApp: QuotifyApp): OkHttpClient { | ||
| return try { | ||
| // Load the trusted certificate | ||
| val keyStore = KeyStore.getInstance(KeyStore.getDefaultType()).apply { | ||
| load(null, null) | ||
| // quotable io trusted certificate | ||
| quotifyApp.resources.openRawResource(R.raw.api_quotable_io).use { certInputStream -> | ||
| val certificateFactory = java.security.cert.CertificateFactory.getInstance("X.509") | ||
| val certificate = certificateFactory.generateCertificate(certInputStream) | ||
| setCertificateEntry("api_quotable_io", certificate) | ||
| } | ||
| } | ||
|
|
||
| // Create a TrustManager that trusts the CAs in our KeyStore | ||
| val trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()).apply { | ||
| init(keyStore) | ||
| } | ||
| val trustManagers = trustManagerFactory.trustManagers | ||
| val sslContext = SSLContext.getInstance("TLS").apply { | ||
| init(null, trustManagers, java.security.SecureRandom()) | ||
| } | ||
| val sslSocketFactory = sslContext.socketFactory | ||
|
|
||
| OkHttpClient.Builder() | ||
| .sslSocketFactory(sslSocketFactory, trustManagers[0] as X509TrustManager) | ||
| .build() | ||
| } catch (e: Exception) { | ||
| throw RuntimeException(e) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,73 +1,43 @@ | ||
| package com.mani.quotify007.data.repository | ||
|
|
||
| import com.mani.quotify007.data.local.FavoriteQuoteDao | ||
| import com.mani.quotify007.QuotifyApp | ||
| import com.mani.quotify007.data.local.FavoriteQuoteEntity | ||
| import com.mani.quotify007.domain.model.Quote | ||
| import com.mani.quotify007.domain.model.QuoteResult | ||
| import com.mani.quotify007.domain.repository.QuoteRepository | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.map | ||
|
|
||
| class QuoteRepositoryImpl(private val favoriteQuoteDao: FavoriteQuoteDao) : QuoteRepository { | ||
| private val quotes = mutableListOf( | ||
| Quote( | ||
| 1, | ||
| "The greatest glory in living lies not in never falling, " + | ||
| "but in rising every time we fall.", | ||
| "Nelson Mandela" | ||
| ), | ||
| Quote( | ||
| 2, | ||
| "The way to get started is to quit talking and begin doing.", | ||
| "Walt Disney" | ||
| ), | ||
| Quote( | ||
| 3, | ||
| "Your time is limited, so don't waste it living someone else's life.", | ||
| "Steve Jobs" | ||
| ), | ||
| Quote( | ||
| 4, | ||
| "If life were predictable it would cease to be life, and be without flavor.", | ||
| "Eleanor Roosevelt" | ||
| ), | ||
| Quote( | ||
| 5, | ||
| "If you look at what you have in life, you'll always have more. " + | ||
| "If you look at what you don't have in life, you'll never have enough.", | ||
| "Oprah Winfrey" | ||
| ), | ||
| Quote( | ||
| 6, | ||
| "If you set your goals ridiculously high and it's a failure, " + | ||
| "you will fail above everyone else's success.", | ||
| "James Cameron" | ||
| ) | ||
| ) | ||
| class QuoteRepositoryImpl(private val application: QuotifyApp) : QuoteRepository { | ||
|
|
||
| override fun getQuotes(): List<Quote> = quotes | ||
| override suspend fun getQuoteResult(): QuoteResult = application.api.getQuoteResult() | ||
|
|
||
| override fun getQuotes(): List<Quote> = quotesDataList | ||
|
|
||
| //TODO: Future implementation | ||
| override fun addQuote(quote: Quote) { | ||
| quotes.add(quote) | ||
| quotesDataList.add(quote) | ||
| } | ||
|
|
||
| //TODO: Future implementation | ||
| override fun removeQuote(quote: Quote) { | ||
| quotes.remove(quote) | ||
| quotesDataList.remove(quote) | ||
| } | ||
|
|
||
| override fun getFavoriteQuotes(): Flow<List<Quote>> = | ||
| favoriteQuoteDao.getAllFavoriteQuotes().map {entities -> | ||
| application.quoteDb.favoriteQuoteDao().getAllFavoriteQuotes().map {entities -> | ||
| entities.map { entity -> entity.toDomainModel() } | ||
| } | ||
|
|
||
| override suspend fun addFavoriteQuote(quote: Quote) { | ||
| favoriteQuoteDao.insertFavoriteQuote(quote.toEntity()) | ||
| application.quoteDb.favoriteQuoteDao().insertFavoriteQuote(quote.toEntity()) | ||
| } | ||
|
|
||
| override suspend fun removeFavoriteQuote(quote: Quote) { | ||
| favoriteQuoteDao.deleteFavoriteQuote(quote.toEntity()) | ||
| application.quoteDb.favoriteQuoteDao().deleteFavoriteQuote(quote.toEntity()) | ||
| } | ||
| } | ||
|
|
||
| private fun Quote.toEntity() = FavoriteQuoteEntity(id = id, text = text, author = author ?: "") | ||
| private fun Quote.toEntity() = FavoriteQuoteEntity(id = _id, content = content, author = author) | ||
|
|
||
| private fun FavoriteQuoteEntity.toDomainModel() = Quote(id, text, author) | ||
| private fun FavoriteQuoteEntity.toDomainModel() = Quote(id, content = content, author = author) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package com.mani.quotify007.data.repository | ||
|
|
||
| import com.mani.quotify007.domain.model.Quote | ||
|
|
||
| val quotesDataList = mutableListOf( | ||
|
||
| Quote( | ||
| _id = "111", | ||
| content = "The greatest glory in living lies not in never falling, " + | ||
| "but in rising every time we fall.", | ||
| author = "Nelson Mandela" | ||
| ), | ||
| Quote( | ||
| _id = "222", | ||
| content = "The way to get started is to quit talking and begin doing.", | ||
| author = "Walt Disney" | ||
| ), | ||
| Quote( | ||
| _id = "333", | ||
| content = "Your time is limited, so don't waste it living someone else's life.", | ||
| author = "Steve Jobs" | ||
| ), | ||
| Quote( | ||
| _id = "444", | ||
| content = "If life were predictable it would cease to be life, and be without flavor.", | ||
| author = "Eleanor Roosevelt" | ||
| ), | ||
| Quote( | ||
| _id = "555", | ||
| content = "If you look at what you have in life, you'll always have more. " + | ||
| "If you look at what you don't have in life, you'll never have enough.", | ||
| author = "Oprah Winfrey" | ||
| ), | ||
| Quote( | ||
| _id = "666", | ||
| content = "If you set your goals ridiculously high and it's a failure, " + | ||
| "you will fail above everyone else's success.", | ||
| author = "James Cameron" | ||
| ) | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,13 @@ | ||
| package com.mani.quotify007.domain.model | ||
|
|
||
| data class Quote( | ||
| val id: Int, | ||
| val text: String, | ||
| val author: String? = null, | ||
| val _id: String, | ||
| val author: String, | ||
| val authorSlug: String? = null, | ||
| val content: String, | ||
| val dateAdded: String? = null, | ||
| val dateModified: String? = null, | ||
| val length: Int? = null, | ||
| val tags: List<String>? = null, | ||
| var isFavorite: Boolean = false | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.mani.quotify007.domain.model | ||
|
|
||
| data class QuoteResult( | ||
| val count: Int, | ||
| val lastItemIndex: Int, | ||
| val page: Int, | ||
| val results: List<Quote>, | ||
| val totalCount: Int, | ||
| val totalPages: Int | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,11 @@ | ||
| package com.mani.quotify007.domain.usecase | ||
|
|
||
| import com.mani.quotify007.domain.model.Quote | ||
| import com.mani.quotify007.domain.model.QuoteResult | ||
| import com.mani.quotify007.domain.repository.QuoteRepository | ||
|
|
||
| class GetQuoteUseCase(private val repository: QuoteRepository) { | ||
| suspend fun result(): QuoteResult = repository.getQuoteResult() | ||
| // TODO: Use when static quote data used | ||
| fun execute(): List<Quote> = repository.getQuotes() | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ import com.mani.quotify007.ui.navigation.MainScreen | |
| import com.mani.quotify007.ui.navigation.viewmodel.MainViewModel | ||
| import com.mani.quotify007.ui.screens.utils.onCopyText | ||
| import com.mani.quotify007.ui.screens.utils.shareQuote | ||
| import com.mani.quotify007.ui.screens.utils.showToast | ||
| import com.mani.quotify007.ui.theme.QuotifyAppTheme | ||
|
|
||
| class MainActivity : ComponentActivity() { | ||
|
|
@@ -23,11 +24,18 @@ class MainActivity : ComponentActivity() { | |
| val state = viewModel.state.collectAsState().value | ||
| MainScreen( | ||
| state, | ||
| onEvent = { event -> viewModel.onEvent(event) }, | ||
| onCopyText = { quote -> onCopyText(this, quote) }, | ||
| onShareClick = { quote -> shareQuote(this, quote) } | ||
| onEvent = { event -> viewModel.onEvent(event) } | ||
| ) | ||
| } | ||
| } | ||
| viewModel.copyTextEvent.observe(this) { quote -> | ||
| onCopyText(this, quote) | ||
| } | ||
| viewModel.shareClickEvent.observe(this) { quote -> | ||
| shareQuote(this, quote) | ||
| } | ||
| viewModel.showToast.observe(this) { message -> | ||
| message?.let { showToast(it) } | ||
| } | ||
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we use kotlin serialization?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved