Commit 8395ef26 authored by m-spi's avatar m-spi

Je pense on tient le bon bout

parent 2d98e570
...@@ -21,7 +21,6 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_V ...@@ -21,7 +21,6 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_V
} }
override fun onCreate(db: SQLiteDatabase) { override fun onCreate(db: SQLiteDatabase) {
Log.i(null, "table sql create : $CREATE_TABLE")
db.execSQL(CREATE_TABLE) db.execSQL(CREATE_TABLE)
} }
......
...@@ -69,7 +69,6 @@ private constructor(private val context: Context) { ...@@ -69,7 +69,6 @@ private constructor(private val context: Context) {
fun fetch(): Cursor? { fun fetch(): Cursor? {
val columns: Array<String> = val columns: Array<String> =
arrayOf(DBHelper._ID, DBHelper.TITLE, DBHelper.ADDRESS, DBHelper.IMAGE, DBHelper.PRIX) arrayOf(DBHelper._ID, DBHelper.TITLE, DBHelper.ADDRESS, DBHelper.IMAGE, DBHelper.PRIX)
columns.forEach { e -> Log.i(null, e) }
val cursor = this.database!!.query(DBHelper.TABLE_NAME, columns, null, null, null, null, null) val cursor = this.database!!.query(DBHelper.TABLE_NAME, columns, null, null, null, null, null)
cursor?.moveToFirst() cursor?.moveToFirst()
return cursor return cursor
......
package com.example.elbuenopeso.ui.add package com.example.elbuenopeso.ui.add
import android.app.Activity import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent import android.content.Intent
import android.graphics.Bitmap import android.graphics.Bitmap
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.provider.MediaStore import android.provider.MediaStore
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageButton import android.widget.ImageButton
import android.widget.ImageView import android.widget.ImageView
import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import com.example.elbuenopeso.R
import com.example.elbuenopeso.database.DBManager
import com.example.elbuenopeso.databinding.FragmentAddBinding import com.example.elbuenopeso.databinding.FragmentAddBinding
import com.example.elbuenopeso.models.AdModel
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
...@@ -28,12 +38,15 @@ class AddFragment : Fragment() { ...@@ -28,12 +38,15 @@ class AddFragment : Fragment() {
private lateinit var cameraButton: ImageButton private lateinit var cameraButton: ImageButton
private lateinit var image: ImageView private lateinit var image: ImageView
private lateinit var sendButton: Button
private var filePath: String = ""
private val galleryActivityResultLauncher: ActivityResultLauncher<Intent> = private val galleryActivityResultLauncher: ActivityResultLauncher<Intent> =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
result -> run { result -> run {
if (result.resultCode == Activity.RESULT_OK) { if (result.resultCode == Activity.RESULT_OK) {
val image = result?.data?.data as Uri val image = result?.data?.data as Uri
this.filePath = image.path!!
this.image.setImageURI(image) this.image.setImageURI(image)
} }
} }
...@@ -41,11 +54,28 @@ class AddFragment : Fragment() { ...@@ -41,11 +54,28 @@ class AddFragment : Fragment() {
private val cameraActivityResultLauncher: ActivityResultLauncher<Intent> = private val cameraActivityResultLauncher: ActivityResultLauncher<Intent> =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
result -> run { result -> if (result.resultCode == Activity.RESULT_OK) {
if (result.resultCode == Activity.RESULT_OK) { val data = result.data
val image = result.data?.extras?.get("data") as Bitmap? val bitmap = data!!.extras!!["data"] as Bitmap?
this.image.setImageBitmap(image) val cw = ContextWrapper(requireContext())
val directory = cw.getDir("imageDir", Context.MODE_PRIVATE)
val tsLong = System.currentTimeMillis() / 1000
val ts = tsLong.toString()
val file = File(directory, "$ts.jpg")
if (!file.exists()) {
Log.d("path", file.toString())
var fos: FileOutputStream? = null
try {
fos = FileOutputStream(file)
bitmap!!.compress(Bitmap.CompressFormat.JPEG, 100, fos)
fos.flush()
fos.close()
} catch (e: IOException) {
e.printStackTrace()
}
} }
this.filePath = file.toString()
this.image.setImageBitmap(bitmap)
} }
} }
...@@ -54,27 +84,52 @@ class AddFragment : Fragment() { ...@@ -54,27 +84,52 @@ class AddFragment : Fragment() {
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View { ): View {
// val notificationsViewModel =
// ViewModelProvider(this)[AddViewModel::class.java]
_binding = FragmentAddBinding.inflate(inflater, container, false) _binding = FragmentAddBinding.inflate(inflater, container, false)
val root: View = binding.root val root: View = binding.root
image = binding.image image = binding.image
cameraButton = binding.cameraButton cameraButton = binding.cameraButton
sendButton = binding.sendButton
// DB manager :
val dbManager = DBManager.getDBManager(requireContext())
dbManager!!.open()
binding.image.setOnClickListener { image.setOnClickListener {
val gallery = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI) val gallery = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
galleryActivityResultLauncher.launch(gallery) galleryActivityResultLauncher.launch(gallery)
} }
binding.cameraButton.setOnClickListener { cameraButton.setOnClickListener {
val camera = Intent(MediaStore.ACTION_IMAGE_CAPTURE) val camera = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
cameraActivityResultLauncher.launch(camera) cameraActivityResultLauncher.launch(camera)
} }
binding.sendButton.setOnClickListener { sendButton.setOnClickListener {
if(binding.title.text.toString().isEmpty() || binding.price.text.toString().isEmpty()){
return@setOnClickListener
}
val ad = AdModel(
binding.title.text.toString(),
binding.address.text.toString(),
this.filePath,
binding.price.text.toString().toDouble()
)
dbManager.insert(ad);
image.setImageResource(R.drawable.baseline_add_24)
binding.title.setText("")
binding.address.setText("")
binding.price.setText("")
this.filePath = ""
// Je sais pas trop ce que c'est mais dans le doute je laisse ça là...
// val intent: Intent = Intent(
// this@AddFragment,
// DbAdListViewActivity::class.java
// )
// startActivity(intent)
} }
return root return root
......
...@@ -68,7 +68,8 @@ class MarketFragment : Fragment() { ...@@ -68,7 +68,8 @@ class MarketFragment : Fragment() {
} }
val dbManager: DBManager? = DBManager.getDBManager(requireContext()) val dbManager: DBManager? = DBManager.getDBManager(requireContext())
dbManager!!.init() //dbManager!!.init()
dbManager!!.open()
val cursor = dbManager.fetch() val cursor = dbManager.fetch()
adapter = DbAdapter(requireContext(), cursor!!, R.layout.item_listview_ad) adapter = DbAdapter(requireContext(), cursor!!, R.layout.item_listview_ad)
adapter.notifyDataSetChanged() adapter.notifyDataSetChanged()
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/ic_menu_report_image" /> app:srcCompat="@drawable/baseline_add_24" />
<ImageButton <ImageButton
android:id="@+id/cameraButton" android:id="@+id/cameraButton"
...@@ -76,18 +76,38 @@ ...@@ -76,18 +76,38 @@
android:paddingHorizontal="36dp" /> android:paddingHorizontal="36dp" />
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/priceLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginHorizontal="20dp"
android:layout_marginVertical="10dp"
android:hint="@string/price"
android:textColorHint="#6E6E6E"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/addressLayout"
tools:ignore="MissingConstraints">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingHorizontal="36dp" />
</com.google.android.material.textfield.TextInputLayout>
<Button <Button
android:id="@+id/sendButton" android:id="@+id/sendButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="32dp" android:layout_marginTop="24dp"
android:layout_marginBottom="8dp" android:layout_marginBottom="8dp"
android:text="@string/send" android:text="@string/send"
android:textSize="20sp" android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/addressLayout" app:layout_constraintTop_toBottomOf="@+id/priceLayout"
tools:ignore="MissingConstraints" /> tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
......
...@@ -9,4 +9,5 @@ ...@@ -9,4 +9,5 @@
<string name="camera">Camera</string> <string name="camera">Camera</string>
<string name="title">Title</string> <string name="title">Title</string>
<string name="gallery">Gallery</string> <string name="gallery">Gallery</string>
<string name="price">Price</string>
</resources> </resources>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment