Skip to content

Commit

Permalink
Fix file dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwinChang24 committed Mar 11, 2023
1 parent 76185d5 commit 1ab65b7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ kotlin {
implementation("org.jetbrains.kotlinx:dataframe:0.9.1")
implementation("org.jetbrains.kotlinx:dataframe-excel:0.9.1")
implementation("com.github.doyaaaaaken:kotlin-csv-jvm:1.8.0")
implementation("io.github.evanrupert:excelkt:1.0.2")
}
}
}
Expand Down
30 changes: 19 additions & 11 deletions src/jvmMain/kotlin/io/FileDialogs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import io.files.matchSchedule
import io.files.settings
import io.files.teamData
import io.files.timData
import io.github.evanrupert.excelkt.workbook
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import org.apache.poi.ss.usermodel.WorkbookFactory
import org.jetbrains.kotlinx.dataframe.io.writeExcel
import java.awt.FileDialog
import java.io.File
import java.time.LocalDateTime
Expand All @@ -36,13 +35,14 @@ fun matchScheduleDialog(window: ComposeWindow): MatchSchedule? {
// The user clicked 'Cancel'
return null
}
val matchschedule: Map<String, Match>? = Json.decodeFromString(File(fileDialog.directory, fileDialog.file).readText())
val matchSchedule: Map<String, Match>? =
Json.decodeFromString(File(fileDialog.directory, fileDialog.file).readText())

// Copy the file to the config directory
File(fileDialog.directory, fileDialog.file).copyTo(MATCH_SCHEDULE_FILE)

// Serialize the data
return matchschedule
return matchSchedule
}

/**
Expand All @@ -63,11 +63,19 @@ fun saveDialog(window: ComposeWindow) {
}
// The user clicked 'Cancel'
if (fileDialog.file == null) return
// Create a new Excel workbook with all the data
val wb = WorkbookFactory.create(true)
timData!!.writeExcel(wb, sheetName = "TIM Data (${settings!!.name})")
teamData!!.writeExcel(wb, sheetName = "Team Data (${settings!!.name})")
// Write the Excel workbook to the chosen file
wb.write(File(fileDialog.directory, fileDialog.file).outputStream())
wb.close()
// Create an Excel workbook from the data and write to the file
workbook {
sheet("TIM Data (${settings!!.name})") {
row { timData!!.first().keys.forEach { cell(it) } }
timData!!.forEach {
row { it.forEach { (_, value) -> cell(value) } }
}
}
sheet("Team Data (${settings!!.name})") {
row { teamData!!.first().keys.forEach { cell(it) } }
teamData!!.forEach {
row { it.forEach { (_, value) -> cell(value) } }
}
}
}.xssfWorkbook.write(File(fileDialog.directory, fileDialog.file).outputStream())
}

0 comments on commit 1ab65b7

Please sign in to comment.