Skip to content

Commit

Permalink
Change package name of util and api classes
Browse files Browse the repository at this point in the history
Also add doc comments

Signed-off-by: starry-shivam <[email protected]>
  • Loading branch information
starry-shivam committed Apr 21, 2024
1 parent f84c962 commit c4137e8
Show file tree
Hide file tree
Showing 43 changed files with 177 additions and 115 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/starry/myne/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.ViewModelProvider
import com.starry.myne.helpers.NetworkObserver
import com.starry.myne.ui.screens.main.MainScreen
import com.starry.myne.ui.screens.settings.viewmodels.SettingsViewModel
import com.starry.myne.ui.theme.MyneTheme
import com.starry.myne.utils.NetworkObserver
import dagger.hilt.android.AndroidEntryPoint


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,19 @@
* limitations under the License.
*/

package com.starry.myne.repo
package com.starry.myne.api

import android.content.Context
import com.google.gson.Gson
import com.starry.myne.BuildConfig
import com.starry.myne.repo.models.BookSet
import com.starry.myne.repo.models.ExtraInfo
import com.starry.myne.utils.book.BookLanguage
import com.starry.myne.api.models.BookSet
import com.starry.myne.api.models.ExtraInfo
import com.starry.myne.helpers.book.BookLanguage
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.Cache
import okhttp3.CacheControl
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
Expand All @@ -42,30 +40,17 @@ import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

class CacheInterceptor : Interceptor {

companion object {
const val CACHE_SIZE = 32L * 1024L * 1024L // 32 MiB
private const val CACHE_MAX_AGE = 10 // 10 days
}

override fun intercept(chain: Interceptor.Chain): Response {
val response: Response = chain.proceed(chain.request())
val cacheControl = CacheControl.Builder()
.maxAge(CACHE_MAX_AGE, TimeUnit.DAYS)
.build()
return response.newBuilder()
.header("Cache-Control", cacheControl.toString())
.build()
}
}

class BookRepository(context: Context) {
/**
* This class is responsible for handling all the API requests related to books.
* It uses OkHttp for making network requests and Gson for parsing JSON responses.
* @param context The context of the application.
*/
class BookAPI(context: Context) {

private val baseApiUrl = "https://myne.pooloftears.xyz/books"
private val googleBooksUrl = "https://www.googleapis.com/books/v1/volumes"

@Suppress("USELESS_ELVIS")
private val googleApiKey =
BuildConfig.GOOGLE_API_KEY ?: "AIzaSyBCaXx-U0sbEpGVPWylSggC4RaR4gCGkVE" // Backup API key

Expand All @@ -77,9 +62,15 @@ class BookRepository(context: Context) {
.addNetworkInterceptor(CacheInterceptor())
.build()

private val gsonClient = Gson()
private val gsonClient = Gson() // Gson client for parsing JSON responses.


/**
* This function fetches all the books from the API.
* @param page The page number of the books to fetch.
* @param bookLanguage The language of the books to fetch.
* @return A Result object containing the BookSet if the request was successful, or an exception if it failed.
*/
suspend fun getAllBooks(
page: Long,
bookLanguage: BookLanguage = BookLanguage.AllBooks
Expand All @@ -92,6 +83,11 @@ class BookRepository(context: Context) {
return makeApiRequest(request)
}

/**
* This function searches for books based on the query provided.
* @param query The query to search for.
* @return A Result object containing the BookSet if the request was successful, or an exception if it failed.
*/
suspend fun searchBooks(query: String): Result<BookSet> {
val encodedString = withContext(Dispatchers.IO) {
URLEncoder.encode(query, "UTF-8")
Expand All @@ -100,11 +96,23 @@ class BookRepository(context: Context) {
return makeApiRequest(request)
}

/**
* This function fetches a book by its ID.
* @param bookId The ID of the book to fetch.
* @return A Result object containing the BookSet if the request was successful, or an exception if it failed.
*/
suspend fun getBookById(bookId: String): Result<BookSet> {
val request = Request.Builder().get().url("${baseApiUrl}?ids=$bookId").build()
return makeApiRequest(request)
}

/**
* This function fetches books by category.
* @param category The category of the books to fetch.
* @param page The page number of the books to fetch.
* @param bookLanguage The language of the books to fetch.
* @return A Result object containing the BookSet if the request was successful, or an exception if it failed.
*/
suspend fun getBooksByCategory(
category: String,
page: Long,
Expand All @@ -118,6 +126,7 @@ class BookRepository(context: Context) {
return makeApiRequest(request)
}

// Helper function to make API requests.
private suspend fun makeApiRequest(request: Request): Result<BookSet> =
suspendCoroutine { continuation ->
okHttpClient.newCall(request).enqueue(object : Callback {
Expand All @@ -137,6 +146,8 @@ class BookRepository(context: Context) {
})
}

// Function to fetch extra info such as cover image, page count, and description of a book.
// From Google Books API.
suspend fun getExtraInfo(bookName: String): ExtraInfo? = suspendCoroutine { continuation ->
val encodedName = URLEncoder.encode(bookName, "UTF-8")
val url = "${googleBooksUrl}?q=$encodedName&startIndex=0&maxResults=1&key=$googleApiKey"
Expand All @@ -155,7 +166,8 @@ class BookRepository(context: Context) {
})
}

fun parseExtraInfoJson(jsonString: String): ExtraInfo? {
// Helper function to parse extra info JSON.
private fun parseExtraInfoJson(jsonString: String): ExtraInfo? {
return try {
val jsonObj = JSONObject(jsonString)
val totalItems = jsonObj.getInt("totalItems")
Expand Down
50 changes: 50 additions & 0 deletions app/src/main/java/com/starry/myne/api/CacheInterceptor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright (c) [2022 - Present] Stɑrry Shivɑm
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


package com.starry.myne.api

import com.starry.myne.api.CacheInterceptor.Companion.CACHE_MAX_AGE
import com.starry.myne.api.CacheInterceptor.Companion.CACHE_SIZE
import okhttp3.CacheControl
import okhttp3.Interceptor
import okhttp3.Response
import java.util.concurrent.TimeUnit

/**
* Interceptor to cache the response for a week to
* reduce the number of network calls.
*
* @property CACHE_SIZE 32 MiB
* @property CACHE_MAX_AGE 1 week
*/
class CacheInterceptor : Interceptor {

companion object {
const val CACHE_SIZE = 32L * 1024L * 1024L // 32 MiB
const val CACHE_MAX_AGE = 7 // 1 week
}

override fun intercept(chain: Interceptor.Chain): Response {
val response: Response = chain.proceed(chain.request())
val cacheControl = CacheControl.Builder()
.maxAge(CACHE_MAX_AGE, TimeUnit.DAYS)
.build()
return response.newBuilder()
.header("Cache-Control", cacheControl.toString())
.build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.starry.myne.repo.models
package com.starry.myne.api.models


import androidx.annotation.Keep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.starry.myne.repo.models
package com.starry.myne.api.models


import androidx.annotation.Keep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.starry.myne.repo.models
package com.starry.myne.api.models


import androidx.annotation.Keep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.starry.myne.repo.models
package com.starry.myne.api.models

import androidx.annotation.Keep

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.starry.myne.repo.models
package com.starry.myne.api.models


import androidx.annotation.Keep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.starry.myne.repo.models
package com.starry.myne.api.models


import androidx.annotation.Keep
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/starry/myne/database/MyneDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.starry.myne.database.library.LibraryDao
import com.starry.myne.database.library.LibraryItem
import com.starry.myne.database.reader.ReaderDao
import com.starry.myne.database.reader.ReaderData
import com.starry.myne.utils.Constants
import com.starry.myne.helpers.Constants

@Database(
entities = [LibraryItem::class, ReaderData::class],
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/starry/myne/di/MainModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
package com.starry.myne.di

import android.content.Context
import com.starry.myne.api.BookAPI
import com.starry.myne.database.MyneDatabase
import com.starry.myne.epub.EpubParser
import com.starry.myne.repo.BookRepository
import com.starry.myne.helpers.PreferenceUtil
import com.starry.myne.helpers.book.BookDownloader
import com.starry.myne.ui.screens.welcome.viewmodels.WelcomeDataStore
import com.starry.myne.utils.PreferenceUtil
import com.starry.myne.utils.book.BookDownloader
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand Down Expand Up @@ -52,7 +52,7 @@ class MainModule {

@Singleton
@Provides
fun provideBooksApi(@ApplicationContext context: Context) = BookRepository(context)
fun provideBooksApi(@ApplicationContext context: Context) = BookAPI(context)

@Singleton
@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.starry.myne.utils
package com.starry.myne.helpers

object Constants {
// Database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.starry.myne.utils
package com.starry.myne.helpers

import android.content.Context
import android.content.ContextWrapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.starry.myne.utils
package com.starry.myne.helpers

import android.content.Context
import android.net.ConnectivityManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.starry.myne.utils
package com.starry.myne.helpers

/**
* A class that helps in paginating data from a remote source.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.starry.myne.utils
package com.starry.myne.helpers

import android.content.Context
import android.content.SharedPreferences
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/


package com.starry.myne.utils
package com.starry.myne.helpers

import android.content.ActivityNotFoundException
import android.content.Context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
* limitations under the License.
*/

package com.starry.myne.utils.book
package com.starry.myne.helpers.book

import android.annotation.SuppressLint
import android.app.DownloadManager
import android.content.Context
import android.database.Cursor
import android.net.Uri
import android.util.Log
import com.starry.myne.repo.models.Book
import com.starry.myne.api.models.Book
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.starry.myne.utils.book
package com.starry.myne.helpers.book

import androidx.annotation.Keep

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/

package com.starry.myne.utils.book
package com.starry.myne.helpers.book

import com.starry.myne.repo.models.Author
import com.starry.myne.api.models.Author
import java.util.Locale

object BookUtils {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.starry.myne.helpers.book.BookLanguage
import com.starry.myne.ui.theme.figeronaFont
import com.starry.myne.utils.book.BookLanguage

@Composable
fun BookLanguageButton(language: BookLanguage, isSelected: Boolean, onClick: () -> Unit) {
Expand Down
Loading

0 comments on commit c4137e8

Please sign in to comment.