forked from dhis2/dhis2-android-capture-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rei 07 - Manuals module implementation (#14)
* Add Room databese and create all the necessary implementations and methods * add hilt viewModel injector to compose * Fix room and add ui state * Implement PDF view * Fix issues and remove annecessary code * Delete ManualService.kt --------- Co-authored-by: Carlos Macaneta <[email protected]>
- Loading branch information
1 parent
bea5d05
commit a8c0f64
Showing
22 changed files
with
480 additions
and
48 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
4 changes: 0 additions & 4 deletions
4
support-module/src/main/java/com/saudigitus/support_module/data/AppModule.kt
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
support-module/src/main/java/com/saudigitus/support_module/data/local/ManualsRepository.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.saudigitus.support_module.data.local | ||
|
||
import android.content.Context | ||
import com.saudigitus.support_module.data.models.manuals.ManualItem | ||
import kotlinx.coroutines.flow.Flow | ||
import okhttp3.ResponseBody | ||
import java.io.File | ||
|
||
interface ManualsRepository { | ||
fun getManualsDataStore(): Flow<List<ManualItem>> | ||
suspend fun openManual(context: Context, url: String, fileName: String): File? | ||
suspend fun downloadManualToLocal( | ||
context: Context, | ||
url: String, | ||
fileName: String | ||
) | ||
} |
18 changes: 18 additions & 0 deletions
18
support-module/src/main/java/com/saudigitus/support_module/data/local/database/DBconfig.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.saudigitus.support_module.data.local.database | ||
|
||
|
||
import androidx.room.Database | ||
import androidx.room.* | ||
import com.saudigitus.support_module.data.models.manuals.ManualItem | ||
|
||
@Database( | ||
entities = [ManualItem::class], | ||
version = 1 | ||
) | ||
abstract class AppDatabase: RoomDatabase() { | ||
abstract fun manualsDAO(): ManualsDao | ||
|
||
companion object { | ||
const val DB_NAME = "manuals_db" | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
support-module/src/main/java/com/saudigitus/support_module/data/local/database/ManualsDAO.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.saudigitus.support_module.data.local.database | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Delete | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.Query | ||
import com.saudigitus.support_module.data.models.manuals.ManualItem | ||
|
||
@Dao | ||
interface ManualsDao { | ||
@Query("SELECT * FROM manualItem") | ||
fun getAll(): List<ManualItem> | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun create(mediaDetails: ManualItem) | ||
|
||
@Query("SELECT * FROM manualItem WHERE uid LIKE :id") | ||
fun getDetailsById(id: String): ManualItem | ||
|
||
@Delete | ||
fun delete(manual: ManualItem) | ||
} |
63 changes: 63 additions & 0 deletions
63
...src/main/java/com/saudigitus/support_module/data/local/repository/ManualsRepositoryImp.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.saudigitus.support_module.data.local.repository | ||
|
||
import android.app.DownloadManager | ||
import android.content.Context | ||
import android.net.Uri | ||
import android.os.Environment | ||
import com.saudigitus.support_module.data.local.ManualsRepository | ||
import com.saudigitus.support_module.data.local.database.ManualsDao | ||
import com.saudigitus.support_module.data.models.manuals.Manual | ||
import com.saudigitus.support_module.data.models.manuals.ManualItem | ||
import com.saudigitus.support_module.utils.Constants | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flowOf | ||
import kotlinx.coroutines.withContext | ||
import org.hisp.dhis.android.core.D2 | ||
import java.io.File | ||
|
||
class ManualsRepositoryImp( | ||
private val manualsDAO: ManualsDao, | ||
private val d2: D2 | ||
): ManualsRepository { | ||
override fun getManualsDataStore(): Flow<List<ManualItem>> { | ||
val dataStoreValue = d2.dataStoreModule().dataStore().byKey() | ||
.eq(Constants.MEDIA_DATA_STORE_KEY).blockingGet() | ||
.getOrNull(0)?.value() | ||
val dataStore = dataStoreValue?.let { Manual.fromJson(it) } | ||
return if (dataStore != null) { | ||
flowOf(dataStore) | ||
} else { | ||
flowOf(emptyList()) | ||
} | ||
} | ||
|
||
override suspend fun downloadManualToLocal(context: Context, url: String, fileName: String): Unit | ||
= withContext(Dispatchers.IO) { | ||
val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager | ||
val uri = Uri.parse(url) | ||
val file = File(context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), | ||
"$fileName.pdf" | ||
) | ||
if (!file.exists()) { | ||
val request = DownloadManager.Request(uri).apply { | ||
setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE) | ||
// Store the file in the app's private external files directory | ||
setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOCUMENTS, "$fileName.pdf") | ||
} | ||
downloadManager.enqueue(request) | ||
} | ||
return@withContext | ||
} | ||
|
||
|
||
override suspend fun openManual(context: Context, url: String, fileName: String): File? | ||
= withContext(Dispatchers.IO) { | ||
// Get the file from the app's private storage | ||
val file = File(context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), "$fileName.pdf") | ||
if (file.exists()) { | ||
return@withContext file | ||
} | ||
return@withContext null | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
support-module/src/main/java/com/saudigitus/support_module/data/models/manuals/Manual.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.saudigitus.support_module.data.models.manuals | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.saudigitus.support_module.utils.Mapper.translateJsonToObject | ||
|
||
class Manual { | ||
private fun toJson(): String = translateJsonToObject().writeValueAsString(this) | ||
|
||
companion object { | ||
fun fromJson(json: String?): List<ManualItem>? = if (json != null) { | ||
val mapper = ObjectMapper() | ||
|
||
translateJsonToObject() | ||
.readValue( | ||
json, | ||
mapper.typeFactory.constructCollectionType( | ||
List::class.java, | ||
ManualItem::class.java, | ||
), | ||
) | ||
} else { | ||
null | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
support-module/src/main/java/com/saudigitus/support_module/data/models/manuals/ManualItem.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.saudigitus.support_module.data.models.manuals | ||
|
||
|
||
import androidx.room.ColumnInfo | ||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties | ||
import com.fasterxml.jackson.annotation.JsonProperty | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
@Entity | ||
data class ManualItem( | ||
@JsonProperty("uid") | ||
@PrimaryKey val uid: String, | ||
|
||
@JsonProperty("title") | ||
@ColumnInfo(name = "title") val title: String, | ||
|
||
@JsonProperty("subtitle") | ||
@ColumnInfo(name = "subtitle") val subtitle: String?, | ||
|
||
@JsonProperty("path") | ||
@ColumnInfo(name = "path") val path: String? | ||
) |
37 changes: 37 additions & 0 deletions
37
support-module/src/main/java/com/saudigitus/support_module/di/AppModule.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.saudigitus.support_module.di | ||
|
||
import android.app.Application | ||
import androidx.room.Room | ||
import com.saudigitus.support_module.data.local.ManualsRepository | ||
import com.saudigitus.support_module.data.local.database.AppDatabase | ||
import com.saudigitus.support_module.data.local.repository.ManualsRepositoryImp | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import org.hisp.dhis.android.core.D2 | ||
import org.hisp.dhis.android.core.D2Manager | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object AppModule { | ||
@Provides | ||
@Singleton | ||
fun manualsDatabase(app: Application): AppDatabase = | ||
Room.databaseBuilder( | ||
app, | ||
AppDatabase::class.java, | ||
AppDatabase.DB_NAME | ||
).build() | ||
|
||
/** | ||
* Inject ManualsRepository | ||
*/ | ||
|
||
@Provides | ||
@Singleton fun providesManualRepository(appDatabase: AppDatabase, d2: D2): ManualsRepository { | ||
return ManualsRepositoryImp(appDatabase.manualsDAO(), d2 = d2) | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
support-module/src/main/java/com/saudigitus/support_module/ui/ManualState.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.saudigitus.support_module.ui | ||
|
||
import com.rizzi.bouquet.ResourceType | ||
import com.rizzi.bouquet.VerticalPdfReaderState | ||
import com.saudigitus.support_module.data.models.manuals.ManualItem | ||
import java.io.File | ||
|
||
data class ManualsUiState( | ||
val isLoading: Boolean = false, | ||
val hasFileLoaded: Boolean = false, | ||
val isDownloading: Boolean = false, | ||
val manualItems : List<ManualItem> = emptyList(), | ||
val pdf: File? = null, | ||
val pdfVerticalReaderState: VerticalPdfReaderState? = null | ||
) |
This file contains 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
This file contains 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
Oops, something went wrong.