alfred4s.models.Items.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of alfred4s_3 Show documentation
Show all versions of alfred4s_3 Show documentation
Simplifies creating Alfred workflows using Scala 3.
The newest version!
/*
* Copyright 2024 Alejandro Hernández
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package alfred4s.models
import scala.concurrent.duration.*
import upickle.default.*
/** Describes a result-set displayed in Alfred */
final case class Items(
items: Seq[Item] = Seq(),
cache: Option[FiniteDuration] = None,
loosereload: Boolean = false
) {
/** Enables loose-reload, which asks the Script Filter to try to show any cached data first. If it's determined to be
* stale, the script runs in the background and replaces results with the new data when it becomes available.
*/
def enableLooseReload = copy(loosereload = true)
/** Scripts which take a while to return can cache results so users see data sooner on subsequent runs.
*
* The Script Filter presents the results from the previous run when caching is active and hasn't expired. Because
* the script won't execute, we recommend this option only be used with "Alfred filters results".
*
* @param duration
* cache's TTL, between 5 seconds and 24 hours
* @return
*/
def cache(duration: FiniteDuration) =
require(duration >= 5.seconds, "Duration must be greater or equal than 5 seconds")
require(duration <= 24.hours, "Duration must be less or equal than 24 hours")
copy(cache = Some(duration))
}
object Items {
given Conversion[Seq[Item], Items] = Items(_)
given Conversion[Item, Items] = item => Items(Seq(item))
given ReadWriter[Items] = readwriter[ujson.Value].bimap[Items](
items =>
ujson.Obj(
"items" -> writeJs(items.items.filter(_.isVisible).sortBy(_.title.toLowerCase())),
items.cache
.map(duration =>
"cache" -> ujson.Obj(
"seconds" -> ujson.Num(duration.toSeconds.toDouble),
"loosereload" -> ujson.Bool(items.loosereload)
)
)
.toList: _*
),
json =>
Items(
items = json("items").arr.toList.map(read[Item](_)),
cache = json.obj
.get("cache")
.map(_("seconds").num.toLong)
.map(FiniteDuration(_, java.util.concurrent.TimeUnit.SECONDS)),
loosereload = json.obj
.get("cache")
.map(_("loosereload").bool)
.getOrElse(false)
)
)
}