說(shuō)明:前面提到了一款美食App的開(kāi)發(fā)醋寝,但是那個(gè)工程由于前期設(shè)計(jì)與準(zhǔn)備簡(jiǎn)單,所以現(xiàn)在從頭開(kāi)始對(duì)這款軟件進(jìn)項(xiàng)完整的設(shè)計(jì)開(kāi)發(fā)绪妹。
軟件功能簡(jiǎn)介
點(diǎn)擊軟件進(jìn)入首頁(yè)甥桂,在app首頁(yè)中,首頁(yè)中有食譜選項(xiàng)菜單邮旷,可以根據(jù)食譜的種類查詢食譜,每個(gè)美食單元都有美食的喜愛(ài)程度以及做該美食需要的時(shí)間蝇摸。點(diǎn)擊某個(gè)美食圖標(biāo)進(jìn)入食物詳情頁(yè)面婶肩,詳情頁(yè)面中有美食的詳細(xì)信息办陷,如食物的簡(jiǎn)介、做法律歼、營(yíng)養(yǎng)價(jià)值民镜、需要的食材。在詳情頁(yè)面中點(diǎn)擊右上角的收藏按鈕可以對(duì)該美食進(jìn)行收藏险毁,然后點(diǎn)擊下方進(jìn)入Favorite頁(yè)面可以查看自己收藏的美食食譜制圈。
下面是demo的實(shí)現(xiàn)效果
制作該demo大概需要用到的知識(shí)點(diǎn)
1、fragment的使用以及navigation設(shè)計(jì)的相關(guān)應(yīng)用
2畔况、recyclerVIew的使用
3鲸鹦、Retrofit
4、單例設(shè)計(jì)模式
5跷跪、MVVM設(shè)計(jì)模式
6馋嗜、viewPager2的使用
7、Glide的了解與使用
8吵瞻、高階函數(shù)回調(diào)數(shù)據(jù)
9葛菇、密封類
10、數(shù)據(jù)可序列化
整個(gè)項(xiàng)目框架
本項(xiàng)目需要的所有依賴
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'androidx.navigation.safeargs.kotlin'
id 'kotlin-kapt'
id 'kotlin-parcelize'
}
//Navigation
def nav_version = "2.3.5"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
//Gson
implementation 'com.google.code.gson:gson:2.8.7'
//retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
//Coroutine
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0'
//ViewModel
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0-alpha02")
implementation "androidx.activity:activity-ktx:1.2.0"
implementation "androidx.fragment:fragment-ktx:1.3.0"
//by viewModels
implementation "androidx.activity:activity-ktx:1.2.0"
implementation "androidx.fragment:fragment-ktx:1.3.0"
//shimmerRecyclerView
implementation'com.facebook.shimmer:shimmer:0.5.0'
implementation'com.todkars:shimmer-recyclerview:0.4.1'
//Glide
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
//room
def room_version = "2.3.0"
implementation("androidx.room:room-runtime:$room_version")
annotationProcessor "androidx.room:room-compiler:$room_version"
implementation("androidx.room:room-ktx:$room_version")
kapt("androidx.room:room-compiler:$room_version")
def lifecycle_version = "2.4.0-alpha02"
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version")
implementation("androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version")
//Jsoup
implementation 'org.jsoup:jsoup:1.13.1'
數(shù)據(jù)的搭建
配置網(wǎng)絡(luò)權(quán)限
進(jìn)入API平臺(tái)獲取數(shù)據(jù)
完成spoonacular的注冊(cè)
根據(jù)需求在網(wǎng)址欄拼接內(nèi)容
https://api.spoonacular.com/recipes/complexSearch?type=main%20course&cuisines=Chinese&addRecipeInformation=true&fillIngredients=true&apiKey=cee86c0392064ef8b4a26f4bd5e4404c
apiKey為申請(qǐng)的spoonacular賬號(hào)的apiKey
使用Retrofit創(chuàng)建數(shù)據(jù)模型
1橡羞、進(jìn)入上方的網(wǎng)址獲让型!(復(fù)制)所有數(shù)據(jù)
2、創(chuàng)建數(shù)據(jù)模型(關(guān)于該插件的下載可進(jìn)入Settings->Plugins進(jìn)行下載安裝)
將復(fù)制的數(shù)據(jù)拷貝到其中并按如下配置
當(dāng)然別忘了Class Name
3卿泽、將需要的信息保留庵朝,刪除掉不用的信息
ExrendedIngredient類
@Parcelize
data class ExtendedIngredient(
@SerializedName("aisle")
val aisle: String,
@SerializedName("amount")
val amount: Double,
@SerializedName("consistency")
val consistency: String,
@SerializedName("id")
val id: Int,
@SerializedName("image")
val image: String,
@SerializedName("name")
val name: String,
@SerializedName("original")
val original: String,
@SerializedName("unit")
val unit: String
):Parcelable
FoodRecipe類
data class FoodRecipe(
@SerializedName("results")
val results: List<Result>
)
Result類
@Parcelize
data class Result(
@SerializedName("aggregateLikes")
val aggregateLikes: Int,
@SerializedName("cheap")
val cheap: Boolean,
@SerializedName("dairyFree")
val dairyFree: Boolean,
@SerializedName("extendedIngredients")
val extendedIngredients: List<ExtendedIngredient>,
@SerializedName("glutenFree")
val glutenFree: Boolean,
@SerializedName("healthScore")
val healthScore: Double,
@SerializedName("id")
val id: Int,
@SerializedName("image")
val image: String,
@SerializedName("pricePerServing")
val pricePerServing: Double,
@SerializedName("readyInMinutes")
val readyInMinutes: Int,
@SerializedName("servings")
val servings: Int,
@SerializedName("sourceUrl")
val sourceUrl: String,
@SerializedName("spoonacularScore")
val spoonacularScore: Double,
@SerializedName("spoonacularSourceUrl")
val spoonacularSourceUrl: String,
@SerializedName("summary")
val summary: String,
@SerializedName("title")
val title: String,
@SerializedName("vegan")
val vegan: Boolean,
@SerializedName("veryHealthy")
val veryHealthy: Boolean,
@SerializedName("veryPopular")
val veryPopular: Boolean
):Parcelable
創(chuàng)建獲取數(shù)據(jù)的接口
這里返回值中使用Response封裝的原因:判斷數(shù)據(jù)是否獲取成功
將type設(shè)置到函數(shù)構(gòu)造函數(shù)參數(shù)中,后面將實(shí)現(xiàn)所搜功能
package com.example.foodresp.data.remote
import com.example.foodresp.data.model.FoodRecipe
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Query
/**
*@Description
*@Author PC
*@QQ 1578684787
*/
interface FoodApi {
//使用Response包裝又厉,判斷響應(yīng)是否成功
//基地址:https://api.spoonacular.com/
@GET("recipes/complexSearch?&addRecipeInformation=true&fillIngredients=true&apiKey=cee86c0392064ef8b4a26f4bd5e4404c")
suspend fun fetchFoodRecipes(@Query("type")type:String):Response<FoodRecipe>
}
創(chuàng)建遠(yuǎn)程(網(wǎng)絡(luò))“數(shù)據(jù)倉(cāng)庫(kù)”
搭建Retrofit
并且給外部提供訪問(wèn)接口
baseUrl中的基地址和@Get中的地址以及type形成完整的訪問(wèn)地址
class RemoteRepository {
//創(chuàng)建foodApi對(duì)象
private val foodApi:FoodApi by lazy {
val retrofit = Retrofit.Builder()
.baseUrl("https://api.spoonacular.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()
retrofit.create(FoodApi::class.java)
}
//給外部提供訪問(wèn)接口
suspend fun fetchFoodRecipes(type:String): Response<FoodRecipe>{
return foodApi.fetchFoodRecipes(type)
}
}
創(chuàng)建VIewModel
MainViewModel類
class MainViewModel(application: Application):AndroidViewModel(application) {
//網(wǎng)絡(luò)請(qǐng)求對(duì)象
private val remoteRepository = RemoteRepository()
//提供給外部觀察數(shù)據(jù)變化
var recipes: MutableLiveData<NetworkResult<FoodRecipe>> = MutableLiveData()
//外部通過(guò)這個(gè)方法發(fā)起網(wǎng)路請(qǐng)求
fun fetchFoodRecipes(type: String) {
val response = remoteResponsitory.fetchFoodRecipe(type)
}
}