Skip to content

Commit 6367e07

Browse files
file loading implemented, with major fix in uploading feature
file loading implemented, with major fix in uploading feature
2 parents e447aa3 + af2aa6d commit 6367e07

23 files changed

+434
-39
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ android {
4747

4848
dependencies {
4949

50-
implementation "org.jetbrains.kotlin:kotlin-stdlib:$versions.kotlin"
50+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$versions.kotlin"
5151
implementation 'androidx.core:core-ktx:1.3.2'
5252
implementation 'androidx.appcompat:appcompat:1.2.0'
5353
implementation 'com.google.android.material:material:1.3.0'

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
android:allowBackup="true"
1313
android:fullBackupContent="false"
1414
tools:replace="android:fullBackupContent"
15+
android:requestLegacyExternalStorage="true"
1516
android:icon="@mipmap/ic_launcher"
1617
android:label="@string/app_name"
1718
android:roundIcon="@mipmap/ic_launcher_round"

app/src/main/java/com/github/code/gambit/backgroundtask/FileUploadWorker.kt

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@ import android.content.Intent
88
import android.os.Build
99
import androidx.core.app.NotificationCompat
1010
import androidx.core.app.NotificationManagerCompat
11+
import androidx.room.Room
1112
import androidx.work.CoroutineWorker
1213
import androidx.work.WorkerParameters
1314
import androidx.work.workDataOf
1415
import com.github.code.gambit.R
16+
import com.github.code.gambit.data.entity.chache.FileCacheEntity
1517
import com.github.code.gambit.data.entity.network.FileNetworkEntity
18+
import com.github.code.gambit.data.local.Database
19+
import com.github.code.gambit.data.local.FileDao
20+
import com.github.code.gambit.data.mapper.cache.FileCacheMapper
21+
import com.github.code.gambit.data.mapper.network.FileNetworkMapper
22+
import com.github.code.gambit.data.model.FileMetaData
1623
import com.github.code.gambit.data.remote.services.ApiService
1724
import com.github.code.gambit.data.remote.services.file.FileService
1825
import com.github.code.gambit.data.remote.services.file.FileServiceImpl
@@ -37,23 +44,39 @@ class FileUploadWorker(ctx: Context, params: WorkerParameters) : CoroutineWorker
3744
* @outputData data: Data toString of FileNetworkEntity
3845
*/
3946
override suspend fun doWork(): Result {
40-
val filePath: String = inputData.getString(AppConstant.Worker.FILE_URI_KEY)!!
41-
val fileName: String = inputData.getString(AppConstant.Worker.FILE_NAME_KEY)!!
42-
val fileSize: Int = inputData.getInt(AppConstant.Worker.FILE_SIZE_KEY, -1)
47+
val fileMetaDataString = inputData.getString(AppConstant.Worker.FILE_META_DATA)!!
48+
val fmd = FileMetaData.fromString(fileMetaDataString)
4349
val uid = System.currentTimeMillis().toInt()
44-
val size = String.format("%.2f", fileSize.div(10.0.pow(6.0)))
45-
makeStatusNotification(uid, "New file", "Uploading file $fileName of size $size MB", true)
46-
val file = File(filePath)
47-
val res = InfuraIPFS().add.file(file, fileName, fileName).Hash
50+
val size = String.format("%.2f", fmd.size.div(10.0.pow(6.0)))
51+
makeStatusNotification(uid, "New file", "Uploading file ${fmd.name} of size $size MB", true)
52+
val file = File(fmd.path)
53+
val a = System.currentTimeMillis()
54+
val res = InfuraIPFS().add.file(file, fmd.name, fmd.name).Hash
55+
val b = System.currentTimeMillis()
4856
// val res = "test-hash-to-avoid-unnecessary-server-call"
4957
Timber.tag("out").i(res)
58+
Timber.tag("out").i("Took ${b - a} ms")
59+
val fileDao = getFileDao()
5060
val fileService = getFileService()
51-
val task = fileService.uploadFile(FileNetworkEntity("", "", res, fileName, fileSize, fileName.split(".")[1]))
61+
val task = fileService.uploadFile(FileNetworkEntity("", "", res, fmd.name, fmd.size, fmd.name.split(".")[1]))
62+
fileDao.insertFiles(getFileCacheEntityFromFileNetworkEntity(task))
5263
val data = workDataOf(AppConstant.Worker.FILE_OUTPUT_KEY to task.toString())
5364
makeStatusNotification(uid, "File Uploaded", task.toString(), false)
5465
return Result.success(data)
5566
}
5667

68+
private fun getFileCacheEntityFromFileNetworkEntity(fileNetworkEntity: FileNetworkEntity): FileCacheEntity {
69+
val networkMapper = FileNetworkMapper()
70+
val cacheMapper = FileCacheMapper()
71+
return cacheMapper.mapToEntity(networkMapper.mapFromEntity(fileNetworkEntity))
72+
}
73+
74+
private fun getFileDao(): FileDao {
75+
return Room.databaseBuilder(applicationContext, Database::class.java, AppConstant.Database.DB_NAME)
76+
.fallbackToDestructiveMigration()
77+
.build().fileDao()
78+
}
79+
5780
private fun getApiService(): ApiService {
5881
return Retrofit.Builder().baseUrl(AppConstant.BASE_URL)
5982
.addConverterFactory(
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.github.code.gambit.data.local
2+
3+
import com.github.code.gambit.data.model.File
4+
5+
interface CacheDataSource {
6+
7+
suspend fun insertFiles(files: List<File>): Long
8+
suspend fun getFiles(): List<File>
9+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.github.code.gambit.data.local
2+
3+
import com.github.code.gambit.data.mapper.cache.FileCacheMapper
4+
import com.github.code.gambit.data.model.File
5+
6+
class CacheDataSourceImpl
7+
constructor(
8+
val fileCacheMapper: FileCacheMapper,
9+
val fileDao: FileDao
10+
) : CacheDataSource {
11+
12+
override suspend fun getFiles(): List<File> {
13+
val fileCacheEntities = fileDao.getFiles()
14+
return fileCacheMapper.mapFromEntityList(fileCacheEntities)
15+
}
16+
17+
override suspend fun insertFiles(files: List<File>): Long {
18+
var c = 0L
19+
for (file in files) {
20+
c += fileDao.insertFiles(fileCacheMapper.mapToEntity(file))
21+
}
22+
return c
23+
}
24+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.github.code.gambit.data.local
2+
3+
import androidx.room.Database
4+
import androidx.room.RoomDatabase
5+
import com.github.code.gambit.data.entity.chache.FileCacheEntity
6+
7+
@Database(entities = [FileCacheEntity::class], version = 1)
8+
abstract class Database : RoomDatabase() {
9+
10+
abstract fun fileDao(): FileDao
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.github.code.gambit.data.local
2+
3+
import androidx.room.Dao
4+
import androidx.room.Insert
5+
import androidx.room.OnConflictStrategy
6+
import androidx.room.Query
7+
import com.github.code.gambit.data.entity.chache.FileCacheEntity
8+
9+
@Dao
10+
interface FileDao {
11+
12+
@Insert(onConflict = OnConflictStrategy.REPLACE)
13+
suspend fun insertFiles(fileCacheEntity: FileCacheEntity): Long
14+
15+
@Query("SELECT * FROM files ORDER BY timestamp DESC")
16+
suspend fun getFiles(): List<FileCacheEntity>
17+
18+
@Query("SELECT * FROM files WHERE id = :id")
19+
suspend fun getFile(id: String): List<FileCacheEntity>
20+
}

app/src/main/java/com/github/code/gambit/data/mapper/network/FileNetworkMapper.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.github.code.gambit.data.mapper.network
33
import com.github.code.gambit.data.EntityMapper
44
import com.github.code.gambit.data.entity.network.FileNetworkEntity
55
import com.github.code.gambit.data.model.File
6+
import com.github.code.gambit.utility.extention.byteToMb
67
import com.github.code.gambit.utility.extention.fromBase64
78
import com.github.code.gambit.utility.extention.toBase64
89
import javax.inject.Inject
@@ -18,8 +19,8 @@ constructor() : EntityMapper<FileNetworkEntity, File> {
1819
hash = entity.hash,
1920
type = entity.type,
2021
timestamp = entity.sk.split("#")[1],
21-
size = "${entity.size} Mb",
22-
extension = entity.ls1_sk.split(".")[1]
22+
size = entity.size.byteToMb(),
23+
extension = ".${entity.type}"
2324
)
2425
}
2526

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.github.code.gambit.data.model
2+
3+
import java.lang.IndexOutOfBoundsException
4+
5+
class FileMetaData(val path: String, val name: String, val size: Int) {
6+
7+
companion object {
8+
fun fromString(string: String): FileMetaData {
9+
val arr = string.split("\n")
10+
if (arr.size < 3) {
11+
throw throw IndexOutOfBoundsException("Invalid string")
12+
}
13+
return FileMetaData(arr[0], arr[1], arr[2].toInt())
14+
}
15+
}
16+
17+
override fun toString(): String {
18+
return "$path\n$name\n$size"
19+
}
20+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.github.code.gambit.di
2+
3+
import android.content.Context
4+
import androidx.room.Room
5+
import com.github.code.gambit.data.local.CacheDataSource
6+
import com.github.code.gambit.data.local.CacheDataSourceImpl
7+
import com.github.code.gambit.data.local.Database
8+
import com.github.code.gambit.data.local.FileDao
9+
import com.github.code.gambit.data.mapper.cache.FileCacheMapper
10+
import com.github.code.gambit.utility.AppConstant
11+
import dagger.Module
12+
import dagger.Provides
13+
import dagger.hilt.InstallIn
14+
import dagger.hilt.android.qualifiers.ApplicationContext
15+
import dagger.hilt.components.SingletonComponent
16+
import javax.inject.Singleton
17+
18+
@Module
19+
@InstallIn(SingletonComponent::class)
20+
object CacheModule {
21+
22+
@Singleton
23+
@Provides
24+
fun provideDatabase(@ApplicationContext context: Context): Database {
25+
return Room.databaseBuilder(context, Database::class.java, AppConstant.Database.DB_NAME)
26+
.fallbackToDestructiveMigration()
27+
.build()
28+
}
29+
30+
@Singleton
31+
@Provides
32+
fun provideFileDao(database: Database): FileDao {
33+
return database.fileDao()
34+
}
35+
36+
@Singleton
37+
@Provides
38+
fun provideCacheDataSource(
39+
fileCacheMapper: FileCacheMapper,
40+
fileDao: FileDao
41+
): CacheDataSource {
42+
return CacheDataSourceImpl(fileCacheMapper, fileDao)
43+
}
44+
}

0 commit comments

Comments
 (0)