From 73a35cd60e740842e1536808f51d3c020aedd951 Mon Sep 17 00:00:00 2001 From: amoledwatchfaces <92080649+amoledwatchfaces@users.noreply.github.com> Date: Wed, 15 Feb 2023 20:38:53 +0100 Subject: [PATCH] * fixed Moon Phase visibility algorithm (re-apply your moon phase complication) --- wear/build.gradle | 5 +- .../MoonPhaseComplicationService.kt | 3 +- .../weekdayutccomp/MoonPhaseHelper.kt | 78 +++++-------------- 3 files changed, 22 insertions(+), 64 deletions(-) diff --git a/wear/build.gradle b/wear/build.gradle index 26b4e39..5ff8b24 100644 --- a/wear/build.gradle +++ b/wear/build.gradle @@ -10,8 +10,8 @@ android { applicationId "com.weartools.weekdayutccomp" minSdk 27 targetSdk 33 - versionCode 46 - versionName "1.4.6" + versionCode 48 + versionName "1.4.8" } @@ -39,4 +39,5 @@ dependencies { implementation 'androidx.wear.watchface:watchface-complications-data-source-ktx:1.1.1' implementation 'androidx.preference:preference-ktx:1.2.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' + implementation("org.shredzone.commons:commons-suncalc:3.5") } \ No newline at end of file diff --git a/wear/src/main/java/com/weartools/weekdayutccomp/MoonPhaseComplicationService.kt b/wear/src/main/java/com/weartools/weekdayutccomp/MoonPhaseComplicationService.kt index 160683d..f4615d7 100644 --- a/wear/src/main/java/com/weartools/weekdayutccomp/MoonPhaseComplicationService.kt +++ b/wear/src/main/java/com/weartools/weekdayutccomp/MoonPhaseComplicationService.kt @@ -27,7 +27,6 @@ import androidx.wear.watchface.complications.datasource.SuspendingComplicationDa import com.weartools.weekdayutccomp.R.drawable import java.math.RoundingMode import java.text.DecimalFormat -import java.util.* class MoonPhaseComplicationService : SuspendingComplicationDataSourceService() { @@ -106,7 +105,7 @@ override fun getPreviewData(type: ComplicationType): ComplicationData? { override suspend fun onComplicationRequest(request: ComplicationRequest): ComplicationData? { Log.d(TAG, "onComplicationRequest() id: ${request.complicationInstanceId}") - MoonPhaseHelper.update(calendar = Calendar.getInstance(), context = this) + MoonPhaseHelper.update(context = this) val preferences = PreferenceManager.getDefaultSharedPreferences(applicationContext) val phaseicon = preferences.getInt(getString(R.string.key_pref_phase_icon), drawable.moon15) diff --git a/wear/src/main/java/com/weartools/weekdayutccomp/MoonPhaseHelper.kt b/wear/src/main/java/com/weartools/weekdayutccomp/MoonPhaseHelper.kt index d86105e..3227539 100644 --- a/wear/src/main/java/com/weartools/weekdayutccomp/MoonPhaseHelper.kt +++ b/wear/src/main/java/com/weartools/weekdayutccomp/MoonPhaseHelper.kt @@ -19,42 +19,37 @@ package com.weartools.weekdayutccomp import android.content.Context import android.util.Log import androidx.preference.PreferenceManager -import java.util.* +import org.shredzone.commons.suncalc.MoonIllumination import kotlin.math.floor + class MoonPhaseHelper{ companion object { - private var mCalendar: Calendar = Calendar.getInstance() - - fun update(calendar: Calendar, context: Context?) { + fun update(context: Context?) { val preferences = PreferenceManager.getDefaultSharedPreferences(context!!) val editor = preferences.edit() - mCalendar = calendar - val normalizedphase = computeMoonPhase() - val phase = normalizedphase * MOON_PHASE_LENGTH + val parameters = MoonIllumination.compute().now() + val visibility = (parameters.execute().fraction * 100.0).toFloat() + val phase = ((parameters.execute().phase)+180.0)*(MOON_PHASE_LENGTH/360.0) + Log.i(TAG, "Computed moon phase: $phase") val phaseValue = floor(phase).toInt() % 30 Log.i(TAG, "Discrete phase value: $phaseValue") val simplePhaseValue: Int = when (phaseValue) { - in 1..6 -> 1 - 7 -> 2 - in 8..14 -> 3 - 15 -> 4 - in 16..22 -> 5 - 23 -> 6 - in 24..30 -> 7 - else -> 0 + in 1..6 -> 1 + 7 -> 2 + in 8..14 -> 3 + 15 -> 4 + in 16..22 -> 5 + 23 -> 6 + in 24..30 -> 7 + else -> 0 } Log.i(TAG, "Simple phase value: $simplePhaseValue") - //val visibility = (phase / 14.7652944265 * 100).toFloat() - - // TODO: NEW VISIBILITY LOGIC - val newvisibility = ((1-kotlin.math.cos(2 * kotlin.math.PI * normalizedphase))*0.5*100).toFloat() - - Log.i(TAG, "Visibility value: $newvisibility") + Log.i(TAG, "Visibility value: $visibility") val isnorthernhemi = preferences.getBoolean(context.getString(R.string.moon_setting_hemi_key), true) val simpleIcon = preferences.getBoolean(context.getString(R.string.moon_setting_simple_icon_key), false) @@ -65,48 +60,11 @@ class MoonPhaseHelper{ else if (isnorthernhemi){ editor.putInt(context.getString(R.string.key_pref_phase_icon),(IMAGE_LOOKUP[phaseValue])).apply() } else { editor.putInt(context.getString(R.string.key_pref_phase_icon),(IMAGE_LOOKUP[30 - phaseValue])).apply() } - editor.putFloat(context.getString(R.string.key_pref_moon_visibility), newvisibility).apply() - } - - // Computes moon phase based upon Bradley E. Schaefer's moon phase algorithm. - private fun computeMoonPhase(): Double { - val year = mCalendar[Calendar.YEAR] - val month = mCalendar[Calendar.MONTH] + 1 - val day = mCalendar[Calendar.DAY_OF_MONTH] - - // Convert the year into the format expected by the algorithm. - val transformedYear = year - floor(((12 - month) / 10).toDouble()) - Log.i(TAG, "transformedYear: $transformedYear") - - // Convert the month into the format expected by the algorithm. - var transformedMonth = month + 9 - if (transformedMonth >= 12) { - transformedMonth -= 12 - } - Log.i(TAG, "transformedMonth: $transformedMonth") - - // Logic to compute moon phase as a fraction between 0 and 1 - val term1 = floor(365.25 * (transformedYear + 4712)) - val term2 = floor(30.6 * transformedMonth + 0.5) - val term3 = floor(floor(transformedYear / 100 + 49) * 0.75) - 38 - var intermediate = term1 + term2 + day + 59 - if (intermediate > 2299160) { - intermediate -= term3 - } - Log.i(TAG, "intermediate: $intermediate") - var normalizedPhase = (intermediate - 2451550.1) / MOON_PHASE_LENGTH - normalizedPhase -= floor(normalizedPhase) - if (normalizedPhase < 0) { - normalizedPhase += 1 - } - Log.i(TAG, "normalizedPhase: $normalizedPhase") - - // Return the result as a value between 0 and MOON_PHASE_LENGTH - return normalizedPhase + editor.putFloat(context.getString(R.string.key_pref_moon_visibility), visibility).apply() } private const val TAG = "MoonView" - private const val MOON_PHASE_LENGTH = 29.530588853 + private const val MOON_PHASE_LENGTH = 29.5 private val IMAGE_LOOKUP = intArrayOf( R.drawable.moon0,