Skip to content

Commit

Permalink
๐Ÿ”€ #11 from boostcampwm-2022/feat/running_tab
Browse files Browse the repository at this point in the history
๋Ÿฌ๋‹ํƒญ ํ™”๋ฉด ๊ตฌํ˜„ (ํ˜„์žฌ ๋Ÿฌ๋„ˆ ์นด์šดํŠธ ๋ฐ›์•„์˜ค๊ธฐ)
  • Loading branch information
bngsh authored Nov 17, 2022
2 parents 680773f + 5811202 commit 894848f
Show file tree
Hide file tree
Showing 20 changed files with 382 additions and 5 deletions.
9 changes: 9 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
plugins {
id "com.android.application"
id "org.jetbrains.kotlin.android"
id "kotlin-kapt"
id "dagger.hilt.android.plugin"
}

android {
Expand Down Expand Up @@ -30,6 +32,9 @@ android {
kotlinOptions {
jvmTarget = "11"
}
buildFeatures {
dataBinding true
}
}

dependencies {
Expand All @@ -51,4 +56,8 @@ dependencies {

// leakcanary ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜ ์ฒดํฌ
debugImplementation "com.squareup.leakcanary:leakcanary-android:$leakcanaryVersion"

// Hilt
implementation "com.google.dagger:hilt-android:$hiltVersion"
kapt "com.google.dagger:hilt-android-compiler:$hiltVersion"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.whyranoid.mogakrun

import android.app.Application
import com.whyranoid.mogakrun.util.TimberDebugTree
import dagger.hilt.android.HiltAndroidApp
import timber.log.Timber

@HiltAndroidApp
class MogakrunApplication : Application() {
override fun onCreate() {
super.onCreate()
Expand Down
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ buildscript {
leakcanaryVersion = "2.10"
kotlinxCoroutinesVersion = "1.6.4"
paging3Version = "3.1.1"
hiltVersion = "2.44"
lottieVersion = "5.2.0" // ๋ณ‘ํ•œ์ด : ๋กœํ‹ฐ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹น
activityKtxVersion = "1.6.1"
fragmentKtxVersion = "1.5.4"
lifecycleViewmodelKtxVersion = "2.5.1"
}
dependencies {
classpath "com.google.gms:google-services:$googleServiceVersion"
Expand All @@ -24,4 +29,5 @@ plugins {
id "com.android.library" version "7.3.1" apply false
id "org.jetbrains.kotlin.android" version "1.7.20" apply false
id 'org.jetbrains.kotlin.jvm' version '1.7.20' apply false
id "com.google.dagger.hilt.android" version "2.44" apply false
}
6 changes: 6 additions & 0 deletions data/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ plugins {
id "com.android.library"
id "org.jetbrains.kotlin.android"
id "com.google.gms.google-services"
id "kotlin-kapt"
id "dagger.hilt.android.plugin"
}

android {
Expand Down Expand Up @@ -42,4 +44,8 @@ dependencies {
implementation platform("com.google.firebase:firebase-bom:$firebaseVersion")
implementation "com.google.firebase:firebase-analytics-ktx"
implementation "com.google.firebase:firebase-firestore-ktx"

// Hilt
implementation "com.google.dagger:hilt-android:$hiltVersion"
kapt "com.google.dagger:hilt-android-compiler:$hiltVersion"
}
20 changes: 20 additions & 0 deletions data/src/main/java/com/whyranoid/data/di/NetworkModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.whyranoid.data.di

import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
class NetworkModule {

@Provides
@Singleton
fun provideFireStoreDatabase(): FirebaseFirestore =
Firebase.firestore
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.whyranoid.data.di.running

import kotlinx.coroutines.flow.Flow

interface RunningDataSource {

fun getCurrentRunnerCount(): Flow<Int>

suspend fun startRunning(uid: String): Boolean

suspend fun finishRunning(uid: String): Boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.whyranoid.data.di.running

import com.google.firebase.firestore.FieldValue
import com.google.firebase.firestore.FirebaseFirestore
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

class RunningDataSourceImpl(private val db: FirebaseFirestore) : RunningDataSource {

override fun getCurrentRunnerCount(): Flow<Int> = callbackFlow {
db.collection("Runners")
.document("runnersId")
.addSnapshotListener { snapshot, _ ->
snapshot?.let {
val count = it.data?.size ?: -1
trySend(count)
}
}

awaitClose()
}

override suspend fun startRunning(uid: String): Boolean {
return suspendCoroutine { continuation ->
db.collection("Runners")
.document("runnersId")
.update(uid, uid)
.addOnSuccessListener {
continuation.resume(true)
}
.addOnFailureListener {
continuation.resume(false)
}
}
}

override suspend fun finishRunning(uid: String): Boolean {
return suspendCoroutine { continuation ->
db.collection("Runners")
.document("runnersId")
.update(uid, FieldValue.delete())
.addOnSuccessListener {
continuation.resume(true)
}
.addOnFailureListener {
continuation.resume(false)
}
}
}
}
27 changes: 27 additions & 0 deletions data/src/main/java/com/whyranoid/data/di/running/RunningModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.whyranoid.data.di.running

import com.google.firebase.firestore.FirebaseFirestore
import com.whyranoid.data.running.RunningRepositoryImpl
import com.whyranoid.domain.repository.RunningRepository
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
class RunningModule {

@Provides
@Singleton
fun provideRunningDataSource(db: FirebaseFirestore): RunningDataSource {
return RunningDataSourceImpl(db)
}

@Provides
@Singleton
fun provideRunningRepository(runningDataSource: RunningDataSource): RunningRepository {
return RunningRepositoryImpl(runningDataSource)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.whyranoid.data.running

import com.whyranoid.data.di.running.RunningDataSource
import com.whyranoid.domain.repository.RunningRepository
import kotlinx.coroutines.flow.Flow

class RunningRepositoryImpl(private val runningDataSource: RunningDataSource) : RunningRepository {
override fun getCurrentRunnerCount(): Flow<Int> {
return runningDataSource.getCurrentRunnerCount()
}

override suspend fun startRunning(uid: String): Boolean {
return true
}

override suspend fun finishRunning(uid: String): Boolean {
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.whyranoid.domain.repository.RunningRepository
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

class GetCurrentRunnerCountUseCase @Inject constructor(private val runningRepository: RunningRepository) {
class GetRunnerCountUseCase @Inject constructor(private val runningRepository: RunningRepository) {
operator fun invoke(): Flow<Int> {
return runningRepository.getCurrentRunnerCount()
}
Expand Down
23 changes: 21 additions & 2 deletions presentation/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
plugins {
id "com.android.library"
id "org.jetbrains.kotlin.android"
id "kotlin-kapt"
id "dagger.hilt.android.plugin"
}

android {
Expand Down Expand Up @@ -28,13 +30,15 @@ android {
kotlinOptions {
jvmTarget = "11"
}
dataBinding {
enable = true
buildFeatures {
dataBinding true
}
}

dependencies {

implementation project(":domain")

implementation "androidx.core:core-ktx:$coreKtxVersion"
implementation "androidx.appcompat:appcompat:$appcompatVersion"
implementation "com.google.android.material:material:$materialVersion"
Expand All @@ -45,4 +49,19 @@ dependencies {

// timber ๋กœ๊ทธ
implementation "com.jakewharton.timber:timber:$timberVersion"

// ๋ณ‘ํ•œ์ด : ๋กœํ‹ฐ ๋””ํŽœ๋˜์‹œ ์ถ”๊ฐ€
// lottie
implementation "com.airbnb.android:lottie:$lottieVersion"

// Hilt
implementation "com.google.dagger:hilt-android:$hiltVersion"
kapt "com.google.dagger:hilt-android-compiler:$hiltVersion"

// by viewModels
implementation "androidx.activity:activity-ktx:$activityKtxVersion"
implementation "androidx.fragment:fragment-ktx:$fragmentKtxVersion"

// ViewModelScope
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleViewmodelKtxVersion"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package com.whyranoid.presentation

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ internal abstract class BaseFragment<VDB : ViewDataBinding>(
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
super.onCreateView(inflater, container, savedInstanceState)
_binding = DataBindingUtil.inflate(inflater, layoutRes, container, false)
binding.lifecycleOwner = viewLifecycleOwner
return binding.root
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.whyranoid.presentation.runningstart

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.whyranoid.presentation.R
import com.whyranoid.presentation.base.BaseFragment
import com.whyranoid.presentation.databinding.FragmentRunningStartBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
internal class RunningStartFragment :
BaseFragment<FragmentRunningStartBinding>(R.layout.fragment_running_start) {

private val viewModel by viewModels<RunningStartViewModel>()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.vm = viewModel

lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.runnerCount.collect { runnerCount ->
binding.tvRunnerCountNumber.text = runnerCount.toString()
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.whyranoid.presentation.runningstart

import androidx.lifecycle.ViewModel
import com.whyranoid.domain.usecase.GetRunnerCountUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class RunningStartViewModel @Inject constructor(
getRunnerCountUseCase: GetRunnerCountUseCase
) : ViewModel() {

val runnerCount = getRunnerCountUseCase()
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 894848f

Please sign in to comment.