Skip to content
This repository has been archived by the owner on Nov 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #70 from ckipp01/fixOrdering
Browse files Browse the repository at this point in the history
fix: refactor ordering to make sure it always works
  • Loading branch information
ckipp01 authored Sep 21, 2023
2 parents 834a33c + 45d796b commit f037c10
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 37 deletions.
28 changes: 21 additions & 7 deletions skan/src/BoardState.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@ final case class BoardState(
todoState: ListWidget.State,
inProgressState: ListWidget.State,
items: Array[BoardItem],
focusedList: Status
focusedList: Status,
boardOrder: Order
):

// TODO an improvement would be during progress don't edit the array in place,
// instead make it a vector and then these can be vals here.
def todoItems(): Array[BoardItem] = items.collect:
case item @ BoardItem(_, _, _, Status.TODO, _) => item
def todoItems(): Array[BoardItem] = items
.collect:
case item @ BoardItem(_, _, _, Status.TODO, _) => item
.sortWith(ordering)

def inProgressItems(): Array[BoardItem] = items.collect:
case item @ BoardItem(_, _, _, Status.INPROGRESS, _) => item
def inProgressItems(): Array[BoardItem] = items
.collect:
case item @ BoardItem(_, _, _, Status.INPROGRESS, _) => item
.sortWith(ordering)

/** Switches the main column view in the board UI. The progression goes:
*
Expand Down Expand Up @@ -188,6 +193,11 @@ final case class BoardState(
*/
def withNewItem(item: BoardItem): BoardState =
this.copy(items = items.appended(item))

private val ordering: (BoardItem, BoardItem) => Boolean = (a, b) =>
boardOrder match
case Order.date => a.date.compareTo(b.date) < 0
case Order.priority => a.priority.ordinal > b.priority.ordinal
end BoardState

object BoardState:
Expand All @@ -199,11 +209,15 @@ object BoardState:
* @return
* The newly created state.
*/
def fromItems(items: Vector[BoardItem]): BoardState =
def fromItems(
items: Vector[BoardItem],
boardOrder: Order = Order.priority
): BoardState =
BoardState(
todoState =
ListWidget.State(selected = if items.size > 0 then Some(0) else None),
inProgressState = ListWidget.State(selected = None),
items = items.toArray,
focusedList = Status.TODO
focusedList = Status.TODO,
boardOrder = boardOrder
)
6 changes: 1 addition & 5 deletions skan/src/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ final case class Config(
dataDir: os.Path = Config.defaultDataDir,
zoneId: ZoneId = ZoneId.of("GMT+2"),
boardOrder: Order = Order.priority
) derives ReadWriter:
val ordering: (BoardItem, BoardItem) => Boolean = (a, b) =>
boardOrder match
case Order.date => a.date.compareTo(b.date) < 0
case Order.priority => a.priority.ordinal > b.priority.ordinal
) derives ReadWriter

object Config:
private val projectDirs = ProjectDirectories.from("io", "kipp", "skan")
Expand Down
15 changes: 8 additions & 7 deletions skan/src/ContextState.scala
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,10 @@ case class ContextState(
activeContext = newName
)

def addContext(name: String): ContextState =
def addContext(name: String, boardOrder: Order): ContextState =
this.copy(
boards = boards.updated(name, BoardState.fromItems(Vector.empty)),
boards =
boards.updated(name, BoardState.fromItems(Vector.empty, boardOrder)),
activeContext = name
)

Expand Down Expand Up @@ -153,23 +154,23 @@ object ContextState:
val contents = os.read(file)
val items = fromJson(contents)
val name = file.baseName
(name, BoardState.fromItems(items))
(name, BoardState.fromItems(items, config.boardOrder))
.toMap
if boards.isEmpty then default(config.dataDir)
if boards.isEmpty then default(config.dataDir, config.boardOrder)
else ContextState(boards, boards.keys.toVector.sorted.head)
else default(config.dataDir)
else default(config.dataDir, config.boardOrder)

private def toJson(items: Array[BoardItem]): String =
upickle.default.write(items)

private def default(dir: os.Path) =
private def default(dir: os.Path, boardOrder: Order) =
os.write.over(
target = dir / "default.json",
data = "",
createFolders = true
)
ContextState(
Map("default" -> BoardState.fromItems(Vector.empty)),
Map("default" -> BoardState.fromItems(Vector.empty, boardOrder)),
"default"
)

Expand Down
5 changes: 3 additions & 2 deletions skan/src/project.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//> using scala 3.3.1
//> using jvm 17
//> using jvm 19
//> using dep com.olvind.tui::tui:0.0.7
//> using dep com.lihaoyi::upickle:3.1.3
//> using dep com.lihaoyi::os-lib:0.9.1
Expand Down Expand Up @@ -144,7 +144,8 @@ import skan.ui.*
runNewContext(contextState, newText)
case _: KeyCode.Enter =>
if newContextName.nonEmpty then
val newState = contextState.addContext(newContextName)
val newState =
contextState.addContext(newContextName, config.boardOrder)
runBoard(newState)
else runBoard(contextState)
case _: KeyCode.Esc =>
Expand Down
2 changes: 0 additions & 2 deletions skan/src/ui/Board.scala
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,11 @@ object Board:

val todoItems = state
.todoItems()
.sortWith(config.ordering)
.map: item =>
toListItem(item, horizontalChunks(0).width)

val inProgressItems = state
.inProgressItems()
.sortWith(config.ordering)
.map: item =>
toListItem(item, horizontalChunks(1).width)

Expand Down
4 changes: 2 additions & 2 deletions skan/test/ContextStateSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ContextStateSuite extends munit.FunSuite:
assertEquals(newState.activeContext, "b")

test("can-add-context"):
val newContext = contextState.addContext("test")
val newContext = contextState.addContext("test", config.boardOrder)
val expectedContext = contextState.copy(
boards = contextState.boards
.updated("test", BoardState.fromItems(Vector.empty)),
Expand All @@ -29,7 +29,7 @@ class ContextStateSuite extends munit.FunSuite:
assertEquals(newContext.sortedKeys, expectedContext.sortedKeys)

test("can-remove-context"):
val newContext = contextState.addContext("test")
val newContext = contextState.addContext("test", config.boardOrder)
assertEquals(newContext.activeContext, "test")
assertEquals(newContext.sortedKeys.size, 3)
val deleted = newContext.deleteContext(config)
Expand Down
24 changes: 12 additions & 12 deletions skan/test/ui/BoardUiSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class BoardUiSuite extends munit.FunSuite:
test("basic-board-by-date"):
val state = ContextState(
boards = Map(
"a" -> BoardState.fromItems(defaultItems),
"b" -> BoardState.fromItems(secondaryItems)
"a" -> BoardState.fromItems(defaultItems, Order.date),
"b" -> BoardState.fromItems(secondaryItems, Order.date)
),
activeContext = "a"
)
Expand Down Expand Up @@ -169,13 +169,13 @@ class BoardUiSuite extends munit.FunSuite:
" │ a │ ",
" └──────────────────────────────────────────────────────────────────────────┘ ",
" ┌TODOs-1/2───────────────────────────┐┌In Progress─────────────────────────┐ ",
"IMPORTANT 2023-04-12││URGENT 2023-04-12│ ",
" │Here is an Important issue ││An urgent issue with no descripti...│ ",
"Short description ││ │ ",
"NORMAL 2023-04-11││URGENT 2023-04-12│ ",
" │Here is a normal one ││An urgent issue with no descripti...│ ",
"Some description ││ │ ",
" │ ││ │ ",
" │LOW 2023-04-12││NORMAL 2023-04-11",
" │Here is a low one ││Here is a normal one ",
" │Some lowly description ││Some description ",
" │LOW 2023-04-12││IMPORTANT 2023-04-12",
" │Here is a low one ││Here is an Important issue",
" │Some lowly description ││Short description │ ",
" │ ││ │ ",
" │ ││ │ ",
" │ ││ │ ",
Expand Down Expand Up @@ -400,9 +400,9 @@ class BoardUiSuite extends munit.FunSuite:
" │ a │ ",
" └──────────────────────────────────────────────────────────────────────────┘ ",
" ┌TODOs-1/2───────────────────────────┐┌In Progress─────────────────────────┐ ",
"IMPORTANT 2023-04-12││URGENT 2023-04-12│ ",
" │Here is an Important issue ││An urgent issue with no descripti...│ ",
"Short description ││ │ ",
"NORMAL 2023-04-11││URGENT 2023-04-12│ ",
" │Here is a normal one ││An urgent issue with no descripti...│ ",
"Some description ││ │ ",
" │ ││ │ ",
" │LOW 2023-04-12││ │ ",
" │Here is a low one ││ │ ",
Expand Down Expand Up @@ -483,7 +483,7 @@ class BoardUiSuite extends munit.FunSuite:
activeContext = "a"
)

val updated = state.addContext("a-new-context")
val updated = state.addContext("a-new-context", config.boardOrder)

val expected = Buffer.withLines(
" ",
Expand Down

0 comments on commit f037c10

Please sign in to comment.