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

Fixed build issue - update dependencies - SDK 33 #37

Open
wants to merge 1 commit into
base: codelab-start
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ apply plugin: 'kotlin-android'

apply plugin: 'kotlin-kapt'

apply plugin: 'kotlin-android-extensions'

def firebaseEnabled = false

Expand All @@ -30,12 +29,12 @@ if (firebaseEnabled) {
}

android {
compileSdkVersion 31
compileSdkVersion 33

defaultConfig {
applicationId "PUT_YOUR_APPLICATION_ID_HERE"
minSdkVersion 21
targetSdkVersion 31
targetSdkVersion 33
versionCode 2
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand All @@ -55,6 +54,10 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

buildFeatures {
viewBinding true
}
}

dependencies {
Expand All @@ -64,26 +67,26 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

// Jetpack
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.core:core-ktx:1.9.0'

// Material components
implementation 'com.google.android.material:material:1.4.0'
implementation 'com.google.android.material:material:1.6.0'

// Firebase
implementation 'com.google.firebase:firebase-core:19.0.1'
implementation 'com.google.firebase:firebase-core:21.1.1'
implementation 'com.google.firebase:firebase-appindexing:20.0.0'

// Room DB
def room_version = "2.3.0"
def room_version = "2.4.3"
implementation "androidx.room:room-runtime:$room_version"
implementation "androidx.room:room-ktx:$room_version"
kapt "androidx.room:room-compiler:$room_version"

// Test dependencies
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test:runner:1.5.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
}
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<!-- Needed permission to start the FitTrackingService in foreground mode -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<application
android:name="com.devrel.android.fitactions.FitApp"
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*
*/
package com.devrel.android.fitactions

import android.app.assist.AssistContent
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
Expand All @@ -26,7 +26,6 @@ import com.devrel.android.fitactions.home.FitStatsFragment
import com.devrel.android.fitactions.model.FitActivity
import com.devrel.android.fitactions.model.FitRepository
import com.devrel.android.fitactions.tracking.FitTrackingFragment
import com.devrel.android.fitactions.tracking.FitTrackingService
import com.google.firebase.appindexing.Action
import com.google.firebase.appindexing.FirebaseUserActions
import com.google.firebase.appindexing.builders.AssistActionBuilder
Expand All @@ -43,7 +42,7 @@ class FitMainActivity :
setContentView(R.layout.fit_activity)

// Handle the intent this activity was launched with.
intent?.handleIntent()
handleIntent()
}

/**
Expand All @@ -54,7 +53,7 @@ class FitMainActivity :
*/
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
intent?.handleIntent()
handleIntent()
}

/**
Expand Down Expand Up @@ -122,7 +121,7 @@ class FitMainActivity :
*
* @receiver the intent to handle
*/
private fun Intent.handleIntent() {
private fun handleIntent() {
showDefaultView()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
package com.devrel.android.fitactions.home

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.devrel.android.fitactions.R
import com.devrel.android.fitactions.databinding.FitStatsRowBinding
import com.devrel.android.fitactions.model.FitActivity
import kotlinx.android.synthetic.main.fit_stats_row.view.*
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.TimeUnit
Expand All @@ -42,7 +41,7 @@ class FitStatsAdapter : ListAdapter<FitActivity, FitStatsAdapter.ViewHolder>(DIF
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.fit_stats_row, parent, false))
return ViewHolder(FitStatsRowBinding.inflate(LayoutInflater.from(parent.context),parent,false))
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
Expand All @@ -62,7 +61,7 @@ class FitStatsAdapter : ListAdapter<FitActivity, FitStatsAdapter.ViewHolder>(DIF
}
}

class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
class ViewHolder(private val binding: FitStatsRowBinding) : RecyclerView.ViewHolder(binding.root) {

fun bind(activity: FitActivity, max: Int) {
val context = itemView.context
Expand All @@ -73,7 +72,7 @@ class FitStatsAdapter : ListAdapter<FitActivity, FitStatsAdapter.ViewHolder>(DIF
Locale.getDefault()
)
val monthFormatter = SimpleDateFormat("MM")
itemView.statsRowTitle.text = context.getString(
binding.statsRowTitle.text = context.getString(
R.string.stat_date,
day,
calendar.get(Calendar.DAY_OF_MONTH),
Expand All @@ -84,14 +83,14 @@ class FitStatsAdapter : ListAdapter<FitActivity, FitStatsAdapter.ViewHolder>(DIF
val km = String.format("%.2f", activity.distanceMeters / 1000)
val duration = context.getString(R.string.stat_duration, minutes)
val distance = context.getString(R.string.stat_distance, km)
itemView.statsRowContent.apply {
binding.statsRowContent.apply {
text = duration
append("\n")
append(distance)
}

itemView.statsRowProgress.max = max
itemView.statsRowProgress.progress = activity.distanceMeters.toInt()
binding.statsRowProgress.max = max
binding.statsRowProgress.progress = activity.distanceMeters.toInt()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import com.devrel.android.fitactions.R
import com.devrel.android.fitactions.databinding.FitStatsFragmentBinding
import com.devrel.android.fitactions.model.FitRepository
import kotlinx.android.synthetic.main.fit_stats_fragment.*
import java.util.concurrent.TimeUnit

/**
Expand All @@ -37,43 +37,54 @@ class FitStatsFragment : Fragment() {

lateinit var actionsCallback: FitStatsActions

private var _binding: FitStatsFragmentBinding? =null

private val binding get() = _binding!!

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fit_stats_fragment, container, false)
): View {
_binding = FitStatsFragmentBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

val adapter = FitStatsAdapter()
statsList.adapter = adapter
binding.statsList.adapter = adapter

val repository = FitRepository.getInstance(requireContext())
repository.getStats().observe(viewLifecycleOwner, Observer { fitStats ->
statsActivityCount.text = getString(
repository.getStats().observe(viewLifecycleOwner) { fitStats ->
binding.statsActivityCount.text = getString(
R.string.stats_total_count,
fitStats.totalCount
)
statsDistanceCount.text = getString(
binding.statsDistanceCount.text = getString(
R.string.stats_total_distance,
fitStats.totalDistanceMeters.toInt()
)
val durationInMin = TimeUnit.MILLISECONDS.toMinutes(fitStats.totalDurationMs)
statsDurationCount.text = getString(R.string.stats_total_duration, durationInMin)
})
binding.statsDurationCount.text =
getString(R.string.stats_total_duration, durationInMin)
}

repository.getLastActivities(10).observe(viewLifecycleOwner, Observer {
repository.getLastActivities(10).observe(viewLifecycleOwner) {
adapter.submitList(it)
statsList.smoothScrollToPosition(0)
})
binding.statsList.smoothScrollToPosition(0)
}

statsStartButton.setOnClickListener {
binding.statsStartButton.setOnClickListener {
actionsCallback.onStartActivity()
}
}

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

interface FitStatsActions {
fun onStartActivity()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ abstract class FitDatabase : RoomDatabase() {

private fun buildDatabase(context: Context): FitDatabase {
return Room.databaseBuilder(context, FitDatabase::class.java, "FitActionsDB")
.addCallback(object : RoomDatabase.Callback() {
.addCallback(object : Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)
prepopulate(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.devrel.android.fitactions.model

import android.content.Context
import android.os.Handler
import android.os.Looper
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
Expand Down Expand Up @@ -130,7 +131,7 @@ class FitRepository(private val fitDb: FitDatabase, private val ioExecutor: Exec
private class Tracker : MutableLiveData<FitActivity>() {

private var isRunning = true
private val handler = Handler()
private val handler = Handler(Looper.getMainLooper())
private val runnable = object : Runnable {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import com.devrel.android.fitactions.R
import com.devrel.android.fitactions.databinding.FitTrackingFragmentBinding
import com.devrel.android.fitactions.model.FitActivity
import com.devrel.android.fitactions.model.FitRepository
import kotlinx.android.synthetic.main.fit_tracking_fragment.*
import java.util.concurrent.TimeUnit

/**
Expand All @@ -51,6 +50,10 @@ class FitTrackingFragment : Fragment() {

lateinit var actionsCallback: FitTrackingActions

private var _binding: FitTrackingFragmentBinding? = null

private val binding get() = _binding!!

private val fitRepository: FitRepository by lazy {
FitRepository.getInstance(requireContext())
}
Expand All @@ -73,21 +76,22 @@ class FitTrackingFragment : Fragment() {
countDownMs = millisUntilFinished

val secondsLeft = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished).toString()
startActivityCountDown.text = secondsLeft
binding.startActivityCountDown.text = secondsLeft
}
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fit_tracking_fragment, container, false)
): View {
_binding = FitTrackingFragmentBinding.inflate(inflater,container,false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

startActivityButton.setOnClickListener {
binding.startActivityButton.setOnClickListener {
// Handle the two states of the activity button
// If activity started, stop it and notify activity.
// Otherwise start the tracking service.
Expand All @@ -101,7 +105,7 @@ class FitTrackingFragment : Fragment() {
}

// Observe the ongoing activity and update the view accordingly.
fitRepository.getOnGoingActivity().observe(this, Observer {
fitRepository.getOnGoingActivity().observe(viewLifecycleOwner) {
if (it == null) {
// When no ongoing activity only start the countdown if we haven't already.
if (countDownMs > 0) {
Expand All @@ -111,12 +115,13 @@ class FitTrackingFragment : Fragment() {
} else {
onTracking(it)
}
})
}
}

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

/**
Expand All @@ -139,21 +144,21 @@ class FitTrackingFragment : Fragment() {
private fun onCountDown() {
val type = arguments?.getSerializable(PARAM_TYPE) as? FitActivity.Type
?: FitActivity.Type.UNKNOWN
startActivityTitle.text = getString(R.string.start_activity_title, type.name.toLowerCase())
startActivityCountDown.text = TimeUnit.MILLISECONDS.toSeconds(countDownMs).toString()
startActivityButton.isSelected = false
binding.startActivityTitle.text = getString(R.string.start_activity_title, type.name.lowercase())
binding.startActivityCountDown.text = TimeUnit.MILLISECONDS.toSeconds(countDownMs).toString()
binding.startActivityButton.isSelected = false
}

/**
* Update the tracking view
*/
private fun onTracking(activity: FitActivity) {
startActivityTitle.setText(R.string.tracking_notification_title)
startActivityCountDown.text = getString(
binding.startActivityTitle.setText(R.string.tracking_notification_title)
binding.startActivityCountDown.text = getString(
R.string.stats_tracking_distance,
activity.distanceMeters.toInt()
)
startActivityButton.isSelected = true
binding.startActivityButton.isSelected = true
}

interface FitTrackingActions {
Expand Down
Loading