Skip to content

Commit

Permalink
Merge pull request #65 from JoaquimLey/stream/episode_14
Browse files Browse the repository at this point in the history
Stream Episode 14
  • Loading branch information
JoaquimLey authored Sep 30, 2018
2 parents bf59992 + 5d59f6d commit 89602a9
Show file tree
Hide file tree
Showing 31 changed files with 769 additions and 48 deletions.
2 changes: 2 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ Since I'm always working on some side-projects, I decided to document the develo

- Ep.13: https://github.com/JoaquimLey/transport-eta/pull/64

- Ep.14: https://github.com/JoaquimLey/transport-eta/pull/65


## About the author
Hi, my name is **Joaquim Ley**, I'm a Software Engineer (Android).
Expand Down
30 changes: 30 additions & 0 deletions transport-eta-android/data/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apply plugin: 'kotlin'

// Module configuration
configurations.all {
resolutionStrategy {
force deps.kotlin.stdlib
}
}


dependencies {
// Modules
implementation project(':domain')

// Javax
implementation deps.javax.inject
compileOnly deps.javax.annotation
// Kotlin
implementation deps.kotlin.rx
implementation deps.kotlin.stdlib

/***********
* Testing *
***********/

// Testing
testImplementation deps.junit
testImplementation deps.mockito.kotlin
testImplementation deps.mockito.inline
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.joaquimley.data

import com.joaquimley.data.mapper.TransportMapper
import com.joaquimley.data.store.TransportDataStore
import com.joaquimley.transporteta.domain.model.Transport
import com.joaquimley.transporteta.domain.repository.FavoritesRepository
import io.reactivex.Completable
import io.reactivex.Flowable

class FavoritesRepositoryImpl(private val transportDataStore: TransportDataStore,
private val mapper: TransportMapper) : FavoritesRepository {

override fun markAsFavorite(transport: Transport): Completable {
return transportDataStore.markAsFavorite(mapper.toEntity(transport))
}

override fun removeAsFavorite(transport: Transport): Completable {
return transportDataStore.removeAsFavorite(mapper.toEntity(transport))
}

override fun getAll(): Flowable<List<Transport>> {
return transportDataStore.getAllFavorites().map { mapper.toModel(it) }
}

override fun clearAll(): Completable {
return transportDataStore.clearAllFavorites()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.joaquimley.data

import com.joaquimley.transporteta.domain.model.Transport
import com.joaquimley.transporteta.domain.repository.TransportRepository
import io.reactivex.Completable
import io.reactivex.Observable

class TransportRepositoryImpl: TransportRepository {

override fun requestTransportEta(transportCode: Int): Observable<Transport> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

override fun cancelTransportEtaRequest(transportCode: Int?): Completable {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

override fun getTransport(transportId: String): Observable<Transport> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

override fun saveTransport(transport: Transport): Completable {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

override fun saveTransport(transportList: List<Transport>): Completable {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

override fun deleteTransport(transport: Transport): Completable {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

override fun deleteTransports(transport: List<Transport>): Completable {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.joaquimley.data.executor

import com.joaquimley.transporteta.domain.executor.ThreadExecutor
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ThreadFactory
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit

class ThreadExecutorImpl(private val threadFactory: ThreadFactory = JobThreadFactory(),
private val workQueue: LinkedBlockingQueue<Runnable> = LinkedBlockingQueue(),
private val threadPoolExecutor: ThreadPoolExecutor =
ThreadPoolExecutor(INITIAL_POOL_SIZE, MAX_POOL_SIZE,
KEEP_ALIVE_TIME.toLong(), KEEP_ALIVE_TIME_UNIT,
workQueue, threadFactory))
: ThreadExecutor {


override fun execute(runnable: Runnable?) {
runnable?.let { threadPoolExecutor.execute(runnable) }
?: run {
throw IllegalArgumentException("Runnable to execute cannot be null")
}
}

private class JobThreadFactory : ThreadFactory {
private var counter = 0

override fun newThread(runnable: Runnable): Thread {
return Thread(runnable, THREAD_NAME + counter++)
}

companion object {
private const val THREAD_NAME = "transport_eta_"
}
}

companion object {

private const val INITIAL_POOL_SIZE = 3 // TODO: Get number of CPU cores
private const val MAX_POOL_SIZE = 5 // TODO: Get number of CPU cores

// Sets the amount of time an idle thread waits before terminating
private const val KEEP_ALIVE_TIME = 10

// Sets the Time Unit to seconds
private val KEEP_ALIVE_TIME_UNIT = TimeUnit.SECONDS
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.joaquimley.data.mapper

import com.joaquimley.data.model.TransportEntity
import com.joaquimley.transporteta.domain.model.Transport

class TransportMapper {

fun toEntity(from: List<Transport>): List<TransportEntity> {
return from.map { toEntity(it) }
}

fun toEntity(from: Transport): TransportEntity {
return TransportEntity(from.id, from.name, from.code, from.latestEta, from.isFavorite, from.type)
}

fun toModel(from: List<TransportEntity>): List<Transport> {
return from.map { toModel(it) }
}

fun toModel(from: TransportEntity): Transport {
return Transport(from.id, from.name, from.code, from.latestEta, from.isFavorite, from.type)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.joaquimley.data.model

data class TransportEntity(val id: String, val name: String, val code: Int, val latestEta: String,
val isFavorite: Boolean = false, val type: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.joaquimley.data.source

class SharedPreferencesDataSource
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.joaquimley.data.store

import com.joaquimley.data.model.TransportEntity
import io.reactivex.Completable
import io.reactivex.Flowable
import io.reactivex.Observable

interface TransportDataStore {

fun markAsFavorite(transport: TransportEntity): Completable

fun removeAsFavorite(transport: TransportEntity): Completable

fun getAllFavorites(): Flowable<List<TransportEntity>>

fun clearAllFavorites(): Completable

fun getAll(): Flowable<List<TransportEntity>>

fun getTransport(transportId: String): Observable<TransportEntity>

fun saveTransport(transport: TransportEntity): Completable

fun saveTransport(transportList: List<TransportEntity>): Completable

fun deleteTransport(transport: TransportEntity): Completable

fun deleteTransport(transport: List<TransportEntity>): Completable

// TODO Should these be in the [SmSController] instead? [RequestEtaUseCase]
fun requestTransportEta(transportCode: Int): Observable<TransportEntity>

// TODO Should these be in the [SmSController] instead ? [RequestEtaUseCase]
fun cancelTransportEtaRequest(transportCode: Int?): Completable
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.joaquimley.data.store

import com.joaquimley.data.source.SharedPreferencesDataSource

class TransportDataStoreImpl(val sharedPreferencesDataSource: SharedPreferencesDataSource) {

}
Loading

0 comments on commit 89602a9

Please sign in to comment.