Skip to content

Commit

Permalink
Reorder packages and some cleanup.
Browse files Browse the repository at this point in the history
Close #55 #56
  • Loading branch information
AIDEA775 committed Jun 21, 2023
1 parent 64f7ce8 commit 814ad86
Show file tree
Hide file tree
Showing 30 changed files with 55 additions and 228 deletions.
26 changes: 8 additions & 18 deletions app/src/main/java/com/uncmorfi/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GravityCompat
import androidx.fragment.app.Fragment
import com.google.android.material.navigation.NavigationView
import com.uncmorfi.about.AboutDialog
import com.uncmorfi.balance.BalanceFragment
import com.uncmorfi.faq.FaqFragment
import com.uncmorfi.home.HomeFragment
import com.uncmorfi.map.MapFragment
import com.uncmorfi.menu.MenuFragment
import com.uncmorfi.reservations.RemindersFragment
import com.uncmorfi.servings.ServingsFragment
import com.uncmorfi.ui.about.AboutDialog
import com.uncmorfi.ui.balance.BalanceFragment
import com.uncmorfi.ui.faq.FaqFragment
import com.uncmorfi.ui.home.HomeFragment
import com.uncmorfi.ui.map.MapFragment
import com.uncmorfi.ui.menu.MenuFragment
import com.uncmorfi.ui.reminders.RemindersFragment
import com.uncmorfi.ui.servings.ServingsFragment
import com.uncmorfi.shared.*
import kotlinx.android.synthetic.main.activity_main.*

Expand All @@ -39,16 +39,6 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
Log.i("MainActivity", "new status: $it")
content_layout.snack(it)
}

observe(viewModel.reservation) {
content_layout.snack(it)
}

observe(viewModel.reserveTry) {
if (it > 0) {
content_layout.snack(getString(R.string.snack_loop).format(it), SnackType.LOADING)
}
}
}

private fun setToolbarAndNavigation() {
Expand Down
162 changes: 4 additions & 158 deletions app/src/main/java/com/uncmorfi/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.uncmorfi.data.network.clientBeta
import com.uncmorfi.data.network.models.ReservationRes
import com.uncmorfi.data.persistence.AppDatabase
import com.uncmorfi.data.persistence.entities.DayMenu
import com.uncmorfi.data.persistence.entities.Reservation
import com.uncmorfi.data.persistence.entities.Serving
import com.uncmorfi.data.persistence.entities.User
import com.uncmorfi.data.repository.RepoMenu
Expand All @@ -26,23 +22,17 @@ import retrofit2.HttpException
import java.io.IOException
import java.time.LocalDate
import java.util.*
import kotlin.coroutines.coroutineContext

class MainViewModel(val context: Application) : AndroidViewModel(context) {
private val db: AppDatabase = AppDatabase(context)
private val repoMenu = RepoMenu(context)
private val repoUser = RepoUser(context)
private val repoServings = RepoServings(context)
private val servWorkers = ServWorkers(context)

val status: MutableLiveData<StatusCode> = MutableLiveData()
val reservation: MutableLiveData<ReserveStatus> = MutableLiveData()
val reserveTry: MutableLiveData<Int> = MutableLiveData()
var reserveJob: Job? = null

init {
status.value = BUSY
reservation.value = NOCACHED
}

/*
Expand Down Expand Up @@ -77,6 +67,7 @@ class MainViewModel(val context: Application) : AndroidViewModel(context) {
/*
* Menu stuff
*/

fun getMenu(): LiveData<List<DayMenu>> = repoMenu.getAll()

fun refreshMenu() = launchIO {
Expand Down Expand Up @@ -116,6 +107,7 @@ class MainViewModel(val context: Application) : AndroidViewModel(context) {
/*
* Serving stuff
*/

fun getServings(): LiveData<List<Serving>> = repoServings.getToday()

fun updateServings() = launchIO {
Expand All @@ -135,139 +127,13 @@ class MainViewModel(val context: Application) : AndroidViewModel(context) {
}

/*
* Reservation stuff
* Worker stuff
*/

fun refreshWorkers() {
servWorkers.refreshAllWorkers()
}

fun reserveIsCached(user: User) {
mainDispatch {
val reserve = getReserve(user.card)
if (reserve != null) {
reservation.value = CACHED
} else {
reservation.value = NOCACHED
}
}
}

fun reserveLogin(user: User, captcha: String) {
mainDispatch {
val reserve = clientBeta.getLogin(user.card)
reserve.captchaText = captcha

val result = ioDispatch { clientBeta.doLogin(reserve) }
result?.let {
insertReservation(result)
reservation.value = CACHED
}
}
}

fun reserveConsult(user: User) {
mainDispatch {
val reserve = getReserve(user.card)
if (reserve != null) {
if (context.isOnline()) {
reservation.value = CONSULTING
val result = ioDispatch { clientBeta.status(reserve) }
result?.updateReservation(reserve)
} else {
status.value = NO_ONLINE
}
} else {
reservation.value = REDOLOGIN
}
}
}

fun reserve(user: User) {
mainDispatch {
val reserve = getReserve(user.card)
if (reserve != null) {
if (context.isOnline()) {
reservation.value = RESERVING
val result = ioDispatch { clientBeta.reserve(reserve) }
result?.updateReservation(reserve)
} else {
status.value = NO_ONLINE
}
} else {
reservation.value = REDOLOGIN
}
}
}

fun reserveLoop(user: User) {
mainDispatch {
val reserve = getReserve(user.card)
if (reserve == null) {
reservation.value = REDOLOGIN
return@mainDispatch
}
reserveJob?.cancel()
reserveJob = mainDispatch {
var intent = 0

do {
intent += 1
reserveTry.value = intent
val result = ioDispatch { clientBeta.reserve(reserve) }
val status = result?.updateReservation(reserve)
delay(1500)
} while (status != RESERVED && status != REDOLOGIN)

reserveTry.value = 0
}
}
}

fun reserveStop() {
reserveJob?.cancel()
reserveJob = null
reserveTry.value = 0
status.value = BUSY
}

fun reserveLogout(user: User) {
mainDispatch {
db.reserveDao().delete(user.card)
reservation.value = NOCACHED
}
}

private suspend fun ReservationRes.updateReservation(reserve: Reservation): ReserveStatus {
this.path?.let { reserve.path = it }
this.token?.let { reserve.token = it }

val status = ReserveStatus.valueOf(this.reservationResult.toUpperCase())
reservation.value = status

if (status == REDOLOGIN) {
db.reserveDao().delete(reserve.code)
} else {
insertReservation(reserve)
}
return status
}

private suspend fun insertReservation(reserve: Reservation) {
// Guardar cookie con el codigo de la tarjeta a la que pertenece
reserve.cookies?.map { c -> c.code = reserve.code }
db.reserveDao().insert(reserve)
}

private suspend fun getReserve(code: String): Reservation? {
val reserve = db.reserveDao().getReservation(code)
reserve?.cookies = db.reserveDao().getCookies(code)
return reserve
}

private fun mainDispatch(f: suspend (CoroutineScope) -> Unit): Job {
return viewModelScope.launch(Dispatchers.Main, block = f)
}

private fun launchIO(f: suspend (CoroutineScope) -> Unit) {
viewModelScope.launch(Dispatchers.IO) {
try {
Expand All @@ -288,24 +154,4 @@ class MainViewModel(val context: Application) : AndroidViewModel(context) {
}
}

private suspend fun <T> ioDispatch(f: suspend (CoroutineScope) -> T): T? {
var result: T? = null
try {
result = withContext(coroutineContext + Dispatchers.IO, block = f)
} catch (e: HttpException) {
e.printStackTrace()
status.value = CONNECT_ERROR
} catch (e: IOException) {
e.printStackTrace()
status.value = CONNECT_ERROR
} catch (e: JSONException) {
e.printStackTrace()
status.value = INTERNAL_ERROR
} catch (e: NumberFormatException) {
e.printStackTrace()
status.value = INTERNAL_ERROR
}
return result
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.uncmorfi.about
package com.uncmorfi.ui.about

import android.app.Dialog
import android.os.Bundle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.uncmorfi.balance
package com.uncmorfi.ui.balance

import android.os.Bundle
import android.view.*
Expand All @@ -7,10 +7,9 @@ import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.uncmorfi.MainViewModel
import com.uncmorfi.R
import com.uncmorfi.balance.dialogs.UserOptionsDialog
import com.uncmorfi.ui.balance.dialogs.UserOptionsDialog
import com.uncmorfi.data.persistence.entities.User
import com.uncmorfi.shared.*
import com.uncmorfi.shared.ReserveStatus.NOCACHED
import com.uncmorfi.shared.SnackType.*
import com.uncmorfi.shared.StatusCode.UPDATE_SUCCESS
import com.uncmorfi.shared.StatusCode.USER_INSERTED
Expand Down Expand Up @@ -156,7 +155,6 @@ class BalanceFragment : Fragment() {
super.onStop()
activity?.hideKeyboard()
newUser.clearFocus()
viewModel.reservation.value = NOCACHED
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.uncmorfi.balance
package com.uncmorfi.ui.balance

import android.view.LayoutInflater
import android.view.View
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.uncmorfi.balance.dialogs
package com.uncmorfi.ui.balance.dialogs

import android.app.Dialog
import android.os.Bundle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.uncmorfi.balance.dialogs
package com.uncmorfi.ui.balance.dialogs

import android.app.Dialog
import android.os.Bundle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.uncmorfi.balance.dialogs
package com.uncmorfi.ui.balance.dialogs

import android.os.Bundle
import android.view.LayoutInflater
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.uncmorfi.balance
package com.uncmorfi.ui.balance.views

import android.content.Context
import android.text.InputFilter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.uncmorfi.balance
package com.uncmorfi.ui.balance.views

import android.content.Context
import android.text.format.DateUtils
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.uncmorfi.faq
package com.uncmorfi.ui.faq

import android.annotation.SuppressLint
import android.os.Bundle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.uncmorfi.home
package com.uncmorfi.ui.home

import android.os.Bundle
import android.view.*
Expand All @@ -8,10 +8,9 @@ import androidx.fragment.app.activityViewModels
import com.uncmorfi.MainActivity
import com.uncmorfi.MainViewModel
import com.uncmorfi.R
import com.uncmorfi.balance.dialogs.UserOptionsDialog
import com.uncmorfi.ui.balance.dialogs.UserOptionsDialog
import com.uncmorfi.data.persistence.entities.DayMenu
import com.uncmorfi.data.persistence.entities.User
import com.uncmorfi.shared.ReserveStatus.NOCACHED
import com.uncmorfi.shared.StatusCode
import com.uncmorfi.shared.init
import com.uncmorfi.shared.observe
Expand Down Expand Up @@ -135,9 +134,4 @@ class HomeFragment : Fragment() {
requireActivity().setTitle(R.string.app_name)
}

override fun onStop() {
super.onStop()
viewModel.reservation.value = NOCACHED
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.uncmorfi.map
package com.uncmorfi.ui.map

import android.os.Bundle
import androidx.fragment.app.Fragment
Expand Down Expand Up @@ -31,7 +31,7 @@ class MapFragment : Fragment() {
mapView.onResume()

try {
MapsInitializer.initialize(activity!!.applicationContext)
MapsInitializer.initialize(requireActivity().applicationContext)
} catch (e: Exception) {
e.printStackTrace()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.uncmorfi.menu
package com.uncmorfi.ui.menu

import android.content.Context
import android.util.AttributeSet
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.uncmorfi.menu
package com.uncmorfi.ui.menu

import android.view.LayoutInflater
import android.view.View
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.uncmorfi.menu
package com.uncmorfi.ui.menu

import android.os.Bundle
import android.view.*
Expand Down
Loading

0 comments on commit 814ad86

Please sign in to comment.