Skip to content

Commit

Permalink
fix conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
eliastomas11 committed Oct 30, 2024
2 parents 2231ec4 + 22380b5 commit b11c528
Show file tree
Hide file tree
Showing 18 changed files with 500 additions and 20 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/android-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ jobs:
run: |
ktlint --reporter=checkstyle,output=build/ktlint-report.xml
continue-on-error: true
- name: Apply formatting with ktlint
if: failure() # Solo aplica formato si ktlint encuentra errores
run: |
ktlint --format
- uses: yutailang0119/action-ktlint@v4
with:
report-path: build/*.xml # Support glob patterns by https://www.npmjs.com/package/@actions/glob
Expand Down
17 changes: 16 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ android {
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
excludes +=
arrayOf(
"/META-INF/{AL2.0,LGPL2.1}",
"META-INF/LICENSE.md",
"META-INF/LICENSE.txt",
"META-INF/NOTICE.md",
"META-INF/NOTICE.txt",
"META-INF/LICENSE-notice.md",
)
}
}
}
Expand Down Expand Up @@ -78,9 +86,16 @@ dependencies {
androidTestImplementation(libs.google.dagger.hilt.android.testing)
testImplementation(libs.google.dagger.hilt.android.testing)

// Coroutine
implementation(libs.coroutine)
implementation(libs.coroutineFlow)

// Coroutine testing
testImplementation(libs.kotlinx.coroutines.test)

// Google play services Location
implementation(libs.google.play.service.location)

// Mockk
testImplementation(libs.mockk)
testImplementation(libs.mockk.android)
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name="ScaffoldingApplication"
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.compose.ui.Modifier
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import ar.edu.unlam.mobile.scaffolding.ui.screens.AppNavHost
import ar.edu.unlam.mobile.scaffolding.ui.screens.CaptureUserScreen
import ar.edu.unlam.mobile.scaffolding.ui.screens.GameScreen
import ar.edu.unlam.mobile.scaffolding.ui.screens.HomeScreen
Expand All @@ -28,7 +29,7 @@ class MainActivity : ComponentActivity() {
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background,
) {
MainScreen()
AppNavHost()
}
}
}
Expand All @@ -40,15 +41,15 @@ fun MainScreen() {
val controller = rememberNavController()
NavHost(navController = controller, startDestination = Routes.GAME_ROUTE) {
composable(Routes.HOME_ROUTE) {
HomeScreen()
HomeScreen(navController = controller)
}
composable(Routes.CAPTURE_USER_PHOTO_ROUTE) {
CaptureUserScreen {
controller.navigate("home")
}
}
composable(Routes.GAME_ROUTE) {
GameScreen()
GameScreen(navController = controller)
}
}
}
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)
}
}
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,
)
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?
}
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
}
}
}
}
}
}
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
}
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)
}
}
}
Loading

0 comments on commit b11c528

Please sign in to comment.