Skip to content

Commit

Permalink
* fixed Moon Phase visibility algorithm (re-apply your moon phase com…
Browse files Browse the repository at this point in the history
…plication)
  • Loading branch information
amoledwatchfaces committed Feb 15, 2023
1 parent 777ffbe commit 73a35cd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 64 deletions.
5 changes: 3 additions & 2 deletions wear/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"

}

Expand Down Expand Up @@ -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")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down Expand Up @@ -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)
Expand Down
78 changes: 18 additions & 60 deletions wear/src/main/java/com/weartools/weekdayutccomp/MoonPhaseHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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,
Expand Down

0 comments on commit 73a35cd

Please sign in to comment.