Compare commits

..

3 commits

2 changed files with 110 additions and 16 deletions

View file

@ -1,31 +1,121 @@
package org.seanandroid.mealieurlshare
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import org.seanandroid.mealieurlshare.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var dbHelper: DatabaseHelper
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.saveButton.setOnClickListener {
val url = binding.urlEditText.text.toString()
val token = binding.tokenEditText.text.toString()
// Save URL and TOKEN in SharedPreferences
val sharedPreferences = getSharedPreferences("app_prefs", MODE_PRIVATE)
with(sharedPreferences.edit()) {
putString("url", url)
putString("token", token)
apply()
dbHelper = DatabaseHelper(this)
dbHelper.open()
// Request notification permission on first install
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) {
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.POST_NOTIFICATIONS), 1)
}
}
// Load text fields from database
val url = dbHelper.getUrl()
val token = dbHelper.getToken()
if (url != null) {
binding.urlEditText.setText(url)
}
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
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
}
}

View file

@ -22,20 +22,22 @@ import java.io.IOException
class ShareActivity : AppCompatActivity() {
private val client = OkHttpClient()
private lateinit var dbHelper: DatabaseHelper
private lateinit var handler: Handler
private var isRequestRunning = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Get the shared preferences
val sharedPreferences = getSharedPreferences("app_prefs", MODE_PRIVATE)
val url = sharedPreferences.getString("url", null)
val token = sharedPreferences.getString("token", null)
dbHelper = DatabaseHelper(this)
dbHelper.open()
// Get the shared text
val sharedText = intent.getStringExtra(Intent.EXTRA_TEXT)
val url = dbHelper.getUrl()
val token = dbHelper.getToken()
if (url != null && token != null && sharedText != null) {
sendPostRequest(url, token, sharedText)
} else {
@ -82,9 +84,11 @@ class ShareActivity : AppCompatActivity() {
override fun onResponse(call: Call, response: Response) {
isRequestRunning = false
if (response.isSuccessful) {
sendNotification("Request successful!")
runOnUiThread {
Toast.makeText(this@ShareActivity, "URL sent to your Mealie API", Toast.LENGTH_SHORT).show()
}
} else {
sendNotification("Request failed: ${response.message}")
sendNotification("Request failed: ${response.code} ${response.message}")
}
finish()
}
@ -108,7 +112,7 @@ class ShareActivity : AppCompatActivity() {
val notification = NotificationCompat.Builder(this, channelId)
.setContentTitle("Mealie URL Share")
.setContentText(message)
.setSmallIcon(R.drawable.baseline_error_24) // Replace with your notification icon
.setSmallIcon(R.mipmap.mealie_upload_round) // Replace with your notification icon
.setAutoCancel(true)
.build()