Skip to content

Commit

Permalink
Fix markers, add delete event button click, fix edit event button (#30)
Browse files Browse the repository at this point in the history
* Fix edit event button processing

* Add edit event button click

* Add marker generation

* Add markers to events map screen

* Add delete event button click
  • Loading branch information
outboard authored Jul 23, 2022
1 parent 1148b65 commit c3d715b
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class EditEventActivity : BaseActivity(), TimePickerDialog.OnTimeSetListener, Da

lateinit var dialog: ProgressDialog

lateinit var presenter: EditEventPresenter

@Inject
lateinit var api: EndpointsService

Expand All @@ -65,6 +67,29 @@ class EditEventActivity : BaseActivity(), TimePickerDialog.OnTimeSetListener, Da
setOnClickListeners()
}

private fun showDeleteDialog() {
MaterialDialog.Builder(this)
.title(getString(R.string.delete_event))
.content(getString(R.string.are_you_sure))
.positiveText(getString(R.string.yes))
.negativeText(getString(R.string.no))
.onPositive { _, _ ->
deleteEvent()
}
.show()
}

private fun deleteEvent() {
presenter.deleteEvent(event.get("id").asInt, DeviceInfoStore.getToken(this))
}

fun onEventDeleted() {
val intent = Intent(this@EditEventActivity, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
finish()
}

private fun setOnClickListeners() {
backButton.setOnClickListener { finish() }

Expand All @@ -86,6 +111,8 @@ class EditEventActivity : BaseActivity(), TimePickerDialog.OnTimeSetListener, Da

passwordView.setOnClickListener { showPasswordDialog() }

deleteEventButton.setOnClickListener { showDeleteDialog() }

locationButton.setOnClickListener {
val intent = Intent(this@EditEventActivity, MapActivity::class.java)
intent.putExtra(EXTRA_EDIT, true)
Expand Down Expand Up @@ -201,7 +228,7 @@ class EditEventActivity : BaseActivity(), TimePickerDialog.OnTimeSetListener, Da
dialog = ProgressDialog(this)
dialog.show()

val presenter = EditEventPresenter(api, this)
presenter = EditEventPresenter(api, this)
presenter.editEvent(event.get("id").asInt, generateJson(), DeviceInfoStore.getToken(this))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package ru.binaryblitz.SportUp.activities
import android.app.ProgressDialog
import android.content.Intent
import android.content.res.ColorStateList
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.os.Handler
import android.support.v4.content.ContextCompat
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.RelativeSizeSpan
import com.afollestad.materialdialogs.MaterialDialog
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
Expand All @@ -26,6 +29,8 @@ import ru.binaryblitz.SportUp.presenters.EventPresenter
import ru.binaryblitz.SportUp.server.EndpointsService
import javax.inject.Inject
import com.google.android.gms.maps.model.MapStyleOptions
import com.squareup.picasso.Picasso
import com.squareup.picasso.Target
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import ru.binaryblitz.SportUp.server.DeviceInfoStore
Expand Down Expand Up @@ -54,9 +59,32 @@ class EventActivity : BaseActivity(), OnMapReadyCallback {
private var googleMap: GoogleMap? = null
private lateinit var presenter: EventPresenter

private var latitude: Double = 0.0
private var longitude: Double = 0.0

@Inject
lateinit var api: EndpointsService

internal var target: Target = object : Target {
override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom) {
val icon = BitmapDescriptorFactory.fromBitmap(SportTypesUtil.getMarker(sportTypeId!!, this@EventActivity, bitmap))

googleMap?.addMarker(MarkerOptions()
.position(LatLng(latitude, longitude))
.icon(icon))

moveCamera(latitude, longitude)
}

override fun onBitmapFailed(errorDrawable: Drawable?) {

}

override fun onPrepareLoad(placeHolderDrawable: Drawable?) {

}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_event)
Expand All @@ -72,12 +100,12 @@ class EventActivity : BaseActivity(), OnMapReadyCallback {
fun onEventJoined(id: Int) {
memberShipId = id
isJoined = true
initButton()
initMainButton()
}

fun onEventLeft() {
isJoined = false
initButton()
initMainButton()
}

fun onEventDeleted() {
Expand Down Expand Up @@ -120,6 +148,8 @@ class EventActivity : BaseActivity(), OnMapReadyCallback {
private fun setOnClickListeners() {
backButton.setOnClickListener { finish() }

rightButton.setOnClickListener { openEditEventActivity() }

playersButton.setOnClickListener { openPlayersActivity() }

joinButton.setOnClickListener { processJoinButton() }
Expand All @@ -134,9 +164,28 @@ class EventActivity : BaseActivity(), OnMapReadyCallback {
startActivity(intent)
}

private fun openEditEventActivity() {
val intent = Intent(this@EventActivity, EditEventActivity::class.java)
intent.putExtra(EXTRA_ID, id)
intent.putExtra(EXTRA_COLOR, color)
startActivity(intent)
}

private fun showDeleteDialog() {
MaterialDialog.Builder(this)
.title(getString(R.string.delete_event))
.content(getString(R.string.are_you_sure))
.positiveText(getString(R.string.yes))
.negativeText(getString(R.string.no))
.onPositive { _, _ ->
presenter.deleteEvent(id, DeviceInfoStore.getToken(this))
}
.show()
}

private fun processJoinButton() {
if (isCreatedByUser) {
presenter.deleteEvent(id, DeviceInfoStore.getToken(this))
showDeleteDialog()
} else if (isJoined) {
presenter.leaveEvent(id, DeviceInfoStore.getToken(this))
} else {
Expand Down Expand Up @@ -231,14 +280,10 @@ class EventActivity : BaseActivity(), OnMapReadyCallback {
this.isCreatedByUser = obj.get("creator").asJsonObject.get("id").asInt == DeviceInfoStore.getUserObject(this)?.id
this.isJoined = obj.get("membership") != null && !obj.get("membership").isJsonNull

initButton()
}

private fun initMainButton() {
initButtons()
}

private fun initButton() {
private fun initMainButton() {
try {
joinButton.backgroundTintList = if (isJoined) ColorStateList.valueOf(ContextCompat.getColor(this, R.color.redColor))
else ColorStateList.valueOf(color)
Expand Down Expand Up @@ -293,13 +338,12 @@ class EventActivity : BaseActivity(), OnMapReadyCallback {
}

fun onLoaded(latitude: Double, longitude: Double) {
val icon = BitmapDescriptorFactory.fromResource(R.drawable.icon_pins_footballmid)

googleMap?.addMarker(MarkerOptions()
.position(LatLng(latitude, longitude))
.icon(icon))
this.latitude = latitude
this.longitude = longitude

moveCamera(latitude, longitude)
Picasso.with(this)
.load(SportTypesUtil.findIcon(this, sportTypeId!!))
.into(target)
}

private fun load() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import android.Manifest
import android.annotation.SuppressLint
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.os.Handler
import android.view.View
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.BitmapDescriptorFactory
import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
import com.google.android.gms.maps.model.*
import com.squareup.picasso.Picasso
import com.squareup.picasso.Target
import kotlinx.android.synthetic.main.activity_events_map.*
import kotlinx.android.synthetic.main.dialog_password.*
import ru.binaryblitz.SportUp.R
Expand All @@ -33,13 +34,35 @@ class EventsMapActivity : LocationDependentActivity(), CustomMapFragment.Touchab
private var isDialogOpened = false
private val markers = HashMap<LatLng, Int>()

private lateinit var icon: BitmapDescriptor

@Inject
lateinit var api: EndpointsService

val EXTRA_ID = "id"
val EXTRA_COLOR = "color"
val DEFAULT_COLOR = Color.parseColor("#212121")

internal var target: Target = object : Target {
override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom) {
if (SportEventsActivity.eventsCollection.size == 0) {
return
}

icon = BitmapDescriptorFactory.fromBitmap(
SportTypesUtil.getMarker(SportEventsActivity.eventsCollection[0].sportTypeId, this@EventsMapActivity, bitmap))
onLoaded()
}

override fun onBitmapFailed(errorDrawable: Drawable?) {

}

override fun onPrepareLoad(placeHolderDrawable: Drawable?) {

}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_events_map)
Expand All @@ -49,6 +72,14 @@ class EventsMapActivity : LocationDependentActivity(), CustomMapFragment.Touchab
initGoogleApiClient()
initMap()
setOnClickListeners()

if (SportEventsActivity.eventsCollection.size == 0) {
return
}

Picasso.with(this)
.load(SportTypesUtil.findIcon(this, SportEventsActivity.eventsCollection[0].sportTypeId))
.into(target)
}

private fun openEvent(eventId: Int) {
Expand Down Expand Up @@ -169,8 +200,6 @@ class EventsMapActivity : LocationDependentActivity(), CustomMapFragment.Touchab
}

fun onLoaded() {
val icon = BitmapDescriptorFactory.fromResource(R.drawable.icon_pins_footballmid)

for (event in SportEventsActivity.eventsCollection) {
markers.put(LatLng (event.latitude, event.longitude), event.id)
googleMap?.addMarker(MarkerOptions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ class EditEventPresenter(private val service: EndpointsService, private val view
})
}

fun deleteEvent(id: Int, token: String) {
service.deleteEvent(id, token, object : JsonObjectResponseListener {
override fun onSuccess(obj: JsonObject) {
onEventDeleted()
}

override fun onError(networkError: String) {
onEventDeleted()
}
})
}

private fun onEventDeleted() {
view.onEventDeleted()
}

private fun parseAnswer() {
view.onLoaded()
}
Expand Down
39 changes: 37 additions & 2 deletions app/src/main/java/ru/binaryblitz/SportUp/utils/SportTypesUtil.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package ru.binaryblitz.SportUp.utils

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.support.v4.content.ContextCompat
import android.support.v4.util.Pair
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import ru.binaryblitz.SportUp.R
import ru.binaryblitz.SportUp.models.SportType
import ru.binaryblitz.SportUp.server.ServerConfig
import java.util.ArrayList
import kotlin.collections.HashSet
import kotlin.collections.map
import kotlin.collections.mapTo

object SportTypesUtil {
val types = ArrayList<Pair<Int, String>>()
Expand All @@ -20,6 +24,37 @@ object SportTypesUtil {
sportTypes.add(type)
}

fun getMarker(id: Int, context: Context, icon: Bitmap): Bitmap {
val view = LayoutInflater.from(context).inflate(R.layout.default_marker, null)
view.isDrawingCacheEnabled = true

(view.findViewById(R.id.markerBackground) as ImageView).setColorFilter(findColor(context, id))
(view.findViewById(R.id.sportTypeIcon) as ImageView).setColorFilter(Color.parseColor("#ffffff"))
(view.findViewById(R.id.sportTypeIcon) as ImageView).setImageBitmap(icon)

view.buildDrawingCache()

return getViewBitmap(view, context)
}

fun getViewBitmap(view: View, context: Context): Bitmap {
val width = AndroidUtilities.dpToPx(context, 56f)
val height = AndroidUtilities.dpToPx(context, 56f)

val measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY)
val measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY)

view.measure(measuredWidth, measuredHeight)
view.layout(0, 0, view.measuredWidth, view.measuredHeight)

val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)

view.draw(canvas)

return bitmap
}

fun saveTypes(context: Context?) {
if (context == null) {
return
Expand Down
Binary file added app/src/main/res/drawable/ic_default.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/ic_default_marker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_edit_event.xml
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@
android:background="#f7f9fa"
android:layout_height="100dp">
<Button
android:id="@+id/joinBtn"
android:id="@+id/deleteEventButton"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="170dp"
android:layout_height="48dp"
Expand Down
Loading

0 comments on commit c3d715b

Please sign in to comment.