Compare commits

..

No commits in common. "6efc014ce568c1bda89d5960e85da05345b6861a" and "97920d571e6c2da760b1d70e69aaedaa7b36ce4a" have entirely different histories.

2 changed files with 16 additions and 110 deletions

View file

@ -1,121 +1,31 @@
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
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import org.seanandroid.mealieurlshare.databinding.ActivityMainBinding 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
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 { binding.saveButton.setOnClickListener {
val newUrl = binding.urlEditText.text.toString() val url = binding.urlEditText.text.toString()
val newToken = binding.tokenEditText.text.toString() val token = binding.tokenEditText.text.toString()
// Save URL and TOKEN in SQLite database // Save URL and TOKEN in SharedPreferences
dbHelper.saveUrlAndToken(newUrl, newToken) val sharedPreferences = getSharedPreferences("app_prefs", MODE_PRIVATE)
with(sharedPreferences.edit()) {
putString("url", url)
putString("token", token)
apply()
}
// 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
}
} }

View file

@ -22,22 +22,20 @@ 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)
dbHelper = DatabaseHelper(this) // Get the shared preferences
dbHelper.open() val sharedPreferences = getSharedPreferences("app_prefs", MODE_PRIVATE)
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 {
@ -84,11 +82,9 @@ class ShareActivity : AppCompatActivity() {
override fun onResponse(call: Call, response: Response) { override fun onResponse(call: Call, response: Response) {
isRequestRunning = false isRequestRunning = false
if (response.isSuccessful) { if (response.isSuccessful) {
runOnUiThread { sendNotification("Request successful!")
Toast.makeText(this@ShareActivity, "URL sent to your Mealie API", Toast.LENGTH_SHORT).show()
}
} else { } else {
sendNotification("Request failed: ${response.code} ${response.message}") sendNotification("Request failed: ${response.message}")
} }
finish() finish()
} }
@ -112,7 +108,7 @@ class ShareActivity : AppCompatActivity() {
val notification = NotificationCompat.Builder(this, channelId) val notification = NotificationCompat.Builder(this, channelId)
.setContentTitle("Mealie URL Share") .setContentTitle("Mealie URL Share")
.setContentText(message) .setContentText(message)
.setSmallIcon(R.mipmap.mealie_upload_round) // Replace with your notification icon .setSmallIcon(R.drawable.baseline_error_24) // Replace with your notification icon
.setAutoCancel(true) .setAutoCancel(true)
.build() .build()