This file contains hidden or 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
/** | |
* Example: | |
* var li = ListNode(5) | |
* var v = li.`val` | |
* Definition for singly-linked list. | |
* class ListNode(var `val`: Int) { | |
* var next: ListNode? = null | |
* } | |
*/ | |
class Solution { |
This file contains hidden or 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
//Solves the problem in O(n) time complexity | |
class Solution { | |
fun twoSum(nums: IntArray, target: Int): IntArray { | |
//initialize an empty HashMap | |
val map = hashMapOf<Int, Int>() | |
//and for every element in the array, if it exists in the Map, | |
//check if its complement exists a in the Map or not. If the complement exists then return the indices | |
//of the current element and the complement. | |
//Otherwise, put the element in the Map, and move to the next iteration. | |
for(i in 0..nums.size - 1){ |
This file contains hidden or 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
<style name="checkable_chip" parent="Widget.MaterialComponents.Chip.Choice"> | |
<item name="android:checkable">true</item> | |
<item name="android:textColor">@color/chip_colors_text</item> | |
<item name="chipBackgroundColor">@color/chip_colors</item> | |
<item name="checkedIcon">@null</item> | |
<item name="chipIconVisible">false</item> | |
<item name="chipStrokeColor">@color/purple</item> | |
<item name="chipStrokeWidth">1dp</item> | |
<item name="ensureMinTouchTargetSize">true</item> | |
<item name="checkedIconVisible">false</item> |
This file contains hidden or 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
<com.google.android.material.chip.ChipGroup | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_marginBottom="15dp" | |
app:singleLine="true" | |
android:id="@+id/chip_group" | |
app:singleSelection="true"> | |
<!-- app:checkedChip="@id/yes"--> |
This file contains hidden or 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
// Preference DataStore | |
implementation "androidx.datastore:datastore-preferences:1.0.0-alpha04" |
This file contains hidden or 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 ViewModelFactory(private val apiHelper: ApiHelper) : ViewModelProvider.Factory { | |
override fun <T : ViewModel?> create(modelClass: Class<T>): T { | |
if (modelClass.isAssignableFrom(MainViewModel::class.java)) { | |
return MainViewModel(MainRepository(apiHelper)) as T | |
} | |
throw IllegalArgumentException("Unknown class name") | |
} | |
} |
This file contains hidden or 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 MainViewModel(private val mainRepository: MainRepository) : ViewModel() { | |
fun getSuperHeroes() = liveData(Dispatchers.IO) { | |
emit(Resource.loading(data = null)) | |
try { | |
emit(Resource.success(data = mainRepository.getSuperHeroes())) | |
} catch (exception: Exception) { | |
emit(Resource.error(data = null, message = exception.message ?: "Error Occurred!")) | |
} | |
} |
This file contains hidden or 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 MainActivity : AppCompatActivity() { | |
private lateinit var viewModel: MainViewModel | |
private lateinit var adapter: MainAdapter | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
This file contains hidden or 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 MainAdapter(private val result: ArrayList<Hero>, | |
private val clickListener: (Long) -> Unit): | |
RecyclerView.Adapter<MainAdapter.DataViewHolder>() { | |
class DataViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | |
fun bind(hero: Hero, clickListener: (Long) -> Unit ) { | |
itemView.apply { | |
superheroName.text = hero.name | |
Glide.with(superheroImage.context) | |
.load(hero.image.lg) |
This file contains hidden or 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
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:card_view="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content"> | |
<androidx.cardview.widget.CardView | |
android:id="@+id/card_view" |