Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/other.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import kotlinx.android.parcel.Parcelize

plugins {
alias(libs.plugins.androidApplication)
alias(libs.plugins.jetbrainsKotlinAndroid)
Expand Down Expand Up @@ -75,4 +73,7 @@ dependencies {
// Glide
implementation(libs.glide)
annotationProcessor(libs.compiler)
// DataStore
implementation(libs.androidx.datastore.preferences)
implementation(libs.kotlinx.coroutines.android)
}
Binary file modified app/release/app-release.apk
Binary file not shown.
Binary file modified app/release/baselineProfiles/0/app-release.dm
Binary file not shown.
Binary file modified app/release/baselineProfiles/1/app-release.dm
Binary file not shown.
18 changes: 8 additions & 10 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,23 @@
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@drawable/logo"
android:label=""
android:roundIcon="@mipmap/ic_launcher_round"
android:label="傻瓜廚師"
android:name=".MyApp"
android:roundIcon="@drawable/logo"
android:supportsRtl="true"
android:theme="@style/Theme.IdiotChefAssistant"
tools:targetApi="31">
<activity
android:name=".resultBlock.ResultPage"
android:name=".result.ResultPage"
android:exported="false"/>
<activity
android:name=".HistoryPage"
android:name=".mainLayout.CameraPage"
android:exported="false" />
<activity
android:name=".CameraPage"
android:name=".recipe.RecipePage"
android:exported="false" />
<activity
android:name=".recipeBlock.RecipePage"
android:exported="false" />
<activity
android:name=".searchBlock.SearchPage"
android:name=".search.SearchPage"
android:exported="false" />
<activity
android:name=".login.LoginActivity"
Expand All @@ -42,7 +40,7 @@
android:name=".register.RegisterPage"
android:exported="false"/>
<activity
android:name=".MainActivity"
android:name=".mainLayout.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.example.idiotchefassistant

import android.content.Context
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch

val Context.dataStore by preferencesDataStore(name = "settings")

object AuthTokenManager {
private val tokenKey = stringPreferencesKey("auth_token")
private var _authToken = MutableStateFlow<String?>(null)
val authToken = _authToken.asStateFlow()

fun initialize(context: Context) {
CoroutineScope(Dispatchers.IO).launch {
loadInitialToken(context)
}
}

private suspend fun loadInitialToken(context: Context) {
context.dataStore.data.map { preferences ->
preferences[tokenKey]
}.collect { token ->
_authToken.value = token
}
}

fun getAuthTokenSync(): String? = _authToken.value

suspend fun saveAuthToken(context: Context, token: String) {
context.dataStore.edit { preferences ->
preferences[tokenKey] = token
}
_authToken.value = token
}

suspend fun clearAuthToken(context: Context) {
context.dataStore.edit { preferences ->
preferences.remove(tokenKey)
}
_authToken.value = null
}
}
41 changes: 0 additions & 41 deletions app/src/main/java/com/example/idiotchefassistant/HistoryPage.kt

This file was deleted.

57 changes: 0 additions & 57 deletions app/src/main/java/com/example/idiotchefassistant/HomePage.kt

This file was deleted.

19 changes: 19 additions & 0 deletions app/src/main/java/com/example/idiotchefassistant/MyApp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.idiotchefassistant
import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData

class MyApp: Application() {
override fun onCreate() {
super.onCreate()
AuthTokenManager.initialize(applicationContext)
}
companion object {
private val _isLogin = MutableLiveData(false)
val isLogin: LiveData<Boolean> = _isLogin

fun setLogin(value: Boolean) {
_isLogin.postValue(value)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.example.idiotchefassistant

import DetectAPI
import com.example.idiotchefassistant.data.retrofit.endpoints.User
import com.example.idiotchefassistant.ingredientsBlock.IngredientAPI
import com.example.idiotchefassistant.recipeBlock.RecipeAPI
import com.example.idiotchefassistant.searchBlock.ResultSearchAPI
import android.util.Log
import com.example.idiotchefassistant.ingredients.IngredientAPI
import com.example.idiotchefassistant.recipe.RecipeAPI
import com.example.idiotchefassistant.search.ResultSearchAPI
import java.util.concurrent.TimeUnit
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

val okHttpClient = OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS) // 設置連接超時時間
.readTimeout(60, TimeUnit.SECONDS) // 設置讀取超時時間
.writeTimeout(60, TimeUnit.SECONDS) // 設置寫入超時時間
.connectTimeout(20, TimeUnit.SECONDS) // 設置連接超時時間
.readTimeout(20, TimeUnit.SECONDS) // 設置讀取超時時間
.writeTimeout(20, TimeUnit.SECONDS) // 設置寫入超時時間
.build()

var retrofit: Retrofit = Retrofit.Builder()
Expand All @@ -25,5 +25,27 @@ var retrofit: Retrofit = Retrofit.Builder()
val detectService: DetectAPI = retrofit.create(DetectAPI::class.java)
val ingredientService: IngredientAPI = retrofit.create(IngredientAPI::class.java)
val resultSearchService: ResultSearchAPI = retrofit.create(ResultSearchAPI::class.java)
val userService: User = retrofit.create(User::class.java)
val recipeService: RecipeAPI = retrofit.create(RecipeAPI::class.java)
val userService: User = retrofit.create(User::class.java)

val okHttpClientAddToken = OkHttpClient.Builder()
.addInterceptor { chain ->
val token = AuthTokenManager.getAuthTokenSync()
val request = chain.request().newBuilder()
.addHeader("X-API-Key", "$token") // 這裡放入從 AuthTokenManager 中取得的 Token
.build()
Log.i("Token","$token")
chain.proceed(request)
}
.connectTimeout(20, TimeUnit.SECONDS) // 設置連接超時時間
.readTimeout(20, TimeUnit.SECONDS) // 設置讀取超時時間
.writeTimeout(20, TimeUnit.SECONDS) // 設置寫入超時時間
.build()

var retrofitAddToken: Retrofit = Retrofit.Builder()
.baseUrl("https://topic114.bntw.dev/")
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClientAddToken)
.build()
val userDataService: User = retrofitAddToken.create(User::class.java)
val recipeAddTokenService: RecipeAPI = retrofitAddToken.create(RecipeAPI::class.java)

This file was deleted.

57 changes: 57 additions & 0 deletions app/src/main/java/com/example/idiotchefassistant/User.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.example.idiotchefassistant

import com.example.idiotchefassistant.recipe.RecipeItem
import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST

data class LoginResponse(
val token:String
)

data class LoginRequestBody(
val username:String,
val password:String
)

data class RegisterRequestBody(
val username:String,
val password:String,
val email: String
)

data class UserResponse(
val email:String,
val level:Int,
val username:String
)

data class MessageResponse(
val message:String
)

interface User {

@POST("/user/login")
fun login(
@Body loginRequestBody: LoginRequestBody
): Call<LoginResponse>

@GET("/user/me")
fun me(): Call<UserResponse>

@GET("/user/logout")
fun logout(): Call<MessageResponse>

@GET("/user/recommend")
fun recommend(): Call<List<RecipeItem>>

@POST("/user/register")
fun register(
@Body registerRequestBody: RegisterRequestBody
): Call<MessageResponse>

@GET("/user/history")
fun history (): Call<List<RecipeItem>>
}

This file was deleted.

Loading