Skip to content

Commit

Permalink
sqlAndroid
Browse files Browse the repository at this point in the history
  • Loading branch information
Tharyck Gusmao committed Jun 20, 2022
1 parent 0f61e06 commit d5c7ee1
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 24 deletions.
3 changes: 3 additions & 0 deletions android/fitnesstracker/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Fitnesstracker">
<activity
android:name=".TmbActivity"
android:exported="false" />
<activity
android:name=".ImcActivity"
android:exported="false" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,54 @@ class ImcActivity : AppCompatActivity() {
btnSend.setOnClickListener {

val validForm = validate();
if(!validForm){
Toast.makeText(this,R.string.field_message,Toast.LENGTH_SHORT).show();
if (!validForm) {
Toast.makeText(this, R.string.field_message, Toast.LENGTH_SHORT).show();

}else{
} else {

val sHeight: String = editHeight.text.toString();
val sWeight: String = editWeight.text.toString();

val height: Int = Integer.parseInt(sHeight);
val weight: Int = Integer.parseInt(sWeight);

val resultImc: Double = calculateImc(height,weight);
val resultImc: Double = calculateImc(height, weight);

var imcResponseId = imcResponse(resultImc);


var dialog = AlertDialog.Builder(this).setTitle(getString(R.string.imc_response,resultImc))
.setMessage(imcResponseId).setPositiveButton(android.R.string.ok,
{ dialogInterface, i -> {
var dialog =
AlertDialog.Builder(this).setTitle(getString(R.string.imc_response, resultImc))
.setMessage(imcResponseId).setPositiveButton(android.R.string.ok,
{ dialogInterface, i ->
{

}
}).create()
}
}).setNegativeButton(R.string.save) { _, _ ->
run {


Thread {
val databaseHandler: SqlHelper = SqlHelper(this)
val calcId = databaseHandler.addItem("imc", resultImc)
runOnUiThread {

if (calcId > 0) {
Toast.makeText(this, R.string.saved, Toast.LENGTH_SHORT)
}

}
}.start()

}
}.create()

dialog.show()

val inm :InputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager;
inm.hideSoftInputFromWindow(editWeight.windowToken,0)
inm.hideSoftInputFromWindow(editHeight.windowToken,0)
val inm: InputMethodManager =
getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager;
inm.hideSoftInputFromWindow(editWeight.windowToken, 0)
inm.hideSoftInputFromWindow(editHeight.windowToken, 0)

}

Expand All @@ -65,9 +85,9 @@ class ImcActivity : AppCompatActivity() {
}

@StringRes
private fun imcResponse(imc: Double): Int{
private fun imcResponse(imc: Double): Int {

return when{
return when {
imc < 15 -> R.string.imc_severely_low_weight
imc < 16 -> R.string.imc_very_low_weight
imc < 18.5 -> R.string.imc_low_weight
Expand All @@ -80,14 +100,15 @@ class ImcActivity : AppCompatActivity() {

}

private fun calculateImc(height: Int,weight: Int):Double{
return weight / ((height.toDouble() / 100 ) * ( height.toDouble() / 100 ))
private fun calculateImc(height: Int, weight: Int): Double {
return weight / ((height.toDouble() / 100) * (height.toDouble() / 100))
}

private fun validate(): Boolean {
return (!editHeight.text.toString().startsWith("0") && !editWeight.text.toString()
.startsWith("0") && !editHeight.text.toString()
.isEmpty() && !editWeight.text.toString().isEmpty()
)
.startsWith("0") && !editHeight.text.toString()
.isEmpty() && !editWeight.text.toString().isEmpty()
)
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fitnesstracker

import android.app.ActivityOptions
import android.content.Context
import android.content.Intent
import android.graphics.Color
Expand Down Expand Up @@ -41,9 +42,13 @@ class MainActivity : AppCompatActivity() {
rvMain.layoutManager = GridLayoutManager(this,2)
rvMain.adapter = MainAdapter(mainItems,this, object : OnItemClickListener {;
override fun onClick(id: Int) {
//Implementar when
val intent = Intent(this@MainActivity, ImcActivity::class.java );
startActivity(intent)
when(id){
1 -> startActivity(Intent(this@MainActivity, ImcActivity::class.java ),
ActivityOptions.makeSceneTransitionAnimation(this@MainActivity).toBundle())
else -> startActivity(Intent(this@MainActivity, TmbActivity::class.java ),
ActivityOptions.makeSceneTransitionAnimation(this@MainActivity).toBundle())
}

}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.fitnesstracker

import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.util.Log
import java.text.SimpleDateFormat
import java.util.*

class SqlHelper(private val context: Context):
SQLiteOpenHelper(context,DATABASE_NAME,null,DATABASE_VERSION) {

companion object {
private const val DATABASE_VERSION = 2
private const val DATABASE_NAME = "data"
}
private lateinit var INSTANCE: SqlHelper;
// fun getInstance(context:Context):SqlHelper{
// if(INSTANCE==null){
// INSTANCE = SqlHelper(context);
// }
// return INSTANCE
// }

override fun onCreate(db: SQLiteDatabase?) {
if (db != null) {
db.execSQL(
"CREATE TABLE calc (id INTEGER primary key, type_calc TEXT, res DECIMAL, created_date DATETIME)"
)
}
}

override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int) {
Log.d("Teste","Upgrade disparado")
}

fun addItem(type:String,response:Double):Long{
val db:SQLiteDatabase = writableDatabase

val calcId:Long = 0;
try {
db.beginTransaction()
val values = ContentValues();
values.put("type_calc",type);
values.put("res",response);

val formatDate = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale("pt","BR"))
val now = formatDate.format(Date())
values.put("created_date",now);
db.insertOrThrow("calc",null,values)
db.setTransactionSuccessful()

}catch (e:Exception ){
Log.e("SQLite",e.message,e)

}finally {
if(db.isOpen){
db.endTransaction()
}
}

return calcId


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.fitnesstracker

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class TmbActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_tmb)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".TmbActivity">

</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 2 additions & 0 deletions android/fitnesstracker/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@
<string name="imc_extreme_weight">Extremamente Obeso</string>

<string name="imc_response">Imc: %1$.2f</string>
<string name="save">Salvar</string>
<string name="saved">Salvo</string>

</resources>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Jun 19 13:12:15 BRT 2022
#Sun Jun 19 21:01:34 BRT 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

0 comments on commit d5c7ee1

Please sign in to comment.