generated from unlam-tec-movil/ScaffoldingV2
-
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.
- Loading branch information
Showing
18 changed files
with
500 additions
and
20 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
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
66 changes: 66 additions & 0 deletions
66
.../java/ar/edu/unlam/mobile/scaffolding/data/repository/LocationRepositoryImplementation.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,66 @@ | ||
package ar.edu.unlam.mobile.scaffolding.data.repository | ||
|
||
import android.Manifest | ||
import android.content.Context | ||
import android.content.pm.PackageManager | ||
import androidx.core.content.ContextCompat | ||
import ar.edu.unlam.mobile.scaffolding.domain.models.LocationResult | ||
import ar.edu.unlam.mobile.scaffolding.domain.repository.LocationRepository | ||
import com.google.android.gms.location.FusedLocationProviderClient | ||
import com.google.android.gms.location.LocationServices | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
import javax.inject.Inject | ||
import kotlin.coroutines.resume | ||
|
||
class LocationRepositoryImplementation | ||
@Inject | ||
constructor( | ||
private val fusedLocationProviderClient: FusedLocationProviderClient, | ||
@ApplicationContext private val context: Context, | ||
) : LocationRepository { | ||
override suspend fun getLastKnownLocation(): LocationResult? { | ||
// Verificamos permisos | ||
val hasPermission = | ||
ContextCompat.checkSelfPermission( | ||
context, | ||
Manifest.permission.ACCESS_COARSE_LOCATION, | ||
) == PackageManager.PERMISSION_GRANTED | ||
|
||
// Sin permisos retornamos null | ||
if (!hasPermission) return null | ||
|
||
return suspendCancellableCoroutine { continuation -> | ||
// Intentamos obtener la última ubacación conocida | ||
fusedLocationProviderClient.lastLocation | ||
.addOnSuccessListener { location -> | ||
// Si obtenemos una ubicación válida, retomamos la coroutine con la ubicación | ||
if (location != null) { | ||
continuation.resume((LocationResult(location.latitude, location.longitude))) | ||
} else { | ||
// Si la ubicacion no esta disponible, retomamos la coroutine con null | ||
continuation.resume(null) | ||
} | ||
} | ||
.addOnFailureListener { | ||
// En caso de error, retomamos la coroutine con null | ||
continuation.resume(null) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object FusedLocationProviderModule { | ||
@Provides | ||
fun provideFusedLocationProviderClient( | ||
@ApplicationContext context: Context, | ||
): FusedLocationProviderClient { | ||
return LocationServices.getFusedLocationProviderClient(context) | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
app/src/main/java/ar/edu/unlam/mobile/scaffolding/domain/models/LocationResult.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,6 @@ | ||
package ar.edu.unlam.mobile.scaffolding.domain.models | ||
|
||
data class LocationResult( | ||
val latitude: Double, | ||
val longitude: Double, | ||
) |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/ar/edu/unlam/mobile/scaffolding/domain/repository/LocationRepository.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,7 @@ | ||
package ar.edu.unlam.mobile.scaffolding.domain.repository | ||
|
||
import ar.edu.unlam.mobile.scaffolding.domain.models.LocationResult | ||
|
||
interface LocationRepository { | ||
suspend fun getLastKnownLocation(): LocationResult? | ||
} |
70 changes: 64 additions & 6 deletions
70
app/src/main/java/ar/edu/unlam/mobile/scaffolding/domain/services/GameService.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,19 +1,77 @@ | ||
package ar.edu.unlam.mobile.scaffolding.domain.services | ||
|
||
import android.content.Context | ||
import android.hardware.Sensor | ||
import android.hardware.SensorEvent | ||
import android.hardware.SensorEventListener | ||
import android.hardware.SensorManager | ||
import ar.edu.unlam.mobile.scaffolding.domain.models.Dice | ||
import ar.edu.unlam.mobile.scaffolding.domain.usecases.GameUseCases | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import javax.inject.Inject | ||
import kotlin.math.sqrt | ||
|
||
class GameService | ||
@Inject | ||
constructor() : GameUseCases { | ||
constructor( | ||
@ApplicationContext context: Context, | ||
) : GameUseCases { | ||
private val diceList = listOf(Dice.ONE, Dice.TWO, Dice.THREE, Dice.FOUR, Dice.FIVE, Dice.SIX) | ||
private val sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager | ||
private val accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) | ||
private var shakeDetected = false | ||
private var currentAcceleration = 0f | ||
private var lastAcceleration = 0f | ||
private var acceleration = 0f | ||
private val targetAcceleration = 40f | ||
|
||
override fun getRandomDicePair(): Pair<Dice, Dice> { | ||
val firstDice = diceList.random() | ||
val secondDice = diceList.random() | ||
return Pair(firstDice, secondDice) | ||
} | ||
override fun getRandomDicePair(): Flow<Pair<Dice, Dice>> = | ||
flow { | ||
val firstDice = diceList.random() | ||
val secondDice = diceList.random() | ||
throwDices() | ||
while (shakeDetected.not()) { | ||
} | ||
shakeDetected = false | ||
emit(Pair(firstDice, secondDice)) | ||
} | ||
|
||
override fun getDiceThrowResult(dicePair: Pair<Dice, Dice>): Int = dicePair.first.value + dicePair.second.value | ||
|
||
private fun throwDices() { | ||
sensorManager.registerListener( | ||
sensorEventListener, | ||
accelerometer, | ||
SensorManager.SENSOR_DELAY_GAME, | ||
) | ||
} | ||
|
||
private val sensorEventListener = | ||
object : SensorEventListener { | ||
override fun onAccuracyChanged( | ||
sensor: Sensor, | ||
accuracy: Int, | ||
) { | ||
} | ||
|
||
override fun onSensorChanged(event: SensorEvent) { | ||
when (event.sensor.type) { | ||
Sensor.TYPE_ACCELEROMETER -> { | ||
val x = event.values[0] | ||
val y = event.values[1] | ||
val z = event.values[2] | ||
lastAcceleration = currentAcceleration | ||
currentAcceleration = sqrt((x * x + y * y + z * z).toDouble()).toFloat() | ||
val delta: Float = currentAcceleration - lastAcceleration | ||
acceleration = acceleration * 0.9f + delta | ||
if (acceleration > targetAcceleration) { | ||
sensorManager.unregisterListener(this) | ||
shakeDetected = true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
app/src/main/java/ar/edu/unlam/mobile/scaffolding/domain/usecases/GameUseCases.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,9 +1,10 @@ | ||
package ar.edu.unlam.mobile.scaffolding.domain.usecases | ||
|
||
import ar.edu.unlam.mobile.scaffolding.domain.models.Dice | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface GameUseCases { | ||
fun getRandomDicePair(): Pair<Dice, Dice> | ||
fun getRandomDicePair(): Flow<Pair<Dice, Dice>> | ||
|
||
fun getDiceThrowResult(dicePair: Pair<Dice, Dice>): Int | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/ar/edu/unlam/mobile/scaffolding/ui/screens/AppNavHost.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,32 @@ | ||
package ar.edu.unlam.mobile.scaffolding.ui.screens | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.navigation.compose.NavHost | ||
import androidx.navigation.compose.composable | ||
import androidx.navigation.compose.rememberNavController | ||
|
||
@Composable | ||
fun AppNavHost() { | ||
val controller = rememberNavController() | ||
NavHost( | ||
navController = controller, | ||
startDestination = "captureUserPhoto", | ||
) { | ||
composable("home") { | ||
HomeScreen(navController = controller) | ||
} | ||
composable("captureUserPhoto") { | ||
CaptureUserScreen( | ||
navigateToGame = { | ||
controller.navigate("home") | ||
}, | ||
) | ||
} | ||
composable("game_screen") { | ||
GameScreen(navController = controller) | ||
} | ||
composable("location_screen") { | ||
LocationScreen(navController = controller) | ||
} | ||
} | ||
} |
Oops, something went wrong.