-
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.
- Loading branch information
1 parent
671dfbe
commit 6c6ef76
Showing
17 changed files
with
174 additions
and
85 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
68 changes: 68 additions & 0 deletions
68
app/src/main/java/me/siddheshkothadi/autofism3/database/DataStoreRepositoryImpl.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,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") | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
app/src/main/java/me/siddheshkothadi/autofism3/repository/DataStoreRepository.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 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 | ||
} |
5 changes: 1 addition & 4 deletions
5
...kothadi/autofism3/ui/components/AppBar.kt → ...hkothadi/autofism3/ui/component/AppBar.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
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
25 changes: 12 additions & 13 deletions
25
app/src/main/java/me/siddheshkothadi/autofism3/ui/nav/LoginNavGraph.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,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) | ||
// } | ||
// } | ||
//} |
2 changes: 1 addition & 1 deletion
2
...hadi/autofism3/ui/screens/CameraScreen.kt → ...thadi/autofism3/ui/screen/CameraScreen.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
2 changes: 1 addition & 1 deletion
2
...thadi/autofism3/ui/screens/EmptyScreen.kt → ...othadi/autofism3/ui/screen/EmptyScreen.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
2 changes: 1 addition & 1 deletion
2
...hadi/autofism3/ui/screens/EnterDetails.kt → ...thadi/autofism3/ui/screen/EnterDetails.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
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
52 changes: 0 additions & 52 deletions
52
app/src/main/java/me/siddheshkothadi/autofism3/ui/screens/Login.kt
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
app/src/main/java/me/siddheshkothadi/autofism3/ui/viewmodel/MainViewModel.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,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() | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.