Skip to content

Commit

Permalink
Add persistent editing, adding and removing sources
Browse files Browse the repository at this point in the history
  • Loading branch information
OAarne committed Apr 23, 2018
1 parent 8b454f6 commit 5c5f479
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 79 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
zhuLi is a digital research assistant, written in Kotlin.
It can be used to collect and organize sources and research notes.

# zhuLi
zhuLi is a digital research assistant.
It can be used to collect and organize sources and research notes.

## Documentation
### Documentation

[Req. Spec](https://github.com/OAarne/otm-harjoitustyo/blob/master/zhuLi/documentation/requirements.md)

[Työaikakirjanpito](https://github.com/OAarne/otm-harjoitustyo/blob/master/zhuLi/documentation/tuntikirjanpito.md)

## Usage
### Usage

__All of the commands below are to be called from the zhuLi directory (not the root of the git repo!)__

Expand Down
29 changes: 19 additions & 10 deletions zhuLi/documentation/todo.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
## Things left to do for week 4 deadline
## Things left to do for week 5 deadline

### Necessary

### Extra Credit
* Generate UML diagram of core classes and embed it into architecture.md in documentation
#### The actual app
* Add a form for adding and editing sources
* Make it possible to redefine where the sources are loaded from
* Maybe just a dialog box at launch, where the user can feed a path
* Try to figure out the architechture and package structure better
* Figure out how to link to local files or something
* Maybe have a designated file where all the files (or symlinks) are loaded from
* Possivly just have a field with a path, but that pah can also be relative to some set of directories listed in some config file

#### Other stuff
* Make a way to generate runnable jar and add it to readme
* min 20% test coverage
* Exclude UI code from test report
* Make a way to generate runnable jar and add it to readme
* Keep the hours logged
* Make a sequence diagram of a central part of the program's functionality (making and saving a change?)
* Clean up the readme and add a more detailed user's manual
* Make a release


### Done
* Get ktlint errors below 10
* Switch to gradle and get ./gradlew run to work
* Add jacoco plugin
* Add ktlint to project
* Have packages make sense, separate UI and logic
### Extra Credit
* Generate UML diagram of core classes and embed it into architecture.md in documentation
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import com.squareup.moshi.KotlinJsonAdapterFactory
import com.squareup.moshi.Moshi
import com.squareup.moshi.Rfc3339DateJsonAdapter
import com.squareup.moshi.Types
import zhuLi.ui.models.Source
import zhuLi.domain.Source
import java.io.File
import java.util.Date

class SourceDao(val file: File) {
// TODO: Make this all nice with, like, interfaces and things
class JsonSourceListDao(val file: File) : SourceListDao {

override val sources: List<Source>

val listType = Types.newParameterizedType(List::class.java, Source::class.java)

val moshiSource = Moshi.Builder()
Expand All @@ -28,10 +30,7 @@ class SourceDao(val file: File) {

val listAdapter: JsonAdapter<List<Source>> = moshiList.adapter(listType)

var sources: List<Source>

init {
// test()
sources = load()
}

Expand All @@ -49,17 +48,13 @@ class SourceDao(val file: File) {
}
}

fun load(): List<Source> {
override fun load(): List<Source> {
val jsonList = file.readText()
val sourceList = listAdapter.fromJson(jsonList)
return sourceList!!
}

fun reload() {
sources = load()
}

fun save(sources: List<Source>) {
override fun save(sources: List<Source>) {
var jsonList = listAdapter.toJson(sources)
file.printWriter().use { out ->
out.println(jsonList)
Expand Down
10 changes: 10 additions & 0 deletions zhuLi/src/main/kotlin/zhuLi/dao/SourceListDao.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package zhuLi.dao

import zhuLi.domain.Source

interface SourceListDao {
val sources: List<Source>

fun load(): List<Source>
fun save(sources: List<Source>)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zhuLi.ui.models
package zhuLi.domain

import com.squareup.moshi.Json
import javafx.beans.property.SimpleIntegerProperty
Expand Down Expand Up @@ -42,6 +42,7 @@ class Source(
var publisher by publisherProperty
}

// TODO: figure out what the point of this actually is
class SourceModel : ItemViewModel<Source>() {
val id = bind(Source::idProperty)
val title = bind(Source::titleProperty)
Expand Down
17 changes: 17 additions & 0 deletions zhuLi/src/main/kotlin/zhuLi/domain/SourceListModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package zhuLi.domain

import zhuLi.dao.JsonSourceListDao

class SourceListModel(val sourceListDao: JsonSourceListDao) {
val sources = sourceListDao.sources

fun save(sources: List<Source>) {
sourceListDao.save(sources)
}

fun reload() {
sourceListDao.load()
}

// TODO: something like getByProject should go here?
}
17 changes: 0 additions & 17 deletions zhuLi/src/main/kotlin/zhuLi/domain/SourceService.kt

This file was deleted.

17 changes: 11 additions & 6 deletions zhuLi/src/main/kotlin/zhuLi/ui/controllers/SourceController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,33 @@ package zhuLi.ui.controllers

import javafx.collections.FXCollections
import tornadofx.Controller
import zhuLi.dao.SourceDao
import zhuLi.domain.SourceService
import zhuLi.ui.models.Source
import zhuLi.dao.JsonSourceListDao
import zhuLi.domain.Source
import zhuLi.domain.SourceListModel
import java.io.File
import java.util.Date

class SourceController() : Controller() {
// val sources: FXCollections.observableArrayList<Source>
var sources = FXCollections.observableArrayList<Source>()
val sourceService: SourceService
val listModel: SourceListModel

init {
// TODO: pull the path from somewhere
var source = Source(1, "Top research", Date(1981, 12, 4), "", "ArXiv")
sourceService = SourceService(SourceDao(File("sampleSources.json")))
sources = FXCollections.observableArrayList<Source>(sourceService.sources)
listModel = SourceListModel(JsonSourceListDao(File("sampleSources.json")))
sources = FXCollections.observableArrayList<Source>(listModel.sources)
}

fun addSource(source: Source) {
sources.add(source)
}

fun deleteSource(source: Source) {
sources.remove(source)
}

fun save() {
listModel.save(sources)
}
}
53 changes: 46 additions & 7 deletions zhuLi/src/main/kotlin/zhuLi/ui/views/MainView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,77 @@ package zhuLi.ui.views
import javafx.scene.control.TableView
import tornadofx.View
import tornadofx.action
import tornadofx.bindSelected
import tornadofx.borderpane
import tornadofx.bottom
import tornadofx.button
import tornadofx.center
import tornadofx.column
import tornadofx.hbox
import tornadofx.makeEditable
import tornadofx.singleAssign
import tornadofx.smartResize
import tornadofx.tableview
import zhuLi.domain.Source
import zhuLi.domain.SourceModel
import zhuLi.ui.controllers.SourceController
import zhuLi.ui.models.Source
import zhuLi.ui.models.SourceModel
import java.util.Date

class MainView : View("Zhu Li - Digital Research Assistant") {

private val controller: SourceController by inject()
private val model: SourceModel by inject()
private val sourceModel: SourceModel by inject()
private var sourceTable: TableView<Source> by singleAssign()

override val root = borderpane {
center {
add<SourceTable>()
tableview(controller.sources) {
sourceTable = this
// TODO: date is shown in american format, this is unacceptable
// TODO: implement a way to edit authors
column("id", Source::idProperty)
column("title", Source::titleProperty).makeEditable()
// readonlyColumn("authors", Source::authorsProperty)
column("pub. date", Source::pubDateProperty)//.makeEditable()
column("Date Added", Source::addDateProperty)
column("BibTex", Source::bibTexProperty).makeEditable()
column("publisher", Source::publisherProperty).makeEditable()
bindSelected(sourceModel)
isEditable = true
prefWidth = 800.0
smartResize()
}

}
bottom {
hbox {
button("Add Source").action(::addSource)
button("Delete selected source").action(::deleteSource)
button("Save").action(::save)
}
}
bottom { button("Add Source").action(::addSource) }
}


private fun addSource() {
val newSource = Source(controller.sources.size + 1, "", Date(), "", "")
controller.addSource(newSource)
sourceTable.selectionModel.select(newSource)
sourceTable.edit(sourceTable.items.size - 1, sourceTable.columns.first())
// sourceTable.selectionModel.select(newSource)
sourceTable.selectionModel.select(sourceTable.items.size - 1, sourceTable.columns[1])
}

private fun deleteSource() {
controller.deleteSource(sourceTable.selectionModel.selectedItem)
}

private fun save() {
controller.save()
}

init {
// controller = SourceController("sources.json")
root.setPrefSize(800.0, 600.0)
}

// TODO: Make it save on close?
}
23 changes: 5 additions & 18 deletions zhuLi/src/main/kotlin/zhuLi/ui/views/SourceTable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,16 @@ import tornadofx.column
import tornadofx.makeEditable
import tornadofx.smartResize
import tornadofx.tableview
import tornadofx.vbox
import zhuLi.domain.Source
import zhuLi.domain.SourceModel
import zhuLi.ui.controllers.SourceController
import zhuLi.ui.models.Source
import zhuLi.ui.models.SourceModel

class SourceTable : View() {
private val controller: SourceController by inject()
private val model: SourceModel by inject()
// private var sourceTable: TableView<Source> by singleAssign()

override val root = tableview(controller.sources) {
// sourceTable = this
// TODO: date is shown in american format, this is unacceptable
// TODO: implement a way to edit authors
column("id", Source::idProperty)
column("title", Source::titleProperty).makeEditable()
// readonlyColumn("authors", Source::authorsProperty)
column("pub. date", Source::pubDateProperty)//.makeEditable()
column("Date Added", Source::addDateProperty)
column("BibTex", Source::bibTexProperty).makeEditable()
column("publisher", Source::publisherProperty).makeEditable()
bindSelected(model)
isEditable = true
prefWidth = 800.0
smartResize()
override val root = vbox {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package zhuLi.dao
import org.junit.After
import org.junit.Before

class SourceDaoTest {
class JsonSourceListDaoTest {

@Before
fun setUp() {
Expand Down
2 changes: 1 addition & 1 deletion zhuLi/src/test/kotlin/zhuLi/ui/SourceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package zhuLi.ui

import org.junit.Assert.assertTrue
import org.junit.Test
import zhuLi.ui.models.Source
import zhuLi.domain.Source
import java.util.Date

class SourceTest {
Expand Down

0 comments on commit 5c5f479

Please sign in to comment.