Skip to content

Commit

Permalink
Merge pull request #21 from LeoAndo/main
Browse files Browse the repository at this point in the history
Main-> develop
  • Loading branch information
LeoAndo committed Aug 2, 2023
2 parents 945c91a + 817fc5b commit 9f38cdc
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 44 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ android {
applicationId "com.leoleo.quizapp"
minSdk 29
targetSdk 34
versionCode 102
versionName "1.0.2"
versionCode 103
versionName "1.0.3"

testInstrumentationRunner "com.leoleo.androidapptemplate.CustomTestRunner"
vectorDrawables {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.res.stringResource
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.leoleo.androidapptemplate.domain.repository.QuizRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.leoleo.androidapptemplate.data.quiz.dao
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import com.leoleo.androidapptemplate.data.quiz.entity.COLUMN_TITLE
import com.leoleo.androidapptemplate.data.quiz.entity.CompletedQuizEntity
import com.leoleo.androidapptemplate.data.quiz.entity.TABLE_NAME_COMPLETED_QUIZ
Expand All @@ -14,15 +13,9 @@ internal interface CompletedQuizDao {
@Insert
suspend fun insertQuizData(vararg entity: CompletedQuizEntity)

@Update
suspend fun updateQuizData(vararg entity: CompletedQuizEntity)

@Query("SELECT * FROM $TABLE_NAME_COMPLETED_QUIZ")
fun observeAllCompletedQuiz(): Flow<List<CompletedQuizEntity>>

@Query("SELECT * FROM $TABLE_NAME_COMPLETED_QUIZ")
suspend fun selectAllCompletedQuiz(): List<CompletedQuizEntity>

@Query("SELECT * FROM $TABLE_NAME_COMPLETED_QUIZ WHERE $COLUMN_TITLE = :title")
suspend fun selectCompletedQuizByTitle(title: String): List<CompletedQuizEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,22 @@ package com.leoleo.androidapptemplate.data.repository
import com.leoleo.androidapptemplate.data.quiz.dao.CompletedQuizDao
import com.leoleo.androidapptemplate.data.quiz.entity.CompletedQuizEntity
import com.leoleo.androidapptemplate.data.quiz.entity.toModels
import com.leoleo.androidapptemplate.domain.di.IoDispatcher
import com.leoleo.androidapptemplate.domain.exception.callOrThrow
import com.leoleo.androidapptemplate.domain.model.CompletedQuiz
import com.leoleo.androidapptemplate.domain.repository.QuizRepository
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import javax.inject.Inject

internal class QuizRepositoryImpl @Inject constructor(
private val dao: CompletedQuizDao,
@IoDispatcher private val dispatcher: CoroutineDispatcher,
) : QuizRepository {
internal class QuizRepositoryImpl @Inject constructor(private val dao: CompletedQuizDao) :
QuizRepository {
override fun observeAllCompletedQuiz(): Flow<List<CompletedQuiz>> {
return dao.observeAllCompletedQuiz().map { it.toModels() }.flowOn(dispatcher)
}

override suspend fun getCompletedQuizList(): List<CompletedQuiz> {
return callOrThrow(dispatcher) { dao.selectAllCompletedQuiz().toModels() }
return dao.observeAllCompletedQuiz().map { it.toModels() }
}

override suspend fun addCompleteData(title: String, completedTime: Long) {
callOrThrow(dispatcher) {
val data = CompletedQuizEntity(title = title, completedTime = completedTime)
if (dao.selectCompletedQuizByTitle(title).isEmpty()) {
dao.insertQuizData(data)
}
val data = CompletedQuizEntity(title = title, completedTime = completedTime)
if (dao.selectCompletedQuizByTitle(title).isEmpty()) {
dao.insertQuizData(data)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.leoleo.androidapptemplate.domain.exception

sealed class ApiException : AppException() {
data class UnAuthorized(override val message: String?) : ApiException()
data class Forbidden(override val message: String?) : ApiException()
object Network : ApiException()
data class UnprocessableEntity(override val message: String?) : ApiException()
data class Unknown(override val message: String?) : ApiException()
data class NotFound(override val message: String?) : ApiException()
object Redirect : ApiException()
object Server : ApiException()
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.leoleo.androidapptemplate.domain.exception

open class AppException : Exception()
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext
import kotlin.jvm.Throws

@Throws(AppErrorType::class)
suspend fun <T> callOrThrow(
@Throws(ApiException::class)
suspend fun <T> callApiOrThrow(
dispatcher: CoroutineDispatcher,
apiCall: suspend () -> T
): T {
Expand All @@ -14,7 +14,7 @@ suspend fun <T> callOrThrow(
apiCall.invoke()
} catch (e: Throwable) {
// TODO: アプリ固有のExceptionをthrowする
throw AppErrorType.Unknown(e.message)
throw ApiException.Unknown(e.message)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ import kotlinx.coroutines.flow.Flow

interface QuizRepository {
fun observeAllCompletedQuiz(): Flow<List<CompletedQuiz>>
suspend fun getCompletedQuizList(): List<CompletedQuiz>
suspend fun addCompleteData(title: String, completedTime: Long)
}

0 comments on commit 9f38cdc

Please sign in to comment.