url and token stored and fetched in SQLite
This commit is contained in:
parent
97958f2a53
commit
6efc014ce5
2 changed files with 96 additions and 13 deletions
|
@ -1,5 +1,8 @@
|
||||||
package org.seanandroid.mealieurlshare
|
package org.seanandroid.mealieurlshare
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.database.sqlite.SQLiteDatabase
|
||||||
|
import android.database.sqlite.SQLiteOpenHelper
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
@ -10,12 +13,16 @@ import org.seanandroid.mealieurlshare.databinding.ActivityMainBinding
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
private lateinit var binding: ActivityMainBinding
|
private lateinit var binding: ActivityMainBinding
|
||||||
|
private lateinit var dbHelper: DatabaseHelper
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
binding = ActivityMainBinding.inflate(layoutInflater)
|
binding = ActivityMainBinding.inflate(layoutInflater)
|
||||||
setContentView(binding.root)
|
setContentView(binding.root)
|
||||||
|
|
||||||
|
dbHelper = DatabaseHelper(this)
|
||||||
|
dbHelper.open()
|
||||||
|
|
||||||
// Request notification permission on first install
|
// Request notification permission on first install
|
||||||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
|
||||||
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS) != android.content.pm.PackageManager.PERMISSION_GRANTED) {
|
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS) != android.content.pm.PackageManager.PERMISSION_GRANTED) {
|
||||||
|
@ -23,18 +30,92 @@ class MainActivity : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.saveButton.setOnClickListener {
|
// Load text fields from database
|
||||||
val url = binding.urlEditText.text.toString()
|
val url = dbHelper.getUrl()
|
||||||
val token = binding.tokenEditText.text.toString()
|
val token = dbHelper.getToken()
|
||||||
// Save URL and TOKEN in SharedPreferences
|
|
||||||
val sharedPreferences = getSharedPreferences("app_prefs", MODE_PRIVATE)
|
if (url != null) {
|
||||||
with(sharedPreferences.edit()) {
|
binding.urlEditText.setText(url)
|
||||||
putString("url", url)
|
|
||||||
putString("token", token)
|
|
||||||
apply()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (token != null) {
|
||||||
|
binding.tokenEditText.setText(token)
|
||||||
|
}
|
||||||
|
|
||||||
|
binding.saveButton.setOnClickListener {
|
||||||
|
val newUrl = binding.urlEditText.text.toString()
|
||||||
|
val newToken = binding.tokenEditText.text.toString()
|
||||||
|
// Save URL and TOKEN in SQLite database
|
||||||
|
dbHelper.saveUrlAndToken(newUrl, newToken)
|
||||||
// Show a Toast message to confirm saving
|
// Show a Toast message to confirm saving
|
||||||
Toast.makeText(this, "URL and TOKEN saved", Toast.LENGTH_SHORT).show()
|
Toast.makeText(this, "URL and TOKEN saved", Toast.LENGTH_SHORT).show()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
dbHelper.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class DatabaseHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val DATABASE_NAME = "mealie_url_share.db"
|
||||||
|
private const val DATABASE_VERSION = 1
|
||||||
|
private const val TABLE_NAME = "settings"
|
||||||
|
private const val COLUMN_URL = "url"
|
||||||
|
private const val COLUMN_TOKEN = "token"
|
||||||
|
}
|
||||||
|
|
||||||
|
private var db: SQLiteDatabase? = null
|
||||||
|
|
||||||
|
override fun onCreate(db: SQLiteDatabase) {
|
||||||
|
db.execSQL("CREATE TABLE $TABLE_NAME ($COLUMN_URL TEXT, $COLUMN_TOKEN TEXT)")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
fun open() {
|
||||||
|
db = writableDatabase
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun close() {
|
||||||
|
db?.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun saveUrlAndToken(url: String, token: String) {
|
||||||
|
open()
|
||||||
|
val previousUrl = getUrl()
|
||||||
|
val previousToken = getToken()
|
||||||
|
|
||||||
|
if (url != previousUrl || token != previousToken) {
|
||||||
|
db?.execSQL("DELETE FROM $TABLE_NAME")
|
||||||
|
db?.execSQL("INSERT INTO $TABLE_NAME ($COLUMN_URL, $COLUMN_TOKEN) VALUES ('$url', '$token')")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getUrl(): String? {
|
||||||
|
open()
|
||||||
|
val cursor = db?.rawQuery("SELECT $COLUMN_URL FROM $TABLE_NAME", null)
|
||||||
|
var url: String? = null
|
||||||
|
if (cursor?.moveToFirst() == true) {
|
||||||
|
url = cursor.getString(0)
|
||||||
|
}
|
||||||
|
cursor?.close()
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getToken(): String? {
|
||||||
|
open()
|
||||||
|
val cursor = db?.rawQuery("SELECT $COLUMN_TOKEN FROM $TABLE_NAME", null)
|
||||||
|
var token: String? = null
|
||||||
|
if (cursor?.moveToFirst() == true) {
|
||||||
|
token = cursor.getString(0)
|
||||||
|
}
|
||||||
|
cursor?.close()
|
||||||
|
return token
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,20 +22,22 @@ import java.io.IOException
|
||||||
class ShareActivity : AppCompatActivity() {
|
class ShareActivity : AppCompatActivity() {
|
||||||
|
|
||||||
private val client = OkHttpClient()
|
private val client = OkHttpClient()
|
||||||
|
private lateinit var dbHelper: DatabaseHelper
|
||||||
private lateinit var handler: Handler
|
private lateinit var handler: Handler
|
||||||
private var isRequestRunning = false
|
private var isRequestRunning = false
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
// Get the shared preferences
|
dbHelper = DatabaseHelper(this)
|
||||||
val sharedPreferences = getSharedPreferences("app_prefs", MODE_PRIVATE)
|
dbHelper.open()
|
||||||
val url = sharedPreferences.getString("url", null)
|
|
||||||
val token = sharedPreferences.getString("token", null)
|
|
||||||
|
|
||||||
// Get the shared text
|
// Get the shared text
|
||||||
val sharedText = intent.getStringExtra(Intent.EXTRA_TEXT)
|
val sharedText = intent.getStringExtra(Intent.EXTRA_TEXT)
|
||||||
|
|
||||||
|
val url = dbHelper.getUrl()
|
||||||
|
val token = dbHelper.getToken()
|
||||||
|
|
||||||
if (url != null && token != null && sharedText != null) {
|
if (url != null && token != null && sharedText != null) {
|
||||||
sendPostRequest(url, token, sharedText)
|
sendPostRequest(url, token, sharedText)
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue