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
override fun clear(): Completable { | |
return prefSubject.firstOrError() | |
.clearSharedPreferences { | |
remove(KEY_NAME) | |
} | |
} | |
fun Single<SharedPreferences>.clearSharedPreferences(batch: SharedPreferences.Editor.() -> Unit): Completable = | |
flatMapCompletable { | |
Completable.fromAction { |
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
override fun name(): Observable<String> = prefSubject | |
.map { it.getString(KEY_NAME, "") } |
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
override fun saveName(name: String): Completable = prefSubject | |
.firstOrError() | |
.editSharedPreferences { | |
putString(KEY_NAME, name) | |
} | |
fun Single<SharedPreferences>.editSharedPreferences(batch: SharedPreferences.Editor.() -> Unit): Completable = | |
flatMapCompletable { | |
Completable.fromAction { | |
it.edit().also(batch).apply() |
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
private val prefSubject = BehaviorSubject.createDefault(preferences) | |
private val prefChangeListener = SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, _ -> | |
prefSubject.onNext(sharedPreferences) | |
} | |
init { | |
preferences.registerOnSharedPreferenceChangeListener(prefChangeListener) | |
} |
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
companion object { | |
@JvmStatic | |
fun create(context: Context): SharedPreferencesNameRepository { | |
val preferences = context.getSharedPreferences("RxPrefs", Context.MODE_PRIVATE) | |
return SharedPreferencesNameRepository(preferences) | |
} | |
private const val KEY_NAME = "key_name" |
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
class SharedPreferencesNameRepository(preferences: SharedPreferences) : NameRepository { | |
private val prefSubject = BehaviorSubject.createDefault(preferences) | |
private val prefChangeListener = SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, _ -> | |
prefSubject.onNext(sharedPreferences) | |
} | |
companion object { | |
@JvmStatic |
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
interface NameRepository { | |
fun saveName(name: String): Completable | |
fun name(): Observable<String> | |
fun clear(): Completable | |
} |
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
private fun handleData() { | |
bSubmit.setOnClickListener { | |
val name = etName.text.toString() | |
disposables.add(repository.saveName(name).subscribe()) | |
} | |
disposables.add(repository.name().subscribe({ | |
tvName.text = it | |
}, { | |
Log.e("Error : ", "", it) |
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
private lateinit var repository: NameRepository | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
repository = SharedPreferencesNameRepository.create(this) | |
handleData() | |
} |
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
package com.newsuk.analytics | |
import android.app.Application | |
import android.os.Bundle | |
import com.krux.androidsdk.aggregator.KruxEventAggregator | |
import com.newsuk.rx.SchedulingStrategy | |
import com.newsuk.thesun.mobile.BuildConfig | |
import com.newsuk.thesun.mobile.advert.DeviceIdProvider | |
interface KruxConsentProvider { |
NewerOlder