-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds room, workmanager, hilt and retrofit
- Loading branch information
1 parent
fe4cc31
commit 671dfbe
Showing
15 changed files
with
481 additions
and
3 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
31 changes: 30 additions & 1 deletion
31
app/src/main/java/me/siddheshkothadi/autofism3/FishApplication.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 |
---|---|---|
@@ -1,14 +1,43 @@ | ||
package me.siddheshkothadi.autofism3 | ||
|
||
import android.app.Application | ||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.os.Build | ||
import androidx.hilt.work.HiltWorkerFactory | ||
import androidx.work.Configuration | ||
import dagger.hilt.android.HiltAndroidApp | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
@HiltAndroidApp | ||
class FishApplication: Application(), Configuration.Provider { | ||
|
||
@Inject | ||
lateinit var uploadWorkerFactory: HiltWorkerFactory | ||
|
||
class FishApplication: Application() { | ||
override fun onCreate() { | ||
super.onCreate() | ||
|
||
if (BuildConfig.DEBUG) { | ||
Timber.plant(Timber.DebugTree()) | ||
} | ||
|
||
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
val channel = NotificationChannel( | ||
"upload_channel", | ||
"Upload", | ||
NotificationManager.IMPORTANCE_HIGH | ||
) | ||
|
||
val notificationManager = getSystemService(NotificationManager::class.java) | ||
notificationManager.createNotificationChannel(channel) | ||
} | ||
} | ||
|
||
override fun getWorkManagerConfiguration(): Configuration { | ||
return Configuration.Builder() | ||
.setWorkerFactory(uploadWorkerFactory) | ||
.build() | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
app/src/main/java/me/siddheshkothadi/autofism3/database/FishDAO.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,26 @@ | ||
package me.siddheshkothadi.autofism3.database | ||
|
||
import androidx.room.* | ||
import kotlinx.coroutines.flow.Flow | ||
import me.siddheshkothadi.autofism3.model.Fish | ||
|
||
@Dao | ||
interface FishDAO { | ||
@Query("SELECT * FROM fishentity") | ||
fun getAll(): Flow<List<FishEntity>> | ||
|
||
@Query("SELECT * FROM fishentity") | ||
suspend fun getFishList(): List<FishEntity> | ||
|
||
@Query("SELECT * FROM fishentity WHERE imageUri = :imageUri") | ||
suspend fun getByImageUri(imageUri: String): FishEntity | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun insert(fishEntity: FishEntity) | ||
|
||
@Delete | ||
suspend fun delete(fishEntity: FishEntity) | ||
|
||
@Query("DELETE FROM fishentity") | ||
suspend fun deleteAll() | ||
} |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/me/siddheshkothadi/autofism3/database/FishDatabase.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,9 @@ | ||
package me.siddheshkothadi.autofism3.database | ||
|
||
import androidx.room.Database | ||
import androidx.room.RoomDatabase | ||
|
||
@Database(entities = [FishEntity::class], version = 1, exportSchema = false) | ||
abstract class FishDatabase: RoomDatabase() { | ||
abstract fun fishDAO(): FishDAO | ||
} |
13 changes: 13 additions & 0 deletions
13
app/src/main/java/me/siddheshkothadi/autofism3/database/FishEntity.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,13 @@ | ||
package me.siddheshkothadi.autofism3.database | ||
|
||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
|
||
@Entity | ||
data class FishEntity( | ||
@PrimaryKey val imageUri: String, | ||
val longitude: String, | ||
val latitude: String, | ||
val quantity: String, | ||
val timeStamp: String | ||
) |
24 changes: 24 additions & 0 deletions
24
app/src/main/java/me/siddheshkothadi/autofism3/database/FishMapper.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 me.siddheshkothadi.autofism3.database | ||
|
||
import androidx.room.PrimaryKey | ||
import me.siddheshkothadi.autofism3.model.Fish | ||
|
||
fun FishEntity.toFish(): Fish { | ||
return Fish( | ||
imageUri = imageUri, | ||
longitude = longitude, | ||
latitude = latitude, | ||
quantity = quantity, | ||
timeStamp = timeStamp | ||
) | ||
} | ||
|
||
fun Fish.toFishEntity(): FishEntity { | ||
return FishEntity( | ||
imageUri = imageUri, | ||
longitude = longitude, | ||
latitude = latitude, | ||
quantity = quantity, | ||
timeStamp = timeStamp | ||
) | ||
} |
59 changes: 59 additions & 0 deletions
59
app/src/main/java/me/siddheshkothadi/autofism3/database/FishRepositoryImpl.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,59 @@ | ||
package me.siddheshkothadi.autofism3.database | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import me.siddheshkothadi.autofism3.model.Fish | ||
import me.siddheshkothadi.autofism3.network.FileAPI | ||
import me.siddheshkothadi.autofism3.repository.FishRepository | ||
import okhttp3.MultipartBody | ||
import okhttp3.RequestBody | ||
import timber.log.Timber | ||
|
||
class FishRepositoryImpl( | ||
private val fishDAO: FishDAO, | ||
private val fileAPI: FileAPI | ||
): FishRepository { | ||
override fun getAllFish(): Flow<List<Fish>> { | ||
return fishDAO.getAll().map {spots -> | ||
spots.map { spot -> | ||
spot.toFish() | ||
} | ||
} | ||
} | ||
|
||
override suspend fun getFishList(): List<Fish> { | ||
return fishDAO.getFishList().map { | ||
it.toFish() | ||
} | ||
} | ||
|
||
override suspend fun getFishByImageUri(imageUri: String): Fish { | ||
return fishDAO.getByImageUri(imageUri).toFish() | ||
} | ||
|
||
override suspend fun insertFish(fish: Fish) { | ||
fishDAO.insert(fish.toFishEntity()) | ||
} | ||
|
||
override suspend fun deleteFish(fish: Fish) { | ||
fishDAO.delete(fish.toFishEntity()) | ||
} | ||
|
||
override suspend fun deleteAllFish() { | ||
fishDAO.deleteAll() | ||
} | ||
|
||
override suspend fun uploadFishData( | ||
image: MultipartBody.Part, | ||
longitude: RequestBody, | ||
latitude: RequestBody, | ||
quantity: RequestBody, | ||
timestamp: RequestBody | ||
) { | ||
fileAPI.uploadData(image, longitude, latitude, quantity, timestamp) | ||
} | ||
|
||
override suspend fun getHistory(): List<Fish> { | ||
return fileAPI.getHistory() | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
app/src/main/java/me/siddheshkothadi/autofism3/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,60 @@ | ||
package me.siddheshkothadi.autofism3.di | ||
|
||
import android.content.Context | ||
import androidx.room.Room | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import me.siddheshkothadi.autofism3.FishApplication | ||
import me.siddheshkothadi.autofism3.database.FishDatabase | ||
import me.siddheshkothadi.autofism3.database.FishRepositoryImpl | ||
import me.siddheshkothadi.autofism3.network.FileAPI | ||
import me.siddheshkothadi.autofism3.repository.FishRepository | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
import retrofit2.create | ||
import javax.inject.Singleton | ||
|
||
//const val BASE_URL = "https://file-upload-server.siddheshkothadi.repl.co/" | ||
//const val BASE_URL = "https://autofis-server.siddheshkothadi.repl.co/" | ||
const val BASE_URL = "http://127.0.0.1:5000/" | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object AppModule { | ||
@Singleton | ||
@Provides | ||
fun provideApplication(@ApplicationContext appContext: Context): FishApplication { | ||
return appContext as FishApplication | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun provideRetrofit(): FileAPI { | ||
val retrofit = Retrofit | ||
.Builder() | ||
.baseUrl(BASE_URL) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build() | ||
|
||
return retrofit.create(FileAPI::class.java) | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun provideFishDatabase(appContext: FishApplication): FishDatabase { | ||
return Room.databaseBuilder( | ||
appContext, | ||
FishDatabase::class.java, | ||
"fish_database" | ||
).build() | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun provideFishRepository(fishDatabase: FishDatabase, fileAPI: FileAPI): FishRepository { | ||
return FishRepositoryImpl(fishDatabase.fishDAO(), fileAPI) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/me/siddheshkothadi/autofism3/model/Fish.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,12 @@ | ||
package me.siddheshkothadi.autofism3.model | ||
|
||
data class Fish( | ||
val _id: String = "", | ||
val imageUri: String = "", | ||
val longitude: String, | ||
val latitude: String, | ||
val quantity: String, | ||
val timeStamp: String, | ||
val image_url: String = "", | ||
val name: String = "" | ||
) |
26 changes: 26 additions & 0 deletions
26
app/src/main/java/me/siddheshkothadi/autofism3/network/FileAPI.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,26 @@ | ||
package me.siddheshkothadi.autofism3.network | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import me.siddheshkothadi.autofism3.model.Fish | ||
import okhttp3.MultipartBody | ||
import okhttp3.RequestBody | ||
import retrofit2.http.GET | ||
import retrofit2.http.Multipart | ||
import retrofit2.http.POST | ||
import retrofit2.http.Part | ||
|
||
interface FileAPI { | ||
|
||
@Multipart | ||
@POST("/api/upload") | ||
suspend fun uploadData( | ||
@Part image: MultipartBody.Part, | ||
@Part("longitude") longitude: RequestBody, | ||
@Part("latitude") latitude: RequestBody, | ||
@Part("quantity") quantity: RequestBody, | ||
@Part("timestamp") timestamp: RequestBody, | ||
) | ||
|
||
@GET("/api/history") | ||
suspend fun getHistory() : List<Fish> | ||
} |
30 changes: 30 additions & 0 deletions
30
app/src/main/java/me/siddheshkothadi/autofism3/network/UploadStreamRequestBody.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,30 @@ | ||
import okhttp3.MediaType | ||
import okhttp3.MediaType.Companion.toMediaTypeOrNull | ||
import okhttp3.RequestBody | ||
import okio.BufferedSink | ||
import java.io.InputStream | ||
|
||
class UploadStreamRequestBody( | ||
private val mediaType: String, | ||
private val inputStream: InputStream, | ||
private val onUploadProgress: (Int) -> Unit, | ||
) : RequestBody() { | ||
|
||
override fun contentLength(): Long = inputStream.available().toLong() | ||
|
||
override fun contentType(): MediaType? = mediaType.toMediaTypeOrNull() | ||
|
||
override fun writeTo(sink: BufferedSink) { | ||
val contentLength = inputStream.available().toFloat() | ||
val buffer = ByteArray(DEFAULT_BUFFER_SIZE) // DEFAULT_BUFFER_SIZE constant from kotlin.io.ConstantsKt | ||
inputStream.use { inputStream -> | ||
var uploaded = 0 | ||
var read: Int | ||
while (inputStream.read(buffer).also { read = it } != -1) { // Reads the stream until the content ends | ||
sink.write(buffer, 0, read) | ||
uploaded += read | ||
onUploadProgress((100*uploaded/contentLength).toInt()) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.