Pass Data Between Fragments

This is my note for Udemy lesson: Advanced Android Bootcamp 2024. First, check my previous note Then in HomeFragment, do: val bundle = bundleOf("user_name" to binding.editTextText.text.toString()) it.findNavController().navigate(R.id.actio…

Enum Class書き方

enum class Trend { INCREASING, DECREASING, PEAK }

Use Data Binding In Fragment

This is my note for Udemy lesson: Advanced Android Bootcamp 2024. Check my previous note: Simple Data Binding Step 1 and 2 are the same. In step3, we need to write it in a different way. Before: class BlankFragment : Fragment() { override …

Navigation Between Fragments

This is my note for Udemy lesson: Advanced Android Bootcamp 2024. 1. Add Dependencies Reference In project layer's build.gradle.kts: buildscript { ... dependencies { val nav_version = "2.7.7" classpath("androidx.navigation:navigation-safe-…

2 Way Binding between ViewModel and xml Components

This is my note for Udemy lesson: Advanced Android Bootcamp 2024. 1. Enable xml to Access to ViewModel see this note 2. Edit ViewModel class MainActivityViewModel: ViewModel() { val userName = MutableLiveData("Frank") } 3. In xml, When Dis…

Using LiveData ViewModel in xml

This is my note for Udemy lesson: Advanced Android Bootcamp 2024. 1. Set up Data Binding See this note 2. Set up ViewModel See this note 3. Edit ViewModel class MainActivityViewModel: ViewModel() { private val _count = MutableLiveData(0) v…

LiveData (Which is similar to ObservableObject in SwiftUI)

This is my note for Udemy lesson: Advanced Android Bootcamp 2024. 1. Create ViewModel Please check the previous note. 2. Edit ViewModel class MainActivityViewModel: ViewModel() { private val _count = MutableLiveData(0) val count: LiveData<Int> </int>…

ViewModel

This is my note for Udemy lesson: Advanced Android Bootcamp 2024. 1. Add Dependencies In app/build.gradle, add the following dependencies: dependencies { def lifecycle_version = "2.7.0" implementation "androidx.lifecycle:lifecycle-viewmode…

Pass object from Activity to xml

This is my note for Udemy lesson: Advanced Android Bootcamp 2024. 1. Set up Data Binding Please see my previous note. 2. Edit the xml File <layout ...> <data> <variable name="student" type="com.xxx.xxx.Student" /> </data> <androidx.constraintlayout.widget.ConstraintLayout ...> ... </androidx.constraintlayout.widget.constraintlayout></layout>

Simple Data Binding

1. In app/build.gradle, add the following code: android { ... dataBinding { enabled = true } } Also it can be android { ... buildFeatures { dataBinding = true } } 2. Add an extra layout in layout xml file. Before: <androidx.constraintlayout.widget.ConstraintLayout xmlns:... android:...> ... </androidx.constraintlayout.widget.constraintlayout>

カスとマイズerrorのlocalizedDescription

enum CustomizedError: LocalizedError { case unexpectedError case sessionNotExist var errorDescription: String? { switch self { case .unexpectedError: return "unexpected error" case .sessionNotExist: return "session not exist" } } }

現在のthreadはmain threadかのチェック

Thread.isMainThread

sortedWith

data class CustomDate( val year: Int, val month: Int, val day: Int ) fun main() { var custumDates = listOf( CustomDate(2023, 1, 1), CustomDate(2023, 1, 2), CustomDate(2023, 2, 1), CustomDate(2022, 1, 1), CustomDate(2022, 1, 2), CustomDate(…

SwiftUI新しいdismiss書き方

以前の書き方: 定義: @Environment(\.presentationMode) private var presentationMode 使用: presentationMode.wrappedValue.dismiss() 現在の書き方: 定義: @Environment(\.dismiss) private var dismiss 使用: dismiss()

compareValuesByを使ってもっと簡単にComparableを実装する

data class CustomDate( val year: Int, val month: Int, val day: Int ) : Comparable<CustomDate> { override fun compareTo(other: CustomDate): Int { return compareValuesBy(this, other, CustomDate::year, CustomDate::month, CustomDate::day) } override fun t</customdate>…

Comparableを実装する

data class CustomDate( val year: Int, val month: Int, val day: Int ) : Comparable<CustomDate> { override fun compareTo(other: CustomDate): Int { if (year == other.year) { if (month == other.month) { return day - other.day } else { return month - other</customdate>…

一部cellだけのheight指定する

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if 条件 { return 高さ } else { return UITableView.automaticDimension } }

getOrPut

val paths = mutableMapOf<Int, MutableSet<Int>>() ... paths[a]?.let { it.add(b) } ?: run { paths[a] = mutableSetOf(b) } もしgetOrPutを使うと、下記のようにすごく簡単になる val paths = mutableMapOf<Int, MutableSet<Int>>() ... paths.getOrPut(a) { mutableSetOf() }.add(b)</int,></int,>

特定ViewControllerのダークモード無効化(もしくはライトモード無効化)

override func viewDidLoad() { ... self.overrideUserInterfaceStyle = .light } 上記コードだと、該当ViewControllerはずっとライトモードになる。逆に= .darkにするとずっとダークモードになる。

ラベル付きループ

Kotlin: outer@for (i in 1..5) { for (j in 1..5) { println("i: $i, j: $j") if (j == 3) { continue@outer } } } Swift: outer: for i in 1...5 { for j in 1...5 { print("i: \(i), j: \(j)") if j == 3 { continue outer } } }

連続アニメーション

下記の例は、先に画像を2倍に拡大して、次左に90度回転して、更に右に180度回転して、最後元に戻るアニメーションです。 let scaleTransform = CGAffineTransform(scaleX: 2, y: 2) let leftRotateTransform = scaleTransform.concatenating(CGAffineT…

Kotlinのfor

1, 2, 3, 4, 5: for (i in 1..5) 1, 3, 5: for (i in 1..5 step 2) 1, 2, 3, 4: for (i in 1 until 5) 5, 4, 3, 2, 1: for (i in 5 downTo 1) 5, 3, 1: for (i in 5 downTo 1 step 2)

ArrayDeque

もしリストの先頭または末尾だけ操作する場合、ArrayDequeを使え!

Kotlin enumerated

val fruits = listOf("apple", "banana", "pear") fruits.forEachIndexed { index, fruit -> println(index) println(fruit) }

Priority Queue

Priority Queueはelementを追加時自動的にソートできる便利なコレクションである。一部のLeetCode問題ではPriority Queueを使わないとタイムアウトが発生する。(e.g. 問題502、問題1834、問題1962) これからPriority Queueの使い方を紹介します…

Font Weightの数値とその名前

100: ultraLight 200: thin 300: light 400: regular 500: medium 600: semibold 700: bold 800: heavy 900: black

ViewControllerに検索機能追加

class ViewController: UIViewController { let searchController = UISearchController() override func viewDidLoad() { super.viewDidLoad() searchController.searchResultsUpdater = self navigationItem.searchController = searchController } } exte…

Reduceの小技

let a = [1, 2, 3] let sum = a.reduce(0) { $0 + $1 } は更に簡略できます: let a = [1, 2, 3] let sum = a.reduce(0, +)

ダークモード無効化

Info.plistにキー"Appearance"とバリュー"Light"を追加する。

UIDocumentPickerViewControllerの'init(documentTypes:in:)' was deprecated in iOS 14.0 解決策

元々: let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.json"], in: .import) 現在: let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: [.json])