diff --git a/.idea/misc.xml b/.idea/misc.xml index cae45ab..4b3850f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -120,7 +120,7 @@ - + diff --git a/app/src/main/java/com/fundito/fundito/broadcast/Broadcast.kt b/app/src/main/java/com/fundito/fundito/broadcast/Broadcast.kt index 8b2a4ba..d94f9b6 100644 --- a/app/src/main/java/com/fundito/fundito/broadcast/Broadcast.kt +++ b/app/src/main/java/com/fundito/fundito/broadcast/Broadcast.kt @@ -1,11 +1,15 @@ package com.fundito.fundito.broadcast +import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.channels.BroadcastChannel -import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.channels.ConflatedBroadcastChannel /** * Created by mj on 01, January, 2020 */ +@ExperimentalCoroutinesApi object Broadcast { - val chargeCompleteEvent : BroadcastChannel = BroadcastChannel(Channel.CONFLATED) + val chargeCompleteEvent : BroadcastChannel = ConflatedBroadcastChannel() + + val fundEvent : BroadcastChannel> = ConflatedBroadcastChannel() } \ No newline at end of file diff --git a/app/src/main/java/com/fundito/fundito/common/widget/LoadingDialog.kt b/app/src/main/java/com/fundito/fundito/common/widget/LoadingDialog.kt index 5b36c18..2b4b19e 100644 --- a/app/src/main/java/com/fundito/fundito/common/widget/LoadingDialog.kt +++ b/app/src/main/java/com/fundito/fundito/common/widget/LoadingDialog.kt @@ -21,24 +21,28 @@ import com.fundito.fundito.databinding.DialogLoadingBinding * Created by mj on 24, December, 2019 */ fun Fragment.showLoading() { + if(childFragmentManager.findFragmentByTag("Loading") != null) return + val dialog = LoadingDialog() dialog.show(childFragmentManager, "Loading") } fun AppCompatActivity.showLoading() { + if(supportFragmentManager.findFragmentByTag("Loading") != null) return + val dialog = LoadingDialog() dialog.show(supportFragmentManager, "Loading") } fun Fragment.hideLoading() { childFragmentManager.findFragmentByTag("Loading")?.let { - childFragmentManager.beginTransaction().remove(it).commit() + (it as? LoadingDialog)?.dismiss() } } fun AppCompatActivity.hideLoading() { supportFragmentManager.findFragmentByTag("Loading")?.let { - supportFragmentManager.beginTransaction().remove(it).commit() + (it as? LoadingDialog)?.dismiss() } } diff --git a/app/src/main/java/com/fundito/fundito/data/service/NetworkClient.kt b/app/src/main/java/com/fundito/fundito/data/service/NetworkClient.kt index 13539ed..b83f54f 100644 --- a/app/src/main/java/com/fundito/fundito/data/service/NetworkClient.kt +++ b/app/src/main/java/com/fundito/fundito/data/service/NetworkClient.kt @@ -40,8 +40,8 @@ object NetworkClient { val newRequest = chain.request().newBuilder() .addHeader("token", SPUtil.accessToken).build() - Timber.e("TOKEN : ${SPUtil.accessToken}") - +// Timber.e("TOKEN : ${SPUtil.accessToken}") + Timber.e("${newRequest.method} - ${newRequest.url} \n{\n\t${newRequest.body?.toString()}\n}\n" ) /** * 2) General Response from Server (Unwrapping data) @@ -53,7 +53,7 @@ object NetworkClient { */ val rawJson = response.body?.string() ?: "{}" - Timber.e(rawJson) +// Timber.e(rawJson) /** * 4) Wrap body with gson diff --git a/app/src/main/java/com/fundito/fundito/presentation/charge/ChargeActivity.kt b/app/src/main/java/com/fundito/fundito/presentation/charge/ChargeActivity.kt index ffb6dd2..8225725 100644 --- a/app/src/main/java/com/fundito/fundito/presentation/charge/ChargeActivity.kt +++ b/app/src/main/java/com/fundito/fundito/presentation/charge/ChargeActivity.kt @@ -99,8 +99,16 @@ class ChargeActivity : DaggerAppCompatActivity() { } } + private var isLoading = false private fun observeViewModel() { mViewModel.apply { + + loading.observe(this@ChargeActivity) { + if(isLoading == it) return@observe + isLoading = it + if(it) showLoading() else hideLoading() + } + passwordMatch.observeOnce(this@ChargeActivity) { matched -> /** diff --git a/app/src/main/java/com/fundito/fundito/presentation/charge/ChargeViewModel.kt b/app/src/main/java/com/fundito/fundito/presentation/charge/ChargeViewModel.kt index 10cec11..ec64773 100644 --- a/app/src/main/java/com/fundito/fundito/presentation/charge/ChargeViewModel.kt +++ b/app/src/main/java/com/fundito/fundito/presentation/charge/ChargeViewModel.kt @@ -11,6 +11,10 @@ import javax.inject.Inject * Created by mj on 27, December, 2019 */ class ChargeViewModel @Inject constructor() : ViewModel() { + + private val _loading : MutableLiveData = MutableLiveData() + val loading : LiveData = _loading + val chargeMoney : MutableLiveData = MutableLiveData(0L) val visiblePlaceHolder = chargeMoney.map { @@ -20,21 +24,24 @@ class ChargeViewModel @Inject constructor() : ViewModel() { private val _passwordMatch : MutableLiveData> = MutableLiveData() val passwordMatch : LiveData> = _passwordMatch - val cardData = liveData { + _loading.value = true kotlin.runCatching { NetworkClient.cardService.getCard() }.onSuccess { emit(it) } + _loading.value = false } val funditoMoney = liveData { + _loading.value = true kotlin.runCatching { NetworkClient.userService.getFunditoMoney() }.onSuccess { it.getOrNull(0)?.let { emit(it.point) } } + _loading.value = false } diff --git a/app/src/main/java/com/fundito/fundito/presentation/funding/FundingActivity.kt b/app/src/main/java/com/fundito/fundito/presentation/funding/FundingActivity.kt index b10cf2b..bd6eb8e 100644 --- a/app/src/main/java/com/fundito/fundito/presentation/funding/FundingActivity.kt +++ b/app/src/main/java/com/fundito/fundito/presentation/funding/FundingActivity.kt @@ -8,6 +8,7 @@ import androidx.lifecycle.observe import androidx.viewpager2.widget.ViewPager2 import com.fundito.fundito.R import com.fundito.fundito.common.setVisibilityBinding +import com.fundito.fundito.common.showAlert import com.fundito.fundito.common.widget.* import dagger.android.support.DaggerAppCompatActivity import kotlinx.android.synthetic.main.activity_funding.* @@ -105,9 +106,6 @@ class FundingActivity : DaggerAppCompatActivity() { } }) - - - } 2 -> { // startActivity(Intent(this@FundingActivity,MainActivity::class.java).apply { flags = Intent.FLAG_ACTIVITY_CLEAR_TOP }) @@ -117,9 +115,13 @@ class FundingActivity : DaggerAppCompatActivity() { } } + private var isLoading = false private fun observeViewModel() { mViewModel.apply { loading.observe(this@FundingActivity) { + if(isLoading == it) return@observe + isLoading = it + if (it) showLoading() else hideLoading() } @@ -135,6 +137,10 @@ class FundingActivity : DaggerAppCompatActivity() { keyboardDialog?.onPasswordMatchFailed() } } + + chargeFail.observeOnce(this@FundingActivity) { + showAlert("펀디토 머니 충전 실패") + } } } diff --git a/app/src/main/java/com/fundito/fundito/presentation/funding/FundingCompleteFragment.kt b/app/src/main/java/com/fundito/fundito/presentation/funding/FundingCompleteFragment.kt index 3b2ee31..bebeaa7 100644 --- a/app/src/main/java/com/fundito/fundito/presentation/funding/FundingCompleteFragment.kt +++ b/app/src/main/java/com/fundito/fundito/presentation/funding/FundingCompleteFragment.kt @@ -24,6 +24,24 @@ class FundingCompleteFragment : DaggerFragment() { } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + observeViewModel() + } + + private fun observeViewModel() { + mViewModel.apply { + store.observe(viewLifecycleOwner) { + storeName.text = it.name + } + + inputMoney.observe(viewLifecycleOwner) { + completeCost.text = it.toMoney() + completeCost2.text = "${it.toMoney()} 원" + } + + refundMoney.observe(viewLifecycleOwner) { + additionalCost.text = "(+${it.roundToInt().toMoney()} 원) ${mViewModel.totalMoney.value!!.roundToInt().toMoney()} 원" + } + } } } \ No newline at end of file diff --git a/app/src/main/java/com/fundito/fundito/presentation/funding/FundingViewModel.kt b/app/src/main/java/com/fundito/fundito/presentation/funding/FundingViewModel.kt index 1ea0507..0e70ee8 100644 --- a/app/src/main/java/com/fundito/fundito/presentation/funding/FundingViewModel.kt +++ b/app/src/main/java/com/fundito/fundito/presentation/funding/FundingViewModel.kt @@ -1,9 +1,11 @@ package com.fundito.fundito.presentation.funding import androidx.lifecycle.* +import com.fundito.fundito.broadcast.Broadcast import com.fundito.fundito.common.widget.Once import com.fundito.fundito.data.service.NetworkClient import kotlinx.coroutines.launch +import timber.log.Timber import javax.inject.Inject import javax.inject.Named @@ -21,19 +23,23 @@ class FundingViewModel @Inject constructor( val loading : LiveData = _loading val cardData = liveData { + _loading.value = true kotlin.runCatching { NetworkClient.cardService.getCard() }.onSuccess { emit(it) } + _loading.value = false } val funditoMyMoney = liveData { + _loading.value = true kotlin.runCatching { NetworkClient.userService.getFunditoMoney() }.onSuccess { emit(it.getOrNull(0)?.point ?: 0) } + _loading.value = false } val inputMoney = MutableLiveData() @@ -47,11 +53,13 @@ class FundingViewModel @Inject constructor( } val store = liveData { + _loading.value = true kotlin.runCatching { NetworkClient.storeInfoService.getStoreInfo(storeIdx) }.onSuccess { emit(it) } + _loading.value = false } val password = MutableLiveData("") @@ -59,17 +67,39 @@ class FundingViewModel @Inject constructor( private val _fundResult : MutableLiveData> = MutableLiveData() val fundResult : LiveData> = _fundResult + private val _chargeFail : MutableLiveData> = MutableLiveData() + val chargeFail : LiveData> = _chargeFail + fun onCheckPasswordMatch() { val password = password.value ?: return + val funditoMoney = funditoMyMoney.value ?: return val inputMoney = inputMoney.value ?: return + + val diff = funditoMoney - inputMoney + viewModelScope.launch { + _loading.value = true kotlin.runCatching { + if(diff < 0) { + try { + NetworkClient.userService.chargeFunditoMoney(-diff, password) + Broadcast.chargeCompleteEvent.send(-diff) + Timber.e("send charge") + }catch(t: Throwable) { + _chargeFail.value = Once(Unit) + return@runCatching + } + } + NetworkClient.fundingService.fundWithPassword(password,storeIdx,inputMoney) + Broadcast.fundEvent.send(storeIdx to inputMoney) + Timber.e("fund send") }.onSuccess { _fundResult.value = Once(true) }.onFailure { _fundResult.value = Once(false) } + _loading.value = false } } } \ No newline at end of file diff --git a/app/src/main/java/com/fundito/fundito/presentation/main/home/HomeViewModel.kt b/app/src/main/java/com/fundito/fundito/presentation/main/home/HomeViewModel.kt index d108911..e925de4 100644 --- a/app/src/main/java/com/fundito/fundito/presentation/main/home/HomeViewModel.kt +++ b/app/src/main/java/com/fundito/fundito/presentation/main/home/HomeViewModel.kt @@ -1,15 +1,20 @@ package com.fundito.fundito.presentation.main.home import androidx.lifecycle.* +import com.fundito.fundito.broadcast.Broadcast import com.fundito.fundito.data.model.Funding import com.fundito.fundito.data.service.NetworkClient import com.fundito.fundito.data.service.WifiStoreResponse +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.channels.consumeEach import kotlinx.coroutines.launch +import timber.log.Timber import javax.inject.Inject /** * Created by mj on 31, December, 2019 */ +@UseExperimental(ExperimentalCoroutinesApi::class) class HomeViewModel @Inject constructor() : ViewModel() { val userData = liveData { @@ -29,8 +34,6 @@ class HomeViewModel @Inject constructor() : ViewModel() { private val _connectedStoreTimeLineItem : MutableLiveData = MutableLiveData() val connectedStoreTimeLineItem : LiveData = _connectedStoreTimeLineItem - - fun onWifiStateChanged(ssid : String) { if(!ssid.isBlank()) { @@ -45,7 +48,6 @@ class HomeViewModel @Inject constructor() : ViewModel() { it.second.getOrNull(0)?.let { _connectedStoreTimeLineItem.value = it } - }.onFailure { _storeConnectWithWifi.value = false } @@ -55,4 +57,21 @@ class HomeViewModel @Inject constructor() : ViewModel() { _storeConnectWithWifi.value = true } } + + init { + viewModelScope.launch { + Broadcast.fundEvent.openSubscription().consumeEach { + Timber.e("밤ㄴ이ㅓㅁ") + if(it.first == connectedStoreData.value?.storeIdx) { + kotlin.runCatching { + NetworkClient.storeInfoService.listStoreFundingTimeLine(it.first)[0] + }.onSuccess { + _connectedStoreTimeLineItem.value = it + } + } + } + } + + } + } \ No newline at end of file diff --git a/app/src/main/java/com/fundito/fundito/presentation/main/status/RecentFundingAdapter.kt b/app/src/main/java/com/fundito/fundito/presentation/main/status/RecentFundingAdapter.kt index cf1934e..5c65c61 100644 --- a/app/src/main/java/com/fundito/fundito/presentation/main/status/RecentFundingAdapter.kt +++ b/app/src/main/java/com/fundito/fundito/presentation/main/status/RecentFundingAdapter.kt @@ -59,7 +59,7 @@ class RecentFundingAdapter : ListAdapter = MutableLiveData(0) + val sheetOpenCount: MutableLiveData = MutableLiveData(0) /** * 두번 째 Sheet에서 탭 위치 */ - val sheet2TabIndex : MutableLiveData = MutableLiveData(0) + val sheet2TabIndex: MutableLiveData = MutableLiveData(0) /** * 두번 째 Sheet에서 현재 보여지는 Scene */ - val sceneIndex : MutableLiveData = MutableLiveData(0) + val sceneIndex: MutableLiveData = MutableLiveData(0) - private val _dispatchBackPressEvent : MutableLiveData> = MutableLiveData() - val dispatchBackPressEvent : LiveData> = _dispatchBackPressEvent + private val _dispatchBackPressEvent: MutableLiveData> = MutableLiveData() + val dispatchBackPressEvent: LiveData> = _dispatchBackPressEvent val userData = liveData { _loading.value = true kotlin.runCatching { - NetworkClient.userService.getUser() - }.onSuccess { emit(it) } _loading.value = false } - val fundingData = liveData { - _loading.value = true - kotlin.runCatching { + private val _fundingData: MutableLiveData = MutableLiveData() + val fundingData: LiveData = _fundingData - NetworkClient.userService.getUsingFunditoMoney() - - }.onSuccess { - emit(it) - } - _loading.value = false - } private val _funditoMoney: MutableLiveData = MutableLiveData() val funditoMoney: LiveData = _funditoMoney - val recentFundingHistories: LiveData> = liveData { - _loading.value = true - kotlin.runCatching { + private val _recentFundingHistories: MutableLiveData> = MutableLiveData(listOf()) + val recentFundingHistories: LiveData> = _recentFundingHistories - val list = NetworkClient.fundingService.getMyFundingHistories() - val store = NetworkClient.storeInfoService.getStoreInfo(list[0].storeIdx) - list.map { store to it } - }.onSuccess { - emit(it) - } - _loading.value = false - } - - val currentFundingStores = liveData { - _loading.value = true - kotlin.runCatching { - NetworkClient.fundingService.listCurrentFundingStore() - }.onSuccess { - emit(it) - } - _loading.value = false - } + private val _currentFundingStores: MutableLiveData> = MutableLiveData(listOf()) + val currentFundingStores: LiveData> = _currentFundingStores val completeFundingStores = liveData { _loading.value = true @@ -95,11 +76,35 @@ class StatusViewModel @Inject constructor() : ViewModel() { val selectedShopData = MutableLiveData>>() - private val _loading : MutableLiveData = MutableLiveData() - val loading : LiveData = _loading + private val _loading: MutableLiveData = MutableLiveData() + val loading: LiveData = _loading init { getFunditoMoney() + getFundingData() + getRecentFundingHistories() + getCurrentFundings() + + viewModelScope.launch { + Broadcast.chargeCompleteEvent.asFlow().collect { + Timber.e("충전 완료 $it") + getFunditoMoney() + getFundingData() + getRecentFundingHistories() + getCurrentFundings() + } + } + viewModelScope.launch { + Broadcast.fundEvent.asFlow().collect { + Timber.e("펀딩 완료 ${it.first} - ${it.second}") + getFunditoMoney() + getFundingData() + getRecentFundingHistories() + getCurrentFundings() + } + } + + } fun getFunditoMoney() { @@ -116,19 +121,52 @@ class StatusViewModel @Inject constructor() : ViewModel() { } } + private fun getFundingData() = viewModelScope.launch { + _loading.value = true + kotlin.runCatching { + NetworkClient.userService.getUsingFunditoMoney() + }.onSuccess { + _fundingData.value = it + } + _loading.value = false + } + + private fun getRecentFundingHistories() = viewModelScope.launch { + _loading.value = true + kotlin.runCatching { + val list = NetworkClient.fundingService.getMyFundingHistories() + val store = NetworkClient.storeInfoService.getStoreInfo(list[0].storeIdx) + list.map { store to it } + }.onSuccess { + _recentFundingHistories.value = it + } + _loading.value = false + } + + private fun getCurrentFundings() = viewModelScope.launch { + _loading.value = true + kotlin.runCatching { + NetworkClient.fundingService.listCurrentFundingStore() + }.onSuccess { + _currentFundingStores.value = it + } + _loading.value = false + } + + fun onClickBack() { - if(sceneIndex.value == 1) { + if (sceneIndex.value == 1) { sceneIndex.value = 0 return } - if((sheetOpenCount.value ?: 0) > 0) { + if ((sheetOpenCount.value ?: 0) > 0) { _dispatchBackPressEvent.value = Once(Unit) return } } - fun onSelectStore(storeIdx : Int) { + fun onSelectStore(storeIdx: Int) { viewModelScope.launch { _loading.value = true diff --git a/app/src/main/java/com/fundito/fundito/presentation/store/StoreCheerActivity.kt b/app/src/main/java/com/fundito/fundito/presentation/store/StoreCheerActivity.kt index 3ae44d3..9bb2dc7 100644 --- a/app/src/main/java/com/fundito/fundito/presentation/store/StoreCheerActivity.kt +++ b/app/src/main/java/com/fundito/fundito/presentation/store/StoreCheerActivity.kt @@ -1,5 +1,6 @@ package com.fundito.fundito.presentation.store +import android.content.Context import android.content.Intent import android.net.Uri import android.os.Bundle @@ -20,6 +21,20 @@ import kotlinx.android.synthetic.main.activity_store_cheer.* */ class StoreCheerActivity : AppCompatActivity() { + companion object { + private const val ARG_STORE_NAME = "ARG_STORE_NAME" + + fun newIntent(context: Context, storeName: String) : Intent { + return Intent(context,StoreCheerActivity::class.java).apply { + putExtra(ARG_STORE_NAME,storeName) + } + } + } + + private val storeName: String + get() = intent?.getStringExtra(ARG_STORE_NAME) ?: "" + + private lateinit var shareDialog :ShareDialog private val callbackManager = CallbackManager.Factory.create() @@ -46,7 +61,7 @@ class StoreCheerActivity : AppCompatActivity() { cheerButton setOnDebounceClickListener { val content = ShareLinkContent.Builder() .setContentUrl(Uri.parse("https://fundito.page.link/XktS")) - .setQuote("펀디토 망해라") + .setQuote("$storeName 에 투자하세요! \n#Fundito") .build() shareDialog.show(content,ShareDialog.Mode.AUTOMATIC) } diff --git a/app/src/main/java/com/fundito/fundito/presentation/store/StoreDetailViewModel.kt b/app/src/main/java/com/fundito/fundito/presentation/store/StoreDetailViewModel.kt index 761922e..81d74c2 100644 --- a/app/src/main/java/com/fundito/fundito/presentation/store/StoreDetailViewModel.kt +++ b/app/src/main/java/com/fundito/fundito/presentation/store/StoreDetailViewModel.kt @@ -74,7 +74,7 @@ class StoreDetailViewModel(private val storeIdx : Int) : ViewModel() { }.onFailure { Timber.e(it) } - delay(5000L) + delay(30000L) } } } diff --git a/app/src/main/res/layout/fragment_funding_complete.xml b/app/src/main/res/layout/fragment_funding_complete.xml index d589174..85d3202 100644 --- a/app/src/main/res/layout/fragment_funding_complete.xml +++ b/app/src/main/res/layout/fragment_funding_complete.xml @@ -43,7 +43,7 @@ android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="48dp" - android:text="원 응원완료" + android:text="원 투자완료" android:textColor="@color/black" android:textSize="24sp" app:layout_constraintStart_toEndOf="@+id/completeCost"