1 Star 1 Fork 0

shizhen.zhang / android-mvvm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 6.61 KB
一键复制 编辑 原始数据 按行查看 历史
shizhen.zhang 提交于 2020-09-08 10:44 . initial commit

Android MVVM Architecture Example

This app is purely implemented using Kotlin.

This app is to test DNS servers and figure out the best / fastest DNS server (https://www.techradar.com/sg/news/best-dns-server)

About application architecture

  1. Using MVVM architecture + Repository pattern based on Android architectural components, i.e. Room, LiveData, ViewModel.
  2. Web service is based on Retrofit library.
  3. Database acts as the single source of truth for updating UI widgets, i.e. refresh button and location list.
  4. Manual dependency injection is adopted using InjectorUtils as it can achieve my goals more easily.

About testing strategy

  • Separate tests into unit tests and instrumentation tests.

    • Dao tests and webservice api tests are purely done in unit tests.
    • UI tests are done in Android instrumented tests.
  • Custom test runner created for Retrofit to inject testing end point instead of using real end point.

  • For manual tests

    • Switch off your device network, an error message “Network call has failed for a following reason: xxxxx”
    • Click on the refresh button (left side button), each click will trigger an web service API call and refresh the list and button. The Arophix mock server will always return the same set of XML data, in order to demonstrate that the server data changes of each API call, I intercept the ButtonText by appending a UUID string and display it on the button per each click. Please forgive the urgly REFRESH button.

Steps to build, test and install

  1. Build apk

    ./gradlew clean
    ./gradlew assembleDebug
  2. Auto test

    ./gradlew testDebugUnitTest
    ./gradlew cAT --info
  3. Install apk to device

    ./gradlew installDebug
  4. Manual test fresh installation (with network connected) (Already covered in auto test)

    • shall see two buttons with "REFRESH" and "SHOW BEST LOCATION"
    • Wait for while, the DNS location list shall disaply on screen.
  5. Manual test fresh installation (with network disabled) (Already covered in auto test)

    • Fresh installation, shall see two buttons with "REFRESH" and "SHOW BEST LOCATION"
    • Network error shall display in snackbar message, i.e. "Unable to resolve host xxxx"
    • DNS list shall be empty.
  6. Manual test with existing installation (Already covered in auto test)

    • Click on the button text multiple times, each click will trigger one API call and refresh the button text each time.

Screenshots

Under folder screenshots, you can find some screenshots for cases of

  • A list of successfully loaded DNS locations

    DNS list

  • Best location dialog

    Best_dns_location_dialog

  • Instrumentation test result

    instrumentation_test

  • Unit test result

    unit_test

Other UI behaviors can be oberserved from auto test run

Libraries Used

  • Foundation - Components for core system capabilities, Kotlin extensions and support for multidex and automated testing.

    • AppCompat - Degrade gracefully on older versions of Android.
    • Android KTX - Write more concise, idiomatic Kotlin code.
    • Test - An Android testing framework for unit and runtime UI tests.
  • Architecture - A collection of libraries that help you design robust, testable, and maintainable apps. Start with classes for managing your UI component lifecycle and handling data persistence.

    • Data Binding - Declaratively bind observable data to UI elements.
    • Lifecycles - Create a UI that automatically responds to lifecycle events.
    • LiveData - Build data objects that notify views when the underlying database changes.
    • Room - Access your app's SQLite database with in-app objects and compile-time checks.
    • ViewModel - Store UI-related data that isn't destroyed on app rotations. Easily schedule asynchronous tasks for optimal execution.
    • WorkManager - Manage your Android background jobs.
  • UI - Details on why and how to use UI Components in your apps - together or separate

  • Third party

    • Kotlin Coroutines for managing background threads with simplified code and reducing needs for callbacks

Misc

A MockWebServer bug was found during my implementation and testing. I asked one question java.lang.IllegalArgumentException: Log tag “okhttp3.mockwebserver.MockWebServer” exceeds limit of 23 characters in stackoverflow. The bug was confirmed by the author of MockWebServer.

I have provided my own fix to the bug which unblocked myself from completing the tests.

Pull-request 6229 for bug fix has been approved and merged with Square Okhttp master branch.

TODOS

  • Setup Go lang server (or python server? or nodejs? or J2EE?)
  • Add DNS details screen
  • Implement details screen using Android Compose
  • Implement Navigation controller
  • Add flow
  • Add page
  • Add Dagger/Hilt
  • Implement simple best location based on distance from current location
  • Add notifications when best location is computed.
Kotlin
1
https://gitee.com/shizhenzhang/android-mvvm.git
git@gitee.com:shizhenzhang/android-mvvm.git
shizhenzhang
android-mvvm
android-mvvm
master

搜索帮助