Skip to content

Instantly share code, notes, and snippets.

View 0xtmphey's full-sized avatar

Tim Plotnikov 0xtmphey

View GitHub Profile
Open App
from home screen -> Is user authorized?
from email or deep link -> Email link type
Root
Logged Out
Is user authorized?
Yes -> Logged In
No -> Splash
@propertyWrapper
struct Lateinit<T> {
private var _value: T?
var wrappedValue: T {
get {
guard let value = _value else {
fatalError("Property being accessed without initialization")
}
return value
class Activity {
lateinit var notificationManager: NotificationManager
override fun onCreate() {
super.onCreate()
// lateinit allows to access property without initialization and check for null
// this will cause a crash
notificationManager.readNotification()
// Inject or instantiate the manager
lateinit var environmentManager: EnvironmentManager
// url address depending on DEFAULT_ENV
val serverAddress = environmentManager.get(EnvironmentVariables::baseUrl)
// change env
environmentManager.currentEnvironment = Environment.PRODUCTION
// Now you got the prod adrress
class EnvironmentManager(
// We use shared prefs to save currently selected env between app launches
private val store: SharedPreferences
) {
// This is a key for saving env in prefs
private val currentEnvironmentKey = "current_environment"
/*
* Here we need to describe all our strings
* for all of the supported environments.
enum class Environment(val id: String) {
DEVELOPMENT("development"),
PRODUCTION("production");
companion object {
fun create(env: String): Environment =
when (env) {
"development" -> DEVELOPMENT
"production" -> PRODUCTION
else -> throw Exception("Unsupported environment $env")
data class EnvironmentVariables(
val baseUrl: String,
val algoliaSearchKey: String
)
android {
buildTypes {
debug {
buildConfigField("String", "BASE_URL", "\"https://api-dev.myproject.com/\"")
}
release {
buildConfigField("String", "BASE_URL", "\"https://api.myproject.com/\"")
}
}
}
import Foundation
let date1Timestamp = Double(1545648417) // This is our date since 1970
let date2Str = "2018-12-07 05:32:54" // This is our date in String
// Here we create a `DateFormatter` which will allow us to parse our String
// Since we don't have a timezone data in our String, we indicate that this String in UTC
let date2Formatter = DateFormatter()
date2Formatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
date2Formatter.timeZone = TimeZone(abbreviation: "UTC")!
fun loadPosts() {
retrofitService.getPosts()
.filter { post -> post.author == me }
.map { post -> createViewablePost(post) }
.onComplete { posts -> view.showData(posts) }
.onError { e -> view.showError(e) }
}