Skip to content

Commit

Permalink
Merge pull request #1 from bodybuilders-team/caching
Browse files Browse the repository at this point in the history
Caching implemented
  • Loading branch information
Nyckoka authored May 23, 2024
2 parents 767d324 + ff742a8 commit f211959
Show file tree
Hide file tree
Showing 117 changed files with 2,216 additions and 1,431 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class MemDataSource {
"Farmácia São João",
Location(38.736946, -9.133621),
creatorId = 0L,
pictureUrl = "https://www.indice.eu/img/farmacias/farmacia-estacio-370.jpg",
pictureUrl = "https://www.jf-penhafranca.pt/templates/yootheme/cache/54/farmacia-alves-da-graca-54cca7d1.jpeg",
medicines = mutableListOf(
MedicineStock(medicines[0L]!!, 100),
MedicineStock(medicines[1L]!!, 50),
Expand All @@ -186,7 +186,7 @@ class MemDataSource {
"Farmácia do Chiado",
Location(38.7106, -9.1401),
creatorId = 0L,
pictureUrl = "https://www.indice.eu/img/farmacias/farmacia-estacio-370.jpg",
pictureUrl = "https://www.jf-penhafranca.pt/templates/yootheme/cache/54/farmacia-alves-da-graca-54cca7d1.jpeg",
medicines = mutableListOf(
MedicineStock(medicines[3L]!!, 100),
MedicineStock(medicines[4L]!!, 50),
Expand All @@ -198,7 +198,7 @@ class MemDataSource {
"Farmácia do Rossio",
Location(38.7149, -9.1394),
creatorId = 0L,
pictureUrl = "https://www.indice.eu/img/farmacias/farmacia-estacio-370.jpg",
pictureUrl = "https://www.jf-penhafranca.pt/templates/yootheme/cache/54/farmacia-alves-da-graca-54cca7d1.jpeg",
medicines = mutableListOf(
MedicineStock(medicines[0L]!!, 100),
MedicineStock(medicines[4L]!!, 50),
Expand All @@ -210,7 +210,7 @@ class MemDataSource {
"Farmácia do Restelo",
Location(38.7014, -9.2094),
creatorId = 0L,
pictureUrl = "https://www.indice.eu/img/farmacias/farmacia-estacio-370.jpg",
pictureUrl = "https://www.jf-penhafranca.pt/templates/yootheme/cache/54/farmacia-alves-da-graca-54cca7d1.jpeg",
medicines = mutableListOf(
MedicineStock(medicines[3L]!!, 100),
MedicineStock(medicines[1L]!!, 50),
Expand All @@ -222,21 +222,21 @@ class MemDataSource {
"Farmácia do Areeiro",
Location(38.7425, -9.1321),
creatorId = 0L,
pictureUrl = "https://www.indice.eu/img/farmacias/farmacia-estacio-370.jpg"
pictureUrl = "https://www.jf-penhafranca.pt/templates/yootheme/cache/54/farmacia-alves-da-graca-54cca7d1.jpeg"
),
5L to Pharmacy(
5,
"Farmácia do Lumiar",
Location(38.7706, -9.1601),
creatorId = 0L,
pictureUrl = "https://www.indice.eu/img/farmacias/farmacia-estacio-370.jpg"
pictureUrl = "https://www.jf-penhafranca.pt/templates/yootheme/cache/54/farmacia-alves-da-graca-54cca7d1.jpeg"
),
6L to Pharmacy(
6,
"Farmácia do Parque",
Location(38.7606, -9.1501),
creatorId = 0L,
pictureUrl = "https://www.indice.eu/img/farmacias/farmacia-estacio-370.jpg"
pictureUrl = "https://www.jf-penhafranca.pt/templates/yootheme/cache/54/farmacia-alves-da-graca-54cca7d1.jpeg"
)
)
pharmaciesCounter.set(pharmacies.size.toLong())
Expand Down
24 changes: 19 additions & 5 deletions src/frontend/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
plugins {
alias(libs.plugins.androidApplication)
alias(libs.plugins.jetbrainsKotlinAndroid)
id("com.google.dagger.hilt.android")
id("com.google.devtools.ksp")
}

android {
Expand Down Expand Up @@ -30,17 +32,17 @@ android {
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_19
targetCompatibility = JavaVersion.VERSION_19
}
kotlinOptions {
jvmTarget = "1.8"
jvmTarget = "19"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.1"
kotlinCompilerExtensionVersion = "1.5.2"
}
packaging {
resources {
Expand All @@ -50,7 +52,6 @@ android {
}

dependencies {

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.activity.compose)
Expand Down Expand Up @@ -85,4 +86,17 @@ dependencies {

implementation(libs.google.accompanist.pager)
implementation(libs.accompanist.pager.indicators)

implementation(libs.androidx.room.runtime)
annotationProcessor(libs.androidx.room.compiler)
ksp(libs.androidx.room.compiler)
implementation(libs.androidx.room.ktx)
implementation(libs.androidx.room.rxjava2)
implementation(libs.androidx.room.rxjava3)
implementation(libs.androidx.room.guava)
testImplementation(libs.androidx.room.testing)
implementation(libs.androidx.room.paging)

implementation(libs.hilt.android)
ksp(libs.hilt.android.compiler)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package pt.ulisboa.ist.pharmacist

import android.content.Context
import android.location.Geocoder
import androidx.room.Room
import com.google.android.libraries.places.api.Places
import com.google.android.libraries.places.api.net.PlacesClient
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import pt.ulisboa.ist.pharmacist.repository.local.PharmacistDatabase
import pt.ulisboa.ist.pharmacist.repository.remote.medicines.MedicineApi
import pt.ulisboa.ist.pharmacist.repository.remote.pharmacies.PharmacyApi
import pt.ulisboa.ist.pharmacist.repository.remote.upload.UploaderApi
import pt.ulisboa.ist.pharmacist.repository.remote.users.UsersApi
import pt.ulisboa.ist.pharmacist.service.real_time_updates.RealTimeUpdatesService
import pt.ulisboa.ist.pharmacist.session.SessionManager
import pt.ulisboa.ist.pharmacist.session.SessionManagerSharedPrefs
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object ApplicationModule {

@Provides
@Singleton
fun provideOkHttpClient(): OkHttpClient {
return OkHttpClient.Builder()
.connectTimeout(100, java.util.concurrent.TimeUnit.SECONDS)
.readTimeout(100, java.util.concurrent.TimeUnit.SECONDS)
.writeTimeout(100, java.util.concurrent.TimeUnit.SECONDS)
//.connectionSpecs(listOf(okhttp3.ConnectionSpec.MODERN_TLS))
.build()
}

@Provides
@Singleton
fun provideSessionManager(@ApplicationContext context: Context): SessionManager {
return SessionManagerSharedPrefs(context)
}

@Provides
@Singleton
fun providePharmacistDatabase(@ApplicationContext context: Context): PharmacistDatabase {
return Room.databaseBuilder(
context,
PharmacistDatabase::class.java,
"pharmacist.db"
).build()
}

@Provides
@Singleton
fun provideMedicineApi(
@ApplicationContext context: Context,
httpClient: OkHttpClient,
sessionManager: SessionManager
): MedicineApi {
return MedicineApi(
context = context,
httpClient = httpClient,
sessionManager = sessionManager
)
}

@Provides
@Singleton
fun providePharmacyApi(
@ApplicationContext context: Context,
httpClient: OkHttpClient,
sessionManager: SessionManager
): PharmacyApi {
return PharmacyApi(
context = context,
httpClient = httpClient,
sessionManager = sessionManager
)
}

@Provides
@Singleton
fun provideUsersApi(
@ApplicationContext context: Context,
httpClient: OkHttpClient,
sessionManager: SessionManager
): UsersApi {
return UsersApi(
context = context,
httpClient = httpClient,
sessionManager = sessionManager
)
}

@Provides
@Singleton
fun provideUploaderApi(
@ApplicationContext context: Context,
httpClient: OkHttpClient,
sessionManager: SessionManager
): UploaderApi {
return UploaderApi(
context = context,
httpClient = httpClient,
sessionManager = sessionManager
)
}

@Provides
@Singleton
fun provideRealTimeUpdatesService(
sessionManager: SessionManager,
httpClient: OkHttpClient
): RealTimeUpdatesService {
return RealTimeUpdatesService(
httpClient = httpClient,
sessionManager = sessionManager
)
}

@Provides
@Singleton
fun provideGson(): Gson {
return GsonBuilder().create()
}

@Provides
@Singleton
fun provideGeocoder(@ApplicationContext context: Context): Geocoder {
return Geocoder(context)
}

@Provides
@Singleton
fun providePlacesClient(@ApplicationContext context: Context): PlacesClient {
Places.initialize(context, context.getString(R.string.google_maps_key))
return Places.createClient(context)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pt.ulisboa.ist.pharmacist

import com.google.gson.Gson
import okhttp3.OkHttpClient
import pt.ulisboa.ist.pharmacist.service.http.PharmacistService
import pt.ulisboa.ist.pharmacist.service.real_time_updates.RealTimeUpdatesService
import pt.ulisboa.ist.pharmacist.session.SessionManager

Expand All @@ -11,12 +10,12 @@ import pt.ulisboa.ist.pharmacist.session.SessionManager
*
* @property jsonEncoder the JSON encoder used to serialize/deserialize objects
* @property sessionManager the manager used to handle the user session
* @property pharmacistService the service used to handle the pharmacist requests
* @property realTimeUpdatesService the service used to handle the real-time updates
* @property httpClient the HTTP client used to make requests
*/
interface DependenciesContainer {
val jsonEncoder: Gson
val sessionManager: SessionManager
val realTimeUpdatesService: RealTimeUpdatesService
val pharmacistService: PharmacistService
val httpClient: OkHttpClient
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,43 @@ import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.annotation.RequiresApi
import coil.ImageLoader
import coil.ImageLoaderFactory
import coil.disk.DiskCache
import coil.memory.MemoryCache
import coil.request.CachePolicy
import coil.util.DebugLogger
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import dagger.hilt.android.HiltAndroidApp
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import pt.ulisboa.ist.pharmacist.service.http.PharmacistService
import pt.ulisboa.ist.pharmacist.service.real_time_updates.MedicineNotificationsBackgroundService
import pt.ulisboa.ist.pharmacist.service.real_time_updates.RealTimeUpdatesService
import pt.ulisboa.ist.pharmacist.session.SessionManager
import pt.ulisboa.ist.pharmacist.session.SessionManagerSharedPrefs
import javax.inject.Inject

/**
* The Pharmacist application.
*
* @property jsonEncoder the JSON encoder used to serialize/deserialize objects
* @property sessionManager the manager used to handle the user session
* @property pharmacistService the service used to handle the pharmacist requests
*/
class PharmacistApplication : DependenciesContainer, Application() {
@HiltAndroidApp
class PharmacistApplication : DependenciesContainer, Application(), ImageLoaderFactory {

override val jsonEncoder: Gson = GsonBuilder().create()
@Inject
override lateinit var httpClient: OkHttpClient

override val sessionManager: SessionManager = SessionManagerSharedPrefs(context = this)
@Inject
override lateinit var jsonEncoder: Gson

override val httpClient = OkHttpClient.Builder()
.connectTimeout(100, java.util.concurrent.TimeUnit.SECONDS)
.readTimeout(100, java.util.concurrent.TimeUnit.SECONDS)
.writeTimeout(100, java.util.concurrent.TimeUnit.SECONDS)
// .connectionSpecs(listOf(okhttp3.ConnectionSpec.MODERN_TLS))
.build()
@Inject
override lateinit var sessionManager: SessionManager

override val pharmacistService = PharmacistService(
context = this,
httpClient = httpClient,
sessionManager = sessionManager
)

override val realTimeUpdatesService = RealTimeUpdatesService(
apiEndpoint = API_ENDPOINT,
sessionManager = sessionManager,
httpClient = httpClient
)
@Inject
override lateinit var realTimeUpdatesService: RealTimeUpdatesService

private val serviceScope = CoroutineScope(Dispatchers.Default)

Expand Down Expand Up @@ -84,15 +78,36 @@ class PharmacistApplication : DependenciesContainer, Application() {
companion object {
const val MEDICINE_NOTIFICATION_CHANNEL = "MedicineNotifications"

private const val API_ENDPOINT_TYPE = "render"
private const val API_ENDPOINT_TYPE = "domain"
val API_ENDPOINT = when (API_ENDPOINT_TYPE) {
"localhost" -> "http://10.0.2.2:8080"
"ngrok" -> "https://2b02-2001-818-e871-b700-c937-8172-33bf-a88.ngrok-free.app"
"render" -> "https://pharmacist-e9t4.onrender.com"
"domain" -> "https://thepharmacist.online"
else -> {
throw IllegalStateException("Invalid API_ENDPOINT_TYPE")
}
}
const val TAG = "PharmacistApp"
}

override fun newImageLoader(): ImageLoader {
return ImageLoader.Builder(this)
.memoryCachePolicy(CachePolicy.ENABLED)
.memoryCache {
MemoryCache.Builder(this)
.maxSizePercent(0.1)
.strongReferencesEnabled(true)
.build()
}
.diskCachePolicy(CachePolicy.ENABLED)
.diskCache {
DiskCache.Builder()
.maxSizePercent(0.03)
.directory(cacheDir)
.build()
}
.logger(DebugLogger())
.build()
}
}
Loading

0 comments on commit f211959

Please sign in to comment.