Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #47 from brightec/task/remove-synthetics
Browse files Browse the repository at this point in the history
Task/remove synthetics
  • Loading branch information
alistairsykes authored Jul 12, 2021
2 parents 1b33a40 + 193e84a commit d16454f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 37 deletions.
5 changes: 4 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
apply plugin: "com.android.application"
apply plugin: "kotlin-android"
apply plugin: "kotlin-android-extensions"
apply plugin: "kotlin-allopen"
apply plugin: "com.google.gms.google-services"
apply from: "$project.rootDir/config/quality/quality.gradle"
Expand Down Expand Up @@ -34,6 +33,10 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildFeatures {
viewBinding true
}

lintOptions {
lintConfig file("$project.rootDir/config/quality/lint.xml")
}
Expand Down
18 changes: 11 additions & 7 deletions app/src/main/java/uk/co/brightec/kbarcode/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,37 @@ package uk.co.brightec.kbarcode.app

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import uk.co.brightec.kbarcode.app.camerax.CameraXActivity
import uk.co.brightec.kbarcode.app.databinding.ActivityMainBinding
import uk.co.brightec.kbarcode.app.viewfinder.ViewfinderActivity

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)

button_xml.setOnClickListener {
binding.buttonXml.setOnClickListener {
val intent = XmlActivity.getStartingIntent(this)
startActivity(intent)
}
button_xml_java.setOnClickListener {
binding.buttonXmlJava.setOnClickListener {
val intent = XmlJavaActivity.getStartingIntent(this)
startActivity(intent)
}
button_programmatic.setOnClickListener {
binding.buttonProgrammatic.setOnClickListener {
val intent = ProgrammaticActivity.getStartingIntent(this)
startActivity(intent)
}
button_viewfinder.setOnClickListener {
binding.buttonViewfinder.setOnClickListener {
val intent = ViewfinderActivity.getStartingIntent(this)
startActivity(intent)
}
button_camerax.setOnClickListener {
binding.buttonCamerax.setOnClickListener {
val intent = CameraXActivity.getStartingIntent(this)
startActivity(intent)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_programmatic.*
import uk.co.brightec.kbarcode.Barcode
import uk.co.brightec.kbarcode.BarcodeView
import uk.co.brightec.kbarcode.Options
import uk.co.brightec.kbarcode.app.databinding.ActivityProgrammaticBinding

internal class ProgrammaticActivity :
AppCompatActivity(), ActivityCompat.OnRequestPermissionsResultCallback {

private lateinit var binding: ActivityProgrammaticBinding
private lateinit var barcodeView: BarcodeView

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_programmatic)
binding = ActivityProgrammaticBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
window.decorView.setBackgroundColor(ContextCompat.getColor(this, R.color.black))
setTitle(R.string.title_programmatic)

Expand All @@ -44,7 +47,7 @@ internal class ProgrammaticActivity :
.clearFocusDelay(BarcodeView.CLEAR_FOCUS_DELAY_DEFAULT)
.build()
)
frame_container.addView(barcodeView)
binding.frameContainer.addView(barcodeView)

lifecycle.addObserver(barcodeView)

Expand All @@ -53,7 +56,7 @@ internal class ProgrammaticActivity :
for (barcode in barcodes) {
builder.append(barcode.displayValue).append("\n")
}
text_barcodes.text = builder.toString()
binding.textBarcodes.text = builder.toString()
}

if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
Expand Down
16 changes: 10 additions & 6 deletions app/src/main/java/uk/co/brightec/kbarcode/app/XmlActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,29 @@ import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_xml.*
import uk.co.brightec.kbarcode.app.databinding.ActivityXmlBinding

internal class XmlActivity :
AppCompatActivity(), ActivityCompat.OnRequestPermissionsResultCallback {

private lateinit var binding: ActivityXmlBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_xml)
binding = ActivityXmlBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
window.decorView.setBackgroundColor(ContextCompat.getColor(this, R.color.black))
setTitle(R.string.title_xml)

lifecycle.addObserver(view_barcode)
lifecycle.addObserver(binding.viewBarcode)

view_barcode.barcodes.observe(this) { barcodes ->
binding.viewBarcode.barcodes.observe(this) { barcodes ->
val builder = StringBuilder()
for (barcode in barcodes) {
builder.append(barcode.displayValue).append("\n")
}
text_barcodes.text = builder.toString()
binding.textBarcodes.text = builder.toString()
}

if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
Expand All @@ -47,7 +51,7 @@ internal class XmlActivity :
REQUEST_PERMISSION_CAMERA -> if (
grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED
) {
view_barcode.start()
binding.viewBarcode.start()
}
else ->
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ import androidx.camera.core.Preview
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_camerax.*
import kotlinx.android.synthetic.main.activity_camerax.text_barcodes
import kotlinx.android.synthetic.main.activity_programmatic.*
import uk.co.brightec.kbarcode.app.R
import uk.co.brightec.kbarcode.app.databinding.ActivityCameraxBinding
import java.util.concurrent.ExecutionException
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
Expand All @@ -32,12 +30,15 @@ internal class CameraXActivity :

private val tag = CameraXActivity::class.simpleName ?: "CameraXActivity"

private lateinit var binding: ActivityCameraxBinding
private lateinit var cameraExecutor: ExecutorService
private lateinit var camera: Camera

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_camerax)
binding = ActivityCameraxBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
setTitle(R.string.title_camerax)

if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
Expand Down Expand Up @@ -105,7 +106,7 @@ internal class CameraXActivity :
val previewUseCase = Preview.Builder()
.build()
.also {
it.setSurfaceProvider(preview.surfaceProvider)
it.setSurfaceProvider(binding.preview.surfaceProvider)
}
val imageAnalyzerUseCase = ImageAnalysis.Builder()
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
Expand All @@ -118,7 +119,7 @@ internal class CameraXActivity :
for (barcode in barcodes) {
builder.append(barcode.displayValue).append("\n")
}
text_barcodes.text = builder.toString()
binding.textBarcodes.text = builder.toString()
}
)
}
Expand All @@ -135,7 +136,7 @@ internal class CameraXActivity :
}

private fun setupTapToFocus() {
preview.setOnTouchListener { view, event ->
binding.preview.setOnTouchListener { view, event ->
val actionMasked = event.actionMasked
if (actionMasked == MotionEvent.ACTION_UP) {
view.performClick()
Expand All @@ -146,7 +147,7 @@ internal class CameraXActivity :
}

val cameraControl = camera.cameraControl
val factory = preview.meteringPointFactory
val factory = binding.preview.meteringPointFactory
val point = factory.createPoint(event.x, event.y)
val action = FocusMeteringAction.Builder(point, FocusMeteringAction.FLAG_AF)
.addPoint(point, FocusMeteringAction.FLAG_AE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import kotlinx.android.synthetic.main.fragment_viewfinder.*
import uk.co.brightec.kbarcode.app.R
import uk.co.brightec.kbarcode.app.databinding.FragmentViewfinderBinding

internal class ViewfinderFragment : Fragment() {

private var _binding: FragmentViewfinderBinding? = null

// This property is only valid between onCreateView and onDestroyView.
private val binding get() = _binding!!

@VisibleForTesting
lateinit var viewModel: ViewfinderViewModel

Expand All @@ -35,35 +40,38 @@ internal class ViewfinderFragment : Fragment() {
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? = inflater.inflate(R.layout.fragment_viewfinder, container, false)
): View {
_binding = FragmentViewfinderBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
requireActivity().setTitle(R.string.title_viewfinder)

lifecycle.addObserver(view_barcode)
lifecycle.addObserver(binding.viewBarcode)

viewModel.setData(view_barcode.barcode)
viewModel.setData(binding.viewBarcode.barcode)
viewModel.barcode.observe(viewLifecycleOwner) { resource ->
when (resource) {
is Resource.Success -> {
progress.visibility = View.GONE
binding.progress.visibility = View.GONE
val barcode = resource.data
Toast.makeText(
requireContext(), barcode.displayValue, Toast.LENGTH_SHORT
).show()
view_barcode.resume()
binding.viewBarcode.resume()
}
is Resource.Error -> {
progress.visibility = View.GONE
binding.progress.visibility = View.GONE
Toast.makeText(
requireContext(),
R.string.error_processing_barcode, Toast.LENGTH_SHORT
).show()
view_barcode.resume()
binding.viewBarcode.resume()
}
is Resource.Loading -> {
view_barcode.pause()
progress.visibility = View.VISIBLE
binding.viewBarcode.pause()
binding.progress.visibility = View.VISIBLE
}
}
}
Expand All @@ -75,6 +83,11 @@ internal class ViewfinderFragment : Fragment() {
}
}

override fun onDestroyView() {
_binding = null
super.onDestroyView()
}

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
Expand All @@ -84,7 +97,7 @@ internal class ViewfinderFragment : Fragment() {
REQUEST_PERMISSION_CAMERA -> if (
grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED
) {
view_barcode.start()
binding.viewBarcode.start()
}
else ->
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
Expand Down

0 comments on commit d16454f

Please sign in to comment.