summaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/src/main/AndroidManifest.xml11
-rw-r--r--app/src/main/java/com/example/alcagotchi/AlcoGotchi.kt16
-rw-r--r--app/src/main/java/com/example/alcagotchi/DrinkingActivity.kt52
-rw-r--r--app/src/main/java/com/example/alcagotchi/DrivingActivity.kt2
-rw-r--r--app/src/main/java/com/example/alcagotchi/MainActivity.kt6
-rw-r--r--app/src/main/java/com/example/alcagotchi/Shop.kt118
6 files changed, 192 insertions, 13 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index da51c01..e135506 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -57,6 +57,17 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
+ <activity
+ android:name=".ShopActivity"
+ android:exported="true"
+ android:label="@string/app_name"
+ android:theme="@style/Theme.AlcaGotchi">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
</application>
</manifest> \ No newline at end of file
diff --git a/app/src/main/java/com/example/alcagotchi/AlcoGotchi.kt b/app/src/main/java/com/example/alcagotchi/AlcoGotchi.kt
index e5cd130..0d498b0 100644
--- a/app/src/main/java/com/example/alcagotchi/AlcoGotchi.kt
+++ b/app/src/main/java/com/example/alcagotchi/AlcoGotchi.kt
@@ -19,6 +19,7 @@ class AlcoGotchi private constructor() {
var baseUrl = "http://192.168.4.1:80"
var coins = 100
+ var drunk = 0
companion object {
@Volatile private var instance: AlcoGotchi? = null
@@ -51,6 +52,7 @@ class AlcoGotchi private constructor() {
val json = JSONObject(response.body!!.string())
coins = json.getJSONObject("data").getInt("alco_coin")
+ drunk = json.getJSONObject("data").getInt("drunk")
}
fun getState() {
@@ -70,6 +72,20 @@ class AlcoGotchi private constructor() {
.post(body.toString().toRequestBody(jsonMediaType))
.build()
+ client.newCall(request).enqueue(basicCallback)
+ return withContext(Dispatchers.IO) {
+ handleStateResponse(client.newCall(request).execute())
+ }
+ }
+ suspend fun postDrink(drink: String) {
+ val body = JSONObject()
+ body.put("drink", drink)
+
+ val request = Request.Builder()
+ .url(buildUrl("drink"))
+ .post(body.toString().toRequestBody(jsonMediaType))
+ .build()
+
return withContext(Dispatchers.IO) {
handleStateResponse(client.newCall(request).execute())
}
diff --git a/app/src/main/java/com/example/alcagotchi/DrinkingActivity.kt b/app/src/main/java/com/example/alcagotchi/DrinkingActivity.kt
index 1d02fea..f59a264 100644
--- a/app/src/main/java/com/example/alcagotchi/DrinkingActivity.kt
+++ b/app/src/main/java/com/example/alcagotchi/DrinkingActivity.kt
@@ -17,9 +17,10 @@ package com.example.alcagotchi
//import kotlinx.coroutines.flow.internal.NoOpContinuation.context
//import kotlin.coroutines.jvm.internal.CompletedContinuation.context
+import android.app.Activity
+import android.app.AlertDialog
import android.content.Intent
import android.os.Bundle
-import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
@@ -33,6 +34,7 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
+import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
@@ -44,14 +46,12 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
+import androidx.lifecycle.ViewModel
+import androidx.lifecycle.viewModelScope
import com.example.alcagotchi.ui.theme.AlcaGotchiTheme
-import kotlinx.coroutines.CoroutineScope
-import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
-import java.io.BufferedReader
-import java.io.InputStreamReader
-import java.net.HttpURLConnection
-import java.net.URL
+import kotlinx.coroutines.runBlocking
+
//import java.net.HttpURLConnection
//import java.net.URL
//
@@ -110,13 +110,39 @@ fun DrinkingGreetingText(message: String, from: String, modifier: Modifier = Mod
)
}
}
+class DrinkingViewModel : ViewModel() {
+ val drunk = mutableIntStateOf(0)
+
+ fun drink(drink_choice: String, context: Activity) {
+ val alcoGotchi = AlcoGotchi.getInstance()
+ viewModelScope.launch {
+ runBlocking {
+ try {
+ alcoGotchi.postDrink(drink_choice)
+ } catch (e: Exception) {
+ val alertDialog = AlertDialog.Builder(context)
+
+ alertDialog.apply {
+ setTitle("no")
+ setMessage("ur broke")
+ }.create().show()
+ }
+
+ drunk.intValue = alcoGotchi.drunk
+ }
+ }
+
+ }
+}
@Composable
-fun DrinkingOptions(modifier: Modifier = Modifier) {
+fun DrinkingOptions(viewModel: DrinkingViewModel, modifier: Modifier = Modifier) {
Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
+ val context = LocalContext.current as Activity
+
Text(
text = "What would you like to drink?",
fontSize = 36.sp,
@@ -126,7 +152,7 @@ fun DrinkingOptions(modifier: Modifier = Modifier) {
.padding(end = 16.dp)
)
Button(onClick = {
- PostHttpRequest("http://192.168.4.1/drink")
+ viewModel.drink("beer", context)
})
{
Text(
@@ -134,7 +160,7 @@ fun DrinkingOptions(modifier: Modifier = Modifier) {
)
}
Button(onClick = {
- PostHttpRequest("http://192.168.4.1/drink")
+ viewModel.drink("whisky", context)
})
{
Text(
@@ -142,7 +168,7 @@ fun DrinkingOptions(modifier: Modifier = Modifier) {
)
}
Button(onClick = {
- PostHttpRequest("http://192.168.4.1/drink")
+ viewModel.drink("wine", context)
})
{
Text(
@@ -150,7 +176,7 @@ fun DrinkingOptions(modifier: Modifier = Modifier) {
)
}
Button(onClick = {
- PostHttpRequest("http://192.168.4.1/drink")
+ viewModel.drink("lemonade", context)
})
{
Text(
@@ -181,7 +207,7 @@ fun DrinkingGreeting(message: String, modifier: Modifier = Modifier) {
}) {
Text("Go back")
}
- DrinkingOptions()
+ DrinkingOptions(viewModel = DrinkingViewModel())
}
}
diff --git a/app/src/main/java/com/example/alcagotchi/DrivingActivity.kt b/app/src/main/java/com/example/alcagotchi/DrivingActivity.kt
new file mode 100644
index 0000000..9fb0cb6
--- /dev/null
+++ b/app/src/main/java/com/example/alcagotchi/DrivingActivity.kt
@@ -0,0 +1,2 @@
+package com.example.alcagotchi
+
diff --git a/app/src/main/java/com/example/alcagotchi/MainActivity.kt b/app/src/main/java/com/example/alcagotchi/MainActivity.kt
index 99c8f4f..1f3f7c0 100644
--- a/app/src/main/java/com/example/alcagotchi/MainActivity.kt
+++ b/app/src/main/java/com/example/alcagotchi/MainActivity.kt
@@ -118,6 +118,12 @@ fun GreetingText(message: String, from: String, modifier: Modifier = Modifier) {
}) {
Text("Drinking")
}
+ Button(onClick = {
+ val intent = Intent(context, ShopActivity::class.java)
+ context.startActivity(intent)
+ }) {
+ Text("Go to shop")
+ }
}
}
@Composable
diff --git a/app/src/main/java/com/example/alcagotchi/Shop.kt b/app/src/main/java/com/example/alcagotchi/Shop.kt
new file mode 100644
index 0000000..7e66d0c
--- /dev/null
+++ b/app/src/main/java/com/example/alcagotchi/Shop.kt
@@ -0,0 +1,118 @@
+package com.example.alcagotchi
+
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+import android.os.Bundle
+import androidx.activity.ComponentActivity
+import androidx.activity.compose.setContent
+import androidx.compose.foundation.Image
+import androidx.compose.foundation.layout.Arrangement
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.padding
+import androidx.compose.material3.Button
+import androidx.compose.material3.MaterialTheme
+import androidx.compose.material3.Surface
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.layout.ContentScale
+import androidx.compose.ui.res.colorResource
+import androidx.compose.ui.res.painterResource
+import androidx.compose.ui.res.stringResource
+import androidx.compose.ui.text.style.TextAlign
+import androidx.compose.ui.tooling.preview.Preview
+import androidx.compose.ui.unit.dp
+import androidx.compose.ui.unit.sp
+import com.example.alcagotchi.ui.theme.AlcaGotchiTheme
+import com.example.alcagotchi.ui.theme.AlcaGotchiTheme
+
+class ShopActivity : ComponentActivity() {
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ setContent {
+ AlcaGotchiTheme {
+ Surface(
+ modifier = Modifier.fillMaxSize(),
+ color = MaterialTheme.colorScheme.background
+ ) {
+ ShopGreeting(
+ stringResource(R.string.welcome_to_casino),
+ )
+ }
+ }
+ }
+ }
+}
+
+@Composable
+fun ShopOptions(modifier: Modifier = Modifier) {
+ Column(
+ verticalArrangement = Arrangement.Center,
+ horizontalAlignment = Alignment.CenterHorizontally
+ ) {
+ Text(
+ text = "Your balance: many coin",
+ fontSize = 36.sp,
+ color = Color(0xFF00FF00),
+ modifier = Modifier
+ .padding(top = 16.dp)
+ .padding(end = 16.dp)
+ )
+ Button(onClick = { /*TODO*/ }) {
+ Text("Buy beer")
+ }
+ Button(onClick = { /*TODO*/ }) {
+ Text("Buy wine")
+ }
+ Button(onClick = { /*TODO*/ }) {
+ Text("Buy lemonade")
+ }
+ Button(onClick = { /*TODO*/ }) {
+ Text("Buy whisky")
+ }
+ }
+}
+
+@Composable
+fun ShopGreeting(message: String, modifier: Modifier = Modifier) {
+ // Create a box to overlap image and texts
+ Box(modifier) {
+ Image(
+ painter = painterResource(id = R.drawable.shop),
+ contentDescription = null,
+ contentScale = ContentScale.Crop,
+ alpha = 1F,
+ modifier = Modifier.fillMaxSize()
+
+
+ )
+ ShopOptions()
+ }
+}
+
+@Preview(showBackground = false)
+@Composable
+private fun ShopPreview() {
+ AlcaGotchiTheme {
+ ShopGreeting(
+ stringResource(R.string.welcome_to_casino),
+ )
+ }
+}