Skip to content

Commit

Permalink
Adds data store and device id
Browse files Browse the repository at this point in the history
  • Loading branch information
siddheshkothadi committed Aug 3, 2022
1 parent 671dfbe commit 6c6ef76
Show file tree
Hide file tree
Showing 17 changed files with 174 additions and 85 deletions.
1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ dependencies {
// extra compose dependencies
implementation "androidx.compose.material:material-icons-extended:$compose_version"
implementation("io.coil-kt:coil-compose:2.0.0-rc02")
implementation "com.airbnb.android:lottie-compose:5.0.3"
// implementation "com.airbnb.android:lottie-compose:5.0.3"
implementation "androidx.compose.runtime:runtime-livedata:1.2.0-alpha05"
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.4.1"

Expand Down Expand Up @@ -114,4 +114,7 @@ dependencies {
// WorkManager
implementation "androidx.work:work-runtime-ktx:2.7.1"
implementation 'androidx.hilt:hilt-work:1.0.0'

// Data Store
implementation "androidx.datastore:datastore-preferences:1.0.0"
}
5 changes: 4 additions & 1 deletion app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
#-renamesourcefileattribute SourceFile
-keepclassmembers class * extends androidx.datastore.preferences.protobuf.GeneratedMessageLite {
<fields>;
}
19 changes: 15 additions & 4 deletions app/src/main/java/me/siddheshkothadi/autofism3/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.Manifest
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
Expand All @@ -24,21 +25,31 @@ import com.google.accompanist.navigation.animation.composable
import com.google.accompanist.navigation.animation.rememberAnimatedNavController
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.rememberMultiplePermissionsState
import dagger.hilt.android.AndroidEntryPoint
import me.siddheshkothadi.autofism3.ui.nav.Screen
import me.siddheshkothadi.autofism3.ui.nav.captureGraph
import me.siddheshkothadi.autofism3.ui.screens.CameraScreen
import me.siddheshkothadi.autofism3.ui.screens.EmptyScreen
import me.siddheshkothadi.autofism3.ui.screens.History
import me.siddheshkothadi.autofism3.ui.screen.EmptyScreen
import me.siddheshkothadi.autofism3.ui.screen.History
import me.siddheshkothadi.autofism3.ui.theme.AutoFISM3Theme
import me.siddheshkothadi.autofism3.ui.viewmodel.MainViewModel

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

@OptIn(ExperimentalPermissionsApi::class)
private val mainViewModel: MainViewModel by viewModels()

@OptIn(
ExperimentalMaterial3Api::class, ExperimentalAnimationApi::class,
ExperimentalPermissionsApi::class
)
override fun onCreate(savedInstanceState: Bundle?) {
WindowCompat.setDecorFitsSystemWindows(window, false)
installSplashScreen()
installSplashScreen().apply {
setKeepOnScreenCondition {
mainViewModel.deviceId.value.isNullOrBlank()
}
}
super.onCreate(savedInstanceState)

setContent {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package me.siddheshkothadi.autofism3.database

import android.content.Context
import android.provider.Settings
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import me.siddheshkothadi.autofism3.FishApplication
import me.siddheshkothadi.autofism3.repository.DataStoreRepository
import java.security.AccessController.getContext
import java.util.*
import javax.inject.Inject
import kotlin.math.roundToInt

class DataStoreRepositoryImpl @Inject constructor(
context: FishApplication
) : DataStoreRepository
{
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "device_data")
private val deviceDataStore = context.dataStore

private fun getRandomString(): String {
val allowedChars = ('A'..'Z') + ('a'..'z') + ('0'..'9')
var string = ""
for (i in (1..20)) {
string += allowedChars[(Math.random() * (allowedChars.size - 1)).roundToInt()]
}
return string
}

override val deviceData: Flow<Preferences>
get() = deviceDataStore.data

override val deviceId: Flow<String>
get() = deviceDataStore.data.map { preferences ->
preferences[DEVICE_ID_KEY] ?: ""
}

override fun generateDeviceId(): String {
val randomString = getRandomString()
val timeStamp = System.currentTimeMillis()
val uniqueID = UUID.randomUUID().toString()

return "$randomString-$timeStamp-$uniqueID"
}

override suspend fun setDeviceId(generatedDeviceId: String) {
deviceDataStore.edit { mutablePreferences ->
mutablePreferences[DEVICE_ID_KEY] = generatedDeviceId
}
}

override suspend fun setDeviceId(): String {
val generatedDeviceId = generateDeviceId()
deviceDataStore.edit { mutablePreferences ->
mutablePreferences[DEVICE_ID_KEY] = generatedDeviceId
}
return generatedDeviceId
}

companion object {
val DEVICE_ID_KEY = stringPreferencesKey("device_id")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ 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.DataStoreRepositoryImpl
import me.siddheshkothadi.autofism3.database.FishDatabase
import me.siddheshkothadi.autofism3.database.FishRepositoryImpl
import me.siddheshkothadi.autofism3.network.FileAPI
import me.siddheshkothadi.autofism3.repository.DataStoreRepository
import me.siddheshkothadi.autofism3.repository.FishRepository
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
Expand Down Expand Up @@ -57,4 +59,10 @@ object AppModule {
fun provideFishRepository(fishDatabase: FishDatabase, fileAPI: FileAPI): FishRepository {
return FishRepositoryImpl(fishDatabase.fishDAO(), fileAPI)
}

@Singleton
@Provides
fun provideDataStoreRepository(appContext: FishApplication): DataStoreRepository {
return DataStoreRepositoryImpl(appContext)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package me.siddheshkothadi.autofism3.repository

import androidx.datastore.preferences.core.Preferences
import kotlinx.coroutines.flow.Flow

interface DataStoreRepository {
// For all data
val deviceData: Flow<Preferences>

// for device id
val deviceId: Flow<String>
fun generateDeviceId(): String
suspend fun setDeviceId(generatedDeviceId: String)
suspend fun setDeviceId(): String
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package me.siddheshkothadi.autofism3.ui.components
package me.siddheshkothadi.autofism3.ui.component

import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import com.google.accompanist.navigation.animation.composable
import com.google.accompanist.navigation.animation.navigation
import me.siddheshkothadi.autofism3.ui.screens.CameraScreen
import me.siddheshkothadi.autofism3.ui.screens.EnterDetails
import me.siddheshkothadi.autofism3.ui.screen.CameraScreen
import me.siddheshkothadi.autofism3.ui.screen.EnterDetails

@OptIn(ExperimentalAnimationApi::class)
fun NavGraphBuilder.captureGraph(navController: NavHostController) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package me.siddheshkothadi.autofism3.ui.nav

import androidx.compose.animation.ExperimentalAnimationApi
import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import com.google.accompanist.navigation.animation.composable
import com.google.accompanist.navigation.animation.navigation
import me.siddheshkothadi.autofism3.ui.screens.Login

@OptIn(ExperimentalAnimationApi::class)
fun NavGraphBuilder.loginGraph(navController: NavHostController) {
navigation(startDestination = "login", route = "login") {
composable(
"login"
) {
Login(navController)
}
}
}
//import me.siddheshkothadi.autofism3.ui.screen.Login
//
//@OptIn(ExperimentalAnimationApi::class)
//fun NavGraphBuilder.loginGraph(navController: NavHostController) {
// navigation(startDestination = "login", route = "login") {
// composable(
// "login"
// ) {
// Login(navController)
// }
// }
//}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.siddheshkothadi.autofism3.ui.screens
package me.siddheshkothadi.autofism3.ui.screen

import android.graphics.Color
import android.graphics.Paint
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.siddheshkothadi.autofism3.ui.screens
package me.siddheshkothadi.autofism3.ui.screen

import androidx.compose.runtime.Composable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.siddheshkothadi.autofism3.ui.screens
package me.siddheshkothadi.autofism3.ui.screen

import android.net.Uri
import androidx.compose.foundation.layout.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.siddheshkothadi.autofism3.ui.screens
package me.siddheshkothadi.autofism3.ui.screen

import android.annotation.SuppressLint
import androidx.compose.foundation.layout.*
Expand All @@ -7,7 +7,6 @@ import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Map
import androidx.compose.material.icons.filled.PinDrop
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
Expand All @@ -17,9 +16,8 @@ import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import coil.compose.AsyncImage
import me.siddheshkothadi.autofism3.ui.components.AppBar
import me.siddheshkothadi.autofism3.ui.component.AppBar
import me.siddheshkothadi.autofism3.ui.nav.Screen

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
Expand Down
52 changes: 0 additions & 52 deletions app/src/main/java/me/siddheshkothadi/autofism3/ui/screens/Login.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package me.siddheshkothadi.autofism3.ui.viewmodel

import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import me.siddheshkothadi.autofism3.FishApplication
import me.siddheshkothadi.autofism3.repository.DataStoreRepository
import timber.log.Timber
import javax.inject.Inject

@HiltViewModel
@ExperimentalPermissionsApi
class MainViewModel @Inject constructor(
private val dataStoreRepository: DataStoreRepository,
private val context: FishApplication
) : ViewModel() {
private val _deviceId = MutableStateFlow<String?>("")
val deviceId = _deviceId.asStateFlow()

init {
viewModelScope.launch {
val dataStoreDeviceId = dataStoreRepository.deviceId.first()
if(dataStoreDeviceId.isBlank()) {
Timber.i("Device Id Not Found! Generating new device id...")
_deviceId.value = dataStoreRepository.setDeviceId()
} else {
_deviceId.value = dataStoreDeviceId
}
Timber.i("Device ID: ${deviceId.value}")
Toast.makeText(context, "Device ID: ${deviceId.value}", Toast.LENGTH_LONG).show()
}
}
}
1 change: 0 additions & 1 deletion app/src/main/res/raw/fishing.json

This file was deleted.

0 comments on commit 6c6ef76

Please sign in to comment.