From 24ee0500b324a65b56e1d76fa1bb7147afbe200f Mon Sep 17 00:00:00 2001 From: Daniele Verducci Date: Sun, 10 Nov 2024 15:43:52 +0100 Subject: [PATCH] Predisposition for WebDAV credentials --- .../lunatracker/MainActivity.kt | 35 +++++++++++---- .../repository/LocalSettingsRepository.kt | 45 +++++++++++++++++++ 2 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/it/danieleverducci/lunatracker/repository/LocalSettingsRepository.kt diff --git a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt index 3d6df55..59962c9 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt @@ -17,6 +17,7 @@ import it.danieleverducci.lunatracker.adapters.LunaEventRecyclerAdapter import it.danieleverducci.lunatracker.entities.Logbook import it.danieleverducci.lunatracker.entities.LunaEvent import it.danieleverducci.lunatracker.entities.LunaEventType +import it.danieleverducci.lunatracker.repository.LocalSettingsRepository import it.danieleverducci.lunatracker.repository.LogbookLoadedListener import it.danieleverducci.lunatracker.repository.LogbookRepository import it.danieleverducci.lunatracker.repository.LogbookSavedListener @@ -26,8 +27,6 @@ import kotlinx.coroutines.Runnable class MainActivity : AppCompatActivity() { companion object { val TAG = "MainActivity" - val SHARED_PREFS_FILE_NAME = "lunasettings" - val SHARED_PREFS_BB_CONTENT = "bbcontent" } lateinit var logbook: Logbook @@ -40,14 +39,31 @@ class MainActivity : AppCompatActivity() { loadLogbook() handler.postDelayed(updateListRunnable, 1000*60) } - val logbookRepo: LogbookRepository = WebDAVLogbookRepository( // TODO: support also FileLogbookRepository - TemporaryHardcodedCredentials.URL, - TemporaryHardcodedCredentials.USERNAME, - TemporaryHardcodedCredentials.PASSWORD - ) + lateinit var logbookRepo: LogbookRepository override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + + val localSettings = LocalSettingsRepository(this) + + // TEMPORARY: Save credentials (prepare for update with settings enabled) + localSettings.saveWebdavCredentials( + TemporaryHardcodedCredentials.URL, + TemporaryHardcodedCredentials.USERNAME, + TemporaryHardcodedCredentials.PASSWORD + ) + // END TEMPORARY + + val webDavCredentials = localSettings.loadWebdavCredentials() + if (webDavCredentials == null) { + TODO("Not supported ATM (TODO: apply settings)") + } + logbookRepo = WebDAVLogbookRepository( // TODO: support also FileLogbookRepository + webDavCredentials[0], + webDavCredentials[1], + webDavCredentials[2] + ) + handler = Handler(mainLooper) adapter = LunaEventRecyclerAdapter(this) @@ -133,6 +149,7 @@ class MainActivity : AppCompatActivity() { fun askBabyBottleContent() { // Show number picker dialog + val localSettings = LocalSettingsRepository(this) val d = AlertDialog.Builder(this) val dialogView = layoutInflater.inflate(R.layout.number_picker_dialog, null) d.setTitle(R.string.log_bottle_dialog_title) @@ -143,10 +160,10 @@ class MainActivity : AppCompatActivity() { numberPicker.maxValue = 25 // "250 numberPicker.displayedValues = ((10..250 step 10).map { it.toString() }.toTypedArray()) numberPicker.wrapSelectorWheel = false - numberPicker.value = getSharedPreferences(SHARED_PREFS_FILE_NAME, MODE_PRIVATE).getInt(SHARED_PREFS_BB_CONTENT, 1) + numberPicker.value = localSettings.loadBabyBottleContent() d.setPositiveButton(android.R.string.ok) { dialogInterface, i -> logEvent(LunaEvent(LunaEventType.BABY_BOTTLE, numberPicker.value * 10)) - getSharedPreferences(SHARED_PREFS_FILE_NAME, MODE_PRIVATE).edit().putInt(SHARED_PREFS_BB_CONTENT, numberPicker.value).commit() + localSettings.saveBabyBottleContent(numberPicker.value) } d.setNegativeButton(android.R.string.cancel) { dialogInterface, i -> dialogInterface.dismiss() } val alertDialog = d.create() diff --git a/app/src/main/java/it/danieleverducci/lunatracker/repository/LocalSettingsRepository.kt b/app/src/main/java/it/danieleverducci/lunatracker/repository/LocalSettingsRepository.kt new file mode 100644 index 0000000..9b41de0 --- /dev/null +++ b/app/src/main/java/it/danieleverducci/lunatracker/repository/LocalSettingsRepository.kt @@ -0,0 +1,45 @@ +package it.danieleverducci.lunatracker.repository + +import android.content.Context +import android.content.Context.MODE_PRIVATE +import android.content.SharedPreferences + +class LocalSettingsRepository(val context: Context) { + companion object { + val SHARED_PREFS_FILE_NAME = "lunasettings" + val SHARED_PREFS_BB_CONTENT = "bbcontent" + val SHARED_PREFS_DAV_URL = "webdav_url" + val SHARED_PREFS_DAV_USER = "webdav_user" + val SHARED_PREFS_DAV_PASS = "webdav_password" + } + val sharedPreferences: SharedPreferences + + init { + sharedPreferences = context.getSharedPreferences(SHARED_PREFS_FILE_NAME, MODE_PRIVATE) + } + + fun saveBabyBottleContent(content: Int) { + sharedPreferences.edit().putInt(SHARED_PREFS_BB_CONTENT, content).apply() + } + + fun loadBabyBottleContent(): Int { + return sharedPreferences.getInt(SHARED_PREFS_BB_CONTENT, 1) + } + + fun saveWebdavCredentials(url: String, username: String, password: String) { + val spe = sharedPreferences.edit() + spe.putString(SHARED_PREFS_DAV_URL, url) + spe.putString(SHARED_PREFS_DAV_USER, username) + spe.putString(SHARED_PREFS_DAV_PASS, password) + spe.commit() + } + + fun loadWebdavCredentials(): Array? { + val url = sharedPreferences.getString(SHARED_PREFS_DAV_URL, null) + val user = sharedPreferences.getString(SHARED_PREFS_DAV_USER, null) + val pass = sharedPreferences.getString(SHARED_PREFS_DAV_PASS, null) + if (url == null || user == null || pass == null) + return null + return arrayOf(url, user, pass) + } +} \ No newline at end of file