-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into bitmap-extension
- Loading branch information
Showing
49 changed files
with
393 additions
and
2 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
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,7 @@ | ||
val list = arrayListOf<Double>(2.0, 3.0, 7.0) | ||
var sum = 0 | ||
list.forEach{ | ||
sum += it.toInt() | ||
} | ||
println(sum/list.size) // 4 | ||
println(list.average().toInt()) // 4 |
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,42 @@ | ||
package katas.`3kyu` | ||
|
||
import java.util.* | ||
import kotlin.coroutines.* | ||
|
||
fun plus(o1: Optional<Int>, o2: Optional<Int>): Optional<Int> = `for` { | ||
val i1: Int = bind(o1) | ||
val i2: Int = bind(o2) | ||
yield(i1 + i2) | ||
} | ||
|
||
suspend fun <T> bind(value: Optional<T>): T { | ||
return suspendCoroutine { cont -> | ||
if (value.isPresent) | ||
cont.resumeWith(Result.success(value.get())) | ||
else | ||
cont.resumeWith(Result.failure(fail())) | ||
} | ||
} | ||
|
||
fun <T> yield(value: T) = Optional.of(value) | ||
|
||
fun <T> `for`(lambda: suspend () -> Optional<T>): Optional<T> { | ||
var ret: Optional<T> = Optional.empty() | ||
val completion: Continuation<Optional<T>> = object : Continuation<Optional<T>> { | ||
override val context: CoroutineContext = EmptyCoroutineContext | ||
|
||
override fun resumeWith(result: Result<Optional<T>>) { | ||
ret = result.getOrElse { | ||
when (it) { | ||
is NoSuchElementException -> Optional.empty() | ||
else -> throw it | ||
} | ||
} | ||
} | ||
} | ||
lambda.startCoroutine(completion) | ||
return ret | ||
} | ||
|
||
fun fail() = Exception() | ||
|
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,30 @@ | ||
// Step 1: Add deep link element inside fragment of navigation graph | ||
<deepLink app:uri="test://fetch/account" /> | ||
|
||
// Step 2: To enable implicit deep linking, Add a single <nav-graph> element to an activity that points to an existing navigation graph | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.example.myapplication"> | ||
|
||
<application ... > | ||
|
||
<activity name=".MainActivity" ...> | ||
... | ||
|
||
<nav-graph android:value="@navigation/nav_graph" /> | ||
|
||
... | ||
|
||
</activity> | ||
</application> | ||
</manifest> | ||
|
||
|
||
// Step 3 (Optional): Also you can handle parameters of deep link with placeholder | ||
<deepLink app:uri="test://fetch/account?displayName={display_name}" /> | ||
|
||
// Step 4: To read data from placeholder, you need add an argument below deep link element | ||
<argument android:name="display_name" app:argType="string"/> | ||
|
||
// Step 5: Now you can use deep link placeholder's data in fragment with arguments | ||
val displayName = arguments?.get("display_name") as String |
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,34 @@ | ||
val spanned = spannable { | ||
bold("Sample Bold") + | ||
italic("And Italic Text") + | ||
color(Color.BLUE, "Blue Colored Text") | ||
} | ||
|
||
val nested = spannable{ bold(italic("nested ")) + url("www.google.com", "text") } | ||
|
||
textView.text = spanned + nested | ||
|
||
// Spannable.kt extension for further manipulations | ||
|
||
import android.text.Spannable | ||
import android.text.SpannableString | ||
import android.text.TextUtils | ||
import android.text.style.* | ||
|
||
fun spannable(func: () -> SpannableString) = func() | ||
private fun span(s: CharSequence, o: Any) = (if (s is String) SpannableString(s) else s as? SpannableString | ||
?: SpannableString("")).apply { setSpan(o, 0, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) } | ||
|
||
operator fun SpannableString.plus(s: SpannableString) = SpannableString(TextUtils.concat(this, s)) | ||
operator fun SpannableString.plus(s: String) = SpannableString(TextUtils.concat(this, s)) | ||
|
||
fun bold(s: CharSequence) = span(s, StyleSpan(android.graphics.Typeface.BOLD)) | ||
fun italic(s: CharSequence) = span(s, StyleSpan(android.graphics.Typeface.ITALIC)) | ||
fun underline(s: CharSequence) = span(s, UnderlineSpan()) | ||
fun strike(s: CharSequence) = span(s, StrikethroughSpan()) | ||
fun sup(s: CharSequence) = span(s, SuperscriptSpan()) | ||
fun sub(s: CharSequence) = span(s, SubscriptSpan()) | ||
fun size(size: Float, s: CharSequence) = span(s, RelativeSizeSpan(size)) | ||
fun color(color: Int, s: CharSequence) = span(s, ForegroundColorSpan(color)) | ||
fun background(color: Int, s: CharSequence) = span(s, BackgroundColorSpan(color)) | ||
fun url(url: String, s: CharSequence) = span(s, URLSpan(url)) |
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,21 @@ | ||
// Step 1: Create or retrieve the Master Key for encryption/decryption | ||
val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC) | ||
|
||
// Step 2: Initialize/open an instance of EncryptedSharedPreferences | ||
val sharedPreferences = EncryptedSharedPreferences.create( | ||
"secret_shared_prefs", | ||
masterKeyAlias, | ||
applicationContext, | ||
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, | ||
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM | ||
) | ||
|
||
|
||
// Step 3: Save data to the EncryptedSharedPreferences as usual | ||
sharedPreferences.edit() | ||
.putString("KEY_VALUE", saveText.text.toString()) | ||
.apply() | ||
|
||
|
||
// Step 4: Read data from EncryptedSharedPreferences as usual | ||
val value = sharedPreferences.getString("KEY_VALUE", "") |
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,15 @@ | ||
/* | ||
Your Activity, in the onCreate method, | ||
setContent{ | ||
//call the composable method | ||
setTextIntoTheColumn("TextView Text1", "TextView Text2") | ||
} | ||
*/ | ||
|
||
@Composable | ||
fun setTextIntoTheColumn(messageOne: String, messageTwo: String) { | ||
Column { | ||
Text(text = messageOne) | ||
Text(text = messageTwo) | ||
} | ||
} |
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,46 @@ | ||
// Transformations switchMap and map LiveData-KTX usage | ||
|
||
/* These methods permit functional composition and delegation of LiveData instances. The transformations are calculated | ||
* lazily, and will run only when the returned LiveData is observed. Lifecycle behavior is propagated from the input | ||
* source LiveData to the returned one. - more info.: developer.android.com/reference/androidx/lifecycle/Transformations | ||
*/ | ||
|
||
class DashboardFragmentViewModel @Inject internal constructor(private val currentWeatherUseCase: CurrentWeatherUseCase) : BaseViewModel() { | ||
|
||
private val _currentWeatherParams: MutableLiveData<CurrentWeatherUseCase.CurrentWeatherParams> = MutableLiveData() | ||
fun getCurrentWeatherViewState() = currentWeatherViewState | ||
|
||
private val currentWeatherViewState: LiveData<CurrentWeatherViewState> = _currentWeatherParams.switchMap { params -> | ||
currentWeatherUseCase.execute(params) // When _currentWeatherParams triggered `switchMap` returns useCase.execute() | ||
} | ||
|
||
fun setCurrentWeatherParams(params: CurrentWeatherUseCase.CurrentWeatherParams) { | ||
if (_currentWeatherParams.value == params) | ||
return | ||
_currentWeatherParams.postValue(params) | ||
} | ||
} | ||
|
||
|
||
class CurrentWeatherUseCase @Inject internal constructor(private val repository: CurrentWeatherRepository) : UseCaseLiveData<CurrentWeatherViewState, CurrentWeatherUseCase.CurrentWeatherParams, CurrentWeatherRepository>() { | ||
|
||
override fun buildUseCaseObservable(params: CurrentWeatherParams?): LiveData<CurrentWeatherViewState> { | ||
return repository.loadCurrentWeatherByGeoCords( | ||
params?.lat?.toDouble() ?: 0.0, | ||
params?.lon?.toDouble() ?: 0.0, | ||
params?.fetchRequired | ||
?: false, | ||
units = params?.units ?: Constants.Coords.METRIC | ||
).map { // When loadCurrentWeatherByGeoCords triggered `map` returns onCurrentWeatherResultReady() | ||
onCurrentWeatherResultReady(it) | ||
} | ||
} | ||
|
||
private fun onCurrentWeatherResultReady(resource: Resource<CurrentWeatherEntity>): CurrentWeatherViewState { | ||
return CurrentWeatherViewState( | ||
status = resource.status, | ||
error = resource.message, | ||
data = resource.data | ||
) | ||
} | ||
} |
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,46 @@ | ||
// Reading-Writing lists from/to RoomDB with Moshi | ||
|
||
object DataConverter { | ||
|
||
@TypeConverter | ||
@JvmStatic | ||
fun stringToList(data: String?): List<ListItem>? { | ||
if (data == null) { | ||
return emptyList() | ||
} | ||
|
||
val moshi = Moshi.Builder().build() | ||
val type = Types.newParameterizedType(List::class.java, ListItem::class.java) | ||
val adapter = moshi.adapter<List<ListItem>>(type) | ||
return adapter.fromJson(data) | ||
} | ||
|
||
@TypeConverter | ||
@JvmStatic | ||
fun listToString(objects: List<ListItem>): String { | ||
val moshi = Moshi.Builder().build() | ||
val type = Types.newParameterizedType(List::class.java, ListItem::class.java) | ||
val adapter = moshi.adapter<List<ListItem>>(type) | ||
return adapter.toJson(objects) | ||
} | ||
} | ||
|
||
// -- Don't forget to add your TypeConverter to Database. -- | ||
|
||
@Database( | ||
entities = [ | ||
ForecastEntity::class, | ||
CurrentWeatherEntity::class, | ||
CitiesForSearchEntity::class | ||
], | ||
version = 2 | ||
) | ||
@TypeConverters(DataConverter::class) | ||
abstract class WeatherDatabase : RoomDatabase() { | ||
|
||
abstract fun forecastDao(): ForecastDao | ||
|
||
abstract fun currentWeatherDao(): CurrentWeatherDao | ||
|
||
abstract fun citiesForSearchDao(): CitiesForSearchDao | ||
} |
Oops, something went wrong.