Skip to content

Instantly share code, notes, and snippets.

View mariusz-blaszczak's full-sized avatar

Mariusz Błaszczak mariusz-blaszczak

View GitHub Profile
class DoorsStateController < ApplicationController
def toggle
event = Doors.toggle
render json: {
status: 'success',
body: event
}
end
def read
module Firebase
class PushToAndroid
SERVER_KEY = ENV.fetch('FIREBASE_APP_SERVER_KEY')
def call
registration_ids = User.all.map(&:registration_id)
options = { data: { state: Doors.last_event.attributes }, collapse_key: 'updated_score' }
fcm = FCM.new(SERVER_KEY)
fcm.send(registration_ids, options)
end
end
module Slack
class MessageReceivedEventHandler
attr_reader :message
def initialize(message)
@message = message
end
def handle
if message_implicate_that_doors_were_just_opened?
Doors.open!
<receiver android:name=".widget.LockWidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/appwidget_info"/>
</receiver>
open class LockWidgetProvider : AppWidgetProvider() {
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
for (id in appWidgetIds) {
appWidgetManager.updateAppWidget(id, createLockRemoteViews(context, AppCache.lockState))
}
}
override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)
class LockWidgetJobService : JobIntentService() {
companion object {
...
private val SUPPORTED_ACTIONS = listOf(ACTION_TOGGLE, ACTION_REFRESH)
fun handleAction(context: Context, action: String?) {
if (!canHandle(action)) return
class ApiClient private constructor(context: Context) {
val apiService = createApiService(context)
private fun createApiService(context: Context): ApiService {
return Retrofit.Builder()
.baseUrl(context.getString(R.string.api_base_url))
.addConverterFactory(createConverterFactory())
.client(createHttpClient(context))
.build()
<service
android:name=".push.AppMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
class AppMessagingService : FirebaseMessagingService() {
override fun onNewToken(token: String?) {
val registrationId = token ?: return
val userId = UUIDProvider.provide(this) // described later
// request push registration
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
object AppCache {
var lockState: LockState? = null
set(value) {
if (value == null) return
if (field == null || field!!.created_at.before(value.created_at)) {
field = value
}
}