Skip to content

Instantly share code, notes, and snippets.

View fahrican's full-sized avatar

Fahri Can fahrican

View GitHub Profile
@fahrican
fahrican / Challenge.kt
Last active September 28, 2022 22:22
5G Systems Challenge
fun compress(userInput: String): String {
val charactersMap = HashMap<Char, Int>()
val indexWithCharacter = StringBuilder()
for (i in userInput.indices) {
if ((i + 1) != userInput.length && userInput[i] == userInput[i + 1]) {
if (!charactersMap.containsKey(userInput[i])) {
indexWithCharacter.append("$i${userInput[i]}")
charactersMap[userInput[i]] = i
}
} else { // in case character appears only one time in string
characterAdapter.onItemClicked = { character ->
character.let {
if (!character.id.isNullOrBlank()) {
findNavController().navigate(
CharactersListFragmentDirections.navigateToCharacterDetailsFragment(
id = character.id
)
)
}
}
private fun observeLiveData() {
viewModel.character.observe(viewLifecycleOwner) { response ->
when (response) {
is ViewState.Loading -> {
binding.characterDetailsFetchProgress.visibility = View.VISIBLE
binding.characterDetailsNotFound.visibility = View.GONE
}
is ViewState.Success -> {
if (response.value?.data?.character == null) {
binding.characterDetailsFetchProgress.visibility = View.GONE
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_main.xml"
app:startDestination="@id/characters_list_fragment">
<fragment
android:id="@+id/characters_list_fragment"
android:name="com.example.apollographqltutorial.view.ui.CharactersListFragment"
<fragment
android:id="@+id/character_details_fragment"
android:name="com.example.apollographqltutorial.view.ui.CharacterDetailsFragment"
android:label="Character Details"
tools:layout="@layout/fragment_character_details">
<argument
android:name="id"
app:argType="string" />
<fragment
android:id="@+id/characters_list_fragment"
android:name="com.example.apollographqltutorial.view.ui.CharactersListFragment"
android:label="List of all characters"
tools:layout="@layout/fragment_characters_list">
<action
android:id="@+id/navigate_to_character_details_fragment"
app:destination="@id/character_details_fragment" />
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="query"
type="com.example.apollographqltutorial.CharacterQuery.Data" />
fun queryCharacter(id: String) = viewModelScope.launch {
_character.postValue(ViewState.Loading())
try {
val response = repository.queryCharacter(id)
_character.postValue(ViewState.Success(response))
} catch (ae: ApolloException) {
Log.d("ApolloException", "Failure", ae)
_character.postValue(ViewState.Error("Error fetching characters"))
}
}
override suspend fun queryCharacter(id: String): Response<CharacterQuery.Data> {
return webService.getApolloClient().query(CharacterQuery(id)).await()
}
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_main.xml"
app:startDestination="@id/characters_list_fragment">
<fragment
android:id="@+id/characters_list_fragment"
android:name="com.example.apollographqltutorial.view.ui.CharactersListFragment"