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

Commit

Permalink
Docs: added missing documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Murín committed Jul 21, 2017
1 parent 4e3610e commit 0c83a90
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 8 deletions.
25 changes: 21 additions & 4 deletions sdk/src/main/java/com/sygic/travel/sdk/StSDK.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,36 @@ class StSDK internal constructor() {
disposable = dataProvider?.getTours(toursQuery, back)!!
}


/**
* Stores a place's id in a local persistent storage. The place is added to the favorites.
* @param id A place's id, which is stored.
*
* @param back Callback. Either [Callback.onSuccess] with tours is called, or
* [Callback.onFailure] in case of an error is called.
*/
fun addPlaceToFavorites(id: String, back: Callback<String>?) {
dataProvider?.addPlaceToFavorites(id, back)
}


/**
* Removes a place's id from a local persistent storage. The place is removed from the favorites.
* @param id A place's id, which is removed.
*
* @param back Callback. Either [Callback.onSuccess] with tours is called, or
* [Callback.onFailure] in case of an error is called.
*/
fun removePlaceFromFavorites(id: String, back: Callback<String>?) {
dataProvider?.removePlaceFromFavorites(id, back)
}


fun getFavoritesIds(callback: Callback<List<String>?>?) {
dataProvider?.getFavoritesIds(callback)
/**
* Method returns a list of all favorite places' ids.
* @param back Callback. Either [Callback.onSuccess] with tours is called, or
* [Callback.onFailure] in case of an error is called.
*/
fun getFavoritesIds(back: Callback<List<String>?>?) {
dataProvider?.getFavoritesIds(back)
}


Expand Down
9 changes: 9 additions & 0 deletions sdk/src/main/java/com/sygic/travel/sdk/db/Converters.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@ package com.sygic.travel.sdk.db

import android.arch.persistence.room.TypeConverter

/**
* Contains converter methods for working with database.
*/
class Converters {
/**
* Converts a single String, separated by commas (','), to a list of Strings.
*/
@TypeConverter
fun stringToList(value: String): List<String> {
return value.split((",").toRegex(), 0)
}

/**
* Converts a list of String, a single String. Original string are separated by commas (',').
*/
@TypeConverter
fun listToString(values: List<String>): String {
return values.joinToString(",")
Expand Down
18 changes: 18 additions & 0 deletions sdk/src/main/java/com/sygic/travel/sdk/db/dao/FavoriteDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,32 @@ package com.sygic.travel.sdk.db.dao

import android.arch.persistence.room.*
import com.sygic.travel.sdk.model.place.Favorite

/**
* Interface containing methods for working with favorite table in database.
*/
@Dao
interface FavoriteDao {
/**
* @return All favorite places' ids.
*/
@Query("SELECT * FROM favorite")
fun loadAll(): List<Favorite>

/**
* Inserts one place's id into favorite table. If the id has already been inserted before, the
* insertion of [placeId] is skipped. No error is provided.
* @param placeId An id to be stored.
* @return A rowid of the inserted place id (row).
*/
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(placeId: Favorite) : Long

/**
* Removes a given place id from the favorite table.
* @param placeId An id to be removed.
* @return A number of deleted rows.
*/
@Delete
fun delete(placeId: Favorite) : Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package com.sygic.travel.sdk.model.place
import android.arch.persistence.room.Entity
import android.arch.persistence.room.PrimaryKey

/**
* Database entity, representing a table with one column referencing to a place marked as favorite.
*/
@Entity(tableName = "favorite")
class Favorite(
@PrimaryKey
Expand Down
33 changes: 29 additions & 4 deletions sdk/src/main/java/com/sygic/travel/sdk/provider/DataProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers


/**
* Data provider contains methods for fetching data either from API or a database.
*/
internal class DataProvider(
val stApi: StApi? = null,
val stDb: StDb? = null
Expand Down Expand Up @@ -83,6 +86,7 @@ internal class DataProvider(
return preparedObservable.subscribeWith(StObserver(callback, false))
}


/**
*
* Creates and sends a request to get places with detailed information.
Expand All @@ -107,6 +111,7 @@ internal class DataProvider(
return preparedObservable.subscribeWith(StObserver(callback, false))
}


/**
*
* Creates and sends a request to get the place's media.
Expand All @@ -130,6 +135,7 @@ internal class DataProvider(
return preparedObservable.subscribeWith(StObserver(callback, false))
}


/**
* Creates and sends a request to get the Tours.
* @param toursQuery ToursQuery encapsulating data for API request.
Expand Down Expand Up @@ -158,10 +164,17 @@ internal class DataProvider(
}


/**
* Stores a place's id in a local persistent storage. The place is added to the favorites.
* @param id A place's id, which is stored.
*
* @param back Callback. Either [Callback.onSuccess] with tours is called, or
* [Callback.onFailure] in case of an error is called.
*/
fun addPlaceToFavorites(id: String, back: Callback<String>?) {
Thread(Runnable {
val insertedCount = stDb?.favoriteDao()?.insert(Favorite(id))
if (insertedCount != null && insertedCount > 0L) {
val rowid = stDb?.favoriteDao()?.insert(Favorite(id))
if (rowid != null && rowid > 0L) {
back?.onSuccess("Success")
} else {
back?.onFailure(Exception("Favorite not added!"))
Expand All @@ -171,10 +184,17 @@ internal class DataProvider(
}


/**
* Removes a place's id from a local persistent storage. The place is removed from the favorites.
* @param id A place's id, which is removed.
*
* @param back Callback. Either [Callback.onSuccess] with tours is called, or
* [Callback.onFailure] in case of an error is called.
*/
fun removePlaceFromFavorites(id: String, back: Callback<String>?) {
Thread(Runnable {
val insertedCount = stDb?.favoriteDao()?.delete(Favorite(id))
if (insertedCount == 1) {
val removedCount = stDb?.favoriteDao()?.delete(Favorite(id))
if (removedCount == 1) {
back?.onSuccess("Success")
} else {
back?.onFailure(Exception("Favorite not removed!"))
Expand All @@ -183,6 +203,11 @@ internal class DataProvider(
}


/**
* Method returns a list of all favorite places' ids.
* @param back Callback. Either [Callback.onSuccess] with tours is called, or
* [Callback.onFailure] in case of an error is called.
*/
fun getFavoritesIds(back: Callback<List<String>?>?) {
Thread(Runnable {
val favorites = stDb?.favoriteDao()?.loadAll()
Expand Down

0 comments on commit 0c83a90

Please sign in to comment.