diff --git a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt index 92a8f28..74248b0 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt @@ -18,6 +18,9 @@ import android.view.GestureDetector import android.view.Gravity import android.view.MotionEvent import android.view.View +import android.view.View.TEXT_ALIGNMENT_CENTER +import android.view.View.TEXT_ALIGNMENT_TEXT_END +import android.view.View.TEXT_ALIGNMENT_TEXT_START import android.view.inputmethod.InputMethodManager import android.widget.EditText import android.widget.LinearLayout @@ -28,7 +31,6 @@ import androidx.activity.OnBackPressedCallback import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity import androidx.constraintlayout.widget.ConstraintLayout -import androidx.constraintlayout.widget.ConstraintSet import androidx.core.content.res.ResourcesCompat import androidx.core.view.children import androidx.core.view.marginLeft @@ -74,7 +76,6 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh private lateinit var clock: TextClock private var clockMargin = 0 - private lateinit var clockLayout: ConstraintLayout private lateinit var dateText: TextClock @@ -108,13 +109,11 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh clockMargin = clock.marginLeft - clockLayout = findViewById(R.id.clock_layout) - dateText = findViewById(R.id.text_date) dateElements = mutableListOf(dateText.format12Hour.toString(), dateText.format24Hour.toString(), "", "") - setClockAlignment(preferences.getString("clockAlignment", "left"), clock.id, clockMargin) + setClockAlignment(preferences.getString("clockAlignment", "left")) setupApps() @@ -158,7 +157,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh while (true) { val currentWeather = weatherSystem.getTemp(this@MainActivity) withContext(Dispatchers.Main) { - modifyDate(stringUtils.addEndTextIfNotEmpty(currentWeather, "°C"), 3) + modifyDate(currentWeather, 3) } delay(300000) } @@ -169,7 +168,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh when (key) { "clockAlignment" -> { - setClockAlignment(preferences?.getString(key, "left"), clock.id, clockMargin) + setClockAlignment(preferences?.getString(key, "left")) } "shortcutAlignment" -> { @@ -612,41 +611,22 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh return true } - private fun setClockAlignment(alignment: String?, widgetId: Int, margin: Int) { + private fun setClockAlignment(alignment: String?) { - val constraintSet = ConstraintSet() - constraintSet.clone(clockLayout) - println(alignment) - - if (alignment == "right") { - constraintSet.clear(widgetId, ConstraintSet.START) + when (alignment) { + "left" -> { + clock.textAlignment = TEXT_ALIGNMENT_TEXT_START + dateText.textAlignment = TEXT_ALIGNMENT_TEXT_START + } + "center" -> { + clock.textAlignment = TEXT_ALIGNMENT_CENTER + dateText.textAlignment = TEXT_ALIGNMENT_CENTER + } + "right" -> { + clock.textAlignment = TEXT_ALIGNMENT_TEXT_END + dateText.textAlignment = TEXT_ALIGNMENT_TEXT_END + } } - else if (alignment == "left") { - constraintSet.clear(widgetId, ConstraintSet.END) - } - - if (alignment == "center" || alignment == "left") { - constraintSet.connect( - widgetId, - ConstraintSet.START, - ConstraintSet.PARENT_ID, - ConstraintSet.START, - margin - ) - - } - - if (alignment != "left") { - constraintSet.connect( - widgetId, - ConstraintSet.END, - ConstraintSet.PARENT_ID, - ConstraintSet.END, - margin - ) - } - - constraintSet.applyTo(clockLayout) } private fun setShortcutAlignment(alignment: String?, shortcuts: LinearLayout) { diff --git a/app/src/main/java/eu/ottop/yamlauncher/SharedPreferenceManager.kt b/app/src/main/java/eu/ottop/yamlauncher/SharedPreferenceManager.kt index c7daecf..e2a673b 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/SharedPreferenceManager.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/SharedPreferenceManager.kt @@ -81,146 +81,4 @@ class SharedPreferenceManager { return sharedPreferences.getString(key, "") } - fun setClockAlignment(cont: Context, alignment: Int) { - val editor = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE).edit() - val key = "clock_alignment" - editor.putInt(key, alignment) - editor.apply() - } - - fun getClockAlignment(cont: Context) : Int { - val sharedPreferences = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE) - val key = "clock_alignment" - return sharedPreferences.getInt(key, 0) - } - - fun setHomeAppAlignment(cont: Context, alignment: Int) { - val editor = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE).edit() - val key = "home_app_alignment" - editor.putInt(key, alignment) - editor.apply() - } - - fun getHomeAppAlignment(cont: Context) : Int { - val sharedPreferences = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE) - val key = "home_app_alignment" - return sharedPreferences.getInt(key, 0) - } - - fun setAppMenuAlignment(cont: Context, alignment: Int) { - val editor = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE).edit() - val key = "app_menu_alignment" - editor.putInt(key, alignment) - editor.apply() - } - - fun getAppMenuAlignment(cont: Context) : Int { - val sharedPreferences = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE) - val key = "app_menu_alignment" - return sharedPreferences.getInt(key, 0) - } - - fun setSearchAlignment(cont: Context, alignment: Int) { - val editor = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE).edit() - val key = "search_alignment" - editor.putInt(key, alignment) - editor.apply() - } - - fun getSearchAlignment(cont: Context) : Int { - val sharedPreferences = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE) - val key = "search_alignment" - return sharedPreferences.getInt(key, 0) - } - - fun setClockSize(cont: Context, alignment: Int) { - val editor = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE).edit() - val key = "clock_size" - editor.putInt(key, alignment) - editor.apply() - } - - fun getClockSize(cont: Context) : Int { - val sharedPreferences = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE) - val key = "clock_size" - return sharedPreferences.getInt(key, 2) - } - - fun setDateSize(cont: Context, alignment: Int) { - val editor = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE).edit() - val key = "date_size" - editor.putInt(key, alignment) - editor.apply() - } - - fun getDateSize(cont: Context) : Int { - val sharedPreferences = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE) - val key = "date_size" - return sharedPreferences.getInt(key, 2) - } - - fun setShortcutSize(cont: Context, alignment: Int) { - val editor = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE).edit() - val key = "shortcut_size" - editor.putInt(key, alignment) - editor.apply() - } - - fun getShortcutSize(cont: Context) : Int { - val sharedPreferences = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE) - val key = "shortcut_size" - return sharedPreferences.getInt(key, 2) - } - - fun setAppSize(cont: Context, alignment: Int) { - val editor = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE).edit() - val key = "app_size" - editor.putInt(key, alignment) - editor.apply() - } - - fun getAppSize(cont: Context) : Int { - val sharedPreferences = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE) - val key = "app_size" - return sharedPreferences.getInt(key, 2) - } - - fun setSearchSize(cont: Context, alignment: Int) { - val editor = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE).edit() - val key = "search_size" - editor.putInt(key, alignment) - editor.apply() - } - - fun getSearchSize(cont: Context) : Int { - val sharedPreferences = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE) - val key = "search_size" - return sharedPreferences.getInt(key, 2) - } - - fun setCameraEnabled(cont: Context, isEnabled: Boolean) { - val editor = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE).edit() - val key = "camera_enabled" - editor.putBoolean(key, isEnabled) - editor.apply() - } - - fun getCameraEnabled(cont: Context) : Boolean { - val sharedPreferences = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE) - val key = "camera_enabled" - return sharedPreferences.getBoolean(key, true) - } - - fun setContactsEnabled(cont: Context, isEnabled: Boolean) { - val editor = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE).edit() - val key = "contacts_enabled" - editor.putBoolean(key, isEnabled) - editor.apply() - } - - fun getContactsEnabled(cont: Context) : Boolean { - val sharedPreferences = cont.getSharedPreferences("preferences", AppCompatActivity.MODE_PRIVATE) - val key = "contacts_enabled" - return sharedPreferences.getBoolean(key, true) - } } \ No newline at end of file diff --git a/app/src/main/java/eu/ottop/yamlauncher/WeatherSystem.kt b/app/src/main/java/eu/ottop/yamlauncher/WeatherSystem.kt index 2cc365b..e30513b 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/WeatherSystem.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/WeatherSystem.kt @@ -10,6 +10,7 @@ import android.location.LocationManager import android.widget.Toast import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat +import androidx.preference.PreferenceManager import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.json.JSONObject @@ -90,11 +91,16 @@ class WeatherSystem { return foundLocations } - suspend fun getTemp(context: Context) : String { + fun getTemp(context: Context) : String { + + val preferences = PreferenceManager.getDefaultSharedPreferences(context) + val tempUnits = preferences.getString("tempUnits", "celsius") + var currentWeather = "" val location = sharedPreferenceManager.getWeatherLocation(context) - if (location != null) { - val url = URL("https://api.open-meteo.com/v1/forecast?$location¤t=temperature_2m") + if (location != null) { + if (location.isNotEmpty()) { + val url = URL("https://api.open-meteo.com/v1/forecast?$location&temperature_unit=${tempUnits}¤t=temperature_2m") with(url.openConnection() as HttpURLConnection) { requestMethod = "GET" @@ -105,18 +111,26 @@ class WeatherSystem { val currentData = jsonObject.getJSONObject("current") - return currentData.getInt("temperature_2m").toString() + currentWeather = currentData.getInt("temperature_2m").toString() } } } - else { - withContext(Dispatchers.Main) { - Toast.makeText(context, "No weather location set", Toast.LENGTH_SHORT).show() - } + } + + return when (tempUnits) { + "celsius" -> { + stringUtils.addEndTextIfNotEmpty(currentWeather, "°C") } - return "" + "fahrenheit" -> { + stringUtils.addEndTextIfNotEmpty(currentWeather, "°F") + } + + else -> { + "" + } + } } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index b725bdd..026335e 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -88,10 +88,9 @@ medium large + + + °C + °F + + + + celsius + fahrenheit + \ No newline at end of file diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml index c7fe46b..23851cf 100644 --- a/app/src/main/res/xml/root_preferences.xml +++ b/app/src/main/res/xml/root_preferences.xml @@ -126,6 +126,15 @@ app:key="manual_location" app:selectable="true" app:title="Set Manual Location" /> +