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

Java/Kotlin toggle, MainActivity refactor #877

Merged
merged 2 commits into from
Oct 9, 2018
Merged
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
1 change: 1 addition & 0 deletions MapboxAndroidDemo/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.firebase.firebase-perf'
apply plugin: 'com.github.triplet.play'
apply from: "$project.rootDir/gradle/script-git-version.gradle"
Expand Down
7 changes: 7 additions & 0 deletions MapboxAndroidDemo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
</activity>
<activity
android:name=".examples.plugins.KotlinLocationPluginActivity"
android:label="@string/activity_plugins_location_plugin_title">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
</activity>
<activity
android:name=".examples.plugins.LocationPluginActivity"
android:label="@string/activity_plugins_location_plugin_title">
Expand Down
1,487 changes: 825 additions & 662 deletions MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/MainActivity.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.mapbox.mapboxandroiddemo.adapter;

import android.content.Context;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
Expand All @@ -9,61 +11,55 @@
import android.widget.ImageView;
import android.widget.TextView;

import com.mapbox.mapboxandroiddemo.MainActivity;
import com.mapbox.mapboxandroiddemo.R;
import com.mapbox.mapboxandroiddemo.model.ExampleItemModel;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class ExampleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private List<ExampleItemModel> dataSource;
private Context context;
private final List<ExampleItemModel> dataSource = new ArrayList<>();
private final Context context;
private int viewType;

public ExampleAdapter(Context context, List<ExampleItemModel> dataSource) {
this.dataSource = dataSource;
public ExampleAdapter(Context context) {
this.context = context;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// create a new view
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
View view1 = LayoutInflater.from(parent.getContext()).inflate(
R.layout.layout_description_item, parent, false);
View view2 = LayoutInflater.from(parent.getContext()).inflate(
R.layout.layout_javaservices_description_card, parent, false);
View view3 = LayoutInflater.from(parent.getContext()).inflate(
R.layout.layout_query_description_card, parent, false);

View view;
switch (viewType) {
case 1:
return new ViewHolderDescription(view1);
view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.layout_description_item, parent, false);
break;
case 2:
return new ViewHolderDescription(view2);
view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.layout_javaservices_description_card, parent, false);
break;
case 3:
return new ViewHolderDescription(view3);
view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.layout_query_description_card, parent, false);
break;
default:
return new ViewHolder(view);
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
}

return new ViewHolder(view);
}

@Override
public int getItemViewType(int position) {
if (((MainActivity) context).getCurrentCategory() == R.id.nav_lab && position == 0) {
return 1;
} else if (((MainActivity) context).getCurrentCategory() == R.id.nav_java_services && position == 0) {
return 2;
} else if (((MainActivity) context).getCurrentCategory() == R.id.nav_query_map && position == 0) {
return 3;
}
return 0;
return position == 0 ? viewType : 0;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder.getItemViewType() != 0) {
return;
}
Expand Down Expand Up @@ -93,30 +89,51 @@ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

@Override
public int getItemCount() {
return (null != dataSource ? dataSource.size() : 0);
return dataSource.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder {
public void updateDataSet(List<ExampleItemModel> examples, @IdRes int categoryId) {
dataSource.clear();
dataSource.addAll(examples);

public TextView titleTextView;
public TextView descriptionTextView;
public ImageView imageView;
public ImageView newIconImageView;
viewType = getViewType(categoryId);
if (viewType > 0) {
dataSource.add(0, null);
}

public ViewHolder(final View itemView) {
super(itemView);
notifyDataSetChanged();
}

public ExampleItemModel getItemAt(int position) {
return dataSource.get(position);
}

imageView = (ImageView) itemView.findViewById(R.id.example_image);
titleTextView = (TextView) itemView.findViewById(R.id.example_title);
descriptionTextView = (TextView) itemView.findViewById(R.id.example_description);
newIconImageView = (ImageView) itemView.findViewById(R.id.new_icon_image_view);
private int getViewType(@IdRes int categoryId) {
if (categoryId == R.id.nav_lab) {
return 1;
} else if (categoryId == R.id.nav_java_services) {
return 2;
} else if (categoryId == R.id.nav_query_map) {
return 3;
}

return 0;
}

public static class ViewHolderDescription extends RecyclerView.ViewHolder {
static class ViewHolder extends RecyclerView.ViewHolder {

public ViewHolderDescription(final View itemView) {
TextView titleTextView;
TextView descriptionTextView;
ImageView imageView;
ImageView newIconImageView;

ViewHolder(final View itemView) {
super(itemView);

imageView = itemView.findViewById(R.id.example_image);
titleTextView = itemView.findViewById(R.id.example_title);
descriptionTextView = itemView.findViewById(R.id.example_description);
newIconImageView = itemView.findViewById(R.id.new_icon_image_view);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.mapbox.mapboxandroiddemo.examples.plugins

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Toast

import com.mapbox.android.core.permissions.PermissionsListener
import com.mapbox.android.core.permissions.PermissionsManager
import com.mapbox.mapboxandroiddemo.R
import com.mapbox.mapboxsdk.Mapbox
import com.mapbox.mapboxsdk.maps.MapboxMap
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback
import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerPlugin
import com.mapbox.mapboxsdk.plugins.locationlayer.modes.CameraMode
import kotlinx.android.synthetic.main.activity_location_plugin.*

/**
* Use the Location Layer plugin to easily add a device location "puck" to a Mapbox map.
*/
class KotlinLocationPluginActivity : AppCompatActivity(), OnMapReadyCallback, PermissionsListener {

private val permissionsManager: PermissionsManager = PermissionsManager(this)
private lateinit var mapboxMap: MapboxMap

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// Mapbox access token is configured here. This needs to be called either in your application
// object or in the same activity which contains the mapview.
Mapbox.getInstance(this, getString(R.string.access_token))

// This contains the MapView in XML and needs to be called after the access token is configured.
setContentView(R.layout.activity_location_plugin)

mapView.onCreate(savedInstanceState)
mapView.getMapAsync(this)
}

override fun onMapReady(mapboxMap: MapboxMap) {
this.mapboxMap = mapboxMap
enableLocationPlugin()
}

private fun enableLocationPlugin() {
// Check if permissions are enabled and if not request
if (PermissionsManager.areLocationPermissionsGranted(this)) {

// Create an instance of the plugin. Adding in LocationLayerOptions is also an optional
// parameter
val locationLayerPlugin = LocationLayerPlugin(mapView, mapboxMap)

// Set the plugin's camera mode
locationLayerPlugin.cameraMode = CameraMode.TRACKING
lifecycle.addObserver(locationLayerPlugin)
} else {
permissionsManager.requestLocationPermissions(this)
}
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
permissionsManager.onRequestPermissionsResult(requestCode, permissions, grantResults)
}

override fun onExplanationNeeded(permissionsToExplain: List<String>) {
Toast.makeText(this, R.string.user_location_permission_explanation, Toast.LENGTH_LONG).show()
}

override fun onPermissionResult(granted: Boolean) {
if (granted) {
enableLocationPlugin()
} else {
Toast.makeText(this, R.string.user_location_permission_not_granted, Toast.LENGTH_LONG).show()
finish()
}
}

override fun onStart() {
super.onStart()
mapView.onStart()
}

override fun onResume() {
super.onResume()
mapView.onResume()
}

override fun onPause() {
super.onPause()
mapView.onPause()
}

override fun onStop() {
super.onStop()
mapView.onStop()
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
mapView.onSaveInstanceState(outState)
}

override fun onDestroy() {
super.onDestroy()
mapView.onDestroy()
}

override fun onLowMemory() {
super.onLowMemory()
mapView.onLowMemory()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mapbox.mapboxandroiddemo.model

import android.content.Intent

// Just a model for the detailed item recycler
class ExampleItemModel(val categoryId: Int, val title: Int, val description: Int, val javaActivity: Intent?,
val kotlinActivity: Intent?, val imageUrl: Int, val showNewIcon: Boolean,
val minSdkVersion: Int)
Loading