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
}
override fun onCreate(db: SQLiteDatabase) {
Log.i(null, "table sql create : $CREATE_TABLE")
db.execSQL(CREATE_TABLE)
}
......
......@@ -69,7 +69,6 @@ private constructor(private val context: Context) {
fun fetch(): Cursor? {
val columns: Array<String> =
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)
cursor?.moveToFirst()
return cursor
......
package com.example.elbuenopeso.ui.add
import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageButton
import android.widget.ImageView
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
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.models.AdModel
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
@Suppress("DEPRECATION")
......@@ -28,12 +38,15 @@ class AddFragment : Fragment() {
private lateinit var cameraButton: ImageButton
private lateinit var image: ImageView
private lateinit var sendButton: Button
private var filePath: String = ""
private val galleryActivityResultLauncher: ActivityResultLauncher<Intent> =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
result -> run {
if (result.resultCode == Activity.RESULT_OK) {
val image = result?.data?.data as Uri
this.filePath = image.path!!
this.image.setImageURI(image)
}
}
......@@ -41,12 +54,29 @@ class AddFragment : Fragment() {
private val cameraActivityResultLauncher: ActivityResultLauncher<Intent> =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
result -> run {
if (result.resultCode == Activity.RESULT_OK) {
val image = result.data?.extras?.get("data") as Bitmap?
this.image.setImageBitmap(image)
result -> if (result.resultCode == Activity.RESULT_OK) {
val data = result.data
val bitmap = data!!.extras!!["data"] as Bitmap?
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)
}
}
override fun onCreateView(
......@@ -54,27 +84,52 @@ class AddFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// val notificationsViewModel =
// ViewModelProvider(this)[AddViewModel::class.java]
_binding = FragmentAddBinding.inflate(inflater, container, false)
val root: View = binding.root
image = binding.image
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)
galleryActivityResultLauncher.launch(gallery)
}
binding.cameraButton.setOnClickListener {
cameraButton.setOnClickListener {
val camera = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
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
......
......@@ -68,7 +68,8 @@ class MarketFragment : Fragment() {
}
val dbManager: DBManager? = DBManager.getDBManager(requireContext())
dbManager!!.init()
//dbManager!!.init()
dbManager!!.open()
val cursor = dbManager.fetch()
adapter = DbAdapter(requireContext(), cursor!!, R.layout.item_listview_ad)
adapter.notifyDataSetChanged()
......
......@@ -23,7 +23,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/ic_menu_report_image" />
app:srcCompat="@drawable/baseline_add_24" />
<ImageButton
android:id="@+id/cameraButton"
......@@ -76,18 +76,38 @@
android:paddingHorizontal="36dp" />
</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
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_marginTop="24dp"
android:layout_marginBottom="8dp"
android:text="@string/send"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/addressLayout"
app:layout_constraintTop_toBottomOf="@+id/priceLayout"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
......
......@@ -9,4 +9,5 @@
<string name="camera">Camera</string>
<string name="title">Title</string>
<string name="gallery">Gallery</string>
<string name="price">Price</string>
</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