-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
John O'Reilly
committed
Nov 26, 2017
1 parent
1f358e2
commit 1ddb385
Showing
54 changed files
with
1,098 additions
and
283 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,11 @@ | ||
**/*/res/values/secrets.xml | ||
.gradle | ||
.idea | ||
/local.properties | ||
*iml | ||
*.iml | ||
*/build | ||
build/ | ||
.DS_Store | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,8 @@ | ||
Android Galway Bus app | ||
### Android Galway Bus app using Clean Architecture/Architecture Components/Kotlin/Dagger | ||
|
||
|
||
Heavily based on https://github.com/bufferapp/clean-architecture-components-boilerplate. This is stil work in progress and also, at least for now, | ||
have omitted `Mapper` classes (using same data model across the different layers....though this will likely change). | ||
|
||
Note also that this is using REST endpoint provided by @appsandwich (Thanks Vinny!) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
app/src/androidTest/java/com/surrus/galwaybus/ExampleInstrumentedTest.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
app/src/main/java/com/surrus/galwaybus/GalwayBusApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.surrus.galwaybus | ||
|
||
import android.app.Activity | ||
import android.app.Application | ||
import com.surrus.galwaybus.di.DaggerApplicationComponent | ||
import dagger.android.AndroidInjector | ||
import dagger.android.DispatchingAndroidInjector | ||
import dagger.android.HasActivityInjector | ||
import javax.inject.Inject | ||
|
||
class GalwayBusApplication : Application(), HasActivityInjector { | ||
|
||
@Inject lateinit var activityDispatchingAndroidInjector: DispatchingAndroidInjector<Activity> | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
DaggerApplicationComponent | ||
.builder() | ||
.application(this) | ||
.build() | ||
.inject(this) | ||
} | ||
|
||
override fun activityInjector(): AndroidInjector<Activity> { | ||
return activityDispatchingAndroidInjector | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
app/src/main/java/com/surrus/galwaybus/cache/GalwayBusCacheImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.surrus.galwaybus.cache | ||
|
||
import com.surrus.galwaybus.cache.db.GalwayBusDatabase | ||
import com.surrus.galwaybus.data.repository.GalwayBusCache | ||
import com.surrus.galwaybus.model.BusRoute | ||
import io.reactivex.Completable | ||
import io.reactivex.Flowable | ||
import io.reactivex.Single | ||
import javax.inject.Inject | ||
|
||
class GalwayBusCacheImpl @Inject constructor(val galwayBusDatabase: GalwayBusDatabase, | ||
private val preferencesHelper: PreferencesHelper) : GalwayBusCache { | ||
|
||
private val EXPIRATION_TIME = (60 * 10 * 1000).toLong() | ||
|
||
|
||
/** | ||
* Retrieve an instance from the database, used for tests. | ||
*/ | ||
internal fun getDatabase(): GalwayBusDatabase { | ||
return galwayBusDatabase | ||
} | ||
|
||
|
||
override fun saveBusRoutes(busRoutes: List<BusRoute>): Completable { | ||
return Completable.defer { | ||
busRoutes.forEach { | ||
galwayBusDatabase.galwayBusDao().insertBusRoute(it) | ||
} | ||
Completable.complete() | ||
} | ||
} | ||
|
||
override fun getBusRoutes(): Flowable<List<BusRoute>> { | ||
return Flowable.defer { | ||
Flowable.just(galwayBusDatabase.galwayBusDao().getBusRoutes()) | ||
} | ||
} | ||
|
||
override fun clearBusRoutes(): Completable { | ||
return Completable.defer { | ||
galwayBusDatabase.galwayBusDao().clearBusRoutes() | ||
Completable.complete() | ||
} | ||
} | ||
|
||
|
||
override fun isCached(): Single<Boolean> { | ||
return Single.defer { | ||
Single.just(galwayBusDatabase.galwayBusDao().getBusRoutes().isNotEmpty()) | ||
} | ||
} | ||
|
||
override fun setLastCacheTime(lastCache: Long) { | ||
preferencesHelper.lastCacheTime = lastCache | ||
} | ||
|
||
override fun isExpired(): Boolean { | ||
val currentTime = System.currentTimeMillis() | ||
val lastUpdateTime = this.getLastCacheUpdateTimeMillis() | ||
return currentTime - lastUpdateTime > EXPIRATION_TIME | ||
} | ||
|
||
/** | ||
* Get in millis, the last time the cache was accessed. | ||
*/ | ||
private fun getLastCacheUpdateTimeMillis(): Long { | ||
return preferencesHelper.lastCacheTime | ||
} | ||
|
||
|
||
|
||
} |
35 changes: 35 additions & 0 deletions
35
app/src/main/java/com/surrus/galwaybus/cache/PreferenceHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.surrus.galwaybus.cache | ||
|
||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
|
||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
/** | ||
* General Preferences Helper class, used for storing preference values using the Preference API | ||
*/ | ||
@Singleton | ||
open class PreferencesHelper @Inject constructor(context: Context) { | ||
|
||
companion object { | ||
private val PREF_BUFFER_PACKAGE_NAME = "com.surrus.galwaybus.preferences" | ||
|
||
private val PREF_KEY_LAST_CACHE = "last_cache" | ||
} | ||
|
||
private val bufferPref: SharedPreferences | ||
|
||
init { | ||
bufferPref = context.getSharedPreferences(PREF_BUFFER_PACKAGE_NAME, Context.MODE_PRIVATE) | ||
} | ||
|
||
/** | ||
* Store and retrieve the last time data was cached | ||
*/ | ||
var lastCacheTime: Long | ||
get() = bufferPref.getLong(PREF_KEY_LAST_CACHE, 0) | ||
set(lastCache) = bufferPref.edit().putLong(PREF_KEY_LAST_CACHE, lastCache).apply() | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
app/src/main/java/com/surrus/galwaybus/cache/dao/GalwayBusDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.surrus.galwaybus.cache.dao | ||
|
||
import android.arch.persistence.room.Dao | ||
import android.arch.persistence.room.Insert | ||
import android.arch.persistence.room.OnConflictStrategy | ||
import android.arch.persistence.room.Query | ||
import com.surrus.galwaybus.cache.db.GalwayBusDatabaseConstants | ||
import com.surrus.galwaybus.model.BusRoute | ||
|
||
@Dao | ||
abstract class GalwayBusDao { | ||
|
||
@Query(GalwayBusDatabaseConstants.QUERY_BUS_ROUTES) | ||
abstract fun getBusRoutes(): List<BusRoute> | ||
|
||
@Query(GalwayBusDatabaseConstants.DELETE_ALL_BUS_ROUTES) | ||
abstract fun clearBusRoutes() | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
abstract fun insertBusRoute(busRoute: BusRoute) | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
app/src/main/java/com/surrus/galwaybus/cache/db/GalwayBusDatabase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.surrus.galwaybus.cache.db | ||
|
||
import android.arch.persistence.room.Database | ||
import android.arch.persistence.room.RoomDatabase | ||
import com.surrus.galwaybus.cache.dao.GalwayBusDao | ||
import com.surrus.galwaybus.model.BusRoute | ||
import javax.inject.Inject | ||
|
||
@Database(entities = arrayOf(BusRoute::class), version = 1) | ||
abstract class GalwayBusDatabase : RoomDatabase() { | ||
|
||
abstract fun galwayBusDao(): GalwayBusDao | ||
} |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/surrus/galwaybus/cache/db/GalwayBusDatabaseConstants.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.surrus.galwaybus.cache.db | ||
|
||
object GalwayBusDatabaseConstants { | ||
|
||
const val BUS_ROUTES_TABLE_NAME = "bus_routes" | ||
const val QUERY_BUS_ROUTES = "SELECT * FROM" + " " + BUS_ROUTES_TABLE_NAME | ||
const val DELETE_ALL_BUS_ROUTES = "DELETE FROM" + " " + BUS_ROUTES_TABLE_NAME | ||
} |
Oops, something went wrong.