forked from hammerheadnav/karoo-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RideCard.kt
81 lines (73 loc) · 2.93 KB
/
RideCard.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* Copyright (c) 2021 Hammerhead Navigation Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.hammerhead.samplemodule.kotlin
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.garmin.fit.Mesg
import com.garmin.fit.RecordMesg
import io.hammerhead.sample.R
import io.hammerhead.samplemodule.kotlin.appval.AppValDataType
import io.hammerhead.samplemodule.kotlin.appval.AppValFormatter
import io.hammerhead.sdk.v0.SdkContext
import io.hammerhead.sdk.v0.card.FitFileListener
import io.hammerhead.sdk.v0.card.PostRideCard
import io.hammerhead.sdk.v0.card.RideDetailsI
import io.hammerhead.sdk.v0.card.RideDetailsI.Companion.STRAVA
import kotlinx.android.synthetic.main.ride_card_view.view.*
import timber.log.Timber
class RideCard(context: SdkContext, rideDetails: RideDetailsI) : PostRideCard(context, rideDetails), FitFileListener {
private val elevationPoints = mutableListOf<Double>()
private var elevationPointsReady = false
private var view: View? = null
init {
rideDetailsI.registerFitFileListener(this)
}
override fun createView(layoutInflater: LayoutInflater, parent: ViewGroup): View {
val view = layoutInflater.inflate(R.layout.ride_card_view, parent, false)
rideDetailsI.summaryFields[AppValDataType.TYPE_ID]?.let {
view.finalAppVal.text = AppValFormatter().formatValue(it)
}
view.rideName.text = rideDetailsI.name
view.setOnClickListener {
val detail = Intent().setClassName("io.hammerhead.sample", "io.hammerhead.sample.DetailActivity")
context.startActivity(detail)
}
Timber.d("Strava external ID is ${rideDetailsI.externalIds[STRAVA]}")
if (elevationPointsReady) {
view.elevationBackground.setPoints(elevationPoints)
}
this.view = view
return view
}
override fun onMesg(mesg: Mesg) {
if (mesg.num == 20) {
val record = RecordMesg(mesg)
if (record.hasField(RecordMesg.AltitudeFieldNum)) {
elevationPoints.add(record.getField(RecordMesg.AltitudeFieldNum).doubleValue)
}
}
}
override fun onStartDecoding() {
elevationPoints.clear()
elevationPointsReady = false
}
override fun onEndDecoding() {
view?.elevationBackground?.setPoints(elevationPoints)
elevationPointsReady = true
}
}