Skip to content

Commit

Permalink
extend Muzei integration to add chains of Yesterday in History and …
Browse files Browse the repository at this point in the history
…`Recent` options
  • Loading branch information
dave7895 committed Apr 30, 2024
1 parent becd051 commit 2bce532
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 43 deletions.
133 changes: 91 additions & 42 deletions app/src/main/java/site/leos/apps/lespas/muzei/LesPasArtProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.app.Application
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.BitmapRegionDecoder
Expand All @@ -36,6 +37,7 @@ import site.leos.apps.lespas.R
import site.leos.apps.lespas.album.AlbumRepository
import site.leos.apps.lespas.helper.OkHttpWebDav
import site.leos.apps.lespas.helper.Tools
import site.leos.apps.lespas.photo.MuzeiPhoto
import site.leos.apps.lespas.photo.Photo
import site.leos.apps.lespas.photo.PhotoRepository
import site.leos.apps.lespas.tflite.ObjectDetectionModel
Expand All @@ -60,7 +62,7 @@ class LesPasArtProvider: MuzeiArtProvider() {
if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(LesPasArtProviderSettingActivity.KEY_SKIP_LATE_NIGHT_UPDATE, false)) {
LocalDateTime.now().hour in 0..5
}
else false
else false
if (initial || ((Date().time - (lastAddedArtwork?.dateAdded ?: Date()).time > 30000) && !skipLateNightUpdate)) updateArtwork()
}

Expand Down Expand Up @@ -153,6 +155,63 @@ class LesPasArtProvider: MuzeiArtProvider() {
return PendingIntent.getActivity(context!!, 0, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
}

// get a single photo that was taken today (in history) if any exists
private fun getInHistory(
photoList: List<MuzeiPhoto>, today: LocalDate, sp: SharedPreferences
): MuzeiPhoto? {
return photoList.filter { p -> today.dayOfMonth == p.dateTaken.dayOfMonth && today.month == p.dateTaken.month }
.let { sameDayHits ->
when(sameDayHits.size) {
0 -> null
1 -> sameDayHits[0]
else -> {
/*
var index = random.nextInt(sameDayHits.size)
lastAddedArtwork?.let {
// Prevent from choosing the last one again
while(sameDayHits[index].id == it.token) index = random.nextInt(sameDayHits.size)
}
sameDayHits[index]
*/
// Try to get today's photo list from Preference
val photosOfDate = mutableListOf<String>().apply { addAll(sp.getString(PHOTOS_OF_TODAY, "")?.split(',') ?: listOf()) }
if (photosOfDate.contains(sameDayHits[0].id)) {
// If list existed, loop thru. it
photosOfDate[0] = (photosOfDate[0].toInt() + 1).let { i -> if (i == sameDayHits.size) 0 else i }.toString()
} else {
// Create today's photo shuffled list, add current index to the top of the list
photosOfDate.clear()
sameDayHits.shuffled().forEach { photosOfDate.add(it.id) }
photosOfDate.add(0, "0")
}

// Save the list in Preference
sp.edit().apply {
putString(PHOTOS_OF_TODAY, photosOfDate.joinToString(","))
apply()
}

// Supply the next in list to Muzei
sameDayHits.find { it.id == photosOfDate[photosOfDate[0].toInt() + 1] }
}
}
}
}

// get a single photo from the last month if any exists
private fun getLatest(
photoList: List<MuzeiPhoto>, today: LocalDate, random: Random
): MuzeiPhoto? {
return photoList.filter { p ->
Period.between(p.dateTaken.toLocalDate(), today).toTotalMonths() < 1
}.let { recentList ->
when {
recentList.size == 1 -> recentList[0]
recentList.isNotEmpty() -> recentList[random.nextInt(recentList.size)]
else -> null
}
}
}
private fun updateArtwork() {
Thread {
var additionalCaption = false
Expand All @@ -168,51 +227,41 @@ class LesPasArtProvider: MuzeiArtProvider() {
val random = Random(System.currentTimeMillis() * LocalDate.now().dayOfMonth / LocalDate.now().dayOfWeek.value)
when (sp.getInt(LesPasArtProviderSettingActivity.KEY_PREFER, LesPasArtProviderSettingActivity.PREFER_RANDOM)) {
LesPasArtProviderSettingActivity.PREFER_LATEST -> {
photoList.filter { p -> Period.between(p.dateTaken.toLocalDate(), today).toTotalMonths() < 1 }.let { recentList ->
when {
recentList.size == 1 -> recentList[0]
recentList.isNotEmpty() -> recentList[random.nextInt(recentList.size)]
else -> photoList[random.nextInt(photoList.size)]
}
}
getLatest(photoList, today, random) ?: photoList[random.nextInt(photoList.size)]
}
LesPasArtProviderSettingActivity.PREFER_TODAY_IN_HISTORY -> {
photoList.filter { p -> today.dayOfMonth == p.dateTaken.dayOfMonth && today.month == p.dateTaken.month }.let { sameDayHits ->
additionalCaption = sameDayHits.isNotEmpty()

when(sameDayHits.size) {
0 -> photoList[random.nextInt(photoList.size)]
1 -> sameDayHits[0]
else -> {
/*
var index = random.nextInt(sameDayHits.size)
lastAddedArtwork?.let {
// Prevent from choosing the last one again
while(sameDayHits[index].id == it.token) index = random.nextInt(sameDayHits.size)
}
sameDayHits[index]
*/
// Try to get today's photo list from Preference
val photosOfDate = mutableListOf<String>().apply { addAll(sp.getString(PHOTOS_OF_TODAY, "")?.split(',') ?: listOf()) }
if (photosOfDate.contains(sameDayHits[0].id)) {
// If list existed, loop thru. it
photosOfDate[0] = (photosOfDate[0].toInt() + 1).let { i -> if (i == sameDayHits.size) 0 else i }.toString()
} else {
// Create today's photo shuffled list, add current index to the top of the list
photosOfDate.clear()
sameDayHits.shuffled().forEach { photosOfDate.add(it.id) }
photosOfDate.add(0, "0")
}
// temporary variable needed for side effect of caption
val sameDayHit = getInHistory(photoList, today, sp)
if (sameDayHit != null) {
additionalCaption = true
sameDayHit
} else {
photoList[random.nextInt(photoList.size)]
}
}

// Save the list in Preference
sp.edit().apply {
putString(PHOTOS_OF_TODAY, photosOfDate.joinToString(","))
apply()
}
LesPasArtProviderSettingActivity.PREFER_HISTORY_OVER_LATEST -> {
val sameDayHit = getInHistory(photoList, today, sp)
if (sameDayHit != null) {
additionalCaption = true
sameDayHit
} else {
getLatest(photoList, today, random) ?: photoList[random.nextInt(photoList.size)]
}
}

// Supply the next in list to Muzei
sameDayHits.find { it.id == photosOfDate[photosOfDate[0].toInt() + 1] }
}
LesPasArtProviderSettingActivity.PREFER_LATEST_OVER_HISTORY -> {
val retval = getLatest(photoList, today, random)
// this if construct is needed as the Kotlin compiler can't reason about longer expressions with the ?: operator
if (retval != null){
retval
} else {
val sameDayHit = getInHistory(photoList, today, sp)
if (sameDayHit != null) {
additionalCaption = true
sameDayHit
} else {
getLatest(photoList, today, random) ?: photoList[random.nextInt(photoList.size)]
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class LesPasArtProviderSettingActivity: AppCompatActivity() {
preferRadioGroup.check(when(lastPreferSetting) {
PREFER_LATEST-> R.id.prefer_latest
PREFER_TODAY_IN_HISTORY-> R.id.prefer_day_in_history
PREFER_HISTORY_OVER_LATEST-> R.id.prefer_history_over_latest
PREFER_LATEST_OVER_HISTORY-> R.id.prefer_latest_over_history
else-> R.id.prefer_random
})

Expand All @@ -79,6 +81,8 @@ class LesPasArtProviderSettingActivity: AppCompatActivity() {
val currentPreferSetting = when(preferRadioGroup.checkedRadioButtonId) {
R.id.prefer_latest-> PREFER_LATEST
R.id.prefer_day_in_history-> PREFER_TODAY_IN_HISTORY
R.id.prefer_history_over_latest-> PREFER_HISTORY_OVER_LATEST
R.id.prefer_latest_over_history-> PREFER_LATEST_OVER_HISTORY
else-> PREFER_RANDOM
}

Expand Down Expand Up @@ -135,5 +139,7 @@ class LesPasArtProviderSettingActivity: AppCompatActivity() {
const val PREFER_LATEST = 0
const val PREFER_TODAY_IN_HISTORY = 1
const val PREFER_RANDOM = 2
const val PREFER_HISTORY_OVER_LATEST = 3
const val PREFER_LATEST_OVER_HISTORY = 4
}
}
14 changes: 14 additions & 0 deletions app/src/main/res/layout/activity_muzei_setting.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@
android:layout_marginVertical="@dimen/small_padding"
android:textSize="16sp"
android:text="@string/prefer_day_in_history" />
<RadioButton
android:id="@+id/prefer_history_over_latest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="@dimen/small_padding"
android:text="@string/prefer_history_over_latest"
android:textSize="16sp" />
<RadioButton
android:id="@+id/prefer_latest_over_history"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="@dimen/small_padding"
android:text="@string/prefer_latest_over_history"
android:textSize="16sp" />
<RadioButton
android:id="@+id/prefer_random"
android:layout_width="match_parent"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -405,4 +405,6 @@
<string name="tooltips_local_copy">Lokale Kopie</string>
<string name="tooltips_archive_copy">Archivierte Kopie</string>
<string name="show_archive_title">Server-Archiv in der Galerie anzeigen</string>
<string name="prefer_history_over_latest">1. Damals und Heute 2. Kürzlich</string>
<string name="prefer_latest_over_history">1. Kürzlich 2. Damals und Heute</string>
</resources>
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -492,5 +492,6 @@
<item quantity="one">, %1$d year ago today</item>
<item quantity="other">, %1$d years ago today</item>
</plurals>

<string name="prefer_history_over_latest">1. Then and Now 2. Recent</string>
<string name="prefer_latest_over_history">1. Recent 2. Then and Now</string>
</resources>

0 comments on commit 2bce532

Please sign in to comment.