SettingsActivity.kt

PHOTO EMBED

Tue Feb 27 2024 00:20:21 GMT+0000 (Coordinated Universal Time)

Saved by @MAxxxx

package com.example.calcapptoturial

import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.EditText
import android.widget.ImageButton
import android.widget.Spinner
import androidx.appcompat.app.AppCompatActivity

class SettingsActivity : AppCompatActivity() {

    private lateinit var editTextFormativeWeight: EditText
    private lateinit var editTextSummativeWeight: EditText
    private lateinit var sharedPreferences: SharedPreferences

    companion object {
        const val EXTRA_FORMATIVE_WEIGHT = "extra_formative_weight"
        const val EXTRA_SUMMATIVE_WEIGHT = "extra_summative_weight"
    }

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

        // Initialize EditText fields
        editTextFormativeWeight = findViewById(R.id.editTextFormativeWeight)
        editTextSummativeWeight = findViewById(R.id.editTextSummativeWeight)

        sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE)

        // Retrieve current weights from SharedPreferences
        val currentFormativeWeight = sharedPreferences.getFloat("formativeWeight", 40.0f)
        val currentSummativeWeight = sharedPreferences.getFloat("summativeWeight", 60.0f)

        // Set current weights as hints for EditText fields
        editTextFormativeWeight.hint = "Current Formative Weight: $currentFormativeWeight"
        editTextSummativeWeight.hint = "Current Summative Weight: $currentSummativeWeight"

        // Set click listener for the save button
        findViewById<Button>(R.id.btnSave).setOnClickListener {
            saveWeights()
            finish() // Close the settings activity
        }

        val themeOptions = arrayOf(
            "Keller HS Light",
            "Timber Creek HS Light",
            "Fossil Ridge HS Light",
            "Keller Central HS Light",
            "Keller HS Dark",
            "Timber Creek HS Dark",
            "Fossil Ridge HS Dark",
            "Keller Central HS Dark",
            "Default (Light + Orange)"
        )

        val themeSpinner = findViewById<Spinner>(R.id.themeSpinner)
        val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, themeOptions)
        themeSpinner.adapter = adapter

        // Find the close button
        val closeButton = findViewById<ImageButton>(R.id.btnClose)

        // Set click listener for the close button
        closeButton.setOnClickListener {
            // Close the settings activity
            finish()
        }
    }

    private fun saveWeights() {
        // Get the input values from EditText fields
        val formativeWeightInput = editTextFormativeWeight.text.toString()
        val summativeWeightInput = editTextSummativeWeight.text.toString()

        // Convert input values to doubles and save to SharedPreferences
        sharedPreferences.edit().apply {
            putFloat("formativeWeight", formativeWeightInput.toFloatOrNull() ?: 40.0f)
            putFloat("summativeWeight", summativeWeightInput.toFloatOrNull() ?: 60.0f)
            apply()
        }
    }
}
content_copyCOPY