-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
release: 0.0.3(3) 버전 배포
- Loading branch information
Showing
12 changed files
with
240 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
app/src/main/java/com/woozoo/menumonya/repository/FireStoreRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.woozoo.menumonya.repository | ||
|
||
import com.google.firebase.firestore.ktx.firestore | ||
import com.google.firebase.firestore.ktx.toObject | ||
import com.google.firebase.ktx.Firebase | ||
import com.woozoo.menumonya.model.Menu | ||
import com.woozoo.menumonya.model.Restaurant | ||
import com.woozoo.menumonya.util.DateUtils | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.tasks.await | ||
import kotlinx.coroutines.withContext | ||
|
||
class FireStoreRepository { | ||
private val db = Firebase.firestore | ||
private var remoteConfigRepository = RemoteConfigRepository.get() | ||
|
||
private lateinit var restaurantCollectionName: String | ||
private lateinit var menuCollectionName: String | ||
|
||
companion object { | ||
private var instance: FireStoreRepository? = null | ||
|
||
fun initialize() { | ||
if (instance == null) { | ||
instance = FireStoreRepository() | ||
} | ||
} | ||
|
||
fun get(): FireStoreRepository { | ||
return instance ?: throw java.lang.IllegalStateException("FireStoreRepository must be initialized") | ||
} | ||
} | ||
|
||
suspend fun getRestaurantInLocation(location: String) = withContext(Dispatchers.IO) { | ||
restaurantCollectionName = remoteConfigRepository.getRestaurantsCollectionName() | ||
|
||
val restaurantInfo = ArrayList<Restaurant>() | ||
val restaurantRef = db.collection(restaurantCollectionName) | ||
val query = restaurantRef.whereArrayContainsAny("locationCategory", listOf(location)) | ||
|
||
val result = query.get().await() | ||
val documents = result.documents | ||
|
||
for (document in documents) { | ||
val restaurant = document.toObject<Restaurant>() | ||
|
||
if (restaurant != null) { | ||
// 메뉴 정보 조회 | ||
val menu = getMenu(document.id) | ||
|
||
val todayMenu = menu.date.get(DateUtils.getTodayDate()) | ||
if (todayMenu != null) restaurant.todayMenu = todayMenu | ||
|
||
restaurantInfo.add(restaurant) | ||
} | ||
} | ||
|
||
// locationCategoryOrder값으로 순서 재정렬(가까운 블록에 위치한 순서대로) | ||
for (restaurant in restaurantInfo) { | ||
restaurant.locationCategoryOrder.removeAll { !it.contains(location) } | ||
} | ||
restaurantInfo.sortBy { it.locationCategoryOrder[0] } | ||
|
||
restaurantInfo | ||
} | ||
|
||
suspend fun getMenu(restaurantId: String) = withContext(Dispatchers.IO) { | ||
menuCollectionName = remoteConfigRepository.getMenuCollectionName() | ||
|
||
var menu = Menu() | ||
|
||
val menuRef = db.collection(menuCollectionName) | ||
val query = menuRef.whereEqualTo("restaurantId", restaurantId) | ||
|
||
val result = query.get().await() | ||
val documents = result.documents | ||
|
||
if (documents.size > 0) { | ||
menu = documents[0].toObject<Menu>()!! | ||
} | ||
|
||
menu | ||
} | ||
} |
Oops, something went wrong.