diff --git a/app/src/main/java/edu/nitt/delta/orientation22/compose/Utils.kt b/app/src/main/java/edu/nitt/delta/orientation22/compose/Utils.kt index 9896892..a78bd71 100644 --- a/app/src/main/java/edu/nitt/delta/orientation22/compose/Utils.kt +++ b/app/src/main/java/edu/nitt/delta/orientation22/compose/Utils.kt @@ -1,6 +1,7 @@ package edu.nitt.delta.orientation22.compose import android.Manifest +import android.annotation.SuppressLint import android.content.Context import android.content.Intent import android.content.pm.PackageManager @@ -8,6 +9,7 @@ import android.location.Location import android.net.Uri import android.provider.Settings import android.widget.Toast +import androidx.activity.viewModels import androidx.annotation.DrawableRes import androidx.compose.animation.animateColor import androidx.compose.animation.core.animateFloat @@ -56,6 +58,7 @@ import androidx.compose.ui.window.Dialog import androidx.core.app.ActivityCompat import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleEventObserver +import androidx.lifecycle.ViewModelProvider import androidx.navigation.NavController import com.google.accompanist.permissions.ExperimentalPermissionsApi import com.google.accompanist.permissions.MultiplePermissionsState @@ -65,8 +68,18 @@ import com.google.android.gms.maps.model.LatLng import edu.nitt.delta.orientation22.ArActivity import edu.nitt.delta.orientation22.R import edu.nitt.delta.orientation22.compose.navigation.NavigationRoutes +import edu.nitt.delta.orientation22.di.api.ApiInterface +import edu.nitt.delta.orientation22.di.api.ApiRoutes +import edu.nitt.delta.orientation22.di.api.ResponseConstants +import edu.nitt.delta.orientation22.di.viewModel.uiState.ArStateViewModel +import edu.nitt.delta.orientation22.models.game.LocationRequest import edu.nitt.delta.orientation22.ui.theme.* +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import okhttp3.OkHttpClient +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory fun Context.toast(message: CharSequence) @@ -122,7 +135,16 @@ fun openAr( ) { when{ permissionState.hasPermission -> { - distanceCalculator(fusedLocationProviderClient, mContext, currentClueLocation,glbUrl,anchorHash,currentScale, currentLevel) + CoroutineScope(Dispatchers.IO).launch { + distanceCalculator( + fusedLocationProviderClient, + mContext, + glbUrl, + anchorHash, + currentScale, + currentLevel + ) + } } permissionState.shouldShowRationale -> { mContext.toast("Camera Access is required for AR Explore.") @@ -237,47 +259,49 @@ fun ClueAlertBox(clueName: String, } } -fun distanceCalculator( +@SuppressLint("SuspiciousIndentation") +suspend fun distanceCalculator( fusedLocationProviderClient: FusedLocationProviderClient, mContext: Context, - currentClueLocation: LatLng, glbUrl: String, anchorHash: String, currentScale: Double, currentLevel: Int, ){ - val innerRadius = 40 - val outerRadius = 70 - if ( ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ) { val location = fusedLocationProviderClient.lastLocation location.addOnSuccessListener { try { - val results = FloatArray(1) - Location.distanceBetween( - it.latitude, - it.longitude, - currentClueLocation.latitude, - currentClueLocation.longitude, - results + val retrofit=Retrofit.Builder() + .client(OkHttpClient()) + .baseUrl(ApiRoutes.BASE_URL) + .addConverterFactory(GsonConverterFactory.create()) + .build() + val apiInterface=retrofit.create(ApiInterface::class.java) + val locationRequest = LocationRequest( + latitude = it.latitude, + longitude = it.longitude ) - val distanceInMeters = results[0] -// mContext.toast(distanceInMeters.toString()) - if (distanceInMeters <= innerRadius) { - val intent = Intent(mContext, ArActivity::class.java) - intent.putExtra("glb", glbUrl) - intent.putExtra("anchorHash", anchorHash) - intent.putExtra("anchorScale", currentScale) - intent.putExtra("level", currentLevel) - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - mContext.startActivity(intent) - } else if (distanceInMeters <= outerRadius){ - mContext.toast("You are almost there.") - } else { - mContext.toast("You are too far from your current clue.") - } + CoroutineScope(Dispatchers.IO).launch { + val response=apiInterface.checkDistance(locationRequest) + if(response.message==ResponseConstants.NEAR_OBJECT){ + val intent = Intent(mContext, ArActivity::class.java) + intent.putExtra("glb", glbUrl) + intent.putExtra("anchorHash", anchorHash) + intent.putExtra("anchorScale", currentScale) + intent.putExtra("level", currentLevel) + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + mContext.startActivity(intent) + } + else if(response.message==ResponseConstants.IN_RADIUS){ + mContext.toast("You are almost there. Keep looking!") + } + else { + mContext.toast("You are too far from your current clue!") + } + } } catch (e:Exception){ mContext.toast("Turn On Location Service") } diff --git a/app/src/main/java/edu/nitt/delta/orientation22/compose/screens/ArScreen.kt b/app/src/main/java/edu/nitt/delta/orientation22/compose/screens/ArScreen.kt index 8cdbf49..d4f26a9 100644 --- a/app/src/main/java/edu/nitt/delta/orientation22/compose/screens/ArScreen.kt +++ b/app/src/main/java/edu/nitt/delta/orientation22/compose/screens/ArScreen.kt @@ -37,8 +37,17 @@ import edu.nitt.delta.orientation22.R import edu.nitt.delta.orientation22.compose.SpeedDialData import edu.nitt.delta.orientation22.compose.SpeedDialFloatingActionButton import edu.nitt.delta.orientation22.compose.toast +import edu.nitt.delta.orientation22.di.api.ApiInterface +import edu.nitt.delta.orientation22.di.api.ApiRoutes +import edu.nitt.delta.orientation22.models.game.CheckAnswerRequest import edu.nitt.delta.orientation22.ui.theme.* import io.github.sceneview.ar.ArSceneView +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import okhttp3.OkHttpClient +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory @Composable fun ArScreen( @@ -162,18 +171,27 @@ fun SubmitPopUp(isPopUp : MutableState, onClick: () -> Unit, answer: St Spacer(modifier = Modifier.height((screenHeight / 40).dp)) TextButton( onClick = { - if (answer.equals(text, ignoreCase = true)){ - isPopUp.value = false - mContext.toast("Congratulations!! You've completed this level.") - if (!isCorrect.value){ - onClick() - isCorrect.value = true - } - onBack() - } - else{ - mContext.toast("Wrong code. Please check and try again!") + val retrofit= Retrofit.Builder() + .client(OkHttpClient()) + .baseUrl(ApiRoutes.BASE_URL) + .addConverterFactory(GsonConverterFactory.create()) + .build() + val apiInterface = retrofit.create(ApiInterface::class.java) + CoroutineScope(Dispatchers.IO).launch { + val response = apiInterface.checkAnswer(CheckAnswerRequest(text)) + if(response.message=="correct"){ + isPopUp.value = false + mContext.toast("Congratulations! You've completed this level.") + if (!isCorrect.value){ + onClick() + isCorrect.value = true } + onBack() + } + else{ + mContext.toast("Wrong code. Please try again!") + } + } }, shape = RoundedCornerShape(5.dp), colors = ButtonDefaults.buttonColors(backgroundColor = cyan) diff --git a/app/src/main/java/edu/nitt/delta/orientation22/di/api/ApiInterface.kt b/app/src/main/java/edu/nitt/delta/orientation22/di/api/ApiInterface.kt index 6756e42..c370c97 100644 --- a/app/src/main/java/edu/nitt/delta/orientation22/di/api/ApiInterface.kt +++ b/app/src/main/java/edu/nitt/delta/orientation22/di/api/ApiInterface.kt @@ -3,8 +3,11 @@ package edu.nitt.delta.orientation22.di.api import edu.nitt.delta.orientation22.models.IsLiveResponse import edu.nitt.delta.orientation22.models.IsRegisteredResponse import edu.nitt.delta.orientation22.models.auth.* +import edu.nitt.delta.orientation22.models.game.CheckAnswerRequest import edu.nitt.delta.orientation22.models.game.AnswerResponse import edu.nitt.delta.orientation22.models.game.CurrentLocationResponse +import edu.nitt.delta.orientation22.models.game.DistanceCheckResponse +import edu.nitt.delta.orientation22.models.game.LocationRequest import edu.nitt.delta.orientation22.models.game.PostAnswerRequest import edu.nitt.delta.orientation22.models.game.RouteResponse import edu.nitt.delta.orientation22.models.leaderboard.LeaderboardResponse @@ -57,4 +60,14 @@ interface ApiInterface { suspend fun isLive( @Body token: TokenRequestModel ):IsLiveResponse + + @POST(ApiRoutes.CHECK_DISTANCE) + suspend fun checkDistance( + @Body coordinates: LocationRequest + ):DistanceCheckResponse + + @POST(ApiRoutes.CHECK_ANSWER) + suspend fun checkAnswer( + @Body answer: CheckAnswerRequest + ): DistanceCheckResponse } diff --git a/app/src/main/java/edu/nitt/delta/orientation22/di/api/ApiRoutes.kt b/app/src/main/java/edu/nitt/delta/orientation22/di/api/ApiRoutes.kt index fc021fb..d9b4422 100644 --- a/app/src/main/java/edu/nitt/delta/orientation22/di/api/ApiRoutes.kt +++ b/app/src/main/java/edu/nitt/delta/orientation22/di/api/ApiRoutes.kt @@ -1,7 +1,7 @@ package edu.nitt.delta.orientation22.di.api object ApiRoutes { - const val BASE_URL = "https://utopia.nitt.edu/" + const val BASE_URL = "http://localhost:8000/" const val LEADERBOARD = "api/leaderboard" const val REGISTER_TEAM ="api/registerTeam" @@ -12,4 +12,6 @@ object ApiRoutes { const val AUTH_CALLBACK = "api/auth/callback" const val IS_REGISTERED = "api/isRegistered" const val IS_LIVE="api/isLive" + const val CHECK_DISTANCE="api/checkDistance" + const val CHECK_ANSWER="api/checkAnswer" } diff --git a/app/src/main/java/edu/nitt/delta/orientation22/di/api/ResponseConstants.kt b/app/src/main/java/edu/nitt/delta/orientation22/di/api/ResponseConstants.kt index 49f4174..021ab4b 100644 --- a/app/src/main/java/edu/nitt/delta/orientation22/di/api/ResponseConstants.kt +++ b/app/src/main/java/edu/nitt/delta/orientation22/di/api/ResponseConstants.kt @@ -16,4 +16,7 @@ object ResponseConstants { // const val CALLBACK_RESPONSE_SUCCESS = "User has been authenticated" const val GET_USER_RESPONSE_SUCCESS = "success" const val IS_REGISTERED_SUCCESS = "success" + const val NEAR_OBJECT = "success" + const val IN_RADIUS = "warning" + const val FAR_AWAY = "failure" } \ No newline at end of file diff --git a/app/src/main/java/edu/nitt/delta/orientation22/models/ClientCredentials.kt b/app/src/main/java/edu/nitt/delta/orientation22/models/ClientCredentials.kt index ce6ffb3..27d3a28 100644 --- a/app/src/main/java/edu/nitt/delta/orientation22/models/ClientCredentials.kt +++ b/app/src/main/java/edu/nitt/delta/orientation22/models/ClientCredentials.kt @@ -1,8 +1,8 @@ package edu.nitt.delta.orientation22.models open class ClientCredentials (val param : String) { - object ClientID : ClientCredentials("nu7.9dItKsL0AEzQ") - object RedirectUri : ClientCredentials("https://utopia.nitt.edu/api/auth/callback") + object ClientID : ClientCredentials("m3yrmTh1gfRhRptR") + object RedirectUri : ClientCredentials("http://localhost:8000/api/auth/callback") object State : ClientCredentials("state") object Nonce : ClientCredentials("123456") } \ No newline at end of file diff --git a/app/src/main/java/edu/nitt/delta/orientation22/models/game/CheckAnswerRequest.kt b/app/src/main/java/edu/nitt/delta/orientation22/models/game/CheckAnswerRequest.kt new file mode 100644 index 0000000..2161f97 --- /dev/null +++ b/app/src/main/java/edu/nitt/delta/orientation22/models/game/CheckAnswerRequest.kt @@ -0,0 +1,8 @@ +package edu.nitt.delta.orientation22.models.game + +import com.google.gson.annotations.SerializedName + +data class CheckAnswerRequest ( + @SerializedName("answer") + val answer: String +) \ No newline at end of file diff --git a/app/src/main/java/edu/nitt/delta/orientation22/models/game/DistanceCheckResponse.kt b/app/src/main/java/edu/nitt/delta/orientation22/models/game/DistanceCheckResponse.kt new file mode 100644 index 0000000..1f94274 --- /dev/null +++ b/app/src/main/java/edu/nitt/delta/orientation22/models/game/DistanceCheckResponse.kt @@ -0,0 +1,8 @@ +package edu.nitt.delta.orientation22.models.game + +import com.google.gson.annotations.SerializedName + +data class DistanceCheckResponse ( + @SerializedName("message") + var message: String +) \ No newline at end of file diff --git a/app/src/main/java/edu/nitt/delta/orientation22/models/game/LocationRequest.kt b/app/src/main/java/edu/nitt/delta/orientation22/models/game/LocationRequest.kt new file mode 100644 index 0000000..dbe1977 --- /dev/null +++ b/app/src/main/java/edu/nitt/delta/orientation22/models/game/LocationRequest.kt @@ -0,0 +1,10 @@ +package edu.nitt.delta.orientation22.models.game + +import com.google.gson.annotations.SerializedName + +data class LocationRequest ( + @SerializedName("latitude") + val latitude: Double, + @SerializedName("longitude") + val longitude: Double, +) \ No newline at end of file