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
| private val lock = Mutex() // #1 | |
| private var cachedAccount: Account? = null | |
| suspend fun getAccount(): Account? { | |
| return cachedAccount ?: mutex.withLock { // #2, #3, #6 | |
| cachedAccount ?: withContext(Dispatchers.IO) { // #4, #5, #6 | |
| val networkModel = ... // perform network query and response parsing | |
| cachedAccout = networkModel.mapToAccountDomain() | |
| cachedAccount | |
| } |
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
| private val _loginState = MutableStateFlow<Boolean>(false) | |
| val loginStateFlow: StateFlow<Boolean> = _loginState.asStateFlow() | |
| fun loggedIn() { | |
| _loginState.value = true | |
| } | |
| fun loggedOut() { | |
| _loginState.value = false | |
| } |
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
| val backdropState = rememberBackdropScaffoldState(Revealed) | |
| var offset by (backdropState.offset as MutableState) | |
| // Optional conditions: ex. should-bounce flag, data-loading finished, etc. | |
| if (backdropState.isRevealed) { | |
| // remember the original offset position to go back to | |
| val revealedOffset = remember { backdropState.offset.value } | |
| // value holder Animatable for the actual offset | |
| val offsetAnimatable = remember { Animatable(revealedOffset) } | |
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
| val listState = rememberLazyListState() | |
| // Horizontal List | |
| LazyRow(state = listState) { ... } | |
| // Vertical List | |
| LazyColumn(state = listState) { ... } |
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
| @Composable | |
| fun FunctionName(input: T) { | |
| ... | |
| } |
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
| val items: List<Room> = rooms | |
| val listState = rememberLazyListState() | |
| LazyColumn(state = listState) { | |
| items(items = items) { item -> RoomItem(item) } | |
| } | |
| @Composable | |
| fun RoomItem(item: Room) { | |
| Text(...) |
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
| val rooms: List<Room> = rooms | |
| val recyclerViewAdapter = CustomAdapter(rooms) | |
| val recyclerView: RecyclerView = findViewById(R.id.recycler_view) | |
| recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false) | |
| recyclerView.adapter = recyclerViewAdapter |
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 CustomAdapter(private val dataSet: Array<String>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() { | |
| class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { | |
| val textView: TextView | |
| init { | |
| textView = view.findViewById(R.id.textView) | |
| } | |
| } | |
| override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder { | |
| val view = LayoutInflater.from(viewGroup.context) |
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
| <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| ...> | |
| <TextView | |
| android:id="@+id/textView" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:text="@string/element_text"/> | |
| </FrameLayout> |
NewerOlder