Last active
November 15, 2022 07:14
-
-
Save RageshAntony/deda6e39d349ad339a71cfb0c7a90332 to your computer and use it in GitHub Desktop.
BMI App
This file contains hidden or 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
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.appcompat.widget.LinearLayoutCompat | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
tools:context=".MainActivity"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_horizontal" | |
android:layout_marginTop="10dp" | |
android:text="Body Mass Index Calculator" | |
android:textSize="25sp" | |
android:textStyle="bold" /> | |
<TextView | |
android:id="@+id/address" | |
android:layout_margin="30dp" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_horizontal" | |
android:layout_marginTop="10dp" | |
android:text="Address " | |
android:textSize="20sp" | |
android:textStyle="bold" /> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_horizontal" | |
android:layout_marginTop="50dp" | |
android:text="Weight(in Kg)" | |
android:textSize="20dp" | |
android:textStyle="bold" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
app:layout_constraintVertical_bias="0.292" /> | |
<EditText | |
android:id="@+id/etWeight" | |
android:layout_width="150dp" | |
android:layout_height="50dp" | |
android:layout_gravity="center_horizontal" | |
android:layout_marginTop="20dp" /> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_horizontal" | |
android:layout_marginTop="50dp" | |
android:text="Height (in cm)" | |
android:textSize="20dp" | |
android:textStyle="bold" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
app:layout_constraintVertical_bias="0.292" /> | |
<EditText | |
android:id="@+id/etHeight" | |
android:layout_width="150dp" | |
android:layout_height="50dp" | |
android:layout_gravity="center_horizontal" | |
android:layout_marginTop="20dp" | |
android:textSize="20dp" /> | |
<Button | |
android:id="@+id/calculate_btn" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_horizontal" | |
android:layout_marginTop="40dp" | |
android:backgroundTint="#2CD3BE" | |
android:text="Calculate" /> | |
<TextView | |
android:id="@+id/bmi_tv" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_horizontal" | |
android:layout_marginTop="20dp" | |
android:text="The BMI is " | |
android:textSize="20dp" | |
android:visibility="gone" /> | |
<TextView | |
android:id="@+id/bmi" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_horizontal" | |
android:layout_marginTop="20dp" | |
android:text="BMI" | |
android:textSize="20dp" | |
android:visibility="gone" /> | |
<TextView | |
android:id="@+id/status" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_horizontal" | |
android:layout_marginTop="20dp" | |
android:text="Status" | |
android:textSize="20dp" | |
android:textStyle="bold" | |
android:visibility="gone" /> | |
<Button | |
android:id="@+id/ReCalculate" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_horizontal" | |
android:layout_marginTop="20dp" | |
android:backgroundTint="#EAA268" | |
android:text="Recalculate" | |
android:visibility="gone" /> | |
</androidx.appcompat.widget.LinearLayoutCompat> |
This file contains hidden or 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
package com.example.bmiapp | |
class BMIcalculator { | |
fun calculateBMI(heightCMS: Float, weightKGS: Float ): String { | |
val heightMtr = heightCMS / 100.0 | |
val bmiScore = weightKGS / (heightMtr * heightMtr) | |
return getBMIStatus(bmiScore) | |
} | |
fun getBMIStatus ( bmiScore : Double) : String { | |
return when(bmiScore) { | |
in 0.0..16.0 -> "Very Thin" | |
in 16.0..17.0 -> "Thin" | |
in 17.0..18.5 -> "Under Weight" | |
in 18.5..24.9 -> "Healthy" | |
in 24.9..30.0 -> "Overweight" | |
in 30.0..100.0 -> "Obesity" | |
else -> "Wrong value" | |
} | |
} | |
} |
This file contains hidden or 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
import SwiftUI | |
import shared | |
@available(iOS 15.0, *) | |
struct ContentView: View { | |
let greet = Greeting().greeting() | |
@State private var weightKgs: String = "" | |
@State private var heightCMs: String = "" | |
@State private var bmiShow : Bool = false | |
@State private var bmiStatus : String = "" | |
@available(iOS 15.0, *) | |
var body: some View { | |
if #available(iOS 15.0, *) { | |
VStack (spacing: 30) { | |
Text("Body Mass index calculotor") | |
Text("Weight (KGs)") | |
TextField("Weight", text: $weightKgs) | |
.textFieldStyle(.roundedBorder) | |
Text("Height (CMs)") | |
TextField("Height", text: $heightCMs) | |
.textFieldStyle(.roundedBorder) | |
Button( | |
"Calculate", | |
action: { | |
//some code | |
bmiShow = true | |
bmiStatus = BMIcalculator().calculateBMI(heightCMS: Float(heightCMs)!, weightKGS: Float(weightKgs)!) | |
} | |
) | |
}.padding(30) | |
.alert( | |
"BMI Result is \(bmiStatus)", | |
isPresented: $bmiShow | |
) { | |
Button("Close", action: { bmiShow = false }) | |
} | |
} else { | |
// Fallback on earlier versions | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
if #available(iOS 15.0, *) { | |
ContentView() | |
} else {} | |
} | |
} |
This file contains hidden or 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
package com.example.bmiapp | |
expect class LocationDetails { | |
fun getLocationAddress (locationAddress: LocationAddress) | |
} | |
interface LocationAddress { // just to get async data | |
fun onReceiveLocationAddress(address: String) : String | |
} |
This file contains hidden or 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
package com.example.bmiapp | |
import android.annotation.SuppressLint | |
import android.app.Activity | |
import android.location.Address | |
import android.location.Geocoder | |
import android.location.Location | |
import android.os.Looper | |
import com.google.android.gms.location.* | |
import java.util.* | |
import java.util.concurrent.TimeUnit | |
actual class LocationDetails { | |
lateinit var activity: Activity | |
@SuppressLint("MissingPermission") | |
actual fun getLocationAddress(locationAddress: LocationAddress) { | |
val fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(activity) | |
lateinit var locationCallback: LocationCallback | |
val locationRequest: LocationRequest = LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 1500) | |
.setWaitForAccurateLocation(false) | |
.setMinUpdateIntervalMillis(1000) | |
.setMaxUpdateDelayMillis(2000) | |
.build() | |
locationCallback = object : LocationCallback() { | |
override fun onLocationResult(locationResult: LocationResult) { | |
locationAddress.onReceiveLocationAddress( | |
locationResult.lastLocation?.let { | |
getAddress(it) | |
} ?: "Location error" | |
) } } | |
fusedLocationProviderClient.requestLocationUpdates( | |
locationRequest, | |
locationCallback, | |
Looper.myLooper()!! | |
) } | |
fun getAddress(latLng: Location): String? { | |
val geocoder = Geocoder(activity, Locale.getDefault()) | |
val addresses: List<Address>? = | |
geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1) | |
return if (addresses?.isNotEmpty() == true) { | |
val address = addresses[0] | |
address.getAddressLine(0) | |
} else { | |
"Location not found" | |
} | |
} | |
} |
This file contains hidden or 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
package com.example.bmiapp.android | |
import android.content.pm.PackageManager | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.os.Message | |
import com.example.bmiapp.Greeting | |
import android.widget.TextView | |
import androidx.appcompat.app.AlertDialog | |
import androidx.core.app.ActivityCompat | |
import com.example.bmiapp.BMIcalculator | |
import com.example.bmiapp.LocationDetails | |
import com.example.bmiapp.android.databinding.ActivityMainBinding | |
import com.example.bmiapp.LocationAddress as LocationAddress | |
fun greet(): String { | |
return Greeting().greeting() | |
} | |
class MainActivity : AppCompatActivity() { | |
private lateinit var binding: ActivityMainBinding | |
val requestcode = 123 | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
binding = ActivityMainBinding.inflate(layoutInflater) | |
setContentView(binding.root) | |
binding.calculateBtn.setOnClickListener { | |
val weightKG = binding.etWeight.text.toString().toFloat() | |
val heightCMS = binding.etHeight.text.toString().toFloat() | |
// Call commonMain Class and display in alert | |
val bmiCalculator = BMIcalculator() | |
val bmiResult = bmiCalculator.calculateBMI(heightCMS,weightKG) | |
showAlert(bmiResult) | |
if (isLocationPermissionGranted()) | |
LocationDetails().apply { | |
activity = this@MainActivity | |
getLocationAddress(object : LocationAddress { | |
override fun onReceiveLocationAddress(address: String): String { | |
runOnUiThread { | |
binding.address.text = address | |
} | |
return address | |
} | |
}) | |
} | |
} | |
} | |
fun showAlert(message: String) { | |
AlertDialog.Builder(this) | |
.setTitle("BMI STATUS") | |
.setMessage("You are $message") | |
.show() | |
} | |
private fun isLocationPermissionGranted(): Boolean { | |
return if (ActivityCompat.checkSelfPermission( | |
this, | |
android.Manifest.permission.ACCESS_COARSE_LOCATION | |
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( | |
this, | |
android.Manifest.permission.ACCESS_FINE_LOCATION | |
) != PackageManager.PERMISSION_GRANTED | |
) { | |
ActivityCompat.requestPermissions( | |
this, | |
arrayOf( | |
android.Manifest.permission.ACCESS_FINE_LOCATION, | |
android.Manifest.permission.ACCESS_COARSE_LOCATION | |
), | |
requestcode | |
) | |
false | |
} else { | |
true | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment