Kotlin で Jetpack Compose, Retrofit を使って API リクエスト、JSON パース して、グリッド表示するコードをファイル別に表示して

Kotlin で Jetpack Compose と Retrofit を使って API リクエスト、JSON パースして、グリッド表示するためには、以下のようにファイルを分けることができます。

 

1. API リクエストを行うための Retrofit インターフェースを定義するファイル


interface ApiService {

    @GET("/photos")
    suspend fun getPhotos(): List<Photo>
}

 

2. API レスポンスを表すデータクラスを定義するファイル


data class Photo(
    val id: Int,
    val title: String,
    val url: String
)

 

3. API レスポンスをパースして、表示するための画面を定義するファイル


@Composable
fun PhotoGrid() {
    val photos by remember { mutableStateOf<List<Photo>>(emptyList()) }

    LaunchedEffect(Unit) {
        val apiService = Retrofit.Builder()
            .baseUrl("https://jsonplaceholder.typicode.com")
            .addConverterFactory(Json {
                ignoreUnknownKeys = true
            }.asConverterFactory("application/json".toMediaType()))
            .build()
            .create(ApiService::class.java)

        photos = apiService.getPhotos()
    }

    LazyVerticalGrid(cells = GridCells.Adaptive(minSize = 128.dp)) {
        items(photos) { photo ->
            Card(
                modifier = Modifier.padding(4.dp),
                shape = RoundedCornerShape(8.dp),
                elevation = 4.dp
            ) {
                Column(
                    Modifier
                        .fillMaxWidth()
                        .height(128.dp)
                        .padding(8.dp)
                ) {
                    Text(
                        text = photo.title,
                        style = MaterialTheme.typography.subtitle1,
                        maxLines = 2,
                        overflow = TextOverflow.Ellipsis
                    )
                    Spacer(modifier = Modifier.weight(1f))
                    CoilImage(
                        data = photo.url,
                        contentScale = ContentScale.Crop,
                        modifier = Modifier.fillMaxSize()
                    )
                }
            }
        }
    }
}

 

4. 上記の画面を表示するための Activity もしくは Fragment を定義するファイル


class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            PhotoGrid()
        }
    }
}

以上のように、ファイルを分けることで、API リクエストやパース、表示を分離し、コードを見やすく、メンテナンスしやすくすることができます。

 

■ 感想

ただ、一行問いかけただけ。

ChatGPT で、ここまで書けるとは。



【Android】Version Catalog + JetpackCompose UI + BOM

Compose の BOM。

【Android】Version Catalog + JetpackCompose UI + BOM
👉 Google's Maven Repository hatena-bookmark

公開されているので使ってみます。

少し公式ドキュメントで調べておきます。

BOM の使用は義務ですか?

いいえ。個々の依存関係バージョンを手動で追加することもできます。しかし、すべての最新の安定版を同時に使用することが容易になるので、BOM を使用することをおすすめします。

Compose Compiler ライブラリが BOM に含まれていないのはなぜですか?

Compose Kotlin コンパイラ拡張機能(androidx.compose.compiler)は、Compose ライブラリのバージョンでなく、Kotlin コンパイラ プラグインのバージョンにリンクされており、Compose の他の部分とは異なる頻度でリリースされます。したがって、お使いの Kotlin バージョンと互換性のあるバージョンを使用してください。

👉 Using the Bill of Materials  |  Jetpack Compose  |  Android Developers hatena-bookmark

公式サンプルコードを少し変更したものを準備しておきます。


# libs.versions.toml

[libraries]
compose-ui-bom = { module = "androidx.compose:compose-bom", version = "2023.01.00" }


// build.gradle

dependencies {
  implementation platform(libs.compose.ui.bom)
}

👉 BOM to library version mapping  |  Jetpack Compose  |  Android Developers hatena-bookmark

 

■ 実装

実装としては、以下でいけました。


# libs.versions.toml

[libraries]

compose-ui-bom = { module = "androidx.compose:compose-bom", version = "2023.01.00"}
compose-material = { module = "androidx.compose.material:material" }
compose-material-icons = { module = "androidx.compose.material:material-icons-extended" }
compose-ui = { module = "androidx.compose.ui:ui" }
compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview" }


// build.gradle

dependencies {

  // compose-ui
  implementation platform(libs.compose.ui.bom)
  implementation libs.compose.material
  implementation libs.compose.material.icons
  implementation libs.compose.ui
  implementation libs.compose.ui.tooling.preview

}

 

■ まとめ

BOM を使っていなかったのですが、エラーが出始めたので、使うことにしました。

「整合性の適正化」と「グループ化」の機能を持っている「BOM」は、Kotlin, Firebase などでも公開されています。

便利に利用するべきでしょう。

しかし、AndroidStudio のコレ。

JetpackCompose UI で BOM

「null」 の表示。

どうにかなりませんかね。

👉 今はまだ必要な Android ライブラリ の Bill of Materials(BOM) のURLs hatena-bookmark


【Git】.gitignore の状態を確認するコマンドたち

git status --ignored

まずは、状態の確認をしましょう。

以下は、基本的な Android プロジェクトでの例。


 

■ 位置を確認する


❯ find . -name .gitignore                          
./app/.gitignore
./.gitignore
./.idea/.gitignore

👉 AndroidStudio 新規プロジェクトテンプレートから考える .gitignore hatena-bookmark

 

■ git status


❯ git status          
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

 

■ git status --ignored


❯ git status --ignored
On branch main
Your branch is up to date with 'origin/main'.

Ignored files:
  (use "git add -f <file>..." to include in what will be committed)
        .gradle/
        .idea/workspace.xml
        app/build/
        local.properties

nothing to commit, working tree clean

→ ignore 対象が確認できます。

 

■ git clean -ndX


❯ git clean -ndX
Would remove .gradle/
Would remove .idea/workspace.xml
Would remove app/build/
Would remove build/
Would remove local.properties

 

■ git check-ignore -v


❯ git check-ignore -v .gradle/           
.gitignore:2:.gradle    .gradle/

❯ git check-ignore -v .idea/workspace.xml
.idea/.gitignore:3:/workspace.xml       .idea/workspace.xml

❯ git check-ignore -v app/build/         
app/.gitignore:1:/build app/build/

❯ git check-ignore -v local.properties
.gitignore:15:local.properties  local.properties

❯ git check-ignore -v **/*
app/.gitignore:1:/build app/build
app/.gitignore:1:/build app/build/generated
app/.gitignore:1:/build app/build/generated/ap_generated_sources
app/.gitignore:1:/build app/build/generated/ap_generated_sources/debug
app/.gitignore:1:/build app/build/generated/ap_generated_sources/debug/out
app/.gitignore:1:/build app/build/generated/res
...

→ そのファイルを ignore するパターン記述を特定することができます。

 

■ git ls-files --other --ignored --exclude-standard


❯ git ls-files --other --ignored --exclude-standard
.gradle/7.5.1/checksums/checksums.lock
.gradle/7.5.1/dependencies-accessors/dependencies-accessors.lock
.gradle/7.5.1/dependencies-accessors/gc.properties
.gradle/7.5.1/executionHistory/executionHistory.bin
.gradle/7.5.1/executionHistory/executionHistory.lock
...

→ 実際に ignore されたファイル一覧が確認できます。Adroid プロジェクトでは出力行が多くなるので省略しています。

 

■ 参考

👉 Git のステータス: リポジトリの検査 | Atlassian Git Tutorial hatena-bookmark


Rename Git branch master to main


❯ git -v
git version 2.39.1

 

■ Delete local branch


git branch -d master

 

■ Rename local branch


git branch -m master main

 

■ Push the new branch, set local branch to track the new remote


git push -u origin main


git push --set-upstream origin main

 

■ Delete remote branch


git push origin :master


git push origin --delete master

I got this.


❯ git push origin :master
To https://github.com/your/project
 ! [remote rejected] master (refusing to delete the current branch: refs/heads/master)
error: failed to push some refs to 'https://github.com/your/project'

Switch default branch master to main at https://github.com/your/project

Rename Git branch master to main

 

■ Show status


❯ git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean

❯ git branch -a
* main
  remotes/origin/main

❯ git branch -l
* main

❯ git branch -r
  origin/main

 

■ Conclusion



Repository default branch
Choose the default branch for your new personal repositories. You might want to change the default name due to different workflows, or because your integrations still require “master” as the default branch name. You can always change the default branch name on individual repositories.


👉 GitHub - Settings - Repositories hatena-bookmark

👉 Managing remote repositories - GitHub Docs hatena-bookmark


Android Studio Electric Eel | 2022.1.1 の JDK11 設定

一番標準的で簡単な Android Studio Electric Eel での JDK設定です。

メニューから About Android Studio で表示される内容は以下。

Build #AI-221.6008.13.2211.9477386, built on January 11, 2023


Android Studio Electric Eel | 2022.1.1
Build #AI-221.6008.13.2211.9477386, built on January 11, 2023
Runtime version: 11.0.15+0-b2043.56-8887301 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 13.1
GC: G1 Young Generation, G1 Old Generation
Memory: 4096M
Cores: 16
Registry:
    external.system.auto.import.disabled=true
    ide.text.editor.with.preview.show.floating.toolbar=false
    documentation.show.toolbar=true

Non-Bundled Plugins:
    org.moe (1.5.2)
    org.jetbrains.compose.desktop.ide (1.2.2)
    com.jetbrains.kmm (0.5.1(221)-61)

表示されてるように Android Studio に同梱されてる JDK は OpenJDK 11.0.15 改 by JetBrains という感じでしょうか。

公式 developers.android.com には以下のように書かれています。


なので、この Android Studio に同梱された JDK を使う設定をします。

メニューから、


Settings...

  ↓

Build, Execution, Deployment

  ↓

Build Tool

  ↓

Gradle

Embedded JDK version 11.0.15 を選択します。

Android Studio Electric Eel | 2022.1.1  での JDK設定

あとは、build.gradle に以下を記述しておきます。


  compileOptions {
    sourceCompatibility JavaVersion.VERSION_11
    targetCompatibility JavaVersion.VERSION_11
  }

  kotlinOptions {
    jvmTarget = JavaVersion.VERSION_11.toString()
  }

以上です。

これが、きっと今現在では最も標準的で簡単です。

👉 【Android Studio】Settings - Build Tools - Gradle JDK の選択肢がゴミだらけで意味不明の場合 hatena-bookmark
👉 AndroidStudio 利用する Java (JDK) の選択・設定の方法 hatena-bookmark