使用 Jetpack Compose 和 HMS 构建瑞克和莫蒂应用程序

关注个人简介,面试不迷路~!

image.png

我们都知道《瑞克和莫蒂》是一部很棒的系列。让我们使用 Jetpack Compose 和华为移动服务构建一个瑞克和莫蒂角色应用程序 今天我们将一起完成这个任务。

首先,让我们将依赖项添加到项目和应用程序 gradle 文件中。

这是应用程序 gradle 文件。

implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.3.1'
    implementation "androidx.compose.ui:ui:$compose_ui_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
    implementation 'androidx.compose.material:material:1.1.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"
    
    // Compose dependencies
    implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.5.0-beta01"
    implementation "androidx.navigation:navigation-compose:2.5.0-beta01"
    implementation "io.coil-kt:coil-compose:2.0.0-rc03"
    implementation "androidx.compose.material:material-icons-extended:$compose_version"

    // Coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1'

    // Retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation "com.squareup.okhttp3:okhttp:5.0.0-alpha.2"
    implementation "com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.2"

    // Dagger - Hilt
    implementation "com.google.dagger:hilt-android:2.38.1"
    kapt "com.google.dagger:hilt-android-compiler:2.38.1"
    kapt "androidx.hilt:hilt-compiler:1.0.0"
    implementation 'androidx.hilt:hilt-navigation-compose:1.0.0'

    // Huawei Ads
    implementation 'com.huawei.hms:ads-prime:3.4.61.302'
    implementation 'com.huawei.hms:ads-omsdk:1.3.35'

    // Account Kit
    implementation 'com.huawei.hms:hwid:6.9.0.301'

    // Auth Service
    implementation 'com.huawei.agconnect:agconnect-core:1.8.1.300'
    implementation 'com.huawei.agconnect:agconnect-auth:1.8.1.300'

    // Push Kit
    implementation 'com.huawei.hms:push:6.9.0.300'

这是项目的 gradle 文件。

buildscript {
    ext {
        compose_ui_version = '1.1.1'
        compose_version = '1.1.1'
    }
    dependencies {

        classpath 'com.android.tools.build:gradle:7.3.1'
        classpath 'com.huawei.agconnect:agcp:1.7.1.300'
    }
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.3.1' apply false
    id 'com.android.library' version '7.3.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
    id 'com.google.dagger.hilt.android' version '2.41' apply false
}

并且不要忘记激活华为移动服务并将 agconnect-services json 文件添加到您的应用程序文件夹中

[图片上传失败...(image-315ecb-1687759723893)]

agconnect-service.json 文件

这是我们的基本 URL

[图片上传失败...(image-199bb8-1687759723893)]

BASE_URL

我创建了 2 个屏幕,其中一个是角色列表屏幕,另一个是角色详细信息屏幕,我为它们创建了 2 个存储库。然后我创建了 2 个存储库实现类并在那里完成了真正的工作。到目前为止,这些都是正常的架构和编码内容。从 API 获取数据并在 recyclerview(lazycolumn) 和一个详细信息屏幕中显示它们。我想通过这个应用程序使用华为移动服务。我使用身份验证服务和帐户套件进行身份验证,使用广告套件进行广告,使用推送套件进行通知。通过 Compose,您会发现使用华为移动服务创建项目就像使用 XML 创建项目一样简单。

首先,从CharactersScreen中的viewmodel调用getCharacters函数

[图片上传失败...(image-62775a-1687759723893)]

人物画面

当 viewmodel 中触发 getCharactersUseCase 时,结果将通过状态显示。

[图片上传失败...(image-91f9fd-1687759723893)]

角色视图模型

在用例方面,GetCharactersUseCase 注入CharactersRepository。这样就可以使用存储库了。我用 invoke 关键字编写了这个函数。所以可以直接用 getCharactersUseCase name 来调用。

[图片上传失败...(image-b41216-1687759723893)]

获取字符用例

存储库方面非常简单。它获取页码并返回 Flow>>

[图片上传失败...(image-9684b1-1687759723893)]

人物库

我已将存储库绑定到存储库实现类。这样我就可以使用它们了。

[图片上传失败...(image-f0f1c1-1687759723893)]

存储库模块

CharactersRepositoryImpl 类注入我们的远程源 RickAndMortyApi 并返回 CharactersRepository。通过这种方式,我覆盖了所有存储库函数。在我们的例子中,我只有 getCharactersRepository 函数。我首先发出加载状态,然后尝试从 api 获取数据。如果成功,则将 dto 对象映射到数据模型类,然后发出成功状态,如果失败,则发出错误状态。就这样。

[图片上传失败...(image-ecb1b5-1687759723893)]

角色库实现

我会将资源代码添加到参考文献中,以便您可以查看该项目。

结论

因此,我使用华为移动服务和 Jetpack Compose 创建了一个简单的 Rick and Morty 应用程序。将 HMS 与 Jetpack Compose 结合使用就像在 XML 项目中使用 HMS 一样简单。

今日分享到此结束,下期更精彩~

你可能感兴趣的:(使用 Jetpack Compose 和 HMS 构建瑞克和莫蒂应用程序)