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

Commit

Permalink
Merge pull request #75 from sygic-travel/STA-405-favorites
Browse files Browse the repository at this point in the history
Sta 405 favorites
  • Loading branch information
Michal Murín authored Jul 21, 2017
2 parents f1e7936 + 06a7f5d commit 85a0cb0
Show file tree
Hide file tree
Showing 72 changed files with 1,441 additions and 635 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
sed -i 's/\(.\)bintray\-api\-key\(.\)/\1'"$ANDROID_SDK_BINTRAY_API_KEY"'\2/g' sdk/build.gradle
echo "Disable OkHttp Logging"
sed -i 's/HttpLoggingInterceptor\.Level\.BODY/HttpLoggingInterceptor\.Level\.NONE/g' sdk/src/main/java/com/sygic/travel/sdk/contentProvider/api/StApiGenerator.kt
sed -i 's/HttpLoggingInterceptor\.Level\.BODY/HttpLoggingInterceptor\.Level\.NONE/g' sdk/src/main/java/com/sygic/travel/sdk/api/StApiGenerator.kt
- run:
name: Clean
command: |
Expand Down
19 changes: 13 additions & 6 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,19 @@
</activity>
<activity
android:name=".list.PlacesListActivity"
android:label="@string/title_activity_list"
android:label="@string/title_activity_places"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".detail.PlaceDetailActivity"
android:label="@string/title_activity_place_detail"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".tours.ToursActivity"
android:label="@string/title_activity_list"
android:screenOrientation="portrait"></activity>
<activity
android:name=".gallery.GalleryActivity"
android:label="@string/title_activity_gallery"
android:screenOrientation="portrait"></activity>
android:screenOrientation="portrait">
</activity>
<activity
android:name=".gallery.PhotoActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
Expand All @@ -67,5 +64,15 @@
android:label="@string/title_activity_search"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".tours.ToursActivity"
android:label="@string/title_activity_tours"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".favorites.FavoritesActivity"
android:label="@string/title_activity_favorites"
android:screenOrientation="portrait">
</activity>
</application>
</manifest>
79 changes: 43 additions & 36 deletions app/src/main/java/com/sygic/travel/sdkdemo/DemoMainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,54 @@ import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.sygic.travel.sdk.StSDK
import com.sygic.travel.sdkdemo.favorites.FavoritesActivity
import com.sygic.travel.sdkdemo.list.PlacesListActivity
import com.sygic.travel.sdkdemo.map.MapsActivity
import com.sygic.travel.sdkdemo.search.SearchActivity
import com.sygic.travel.sdkdemo.tours.ToursActivity

class DemoMainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_demo_main)

// Sygic Travel SDK initialization. Must be called on the application start. You can initialize the SDK
// in your Application class' onCreate() method.
StSDK.initialize(getString(R.string.api_key), this)
initUI()
}

private fun initUI() {
findViewById(R.id.btn_map_activity).setOnClickListener { startMapActivity() }
findViewById(R.id.btn_list_activity).setOnClickListener { startListActivity() }
findViewById(R.id.btn_search_activity).setOnClickListener { startSearchActivity() }
findViewById(R.id.btn_tours_activity).setOnClickListener { startToursActivity() }
}

private fun startToursActivity() {
val toursIntent = Intent(this, ToursActivity::class.java)
startActivity(toursIntent)
}

private fun startMapActivity() {
val mapIntent = Intent(this, MapsActivity::class.java)
startActivity(mapIntent)
}

private fun startListActivity() {
val mapIntent = Intent(this, PlacesListActivity::class.java)
startActivity(mapIntent)
}

private fun startSearchActivity() {
val searchIntent = Intent(this, SearchActivity::class.java)
startActivity(searchIntent)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_demo_main)

// Sygic Travel SDK initialization. Must be called on the application start. You can initialize the SDK
// in your Application class' onCreate() method.
StSDK.initialize(getString(R.string.api_key), this)
initUI()
}

private fun initUI() {
findViewById(R.id.btn_map_activity).setOnClickListener { startMapActivity() }
findViewById(R.id.btn_list_activity).setOnClickListener { startListActivity() }
findViewById(R.id.btn_search_activity).setOnClickListener { startSearchActivity() }
findViewById(R.id.btn_tours_activity).setOnClickListener { startToursActivity() }
findViewById(R.id.btn_favorites_activity).setOnClickListener { startFavoritesActivity() }
}

private fun startMapActivity() {
val mapIntent = Intent(this, MapsActivity::class.java)
startActivity(mapIntent)
}

private fun startListActivity() {
val mapIntent = Intent(this, PlacesListActivity::class.java)
startActivity(mapIntent)
}

private fun startSearchActivity() {
val searchIntent = Intent(this, SearchActivity::class.java)
startActivity(searchIntent)
}

private fun startToursActivity() {
val toursIntent = Intent(this, ToursActivity::class.java)
startActivity(toursIntent)
}

private fun startFavoritesActivity() {
val favoritesIntent = Intent(this, FavoritesActivity::class.java)
startActivity(favoritesIntent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import android.widget.*
import com.google.android.flexbox.FlexboxLayout
import com.squareup.picasso.Picasso
import com.sygic.travel.sdk.StSDK
import com.sygic.travel.sdk.contentProvider.api.Callback
import com.sygic.travel.sdk.api.Callback
import com.sygic.travel.sdk.model.place.Place
import com.sygic.travel.sdk.model.place.Reference
import com.sygic.travel.sdk.model.place.Tag
Expand All @@ -21,12 +18,34 @@ import com.sygic.travel.sdkdemo.gallery.GalleryActivity
import com.sygic.travel.sdkdemo.utils.Utils

class PlaceDetailActivity : AppCompatActivity() {
private val stSdk = StSDK.getInstance()

private var id: String? = null
private var views: Views? = null
private var id: String? = null
private var ratingPattern: String? = null
private var tagPadding: Int = 0

private val favoriteAddRemoveCallback = object : Callback<String?>() {
override fun onSuccess(data: String?) {}

override fun onFailure(t: Throwable) {
runOnUiThread { Toast.makeText(this@PlaceDetailActivity, t.message, Toast.LENGTH_LONG).show() }
t.printStackTrace()
}
}

private val loadAllFavoritesIdsCallback = object : Callback<List<String>?>() {
override fun onSuccess(data: List<String>?) {
val isFavorite = data?.contains(id)!!
views?.cbFavorite?.isChecked = isFavorite
setOnFavoriteChangeListener()
}

override fun onFailure(t: Throwable) {
setOnFavoriteChangeListener()
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_place_detail)
Expand All @@ -36,6 +55,21 @@ class PlaceDetailActivity : AppCompatActivity() {
ratingPattern = "Rating: %.2f"
tagPadding = resources.getDimensionPixelSize(R.dimen.tag_padding)
loadPlaceDetail()
loadAllFavoritesIds()
}

private fun loadAllFavoritesIds() {
stSdk.getFavoritesIds(loadAllFavoritesIdsCallback)
}

private fun setOnFavoriteChangeListener() {
views?.cbFavorite?.setOnCheckedChangeListener( { _, isChecked ->
if (isChecked) {
StSDK.getInstance().addPlaceToFavorites(id!!, favoriteAddRemoveCallback)
} else {
StSDK.getInstance().removePlaceFromFavorites(id!!, favoriteAddRemoveCallback)
}
} )
}

override fun onPause() {
Expand Down Expand Up @@ -180,35 +214,20 @@ class PlaceDetailActivity : AppCompatActivity() {
}

private inner class Views internal constructor() {
internal var ivPhoto: ImageView
internal var tvName: TextView
internal var tvNameSuffix: TextView
internal var tvPerex: TextView
internal var tvDescription: TextView
internal var tvRating: TextView
internal var tvAddress: TextView
internal var tvPhone: TextView
internal var tvEmail: TextView
internal var tvAdmission: TextView
internal var tvOpeningHours: TextView
internal var fblTags: FlexboxLayout
internal var llReferencesList: LinearLayout

init {
ivPhoto = findViewById(R.id.iv_detail_photo) as ImageView
tvName = findViewById(R.id.tv_name) as TextView
tvNameSuffix = findViewById(R.id.tv_name_suffix) as TextView
tvPerex = findViewById(R.id.tv_perex) as TextView
tvDescription = findViewById(R.id.tv_description) as TextView
tvRating = findViewById(R.id.tv_rating) as TextView
tvAddress = findViewById(R.id.tv_address) as TextView
tvPhone = findViewById(R.id.tv_phone) as TextView
tvEmail = findViewById(R.id.tv_email) as TextView
tvAdmission = findViewById(R.id.tv_admission) as TextView
tvOpeningHours = findViewById(R.id.tv_opening_hours) as TextView
fblTags = findViewById(R.id.fbl_tags) as FlexboxLayout
llReferencesList = findViewById(R.id.ll_references_list) as LinearLayout
}
internal var ivPhoto: ImageView = findViewById(R.id.iv_detail_photo) as ImageView
internal var tvName: TextView = findViewById(R.id.tv_name) as TextView
internal var tvNameSuffix: TextView = findViewById(R.id.tv_name_suffix) as TextView
internal var tvPerex: TextView = findViewById(R.id.tv_perex) as TextView
internal var tvDescription: TextView = findViewById(R.id.tv_description) as TextView
internal var tvRating: TextView = findViewById(R.id.tv_rating) as TextView
internal var tvAddress: TextView = findViewById(R.id.tv_address) as TextView
internal var tvPhone: TextView = findViewById(R.id.tv_phone) as TextView
internal var tvEmail: TextView = findViewById(R.id.tv_email) as TextView
internal var tvAdmission: TextView = findViewById(R.id.tv_admission) as TextView
internal var tvOpeningHours: TextView = findViewById(R.id.tv_opening_hours) as TextView
internal var fblTags: FlexboxLayout = findViewById(R.id.fbl_tags) as FlexboxLayout
internal var llReferencesList: LinearLayout = findViewById(R.id.ll_references_list) as LinearLayout
internal var cbFavorite: CheckBox = findViewById(R.id.cb_favorite) as CheckBox
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.sygic.travel.sdkdemo.favorites

import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.widget.Toast
import com.sygic.travel.sdk.StSDK
import com.sygic.travel.sdk.api.Callback
import com.sygic.travel.sdk.model.place.Place
import com.sygic.travel.sdkdemo.R
import com.sygic.travel.sdkdemo.detail.PlaceDetailActivity
import com.sygic.travel.sdkdemo.list.PlacesAdapter
import com.sygic.travel.sdkdemo.utils.Utils

class FavoritesActivity : AppCompatActivity() {

private var rvPlaces: RecyclerView? = null
private var placesAdapter: PlacesAdapter? = null
private var places: List<Place>? = null

private val favoritesIdsCallback = object : Callback<List<String>?>() {
override fun onSuccess(data: List<String>?) {
// Places are sorted by rating, best rated places are at the top of the list
loadFavorites(data!!)
}

override fun onFailure(t: Throwable) {
runOnUiThread {
Toast.makeText(this@FavoritesActivity, t.message, Toast.LENGTH_LONG).show()
}
t.printStackTrace()
}
}

private val favoritesCallback = object : Callback<List<Place>?>() {
override fun onSuccess(data: List<Place>?) {
// Places are sorted by rating, best rated places are at the top of the list
renderPlacesList(data!!)
}

override fun onFailure(t: Throwable) {
runOnUiThread {
Toast.makeText(this@FavoritesActivity, t.message, Toast.LENGTH_LONG).show()
}
t.printStackTrace()
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_list)

initRecycler()
}

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

// Recycler view initialization - list with dividers
private fun initRecycler() {
rvPlaces = findViewById(R.id.rv_places) as RecyclerView
rvPlaces!!.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
rvPlaces!!.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
placesAdapter = PlacesAdapter(onPlaceClick, Utils.getDetailPhotoSize(this))
rvPlaces!!.adapter = placesAdapter
}

// On a place click listener. Opens it's detail.
private val onPlaceClick = object : PlacesAdapter.ViewHolder.PlaceClick {
override fun onPlaceClick(position: Int) {
val placeDetailIntent = Intent(this@FavoritesActivity, PlaceDetailActivity::class.java)
placeDetailIntent.putExtra(ID, places?.get(position)?.id)
startActivity(placeDetailIntent)
}
}

// Use the SDK to load favorite places' ids from database
private fun loadFavoritesIds() {
StSDK.getInstance().getFavoritesIds(favoritesIdsCallback)
}

// Use the SDK to load favorite place from api
private fun loadFavorites(favoritesIds: List<String>) {
StSDK.getInstance().getPlacesDetailed(favoritesIds, favoritesCallback)
}

private fun renderPlacesList(places: List<Place>) {
this.places = places
placesAdapter!!.setPlaces(places)
placesAdapter!!.notifyDataSetChanged()
}

companion object {
private val TAG = FavoritesActivity::class.java.simpleName

val ID = "id"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import android.support.v7.widget.RecyclerView
import android.util.Log
import android.widget.ImageView
import com.sygic.travel.sdk.StSDK
import com.sygic.travel.sdk.contentProvider.api.Callback
import com.sygic.travel.sdk.api.Callback
import com.sygic.travel.sdk.model.media.Medium
import com.sygic.travel.sdkdemo.R
import com.sygic.travel.sdkdemo.utils.Utils
Expand Down
Loading

0 comments on commit 85a0cb0

Please sign in to comment.