Skip to content

Commit

Permalink
Feature - Interfaces (#4)
Browse files Browse the repository at this point in the history
- Initializer interface
- Detector Interface
  • Loading branch information
quentin41500 authored Nov 26, 2018
1 parent 1f9c720 commit 09526fc
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android:
components:
- tools
- platform-tools
- build-tools-27.0.3
- build-tools-28.0.3
- android-28
- extra-android-m2repository
- extra-google-m2repository
Expand Down
13 changes: 11 additions & 2 deletions app/src/main/java/com/prolificinteractive/sample/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.prolificinteractive.sample

import com.prolificinteractive.birdo.BirdoApp
import com.prolificinteractive.birdo.BirdoInitializer
import com.prolificinteractive.birdo.Initializer
import com.prolificinteractive.birdo.ShakerDetector
import com.prolificinteractive.sample.api.ApiClient

Expand All @@ -11,10 +12,18 @@ class App : BirdoApp() {
override fun onCreate() {
super.onCreate()


client = ApiClient(this)

// --- Debug ---
val initializer: Initializer = BirdoInitializer(this, MyBirdoActivity::class.java)
val detector = ShakerDetector(this, initializer)

detector.detect()

ShakerDetector(this, BirdoInitializer(this, MyBirdoActivity::class.java))
// --- Release ---
// val noOpInitializer: Initializer = NoOpInitializer()
// val noOpShakerDetector: ShakerDetector = NoOpShakerDetector(this, noOpInitializer)
//
// noOpShakerDetector.detect()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.prolificinteractive.sample.noop

import android.content.Context
import com.prolificinteractive.birdo.Initializer

class NoOpInitializer : Initializer {
override fun start(context: Context) {
// No Op.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.prolificinteractive.sample.noop

import android.content.Context
import com.prolificinteractive.birdo.Initializer
import com.prolificinteractive.birdo.ShakerDetector

/**
* One of the detector for starting Birdo.
*
* It will listen to shakes from the user and start Birdo upon shake. This listener needs to be
* initialized in the application class of your app.
*/
class NoOpShakerDetector(
context: Context,
initializer: Initializer
) : ShakerDetector(context, initializer) {

/**
* Start the detector.
*
*/
override fun detect() {
// No Op
}
}
2 changes: 1 addition & 1 deletion birdo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ dependencies {
implementation 'io.palaima.debugdrawer:debugdrawer-timber:0.8.0'

implementation 'com.jakewharton:process-phoenix:2.0.0'
implementation 'com.squareup:seismic:1.0.2'
implementation 'com.google.android.gms:play-services-location:16.0.0'
implementation 'com.jakewharton.timber:timber:4.7.1'
api 'com.squareup:seismic:1.0.2'

compileOnly 'com.squareup.okhttp3:okhttp:3.11.0'
compileOnly "com.squareup.picasso:picasso:2.71828"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.prolificinteractive.birdo.utils.PREFS_FILE_NAME
class BirdoInitializer(
context: Context,
private val name: Class<out BirdoActivity> = BirdoActivity::class.java
) {
) : Initializer {

init {
val prefs = context.getSharedPreferences(PREFS_FILE_NAME, 0)
Expand All @@ -22,7 +22,7 @@ class BirdoInitializer(
/**
* Start Birdo by calling this method. Birdo will only start if it's not currently launched.
*/
fun start(context: Context) {
override fun start(context: Context) {
val prefs = context.getSharedPreferences(PREFS_FILE_NAME, 0)
if (!prefs.getBoolean(IS_BIRDO_ACTIVE, false)) {
prefs.edit().putBoolean(IS_BIRDO_ACTIVE, true).apply()
Expand Down
6 changes: 6 additions & 0 deletions birdo/src/main/java/com/prolificinteractive/birdo/Detector.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.prolificinteractive.birdo

/**
* Abstract class for detector, implement this when creating your own.
*/
abstract class Detector(val initializer: Initializer)
13 changes: 13 additions & 0 deletions birdo/src/main/java/com/prolificinteractive/birdo/Initializer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.prolificinteractive.birdo

import android.content.Context

/**
* Interface for initializer providing start method, implement this when creating your own.
*/
interface Initializer {
/**
* Start method for your debug view.
*/
fun start(context: Context)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ import android.hardware.SensorManager
import com.squareup.seismic.ShakeDetector

/**
* One of the detector for starting Birdo. It will listen to shakes from the user and start Birdo
* upon shake. This listener needs to be initialized in the application class of your app.
* One of the detector for starting Birdo.
*
* It will listen to shakes from the user and start Birdo upon shake. This listener needs to be
* initialized in the application class of your app.
*/
class ShakerDetector(
open class ShakerDetector(
private val context: Context,
private val initializer: BirdoInitializer
) : ShakeDetector.Listener {
init {
val sd = ShakeDetector(this)
sd.start(context.getSystemService(SENSOR_SERVICE) as SensorManager?)
initializer: Initializer
) : Detector(initializer), ShakeDetector.Listener {

/**
* Start the detector.
*/
open fun detect() {
ShakeDetector(this).start(context.getSystemService(SENSOR_SERVICE) as SensorManager?)
}

override fun hearShake() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import android.view.KeyEvent
* It will listen to 3 times {@link KeyEvent.KEYCODE_VOLUME_DOWN} from the user and start Birdo.
* This listener needs to be initialized in your base activity to be accessible from anywhere.
*/
class VolumeDownDetector(private val initializer: BirdoInitializer) : KeyUpDetector {
class VolumeDownDetector(initializer: Initializer) : Detector(initializer), KeyUpDetector {

private var index: Int = 0

Expand Down

0 comments on commit 09526fc

Please sign in to comment.