Skip to content

Commit

Permalink
Pulizia codice
Browse files Browse the repository at this point in the history
# Finalizzare Room + LineGraph + MainActivity
* testare che effettivamente rimangano solo 5 minuti di registrazione dati nel Room DB
* correggere indicizzazione asse x
* cominciare a registrare dati solo dopo completa visualizzazione preview
* rendere più leggibili scritte in RGB Values
* creare string per italiano

## migliorie
* fotocamera, si può migliorare,
   - controllare `.let` se hai implementato correttamente come su Android Developers
* correggere status bar (modalità scura/chiara-land/port)
* edge to edge
* controlla progetto github per ulteriori migliorie
  • Loading branch information
bellins14 committed Jun 28, 2024
1 parent b594659 commit 6a35656
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<uses-permission android:name="android.permission.CAMERA" />

<application
android:allowBackup="true"
android:allowBackup="false"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/java/com/example/hueharvester/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import android.content.Context
import kotlinx.coroutines.CoroutineScope

@Database(entities = [ColorData::class], version = 2, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
Expand All @@ -15,8 +14,7 @@ abstract class AppDatabase : RoomDatabase() {
private var INSTANCE: AppDatabase? = null

fun getDatabase(
context: Context,
scope: CoroutineScope
context: Context
): AppDatabase {
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
Expand Down
50 changes: 28 additions & 22 deletions app/src/main/java/com/example/hueharvester/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main)

// Initialize database
database = AppDatabase.getDatabase(this, applicationScope)
database = AppDatabase.getDatabase(this)
repository = ColorDataRepository(database.colorDataDao())

// Initialize OpenCV
Expand Down Expand Up @@ -103,8 +103,9 @@ class MainActivity : AppCompatActivity() {
applicationScope.launch {
while (true) {
val lastData = repository.getLastInsertedData()
// Delete data older than 5 minutes before the last data acquisition
lastData?.let { repository.deleteOldData(it.timestamp - (5 * 60 * 1000) ) }
Log.d(TAG, "Deleted old data")
Log.d(TAG, "Deleted too old data")
delay(60000)
}
}
Expand Down Expand Up @@ -155,6 +156,7 @@ class MainActivity : AppCompatActivity() {

private fun initializeCameraAsync(holder: SurfaceHolder) {
Thread {
//Log.d(TAG, "${} - Thread started")
try {
if (isCameraReleased) {
camera = Camera.open()
Expand Down Expand Up @@ -207,26 +209,7 @@ class MainActivity : AppCompatActivity() {
//Log.d(TAG, "Preview callback")
}

applicationScope.launch {
// Save color data to database
val colorData = ColorData(
timestamp = System.currentTimeMillis(),
red = avgRed,
green = avgGreen,
blue = avgBlue
)
repository.insert(colorData)

// Update line graph
val lastFiveMinData = repository.getDataAfter(
colorData.timestamp - (5 * 60 * 1000)
)
runOnUiThread{
if (lineGraphFragment.view != null) {
lineGraphFragment.updateGraph(lastFiveMinData)
}
}
}
manageNewData(avgRed, avgGreen, avgBlue)
}
Log.d(TAG, "Camera setup successful")
} catch (e: Exception) {
Expand All @@ -236,6 +219,29 @@ class MainActivity : AppCompatActivity() {
}.start()
}

private fun manageNewData(avgRed: Int, avgGreen: Int, avgBlue: Int) {
applicationScope.launch {
// Save color data to database
val colorData = ColorData(
timestamp = System.currentTimeMillis(),
red = avgRed,
green = avgGreen,
blue = avgBlue
)
repository.insert(colorData)

// Update line graph
val lastFiveMinData = repository.getDataAfter(
colorData.timestamp - (5 * 60 * 1000)
)
runOnUiThread{
if (lineGraphFragment.view != null) {
lineGraphFragment.updateGraph(lastFiveMinData)
}
}
}
}

private fun adjustCameraOrientation() {
val rotation = windowManager.defaultDisplay.rotation
var degrees = 0
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/xml/data_extraction_rules.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-->
<data-extraction-rules>
<cloud-backup>
<!-- TODO: Use <include> and <exclude> to control what is backed up.
<!--
<include .../>
<exclude .../>
-->
Expand Down

0 comments on commit 6a35656

Please sign in to comment.