Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aliens all around 🛰️🛸 #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 92 additions & 16 deletions app/src/main/java/com/example/mycompanion/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,29 +1,47 @@
package com.example.mycompanion

import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.location.GnssStatus
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.JustifyContent


class MainActivity : AppCompatActivity() {
class MainActivity : AppCompatActivity(), LocationListener {

private lateinit var selectedSport: String
private lateinit var locationManager: LocationManager
private lateinit var gpsStatusCircle: View
private var gpsSignalAcquired = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val suffix = "_emoji"
val filteredResources = getResourcesWithSuffix(suffix)
locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager

gpsStatusCircle = findViewById(R.id.gps_status_circle)

for (resource in filteredResources) {
Log.d("MainActivity", "Resource: $resource")
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 1)
} else {
requestLocationUpdates()
}

// Define the list of sports
val sports = listOf(
Sport(getString(R.string.badminton), getString(R.string.badminton_emoji), getString(R.string.badminton)),
Expand All @@ -46,6 +64,74 @@ class MainActivity : AppCompatActivity() {
recyclerView.adapter = SportAdapter(sports) { sport -> onSportClick(sport) }
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == 1) {
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
requestLocationUpdates()
} else {
// Permission denied, show a message to the user
Log.d("MainActivity", "Permission denied")
}
}
}

private fun requestLocationUpdates() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
gpsStatusCircle.visibility = View.VISIBLE
gpsStatusCircle.setBackgroundResource(R.drawable.yellow_circle)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0f, this)
locationManager.registerGnssStatusCallback(gnssStatusCallback, null)
}
}

private val gnssStatusCallback = object : GnssStatus.Callback() {
override fun onStarted() {
gpsStatusCircle.visibility = View.VISIBLE
gpsStatusCircle.setBackgroundResource(R.drawable.yellow_circle)
gpsSignalAcquired = false
Log.d("MainActivity", "GPS searching for signal")
}

override fun onStopped() {
gpsStatusCircle.visibility = View.GONE
gpsSignalAcquired = false
Log.d("MainActivity", "GPS stopped")
}

override fun onFirstFix(ttffMillis: Int) {
gpsStatusCircle.setBackgroundResource(R.drawable.green_circle)
gpsSignalAcquired = true
Log.d("MainActivity", "GPS signal acquired")
}

override fun onSatelliteStatusChanged(status: GnssStatus) {
if (gpsSignalAcquired) {
gpsStatusCircle.setBackgroundResource(R.drawable.green_circle)
}
}
}

override fun onLocationChanged(location: Location) {
Log.d("MainActivity", "Location: ${location.latitude}, ${location.longitude}")
// GPS signal is acquired, setting the circle to green is handled by onFirstFix
gpsStatusCircle.setBackgroundResource(R.drawable.green_circle)
gpsSignalAcquired = true
}

override fun onProviderEnabled(provider: String) {
gpsStatusCircle.visibility = View.VISIBLE
gpsStatusCircle.setBackgroundResource(R.drawable.yellow_circle)
Log.d("MainActivity", "GPS provider enabled")
}

override fun onProviderDisabled(provider: String) {
gpsStatusCircle.visibility = View.GONE
gpsSignalAcquired = false
Log.d("MainActivity", "GPS provider disabled")
}

private fun onSportClick(sport: Sport) {
selectedSport = sport.name
Log.d("MainActivity", "Selected Sport: $selectedSport")
Expand All @@ -56,15 +142,5 @@ class MainActivity : AppCompatActivity() {
}
startActivity(intent)
}
private fun getResourcesWithSuffix(suffix: String): List<String> {
val resources = mutableListOf<String>()
val fields = R.drawable::class.java.fields // You can change this to R.string, R.layout, etc.

for (field in fields) {
if (field.name.endsWith(suffix)) {
resources.add(field.name)
}
}
return resources
}
}
23 changes: 12 additions & 11 deletions app/src/main/java/com/example/mycompanion/SportAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,30 @@ import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class SportAdapter(private val sports: List<Sport>, private val onClick: (Sport) -> Unit) :
RecyclerView.Adapter<SportAdapter.SportViewHolder>() {
class SportAdapter(
private val sports: List<Sport>,
private val clickListener: (Sport) -> Unit
) : RecyclerView.Adapter<SportAdapter.SportViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SportViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_sport, parent, false)
return SportViewHolder(view)
}

override fun onBindViewHolder(holder: SportViewHolder, position: Int) {
holder.bind(sports[position])
holder.bind(sports[position], clickListener)
}

override fun getItemCount(): Int = sports.size

inner class SportViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val emojiTextView: TextView = itemView.findViewById(R.id.emojiTextView)
private val nameTextView: TextView = itemView.findViewById(R.id.nameTextView)
class SportViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val sportName: TextView = itemView.findViewById(R.id.sport_name)
private val sportEmoji: TextView = itemView.findViewById(R.id.sport_emoji)

fun bind(sport: Sport) {
emojiTextView.text = sport.emoji
nameTextView.text = sport.name
itemView.tag = sport.tag
itemView.setOnClickListener { onClick(sport) }
fun bind(sport: Sport, clickListener: (Sport) -> Unit) {
sportName.text = sport.name
sportEmoji.text = sport.emoji
itemView.setOnClickListener { clickListener(sport) }
}
}
}
19 changes: 18 additions & 1 deletion app/src/main/java/com/example/mycompanion/TimerActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.mycompanion

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
Expand Down Expand Up @@ -58,7 +59,23 @@ class TimerActivity : AppCompatActivity() {

stopButton.setOnClickListener {
Log.d("TimerActivity", "Training stopped at ${timerTextView.text}")
finishAffinity()
running = false
handler.removeCallbacks(runnable)
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish() // Optional: Call this if you want to remove TimerActivity from the back stack
}
}

override fun onPause() {
super.onPause()
handler.removeCallbacks(runnable)
}

override fun onResume() {
super.onResume()
if (running) {
handler.post(runnable)
}
}
}
4 changes: 4 additions & 0 deletions app/src/main/res/drawable/green_circle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/colorAccent" />
</shape>
4 changes: 4 additions & 0 deletions app/src/main/res/drawable/yellow_circle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/colorYellow" />
</shape>
35 changes: 28 additions & 7 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
tools:context=".MainActivity"
android:background="@color/colorBackground">

<LinearLayout
android:layout_width="match_parent"
Expand All @@ -12,17 +13,37 @@
android:padding="16dp"
android:gravity="center">

<TextView
android:layout_width="wrap_content"
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/select_sport"
android:textSize="24sp"
android:layout_marginBottom="16dp"/>
android:layout_marginBottom="16dp">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/select_sport"
android:textSize="24sp"
android:textColor="@color/colorPrimary"
android:layout_centerHorizontal="true"
android:fontFamily="sans-serif-medium"
android:layout_marginBottom="16dp"/>

<View
android:id="@+id/gps_status_circle"
android:layout_width="24dp"
android:layout_height="24dp"
android:background="@drawable/yellow_circle"
android:visibility="gone"
android:layout_alignParentEnd="true"
android:layout_marginEnd="16dp"
android:layout_centerVertical="true"/>
</RelativeLayout>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" />
android:layout_gravity="center"/>
</LinearLayout>
</ScrollView>
33 changes: 22 additions & 11 deletions app/src/main/res/layout/item_sport.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_margin="8dp">
android:orientation="horizontal"
android:padding="8dp"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true">

<TextView
android:id="@+id/emojiTextView"
android:layout_width="64dp"
android:layout_height="64dp"
android:textSize="48sp"
android:id="@+id/sport_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="?android:attr/textColorPrimary"
android:background="@color/buttonBackground"
android:textSize="18sp"
android:padding="8dp"
android:gravity="center"
android:contentDescription="@string/emoji" />
android:fontFamily="sans-serif-medium"/>

<TextView
android:id="@+id/nameTextView"
android:id="@+id/sport_emoji"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp" />
android:textColor="?android:attr/textColorPrimary"
android:background="@color/buttonBackground"
android:textSize="18sp"
android:padding="8dp"
android:gravity="center"
android:fontFamily="sans-serif-medium"/>
</LinearLayout>
12 changes: 12 additions & 0 deletions app/src/main/res/values-night/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<resources>
<color name="colorPrimary">#FF5722</color>
<color name="colorPrimaryDark">#E64A19</color>
<color name="colorAccent">#4CAF50</color>
<color name="colorBackground">#121212</color>
<color name="colorTextPrimary">#FFFFFF</color>
<color name="colorTextSecondary">#B0B0B0</color>
<color name="colorGreen">#4CAF50</color>
<color name="colorYellow">#FFEB3B</color>
<color name="buttonBackground">#FF5722</color> <!-- For button background -->
<color name="buttonTextColor">#FFFFFF</color> <!-- For button text -->
</resources>
11 changes: 11 additions & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,15 @@
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>

<color name="colorPrimary">#FF5722</color>
<color name="colorPrimaryDark">#E64A19</color>
<color name="colorAccent">#4CAF50</color>
<color name="colorBackground">#F5F5F5</color>
<color name="colorTextPrimary">#212121</color>
<color name="colorTextSecondary">#757575</color>
<color name="colorGreen">#4CAF50</color>
<color name="colorYellow">#FFEB3B</color>
<color name="buttonBackground">#FF5722</color> <!-- For button background -->
<color name="buttonTextColor">#FFFFFF</color> <!-- For button text -->
</resources>
12 changes: 12 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<resources>

<!-- Base application theme -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorPrimary">@color/colorTextPrimary</item>
</style>

</resources>