Skip to content

Commit

Permalink
maly-poszukiwacz-skarbow-27 bugfix for photo button
Browse files Browse the repository at this point in the history
  • Loading branch information
mjureczko committed Jan 29, 2024
1 parent a09d9d6 commit b88fd82
Show file tree
Hide file tree
Showing 36 changed files with 609 additions and 231 deletions.
10 changes: 7 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@

<activity
android:name=".activity.result.ResultActivity"
android:exported="false" />
android:exported="false"
android:theme="@style/FullscreenTheme"
/>
<activity
android:name=".activity.map.MapActivity"
android:exported="false"
Expand All @@ -67,10 +69,12 @@
<activity android:name=".activity.bluetooth.BluetoothActivity" />
<activity
android:name=".activity.photo.PhotoActivity"
android:label="@string/photo_tip" />
android:label="@string/photo_tip"
android:theme="@style/FullscreenTheme"
/>
<activity
android:name=".activity.treasureseditor.TreasuresEditorActivity"
android:theme="@style/FullscreenTheme" />
android:theme="@style/AppTheme" />
<activity
android:name=".activity.main.MainActivity"
android:exported="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import pl.marianjureczko.poszukiwacz.App
import pl.marianjureczko.poszukiwacz.R
import pl.marianjureczko.poszukiwacz.activity.facebook.FacebookInputData
import pl.marianjureczko.poszukiwacz.activity.main.Bluetooth
import pl.marianjureczko.poszukiwacz.activity.main.BluetoothException
import pl.marianjureczko.poszukiwacz.model.Route
Expand Down Expand Up @@ -101,7 +102,7 @@ class BluetoothActivity : PermissionActivity(), MemoConsole, BluetoothConnection
setUpAds(findViewById(R.id.adView))
}

override fun getCurrentTreasuresProgress(): TreasuresProgress? {
override fun getTreasureProgress(): TreasuresProgress? {
return null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
import pl.marianjureczko.poszukiwacz.R
import pl.marianjureczko.poszukiwacz.activity.facebook.FacebookInputData
import pl.marianjureczko.poszukiwacz.databinding.ActivityCommemorativeBinding
import pl.marianjureczko.poszukiwacz.model.TreasuresProgress
import pl.marianjureczko.poszukiwacz.shared.ActivityWithAdsAndBackButton
Expand Down Expand Up @@ -58,7 +59,7 @@ class CommemorativeActivity : ActivityWithAdsAndBackButton() {
setUpAds(binding.adView)
}

override fun getCurrentTreasuresProgress(): TreasuresProgress? = model.progress
override fun getTreasureProgress(): TreasuresProgress? = model.progress

private fun rotatePhoto(photoFullPath: String, uri: Uri) {
lifecycleScope.launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import androidx.recyclerview.widget.LinearLayoutManager
import com.facebook.CallbackManager
import com.facebook.FacebookCallback
import com.facebook.FacebookException
import com.facebook.FacebookSdk
import com.facebook.share.Sharer
import com.facebook.share.model.SharePhoto
import com.facebook.share.model.SharePhotoContent
Expand All @@ -36,7 +35,7 @@ class FacebookActivity : PermissionActivity() {
private val model: FacebookViewModel by viewModels()

companion object {
const val TREASURE_PROGRESS = "pl.marianjureczko.poszukiwacz.activity.facebook_treasure_progress"
const val INPUT = "pl.marianjureczko.poszukiwacz.activity.input"
const val STORAGE_DO_NOT_REQUIRE_PERMISSONS = Build.VERSION_CODES.Q
private val xmlHelper = XmlHelper()
}
Expand All @@ -48,11 +47,8 @@ class FacebookActivity : PermissionActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

model.initialize(
progress = xmlHelper.loadFromString(intent.getStringExtra(TREASURE_PROGRESS)!!),
this
)
val input = intent.getSerializableExtra(INPUT) as FacebookInputData

Check warning on line 50 in app/src/main/java/pl/marianjureczko/poszukiwacz/activity/facebook/FacebookActivity.kt

View workflow job for this annotation

GitHub Actions / build

'getSerializableExtra(String!): Serializable?' is deprecated. Deprecated in Java
model.initialize(this, hunterPath = input.hunterPath, progress = input.progress)
callbackManager = CallbackManager.Factory.create()
shareDialog = ShareDialog(this)

Expand Down Expand Up @@ -120,5 +116,5 @@ class FacebookActivity : PermissionActivity() {
}
}

override fun getCurrentTreasuresProgress(): TreasuresProgress? = null
override fun getTreasureProgress(): TreasuresProgress? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.activity.result.contract.ActivityResultContract
import pl.marianjureczko.poszukiwacz.model.HunterPath
import pl.marianjureczko.poszukiwacz.model.TreasuresProgress
import pl.marianjureczko.poszukiwacz.shared.XmlHelper
import java.io.Serializable

data class FacebookInputData(
val hunterPath: HunterPath?,
val progress: TreasuresProgress
) : Serializable

Expand All @@ -18,13 +20,9 @@ data class FacebookOutputData(

class FacebookContract : ActivityResultContract<FacebookInputData, FacebookOutputData?>() {

companion object {
private val xmlHelper = XmlHelper()
}

override fun createIntent(context: Context, input: FacebookInputData): Intent {
return Intent(context, FacebookActivity::class.java).apply {
putExtra(FacebookActivity.TREASURE_PROGRESS, xmlHelper.writeToString(input.progress))
putExtra(FacebookActivity.INPUT, input)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import pl.marianjureczko.poszukiwacz.R
import pl.marianjureczko.poszukiwacz.model.HunterPath
import pl.marianjureczko.poszukiwacz.model.Route
import pl.marianjureczko.poszukiwacz.model.TreasuresProgress
import pl.marianjureczko.poszukiwacz.shared.StorageHelper
Expand All @@ -12,13 +13,16 @@ class FacebookViewModel(private val state: SavedStateHandle) : ViewModel() {
private val TAG = javaClass.simpleName
lateinit var progress: TreasuresProgress
private set
var hunterPath: HunterPath? = null
private set
lateinit var elements: List<ElementDescription>
private set

lateinit var route: Route

fun initialize(progress: TreasuresProgress, context: Context) {
fun initialize(context: Context, hunterPath: HunterPath?, progress: TreasuresProgress) {
this.progress = progress
this.hunterPath = hunterPath
val elements = mutableListOf<ElementDescription>()
elements.add(ElementDescription(Type.TREASURES_SUMMARY, true, context.getString(R.string.collected_treasures)))
val treasure = context.getString(R.string.treasure)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,22 @@ class ReportMap(
}

private fun drawRoute(snapshot: MapSnapshotInterface, mapCanvas: Canvas) {
val locations = model.progress.hunterPath.pathAsCoordinates().toList()
if (locations.size > 1) {
var previousXY = snapshot.screenCoordinate(Point.fromLngLat(locations[0].longitude, locations[0].latitude))
locations.asSequence()
.drop(1)
.forEach {
val xy = snapshot.screenCoordinate(Point.fromLngLat(it.longitude, it.latitude))
mapCanvas.drawLine(
previousXY.x.toFloat(), previousXY.y.toFloat(), xy.x.toFloat(), xy.y.toFloat(),
Paint().apply { color = Color.RED }
)
previousXY = xy
}
model.hunterPath?.let {
val locations = it.pathAsCoordinates().toList()
if (locations.size > 1) {
var previousXY =
snapshot.screenCoordinate(Point.fromLngLat(locations[0].longitude, locations[0].latitude))
locations.asSequence()
.drop(1)
.forEach {
val xy = snapshot.screenCoordinate(Point.fromLngLat(it.longitude, it.latitude))
mapCanvas.drawLine(
previousXY.x.toFloat(), previousXY.y.toFloat(), xy.x.toFloat(), xy.y.toFloat(),
Paint().apply { color = Color.RED }
)
previousXY = xy
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class ReportMapSummary(
val textPaint = ReportCommons.getTextPaint(font, Paint.Align.LEFT)
var textY = currentTop + 50
var x = ReportCommons.REPORT_MARGIN
canvas.drawText(distanceText(context, model.progress.hunterPath), x, textY, textPaint)
canvas.drawText(distanceText(context, model.hunterPath), x, textY, textPaint)
textY += 40
canvas.drawText(timeText(context, model.progress.hunterPath), x, textY, textPaint)
canvas.drawText(timeText(context, model.hunterPath), x, textY, textPaint)
}
}

Expand All @@ -42,17 +42,18 @@ class ReportMapSummary(
context.getResources().getConfiguration().locale

Check warning on line 42 in app/src/main/java/pl/marianjureczko/poszukiwacz/activity/facebook/ReportMapSummary.kt

View workflow job for this annotation

GitHub Actions / build

'locale: Locale!' is deprecated. Deprecated in Java
}

private fun distanceText(context: Context, hunterPath: HunterPath): String {
val formattedDistance = "%.2f".format(hunterPath.pathLengthInKm())
private fun distanceText(context: Context, hunterPath: HunterPath?): String {
val distance = hunterPath?.pathLengthInKm() ?: 0.0
val formattedDistance = "%.2f".format(distance)
return "${context.getString(R.string.walked_route)} $formattedDistance km."
}

private fun timeText(context: Context, hunterPath: HunterPath): String {
private fun timeText(context: Context, hunterPath: HunterPath?): String {
val loc = Locale("en", "US")
val timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, loc)
val dateFormat: DateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, loc)

hunterPath.getStartTime()?.let { start ->
hunterPath?.getStartTime()?.let { start ->
val startDate = dateFormat.format(start)
val startTime = timeFormat.format(start)
hunterPath.getEndTime()?.let { end ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import pl.marianjureczko.poszukiwacz.shared.StorageHelper
class MainActivity : PermissionActivity() {

private val TAG = javaClass.simpleName
override fun getCurrentTreasuresProgress(): TreasuresProgress? {
override fun getTreasureProgress(): TreasuresProgress? {
return null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class MapActivity : ActivityWithAdsAndBackButton() {
setUpAds(binding.adView)
}

override fun getCurrentTreasuresProgress(): TreasuresProgress? = model.progress
override fun getTreasureProgress(): TreasuresProgress? = model.progress

override fun onResume() {
super.onResume()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class PhotoActivity : ActivityWithAdsAndBackButton() {
setUpAds(binding.adView)
}

override fun getCurrentTreasuresProgress(): TreasuresProgress =
override fun getTreasureProgress(): TreasuresProgress =
model.progress

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,49 @@ package pl.marianjureczko.poszukiwacz.activity.result

import android.app.Activity
import android.content.pm.ActivityInfo
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.core.view.isVisible
import pl.marianjureczko.poszukiwacz.R
import pl.marianjureczko.poszukiwacz.activity.commemorative.CommemorativeContract
import pl.marianjureczko.poszukiwacz.activity.commemorative.CommemorativeInputData
import pl.marianjureczko.poszukiwacz.databinding.ActivityResultBinding
import pl.marianjureczko.poszukiwacz.model.TreasuresProgress
import pl.marianjureczko.poszukiwacz.shared.ActivityWithAdsAndBackButton
import pl.marianjureczko.poszukiwacz.shared.PhotoHelper
import pl.marianjureczko.poszukiwacz.shared.StorageHelper

class ResultActivity : ActivityWithAdsAndBackButton() {

companion object {
const val RESULT = "pl.marianjureczko.poszukiwacz.activity.result";
const val RESULT_IN = "pl.marianjureczko.poszukiwacz.activity.result_in"
const val RESULT_OUT = "pl.marianjureczko.poszukiwacz.activity.result_out"
}

private val model: ResultActivityViewModel by viewModels()
private lateinit var binding: ActivityResultBinding
private val storageHelper = StorageHelper(this)
private val photoHelper = PhotoHelper(this, storageHelper)

private val showCommemorativeLauncher: ActivityResultLauncher<CommemorativeInputData> =
registerForActivityResult(CommemorativeContract()) {}

//TODO t: copied from CommemorativeActivity
private val doPhotoLauncher: ActivityResultLauncher<Uri> =
registerForActivityResult(ActivityResultContracts.TakePicture()) { result ->
if (result) {
model.addCommemorativePhoto(storageHelper, photoHelper.moveCommemorativePhotoToPermanentLocation())
configureDoPhotoButton()
Toast.makeText(this, R.string.photo_saved, Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, R.string.photo_not_replaced, Toast.LENGTH_SHORT).show()
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -24,24 +53,51 @@ class ResultActivity : ActivityWithAdsAndBackButton() {
binding = ActivityResultBinding.inflate(layoutInflater)

requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
val input = intent.getSerializableExtra(RESULT) as ResultActivityData
if (input.isError()) {
binding.resultDescription.text = input.error
val input = intent.getSerializableExtra(RESULT_IN) as ResultActivityInput
model.initialize(this, storageHelper, input.treasure, input.progress, input.treasureDescription)

configureView()
setContentView(binding.root)
setUpAds(binding.adView)
}

private fun configureView() {
if (model.isError()) {
binding.resultDescription.text = model.errorMsg
binding.resultImg.visibility = View.GONE
} else {
binding.resultDescription.text = input.treasure!!.quantity.toString()
binding.resultImg.setImageResource(input.treasure.type.image())
binding.resultDescription.text = model.treasure!!.quantity.toString()
binding.resultImg.setImageResource(model.treasure!!.type.image())
}

if (model.canMakeCommemorativePhoto()) {
configureDoPhotoButton()
} else {
binding.buttonsLayout.isVisible = false
}
}

private fun configureDoPhotoButton() {
binding.buttonsLayout.isVisible = true
if (model.currentTreasureHasCommemorativePhoto()) {
binding.doPhoto.setImageResource(R.drawable.camera_show_photo)
binding.doPhoto.setOnClickListener {
model.commemorativeInputData()?.let { showCommemorativeLauncher.launch(it) }
}
} else {
binding.doPhoto.setImageResource(R.drawable.camera_do_photo)
binding.doPhoto.setOnClickListener {
doPhotoLauncher.launch(photoHelper.createCommemorativePhotoTempUri())
}
}
setContentView(binding.root)
setUpAds(binding.adView)
}

override fun getCurrentTreasuresProgress(): TreasuresProgress? {
override fun getTreasureProgress(): TreasuresProgress? {
return null
}

override fun onBackPressed() {
setResult(Activity.RESULT_OK, intent)
setResult(Activity.RESULT_OK, model.activityResult())
super.onBackPressed()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import android.content.Context
import android.content.Intent
import androidx.activity.result.contract.ActivityResultContract

class ResultActivityContract : ActivityResultContract<ResultActivityData, ResultActivityData?>() {
override fun createIntent(context: Context, input: ResultActivityData?): Intent {
class ResultActivityContract : ActivityResultContract<ResultActivityInput, ResultActivityOutput>() {
override fun createIntent(context: Context, input: ResultActivityInput?): Intent {
return Intent(context, ResultActivity::class.java).apply {
putExtra(ResultActivity.RESULT, input)
putExtra(ResultActivity.RESULT_IN, input)
}
}

override fun parseResult(resultCode: Int, intent: Intent?): ResultActivityData? {
return intent?.getSerializableExtra(ResultActivity.RESULT) as ResultActivityData?
override fun parseResult(resultCode: Int, intent: Intent?): ResultActivityOutput {
return intent?.getSerializableExtra(ResultActivity.RESULT_OUT) as ResultActivityOutput
}
}

This file was deleted.

Loading

0 comments on commit b88fd82

Please sign in to comment.