diff --git a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt index 4b3d073..5f1a595 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt @@ -24,6 +24,7 @@ import it.danieleverducci.lunatracker.entities.Logbook import it.danieleverducci.lunatracker.entities.LunaEvent import it.danieleverducci.lunatracker.repository.FileLogbookRepository import it.danieleverducci.lunatracker.repository.LocalSettingsRepository +import it.danieleverducci.lunatracker.repository.LogbookListObtainedListener import it.danieleverducci.lunatracker.repository.LogbookLoadedListener import it.danieleverducci.lunatracker.repository.LogbookRepository import it.danieleverducci.lunatracker.repository.LogbookSavedListener @@ -156,7 +157,7 @@ class MainActivity : AppCompatActivity() { adapter.notifyDataSetChanged() // Reload data - loadLogbook() + loadLogbookList() } override fun onStop() { @@ -304,17 +305,89 @@ class MainActivity : AppCompatActivity() { alertDialog.show() } - fun loadLogbook() { + fun showAddLogbookDialog() { + val d = AlertDialog.Builder(this) + d.setTitle(R.string.dialog_add_logbook_title) + val dialogView = layoutInflater.inflate(R.layout.dialog_add_logbook, null) + val logbookNameEditText = dialogView.findViewById(R.id.dialog_add_logbook_logbookname) + d.setView(dialogView) + d.setPositiveButton(android.R.string.ok) { dialogInterface, i -> addLogbook(logbookNameEditText.text.toString()) } + d.setNegativeButton(android.R.string.cancel) { dialogInterface, i -> dialogInterface.dismiss() } + val alertDialog = d.create() + alertDialog.show() + } + + fun loadLogbookList() { + setLoading(true) + logbookRepo?.listLogbooks(this, object: LogbookListObtainedListener { + override fun onLogbookListObtained(logbooksNames: ArrayList) { + runOnUiThread({ + // Show logbooks buttons + val logbooksButtonsContainer = + findViewById(R.id.logbooks_buttons_container) + logbooksButtonsContainer.removeAllViews() + for (lbn in logbooksNames) { + val lbnButton = layoutInflater.inflate( + R.layout.logbook_button, + logbooksButtonsContainer, + false + ) as TextView + lbnButton.setText( + if (lbn.isEmpty()) getString(R.string.default_logbook_name) else lbn + ) + lbnButton.setOnClickListener({ + loadLogbook(lbn) + }) + logbooksButtonsContainer.addView(lbnButton) + } + // Show "Add logbook" button + val addLogbookButton = layoutInflater.inflate( + R.layout.logbook_button, + logbooksButtonsContainer, + false + ) as TextView + addLogbookButton.setText("+") + addLogbookButton.setOnClickListener({showAddLogbookDialog()}) + logbooksButtonsContainer.addView(addLogbookButton) + + // Load logbook + loadLogbook() + }) + } + + override fun onIOError(error: IOException) { + TODO("Not yet implemented") + } + + override fun onWebDAVError(error: SardineException) { + TODO("Not yet implemented") + } + + override fun onError(error: Exception) { + TODO("Not yet implemented") + } + }) + } + + fun addLogbook(logbookName: String) { + this.logbook = Logbook(logbookName) + saveLogbook() + loadLogbookList() + } + + fun loadLogbook(name: String = LogbookRepository.DEFAULT_LOGBOOK_NAME) { if (savingEvent) return + // TODO: Highlight logbook button + // Reset time counter handler.removeCallbacks(updateListRunnable) handler.postDelayed(updateListRunnable, UPDATE_EVERY_SECS*1000) // Load data setLoading(true) - logbookRepo?.loadLogbook(this, "", object: LogbookLoadedListener{ + logbookRepo?.loadLogbook(this, name, object: LogbookLoadedListener{ override fun onLogbookLoaded(lb: Logbook) { runOnUiThread({ setLoading(false) diff --git a/app/src/main/java/it/danieleverducci/lunatracker/SettingsActivity.kt b/app/src/main/java/it/danieleverducci/lunatracker/SettingsActivity.kt index 9f12cd6..1fd885d 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/SettingsActivity.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/SettingsActivity.kt @@ -9,6 +9,7 @@ import androidx.appcompat.app.AppCompatActivity import com.google.android.material.progressindicator.LinearProgressIndicator import com.thegrizzlylabs.sardineandroid.impl.SardineException import it.danieleverducci.lunatracker.repository.LocalSettingsRepository +import it.danieleverducci.lunatracker.repository.LogbookRepository import it.danieleverducci.lunatracker.repository.WebDAVLogbookRepository import okio.IOException import org.json.JSONException @@ -72,7 +73,7 @@ open class SettingsActivity : AppCompatActivity() { textViewWebDAVPass.text.toString() ) progressIndicator.visibility = View.VISIBLE - webDAVLogbookRepo.createLogbook(this, "", object: WebDAVLogbookRepository.LogbookCreatedListener{ + webDAVLogbookRepo.createLogbook(this, LogbookRepository.DEFAULT_LOGBOOK_NAME, object: WebDAVLogbookRepository.LogbookCreatedListener{ override fun onLogbookCreated() { runOnUiThread({ progressIndicator.visibility = View.INVISIBLE diff --git a/app/src/main/java/it/danieleverducci/lunatracker/repository/FileLogbookRepository.kt b/app/src/main/java/it/danieleverducci/lunatracker/repository/FileLogbookRepository.kt index 8026a27..7d38816 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/repository/FileLogbookRepository.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/repository/FileLogbookRepository.kt @@ -69,7 +69,7 @@ class FileLogbookRepository: LogbookRepository { override fun listLogbooks( context: Context, listener: LogbookListObtainedListener - ): ArrayList { + ) { val logbooksFileNames = context.getFilesDir().list(object: FilenameFilter { override fun accept(dir: File?, name: String?): Boolean { if (name == null) @@ -81,15 +81,15 @@ class FileLogbookRepository: LogbookRepository { }) if (logbooksFileNames == null || logbooksFileNames.isEmpty()) - return arrayListOf() + listener.onLogbookListObtained(arrayListOf()) val logbooksNames = arrayListOf() logbooksFileNames.forEach { it -> logbooksNames.add( - it.replace(FILE_NAME_START, "").replace("${FILE_NAME_START}_", "").replace(FILE_NAME_END, "") + it.replace("${FILE_NAME_START}_", "").replace(FILE_NAME_START, "").replace(FILE_NAME_END, "") ) } - return logbooksNames + listener.onLogbookListObtained(logbooksNames) } private fun getFileName(name: String): String { diff --git a/app/src/main/java/it/danieleverducci/lunatracker/repository/LogbookRepository.kt b/app/src/main/java/it/danieleverducci/lunatracker/repository/LogbookRepository.kt index d8cd6b2..034803b 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/repository/LogbookRepository.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/repository/LogbookRepository.kt @@ -7,9 +7,12 @@ import okio.IOException import org.json.JSONException interface LogbookRepository { + companion object { + val DEFAULT_LOGBOOK_NAME = "" // For compatibility with older app versions + } fun loadLogbook(context: Context, name: String = "", listener: LogbookLoadedListener) fun saveLogbook(context: Context,logbook: Logbook, listener: LogbookSavedListener) - fun listLogbooks(context: Context, listener: LogbookListObtainedListener): ArrayList + fun listLogbooks(context: Context, listener: LogbookListObtainedListener) } interface LogbookLoadedListener { @@ -29,7 +32,7 @@ interface LogbookSavedListener { } interface LogbookListObtainedListener { - fun onLogbookListObtained() + fun onLogbookListObtained(logbooksNames: ArrayList) fun onIOError(error: IOException) fun onWebDAVError(error: SardineException) fun onError(error: Exception) diff --git a/app/src/main/java/it/danieleverducci/lunatracker/repository/WebDAVLogbookRepository.kt b/app/src/main/java/it/danieleverducci/lunatracker/repository/WebDAVLogbookRepository.kt index a2d4ce2..eb11db5 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/repository/WebDAVLogbookRepository.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/repository/WebDAVLogbookRepository.kt @@ -101,16 +101,22 @@ class WebDAVLogbookRepository(val webDavURL: String, val username: String, val p override fun listLogbooks( context: Context, listener: LogbookListObtainedListener - ): ArrayList { - val logbooksNames = arrayListOf() - for (dr: DavResource in sardine.list(webDavURL)){ - logbooksNames.add( - dr.name.replace(FileLogbookRepository.Companion.FILE_NAME_START, "") - .replace("${FileLogbookRepository.Companion.FILE_NAME_START}_", "") - .replace(FileLogbookRepository.Companion.FILE_NAME_END, "") - ) - } - return logbooksNames + ) { + Thread(Runnable { + val logbooksNames = arrayListOf() + for (dr: DavResource in sardine.list(webDavURL)){ + if(!dr.name.startsWith(FILE_NAME_START)) + continue + if(!dr.name.endsWith(FILE_NAME_END)) + continue + logbooksNames.add( + dr.name.replace("${FILE_NAME_START}_", "") + .replace(FILE_NAME_START, "") + .replace(FILE_NAME_END, "") + ) + } + listener.onLogbookListObtained(logbooksNames) + }).start() } private fun saveLogbook(context: Context, logbook: Logbook) { diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index ca1df24..48cde10 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -17,6 +17,22 @@ android:text="@string/title" android:textSize="30dp" android:gravity="center_horizontal"/> + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/logbook_button.xml b/app/src/main/res/layout/logbook_button.xml new file mode 100644 index 0000000..92963bb --- /dev/null +++ b/app/src/main/res/layout/logbook_button.xml @@ -0,0 +1,13 @@ + + \ No newline at end of file diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index efccd63..e540ab1 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -80,4 +80,10 @@ Dettaglio evento + Aggiungi diario + Nome del diario + Scrivi un nome per identificare questo diario. Comparirà in cima allo schermo, e se usi WebDAV sarà incluso anche nel nome del file di salvataggio. + + Senza nome + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 74c1bf6..87594b8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -103,4 +103,10 @@ Event detail + Add logbook + Logbook name + Write a name to identify this logbook. This name will appear on top of the screen and, if you use WebDAV, will be in the save file name as well. + + Unnamed + \ No newline at end of file