Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NO ISSUE] Migrated Sample App main activity to Jetpack Compose #348

Merged
merged 8 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
24 changes: 19 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ apply plugin: "org.jlleitschuh.gradle.ktlint"
apply plugin: "io.gitlab.arturbosch.detekt"

android {
compileSdkVersion 31
compileSdkVersion 32

defaultConfig {
applicationId "com.schibsted.mappicker"
minSdkVersion 21
//noinspection OldTargetApi
targetSdkVersion 31
targetSdkVersion 32
versionCode 1
versionName "1.0"
vectorDrawables.useSupportLibrary = true
Expand All @@ -26,9 +25,17 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
lintOptions {

buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.1.1'
}
lint {
disable 'ObsoleteLintCustomCheck'
}
namespace 'com.adevinta.mappicker'
}

configurations.all {
Expand Down Expand Up @@ -60,8 +67,15 @@ ktlint {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.multidex:multidex:2.0.1'
implementation "com.google.android.material:material:1.6.0-beta01"
implementation "com.google.android.material:material:1.7.0-alpha01"
implementation "com.google.android.gms:play-services-maps:18.0.2"

implementation 'androidx.activity:activity-compose:1.4.0'
implementation 'androidx.compose.material:material:1.1.1'
implementation 'androidx.compose.animation:animation:1.1.1'
implementation 'androidx.compose.ui:ui-tooling:1.1.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.4.1'

implementation project(':leku')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
2 changes: 0 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.adevinta.mappicker"
>

<uses-permission android:name="android.permission.INTERNET"/>
Expand Down Expand Up @@ -42,7 +41,6 @@

<activity
android:name=".MainActivity"
android:label="@string/leku_app_name"
android:exported="true"
>
<intent-filter>
Expand Down
264 changes: 189 additions & 75 deletions app/src/main/java/com/adevinta/mappicker/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,30 @@ import android.location.Location
import android.os.Bundle
import android.os.StrictMode
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.adevinta.leku.ADDRESS
import com.adevinta.leku.LATITUDE
import com.adevinta.leku.LEKU_POI
Expand Down Expand Up @@ -40,87 +61,15 @@ private const val POI2_LONGITUDE = 2.1741417

class MainActivity : AppCompatActivity() {

private val lekuPois: List<LekuPoi>
get() {
val pois = ArrayList<LekuPoi>()

val locationPoi1 = Location("leku")
locationPoi1.latitude = POI1_LATITUDE
locationPoi1.longitude = POI1_LONGITUDE
val poi1 = LekuPoi(UUID.randomUUID().toString(), "Los bellota", locationPoi1)
pois.add(poi1)

val locationPoi2 = Location("leku")
locationPoi2.latitude = POI2_LATITUDE
locationPoi2.longitude = POI2_LONGITUDE
val poi2 = LekuPoi(UUID.randomUUID().toString(), "Starbucks", locationPoi2)
poi2.address = "Plaça de la Sagrada Família, 19, 08013 Barcelona"
pois.add(poi2)

return pois
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
StrictMode.setThreadPolicy(
StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build()
)
StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build())
setContentView(R.layout.activity_main)
val mapButton = findViewById<View>(R.id.map_button)
mapButton.setOnClickListener {
val locationPickerIntent = LocationPickerActivity.Builder()
.withLocation(DEMO_LATITUDE, DEMO_LONGITUDE)
// .withGeolocApiKey("<PUT API KEY HERE>")
// .withGooglePlacesApiKey("<PUT API KEY HERE>")
.withSearchZone("es_ES")
// .withSearchZone(SearchZoneRect(LatLng(26.525467, -18.910366), LatLng(43.906271, 5.394197)))
.withDefaultLocaleSearchZone()
// .shouldReturnOkOnBackPressed()
// .withStreetHidden()
// .withCityHidden()
// .withZipCodeHidden()
// .withSatelliteViewHidden()
.withGoogleTimeZoneEnabled()
// .withVoiceSearchHidden()
.withUnnamedRoadHidden()
// .withSearchBarHidden()
.build(applicationContext)

// this is optional if you want to return RESULT_OK if you don't set the
// latitude/longitude and click back button
locationPickerIntent.putExtra("test", "this is a test")

startActivityForResult(locationPickerIntent, MAP_BUTTON_REQUEST_CODE)
}

val mapLegacyButton = findViewById<View>(R.id.map_button_legacy)
mapLegacyButton.setOnClickListener {
val locationPickerIntent = LocationPickerActivity.Builder()
.withLocation(DEMO_LATITUDE, DEMO_LONGITUDE)
.withUnnamedRoadHidden()
.withLegacyLayout()
.build(applicationContext)
startActivityForResult(locationPickerIntent, MAP_BUTTON_REQUEST_CODE)
}

val mapPoisButton = findViewById<View>(R.id.map_button_with_pois)
mapPoisButton.setOnClickListener {
val locationPickerIntent = LocationPickerActivity.Builder()
.withLocation(DEMO_LATITUDE, DEMO_LONGITUDE)
.withPois(lekuPois)
.build(applicationContext)

startActivityForResult(locationPickerIntent, MAP_POIS_BUTTON_REQUEST_CODE)
}

val mapStyleButton = findViewById<View>(R.id.map_button_with_style)
mapStyleButton.setOnClickListener {
val locationPickerIntent = LocationPickerActivity.Builder()
.withLocation(DEMO_LATITUDE, DEMO_LONGITUDE)
.withMapStyle(R.raw.map_style_retro)
.build(applicationContext)
startActivityForResult(locationPickerIntent, MAP_POIS_BUTTON_REQUEST_CODE)
setContent {
MainView()
}

initializeLocationPickerTracker()
Expand Down Expand Up @@ -179,3 +128,168 @@ class MainActivity : AppCompatActivity() {
}
}
}

private fun onLaunchMapPickerClicked(context: Context) {
val activity = context as Activity
val locationPickerIntent = LocationPickerActivity.Builder()
.withLocation(DEMO_LATITUDE, DEMO_LONGITUDE)
// .withGeolocApiKey("<PUT API KEY HERE>")
// .withGooglePlacesApiKey("<PUT API KEY HERE>")
.withSearchZone("es_ES")
// .withSearchZone(SearchZoneRect(LatLng(26.525467, -18.910366), LatLng(43.906271, 5.394197)))
.withDefaultLocaleSearchZone()
// .shouldReturnOkOnBackPressed()
// .withStreetHidden()
// .withCityHidden()
// .withZipCodeHidden()
// .withSatelliteViewHidden()
.withGoogleTimeZoneEnabled()
// .withVoiceSearchHidden()
.withUnnamedRoadHidden()
// .withSearchBarHidden()
.build(activity)

// this is optional if you want to return RESULT_OK if you don't set the
// latitude/longitude and click back button
locationPickerIntent.putExtra("test", "this is a test")

activity.startActivityForResult(locationPickerIntent, MAP_BUTTON_REQUEST_CODE)
}

private fun onLegacyMapClicked(context: Context) {
val activity = context as Activity
val locationPickerIntent = LocationPickerActivity.Builder()
.withLocation(DEMO_LATITUDE, DEMO_LONGITUDE)
.withUnnamedRoadHidden()
.withLegacyLayout()
.build(activity)
activity.startActivityForResult(locationPickerIntent, MAP_BUTTON_REQUEST_CODE)
}

private val lekuPois: List<LekuPoi>
get() {
val pois = ArrayList<LekuPoi>()

val locationPoi1 = Location("leku")
locationPoi1.latitude = POI1_LATITUDE
locationPoi1.longitude = POI1_LONGITUDE
val poi1 = LekuPoi(UUID.randomUUID().toString(), "Los bellota", locationPoi1)
pois.add(poi1)

val locationPoi2 = Location("leku")
locationPoi2.latitude = POI2_LATITUDE
locationPoi2.longitude = POI2_LONGITUDE
val poi2 = LekuPoi(UUID.randomUUID().toString(), "Starbucks", locationPoi2)
poi2.address = "Plaça de la Sagrada Família, 19, 08013 Barcelona"
pois.add(poi2)

return pois
}

private fun onMapPoisClicked(context: Context) {
val activity = context as Activity
val locationPickerIntent = LocationPickerActivity.Builder()
.withLocation(DEMO_LATITUDE, DEMO_LONGITUDE)
.withPois(lekuPois)
.build(activity)

activity.startActivityForResult(locationPickerIntent, MAP_POIS_BUTTON_REQUEST_CODE)
}

private fun onMapWithStylesClicked(context: Context) {
val activity = context as Activity
val locationPickerIntent = LocationPickerActivity.Builder()
.withLocation(DEMO_LATITUDE, DEMO_LONGITUDE)
.withMapStyle(R.raw.map_style_retro)
.build(activity)
activity.startActivityForResult(locationPickerIntent, MAP_POIS_BUTTON_REQUEST_CODE)
}

@Composable
@Preview(showBackground = true)
fun MainView() {
val context = LocalContext.current

Column(Modifier.padding(16.dp, 0.dp, 16.dp, 0.dp).fillMaxSize()) {
Spacer(modifier = Modifier.size(20.dp))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could replace all the Spacer(modifier = Modifier.size(20.dp)) with a parameter in the parent Column:

Column(
  Modifier
    .padding(horizontal = 16.dp)
    .fillMaxSize(),
  verticalArrangement = Arrangement.spacedBy(20.dp)
) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for your suggestion!

Image(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
painter = painterResource(id = R.mipmap.leku_img_logo),
contentDescription = null
)
Spacer(modifier = Modifier.size(20.dp))
Button(
colors = ButtonDefaults.buttonColors(
backgroundColor = Color(context.resources.getColor(R.color.leku_app_blue)),
contentColor = Color.White
),
Comment on lines +223 to +227
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ok for now, but you should use Theme color when you create the theme.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! The next step is to create the Theme for the picker activity :)

onClick = {
onLaunchMapPickerClicked(context)
}
) {
Text(
stringResource(id = R.string.launch_map_picker),
Modifier.padding(8.dp).fillMaxWidth(),
textAlign = TextAlign.Center
)
}
Spacer(modifier = Modifier.size(20.dp))
Button(
colors = ButtonDefaults.buttonColors(
backgroundColor = Color(context.resources.getColor(R.color.leku_app_blue)),
contentColor = Color.White
),
onClick = {
onLegacyMapClicked(context)
}
) {
Text(
stringResource(id = R.string.launch_legacy_map_picker),
Modifier.padding(8.dp).fillMaxWidth(),
textAlign = TextAlign.Center
)
}
Spacer(modifier = Modifier.size(20.dp))
Button(
colors = ButtonDefaults.buttonColors(
backgroundColor = Color(context.resources.getColor(R.color.leku_app_blue)),
contentColor = Color.White
),
onClick = {
onMapPoisClicked(context)
}
) {
Text(
stringResource(id = R.string.launch_map_picker_with_style),
Modifier.padding(8.dp).fillMaxWidth(),
textAlign = TextAlign.Center
)
}
Spacer(modifier = Modifier.size(20.dp))
Button(
colors = ButtonDefaults.buttonColors(
backgroundColor = Color(context.resources.getColor(R.color.leku_app_blue)),
contentColor = Color.White
),
onClick = {
onMapWithStylesClicked(context)
}
) {
Text(
stringResource(id = R.string.launch_map_picker_with_pois),
Modifier.padding(8.dp).fillMaxWidth(),
textAlign = TextAlign.Center
)
}
Spacer(modifier = Modifier.size(20.dp))
Box(modifier = Modifier.fillMaxSize()) {
Text(
stringResource(id = R.string.leku_lib_version),
modifier = Modifier.align(Alignment.BottomCenter).padding(0.dp, 0.dp, 0.dp, 8.dp),
textAlign = TextAlign.Center
)
}
}
}
6 changes: 0 additions & 6 deletions app/src/main/res/drawable/leku_app_button_border.xml

This file was deleted.

Loading