Skip to content

Commit

Permalink
Merge pull request #30 from mjureczko/maly-poszukiwacz-skarbow-27
Browse files Browse the repository at this point in the history
Maly poszukiwacz skarbow 27
  • Loading branch information
mjureczko authored Jan 30, 2024
2 parents a09d9d6 + a34324e commit 2737ab8
Show file tree
Hide file tree
Showing 41 changed files with 631 additions and 260 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal class ReportCommemorativePhotosTest : ReportAbstractTest() {
treasuresProgress.commemorativePhotosByTreasuresDescriptionIds.put(2, tempPhoto(600, 600))

StorageHelper(context).save(Route(treasuresProgress.routeName))
model.initialize(treasuresProgress, context)
model.initialize(context, null, treasuresProgress)
val reportPhotos = ReportCommemorativePhotos(model, font, seed)

//when
Expand All @@ -59,7 +59,7 @@ internal class ReportCommemorativePhotosTest : ReportAbstractTest() {
treasuresProgress.commemorativePhotosByTreasuresDescriptionIds.put(2, tempPhoto(600, 600))
treasuresProgress.commemorativePhotosByTreasuresDescriptionIds.put(3, tempPhoto(400, 800))
treasuresProgress.commemorativePhotosByTreasuresDescriptionIds.put(4, tempPhoto(600, 500))
model.initialize(treasuresProgress, context)
model.initialize(context, null, treasuresProgress)
val reportPhotos = ReportCommemorativePhotos(model, font, seed)

//when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Test
import org.junit.runner.RunWith
import pl.marianjureczko.poszukiwacz.activity.treasureselector.Coordinates
import pl.marianjureczko.poszukiwacz.model.HunterPath
import pl.marianjureczko.poszukiwacz.model.Route
import pl.marianjureczko.poszukiwacz.model.Treasure
import pl.marianjureczko.poszukiwacz.model.TreasureType
Expand All @@ -33,14 +34,15 @@ class ReportGeneratorTest {
treasuresProgress.commemorativePhotosByTreasuresDescriptionIds.put(3, photos[2])
treasuresProgress.commemorativePhotosByTreasuresDescriptionIds.put(13, photos[4])
treasuresProgress.commemorativePhotosByTreasuresDescriptionIds.put(0, photos[5])
treasuresProgress.hunterPath.addLocation(Coordinates(10.0, 10.0), Date(1))
treasuresProgress.hunterPath.addLocation(Coordinates(10.0, 11.0), Date(1_000_000))
treasuresProgress.hunterPath.addLocation(Coordinates(10.0, 11.0), Date(2_000_000))
val hunterPath = HunterPath()
hunterPath.addLocation(Coordinates(10.0, 10.0), Date(1))
hunterPath.addLocation(Coordinates(10.0, 11.0), Date(1_000_000))
hunterPath.addLocation(Coordinates(10.0, 11.0), Date(2_000_000))
StorageHelper(context).save(Route(treasuresProgress.routeName))

//when
val model = FacebookViewModel(SavedStateHandle(mapOf()))
model.initialize(treasuresProgress, context)
model.initialize(context, hunterPath, treasuresProgress)
//MapBox doesn't work in tests
model.getMap()?.isSelected = false
val actual = report.create(context, model) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ReportMapHeaderTest : ReportAbstractTest() {
val bitmap = Bitmap.createBitmap(ReportCommons.REPORT_WIDTH, 200, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
canvas.drawColor(Color.WHITE)
model.initialize(treasuresProgress, context)
model.initialize(context, null, treasuresProgress)

//when
reportMapHeader.draw(context, canvas, 0f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import junit.framework.TestCase
import org.junit.Test
import org.junit.runner.RunWith
import pl.marianjureczko.poszukiwacz.activity.treasureselector.Coordinates
import pl.marianjureczko.poszukiwacz.model.HunterPath
import java.io.File
import java.util.Date

Expand All @@ -22,10 +23,11 @@ class ReportMapSummaryTest : ReportAbstractTest() {
val bitmap = Bitmap.createBitmap(ReportCommons.REPORT_WIDTH, 200, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
canvas.drawColor(Color.WHITE)
treasuresProgress.hunterPath.addLocation(Coordinates(10.0, 10.0), Date(1))
treasuresProgress.hunterPath.addLocation(Coordinates(10.0, 11.0), Date(1_000_000))
treasuresProgress.hunterPath.addLocation(Coordinates(10.0, 11.0), Date(2_000_000))
model.initialize(treasuresProgress, context)
val hunterPath = HunterPath()
hunterPath.addLocation(Coordinates(10.0, 10.0), Date(1))
hunterPath.addLocation(Coordinates(10.0, 11.0), Date(1_000_000))
hunterPath.addLocation(Coordinates(10.0, 11.0), Date(2_000_000))
model.initialize(context, hunterPath, treasuresProgress)

//when
reportMapHeader.draw(context, canvas, 0f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal class ReportSummaryTest : ReportAbstractTest() {

val treasure = Treasure("1", 17, TreasureType.GOLD)
treasuresProgress.collect(treasure)
model.initialize(treasuresProgress, context)
model.initialize(context, null, treasuresProgress)

//when
reportSummary.draw(context, canvas, 0f)
Expand Down
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 All @@ -26,7 +27,8 @@ class CommemorativeActivity : ActivityWithAdsAndBackButton() {
private val model: CommemorativeViewModel by viewModels()
private val storageHelper = StorageHelper(this)
private val photoHelper = PhotoHelper(this, storageHelper)
private val doPhotoLauncher: ActivityResultLauncher<Uri> = registerForActivityResult(ActivityResultContracts.TakePicture()) { result ->
private val doPhotoLauncher: ActivityResultLauncher<Uri> =
registerForActivityResult(ActivityResultContracts.TakePicture()) { result ->
if (result) {
photoHelper.moveCommemorativePhotoToPermanentLocation(target = model.commemorativePhotoAbsolutePath)
reloadImage(model.commemorativePhotoUri())
Expand Down Expand Up @@ -58,7 +60,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

}
Loading

0 comments on commit 2737ab8

Please sign in to comment.