Date now aligns with the clock and temperature units can be selected

This commit is contained in:
ottoptj 2024-08-01 18:02:47 +03:00
commit dade0c9a3b
6 changed files with 66 additions and 194 deletions

View file

@ -18,6 +18,9 @@ import android.view.GestureDetector
import android.view.Gravity import android.view.Gravity
import android.view.MotionEvent import android.view.MotionEvent
import android.view.View 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.view.inputmethod.InputMethodManager
import android.widget.EditText import android.widget.EditText
import android.widget.LinearLayout import android.widget.LinearLayout
@ -28,7 +31,6 @@ import androidx.activity.OnBackPressedCallback
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import androidx.core.content.res.ResourcesCompat import androidx.core.content.res.ResourcesCompat
import androidx.core.view.children import androidx.core.view.children
import androidx.core.view.marginLeft import androidx.core.view.marginLeft
@ -74,7 +76,6 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
private lateinit var clock: TextClock private lateinit var clock: TextClock
private var clockMargin = 0 private var clockMargin = 0
private lateinit var clockLayout: ConstraintLayout
private lateinit var dateText: TextClock private lateinit var dateText: TextClock
@ -108,13 +109,11 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
clockMargin = clock.marginLeft clockMargin = clock.marginLeft
clockLayout = findViewById(R.id.clock_layout)
dateText = findViewById(R.id.text_date) dateText = findViewById(R.id.text_date)
dateElements = mutableListOf(dateText.format12Hour.toString(), dateText.format24Hour.toString(), "", "") dateElements = mutableListOf(dateText.format12Hour.toString(), dateText.format24Hour.toString(), "", "")
setClockAlignment(preferences.getString("clockAlignment", "left"), clock.id, clockMargin) setClockAlignment(preferences.getString("clockAlignment", "left"))
setupApps() setupApps()
@ -158,7 +157,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
while (true) { while (true) {
val currentWeather = weatherSystem.getTemp(this@MainActivity) val currentWeather = weatherSystem.getTemp(this@MainActivity)
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
modifyDate(stringUtils.addEndTextIfNotEmpty(currentWeather, "°C"), 3) modifyDate(currentWeather, 3)
} }
delay(300000) delay(300000)
} }
@ -169,7 +168,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
when (key) { when (key) {
"clockAlignment" -> { "clockAlignment" -> {
setClockAlignment(preferences?.getString(key, "left"), clock.id, clockMargin) setClockAlignment(preferences?.getString(key, "left"))
} }
"shortcutAlignment" -> { "shortcutAlignment" -> {
@ -612,41 +611,22 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
return true return true
} }
private fun setClockAlignment(alignment: String?, widgetId: Int, margin: Int) { private fun setClockAlignment(alignment: String?) {
val constraintSet = ConstraintSet() when (alignment) {
constraintSet.clone(clockLayout) "left" -> {
println(alignment) clock.textAlignment = TEXT_ALIGNMENT_TEXT_START
dateText.textAlignment = TEXT_ALIGNMENT_TEXT_START
if (alignment == "right") {
constraintSet.clear(widgetId, ConstraintSet.START)
} }
else if (alignment == "left") { "center" -> {
constraintSet.clear(widgetId, ConstraintSet.END) clock.textAlignment = TEXT_ALIGNMENT_CENTER
dateText.textAlignment = TEXT_ALIGNMENT_CENTER
} }
"right" -> {
if (alignment == "center" || alignment == "left") { clock.textAlignment = TEXT_ALIGNMENT_TEXT_END
constraintSet.connect( dateText.textAlignment = TEXT_ALIGNMENT_TEXT_END
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) { private fun setShortcutAlignment(alignment: String?, shortcuts: LinearLayout) {

View file

@ -81,146 +81,4 @@ class SharedPreferenceManager {
return sharedPreferences.getString(key, "") 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)
}
} }

View file

@ -10,6 +10,7 @@ import android.location.LocationManager
import android.widget.Toast import android.widget.Toast
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.preference.PreferenceManager
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.json.JSONObject import org.json.JSONObject
@ -90,11 +91,16 @@ class WeatherSystem {
return foundLocations 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) val location = sharedPreferenceManager.getWeatherLocation(context)
if (location != null) { if (location != null) {
val url = URL("https://api.open-meteo.com/v1/forecast?$location&current=temperature_2m") if (location.isNotEmpty()) {
val url = URL("https://api.open-meteo.com/v1/forecast?$location&temperature_unit=${tempUnits}&current=temperature_2m")
with(url.openConnection() as HttpURLConnection) { with(url.openConnection() as HttpURLConnection) {
requestMethod = "GET" requestMethod = "GET"
@ -105,18 +111,26 @@ class WeatherSystem {
val currentData = jsonObject.getJSONObject("current") 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")
}
"fahrenheit" -> {
stringUtils.addEndTextIfNotEmpty(currentWeather, "°F")
}
else -> {
""
} }
} }
return ""
} }
} }

View file

@ -88,10 +88,9 @@
<TextClock <TextClock
android:id="@+id/text_clock" android:id="@+id/text_clock"
android:layout_width="wrap_content" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginHorizontal="32dp" android:layout_marginHorizontal="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="40dp" android:layout_marginTop="40dp"
android:layout_marginBottom="27dp" android:layout_marginBottom="27dp"
android:fontFamily="@null" android:fontFamily="@null"
@ -103,16 +102,18 @@
android:textColor="#F3F3F3" android:textColor="#F3F3F3"
android:textSize="68sp" android:textSize="68sp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<TextClock <TextClock
android:id="@+id/text_date" android:id="@+id/text_date"
android:layout_width="wrap_content" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:fontFamily="@null" android:fontFamily="@null"
android:format12Hour="dd MMM yyyy" android:format12Hour="dd MMM yyyy"
android:format24Hour="dd MMM yyyy" android:format24Hour="dd MMM yyyy"
android:paddingHorizontal="2dp"
android:textAlignment="textStart" android:textAlignment="textStart"
android:textAppearance="@android:style/TextAppearance.DeviceDefault" android:textAppearance="@android:style/TextAppearance.DeviceDefault"
android:textColor="#F3F3F3" android:textColor="#F3F3F3"

View file

@ -33,4 +33,14 @@
<item>medium</item> <item>medium</item>
<item>large</item> <item>large</item>
</string-array> </string-array>
<string-array name="temp_units">
<item>°C</item>
<item>°F</item>
</string-array>
<string-array name="unit_values">
<item>celsius</item>
<item>fahrenheit</item>
</string-array>
</resources> </resources>

View file

@ -126,6 +126,15 @@
app:key="manual_location" app:key="manual_location"
app:selectable="true" app:selectable="true"
app:title="Set Manual Location" /> app:title="Set Manual Location" />
<ListPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:defaultValue="celsius"
app:entries="@array/temp_units"
app:entryValues="@array/unit_values"
app:key="tempUnits"
app:title="Units"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory
android:layout_width="wrap_content" android:layout_width="wrap_content"