Compare commits
3 commits
97920d571e
...
6efc014ce5
Author | SHA1 | Date | |
---|---|---|---|
6efc014ce5 | |||
97958f2a53 | |||
5b7e327d2f |
2 changed files with 110 additions and 16 deletions
|
@ -1,31 +1,121 @@
|
||||||
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)
|
||||||
|
|
||||||
binding.saveButton.setOnClickListener {
|
dbHelper = DatabaseHelper(this)
|
||||||
val url = binding.urlEditText.text.toString()
|
dbHelper.open()
|
||||||
val token = binding.tokenEditText.text.toString()
|
|
||||||
// Save URL and TOKEN in SharedPreferences
|
// Request notification permission on first install
|
||||||
val sharedPreferences = getSharedPreferences("app_prefs", MODE_PRIVATE)
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
|
||||||
with(sharedPreferences.edit()) {
|
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS) != android.content.pm.PackageManager.PERMISSION_GRANTED) {
|
||||||
putString("url", url)
|
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.POST_NOTIFICATIONS), 1)
|
||||||
putString("token", token)
|
|
||||||
apply()
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
// 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 {
|
||||||
|
@ -82,9 +84,11 @@ 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) {
|
||||||
sendNotification("Request successful!")
|
runOnUiThread {
|
||||||
|
Toast.makeText(this@ShareActivity, "URL sent to your Mealie API", Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
sendNotification("Request failed: ${response.message}")
|
sendNotification("Request failed: ${response.code} ${response.message}")
|
||||||
}
|
}
|
||||||
finish()
|
finish()
|
||||||
}
|
}
|
||||||
|
@ -108,7 +112,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.drawable.baseline_error_24) // Replace with your notification icon
|
.setSmallIcon(R.mipmap.mealie_upload_round) // Replace with your notification icon
|
||||||
.setAutoCancel(true)
|
.setAutoCancel(true)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue