Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add staff list page #557

Merged
merged 4 commits into from
Jan 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import io.github.droidkaigi.confsched2019.data.api.response.Response
import io.github.droidkaigi.confsched2019.data.api.response.ResponseImpl
import io.github.droidkaigi.confsched2019.data.api.response.SponsorResponse
import io.github.droidkaigi.confsched2019.data.api.response.SponsorResponseImpl
import io.github.droidkaigi.confsched2019.data.api.response.StaffItemResponseImpl
import io.github.droidkaigi.confsched2019.data.api.response.StaffResponse
import io.github.droidkaigi.confsched2019.data.api.response.StaffResponseImpl
import io.ktor.client.HttpClient
import io.ktor.client.request.accept
import io.ktor.client.request.get
Expand Down Expand Up @@ -62,4 +65,13 @@ open class KtorDroidKaigiApi constructor(
accept(ContentType.Application.Json)
}
}

override suspend fun getStaffs(): StaffResponse {
val rawResponse = httpClient.get<String> {
url("$apiEndpoint/staffs")
accept(ContentType.Application.Json)
}

return StaffResponseImpl(JSON.parse(StaffItemResponseImpl.serializer().list, rawResponse))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.droidkaigi.confsched2019.data.api.response

import kotlinx.serialization.Serializable

@Serializable
data class StaffItemResponseImpl(
override val id: String?,
override val name: String?,
override val iconUrl: String?,
override val profileUrl: String?
) : StaffItemResponse
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.droidkaigi.confsched2019.data.api.response

import kotlinx.serialization.Serializable

@Serializable
data class StaffResponseImpl(
override val staffs: List<StaffItemResponseImpl>
) : StaffResponse
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.github.droidkaigi.confsched2019.data.api.parameter.LangParameter
import io.github.droidkaigi.confsched2019.data.api.response.AnnouncementListResponse
import io.github.droidkaigi.confsched2019.data.api.response.Response
import io.github.droidkaigi.confsched2019.data.api.response.SponsorResponse
import io.github.droidkaigi.confsched2019.data.api.response.StaffResponse

interface DroidKaigiApi {
suspend fun getSessions(): Response
Expand All @@ -13,4 +14,6 @@ interface DroidKaigiApi {
suspend fun getSponsors(): SponsorResponse

suspend fun getAnnouncements(lang: LangParameter): AnnouncementListResponse

suspend fun getStaffs(): StaffResponse
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.droidkaigi.confsched2019.data.api.response

interface StaffItemResponse {
val id: String?
val name: String?
val iconUrl: String?
val profileUrl: String?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.github.droidkaigi.confsched2019.data.api.response

interface StaffResponse {
val staffs: List<StaffItemResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import io.github.droidkaigi.confsched2019.data.db.dao.SessionFeedbackDao
import io.github.droidkaigi.confsched2019.data.db.dao.SessionSpeakerJoinDao
import io.github.droidkaigi.confsched2019.data.db.dao.SpeakerDao
import io.github.droidkaigi.confsched2019.data.db.dao.SponsorDao
import io.github.droidkaigi.confsched2019.data.db.dao.StaffDao
import io.github.droidkaigi.confsched2019.data.db.entity.AnnouncementEntityImpl
import io.github.droidkaigi.confsched2019.data.db.entity.SessionEntityImpl
import io.github.droidkaigi.confsched2019.data.db.entity.SessionFeedbackImpl
import io.github.droidkaigi.confsched2019.data.db.entity.SessionSpeakerJoinEntityImpl
import io.github.droidkaigi.confsched2019.data.db.entity.SpeakerEntityImpl
import io.github.droidkaigi.confsched2019.data.db.entity.SponsorEntityImpl
import io.github.droidkaigi.confsched2019.data.db.entity.StaffEntityImpl

@Database(
entities = [
Expand All @@ -23,9 +25,10 @@ import io.github.droidkaigi.confsched2019.data.db.entity.SponsorEntityImpl
(SessionSpeakerJoinEntityImpl::class),
(SponsorEntityImpl::class),
(SessionFeedbackImpl::class),
(AnnouncementEntityImpl::class)
(AnnouncementEntityImpl::class),
(StaffEntityImpl::class)
],
version = 11
version = 12
)
abstract class CacheDatabase : RoomDatabase() {
abstract fun sessionDao(): SessionDao
Expand All @@ -34,6 +37,7 @@ abstract class CacheDatabase : RoomDatabase() {
abstract fun sponsorDao(): SponsorDao
abstract fun sessionFeedbackDao(): SessionFeedbackDao
abstract fun announcementDao(): AnnouncementDao
abstract fun staffDao(): StaffDao
fun sqlite(): SupportSQLiteDatabase {
return mDatabase
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface DbComponent {
fun sessionDatabase(): SessionDatabase
fun sponsorDatabase(): SponsorDatabase
fun announcementDatabase(): AnnouncementDatabase
fun staffDatabase(): StaffDatabase

@Component.Builder
interface Builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import io.github.droidkaigi.confsched2019.data.db.dao.SessionDao
import io.github.droidkaigi.confsched2019.data.db.dao.SessionSpeakerJoinDao
import io.github.droidkaigi.confsched2019.data.db.dao.SpeakerDao
import io.github.droidkaigi.confsched2019.data.db.dao.SponsorDao
import io.github.droidkaigi.confsched2019.data.db.dao.StaffDao
import javax.inject.Singleton

@Module(includes = [DbModule.Providers::class])
internal abstract class DbModule {
@Binds abstract fun sessionDatabase(impl: RoomDatabase): SessionDatabase
@Binds abstract fun sponsorDatabase(impl: RoomDatabase): SponsorDatabase
@Binds abstract fun announcementDatabase(impl: RoomDatabase): AnnouncementDatabase
@Binds abstract fun staffDatabase(impl: RoomDatabase): StaffDatabase

@Module
internal object Providers {
Expand Down Expand Up @@ -54,5 +56,9 @@ internal abstract class DbModule {
@JvmStatic @Provides fun announcementDao(database: CacheDatabase): AnnouncementDao {
return database.announcementDao()
}

@JvmStatic @Provides fun staffDao(database: CacheDatabase): StaffDao {
return database.staffDao()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ package io.github.droidkaigi.confsched2019.data.db
import io.github.droidkaigi.confsched2019.data.api.response.AnnouncementResponse
import io.github.droidkaigi.confsched2019.data.api.response.Response
import io.github.droidkaigi.confsched2019.data.api.response.SponsorResponse
import io.github.droidkaigi.confsched2019.data.api.response.StaffResponse
import io.github.droidkaigi.confsched2019.data.db.dao.AnnouncementDao
import io.github.droidkaigi.confsched2019.data.db.dao.SessionDao
import io.github.droidkaigi.confsched2019.data.db.dao.SessionSpeakerJoinDao
import io.github.droidkaigi.confsched2019.data.db.dao.SpeakerDao
import io.github.droidkaigi.confsched2019.data.db.dao.SponsorDao
import io.github.droidkaigi.confsched2019.data.db.dao.StaffDao
import io.github.droidkaigi.confsched2019.data.db.entity.AnnouncementEntity
import io.github.droidkaigi.confsched2019.data.db.entity.SessionWithSpeakers
import io.github.droidkaigi.confsched2019.data.db.entity.SpeakerEntity
import io.github.droidkaigi.confsched2019.data.db.entity.SponsorEntity
import io.github.droidkaigi.confsched2019.data.db.entity.StaffEntity
import io.github.droidkaigi.confsched2019.data.db.entity.mapper.toAnnouncementEntities
import io.github.droidkaigi.confsched2019.data.db.entity.mapper.toSessionEntities
import io.github.droidkaigi.confsched2019.data.db.entity.mapper.toSessionSpeakerJoinEntities
import io.github.droidkaigi.confsched2019.data.db.entity.mapper.toSpeakerEntities
import io.github.droidkaigi.confsched2019.data.db.entity.mapper.toSponsorEntities
import io.github.droidkaigi.confsched2019.data.db.entity.mapper.toStaffEntities
import io.github.droidkaigi.confsched2019.model.SponsorCategory
import kotlinx.coroutines.withContext
import javax.inject.Inject
Expand All @@ -29,8 +33,9 @@ class RoomDatabase @Inject constructor(
private val sessionSpeakerJoinDao: SessionSpeakerJoinDao,
private val coroutineContext: CoroutineContext,
private val sponsorDao: SponsorDao,
private val announcementDao: AnnouncementDao
) : SessionDatabase, SponsorDatabase, AnnouncementDatabase {
private val announcementDao: AnnouncementDao,
private val staffDao: StaffDao
) : SessionDatabase, SponsorDatabase, AnnouncementDatabase, StaffDatabase {
override suspend fun sessions(): List<SessionWithSpeakers> {
return sessionSpeakerJoinDao.getAllSessions()
}
Expand Down Expand Up @@ -92,4 +97,16 @@ class RoomDatabase @Inject constructor(
}
}
}

override suspend fun staffs(): List<StaffEntity> = staffDao.allStaffs()

override suspend fun save(apiResponse: StaffResponse) {
withContext(coroutineContext) {
database.runInTransaction {
val staffs = apiResponse.staffs.toStaffEntities()
staffDao.deleteAll()
staffDao.insert(staffs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should use "deleteAll()" before insert(). Because If someone quits the staff for some reason, it seems to remain. 👀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good catch 💯

}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.github.droidkaigi.confsched2019.data.db.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import io.github.droidkaigi.confsched2019.data.db.entity.StaffEntityImpl

@Dao
abstract class StaffDao {
@Query("SELECT * FROM staff")
abstract suspend fun allStaffs(): List<StaffEntityImpl>

@Insert(onConflict = OnConflictStrategy.REPLACE)
abstract fun insert(staffs: List<StaffEntityImpl>)

@Query("DELETE FROM staff")
abstract fun deleteAll()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.github.droidkaigi.confsched2019.data.db.entity

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "staff")
class StaffEntityImpl(
@PrimaryKey override var id: String,
@ColumnInfo(name = "staff_name")
override var name: String,
@ColumnInfo(name = "staff_icon_url")
override var iconUrl: String?,
@ColumnInfo(name = "staff_profile_url")
override var profileUrl: String?
) : StaffEntity
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.droidkaigi.confsched2019.data.db.entity.mapper

import io.github.droidkaigi.confsched2019.data.api.response.StaffItemResponse
import io.github.droidkaigi.confsched2019.data.db.entity.StaffEntityImpl

fun List<StaffItemResponse>.toStaffEntities(): List<StaffEntityImpl> = map {
it.toStaffEntityImpl()
}

fun StaffItemResponse.toStaffEntityImpl(): StaffEntityImpl {
return StaffEntityImpl(
id = requireNotNull(id),
name = requireNotNull(name),
iconUrl = iconUrl,
profileUrl = profileUrl
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.github.droidkaigi.confsched2019.data.db

import io.github.droidkaigi.confsched2019.data.api.response.StaffResponse
import io.github.droidkaigi.confsched2019.data.db.entity.StaffEntity

interface StaffDatabase {
suspend fun staffs(): List<StaffEntity>
suspend fun save(apiResponse: StaffResponse)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.droidkaigi.confsched2019.data.db.entity

interface StaffEntity {
var id: String
var name: String
var iconUrl: String?
var profileUrl: String?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.droidkaigi.confsched2019.data.repository

import io.github.droidkaigi.confsched2019.data.api.DroidKaigiApi
import io.github.droidkaigi.confsched2019.data.db.StaffDatabase
import io.github.droidkaigi.confsched2019.data.db.entity.StaffEntity
import io.github.droidkaigi.confsched2019.model.Staff
import io.github.droidkaigi.confsched2019.model.StaffContents
import javax.inject.Inject

class DataStaffRepository @Inject constructor(
private val api: DroidKaigiApi,
private val staffDatabase: StaffDatabase
) : StaffRepository {
override suspend fun staffContents() = StaffContents(staffs())

override suspend fun refresh() {
val response = api.getStaffs()
staffDatabase.save(response)
}

private suspend fun staffs() = staffDatabase
.staffs()
.map { it.toStaff() }
}

private fun StaffEntity.toStaff(): Staff = Staff(id, name, iconUrl, profileUrl)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.github.droidkaigi.confsched2019.data.api.GoogleFormApi
import io.github.droidkaigi.confsched2019.data.db.AnnouncementDatabase
import io.github.droidkaigi.confsched2019.data.db.SessionDatabase
import io.github.droidkaigi.confsched2019.data.db.SponsorDatabase
import io.github.droidkaigi.confsched2019.data.db.StaffDatabase
import io.github.droidkaigi.confsched2019.data.firestore.Firestore
import javax.inject.Singleton

Expand All @@ -20,6 +21,7 @@ interface RepositoryComponent {
fun sessionRepository(): SessionRepository
fun sponsorRepository(): SponsorRepository
fun announcementRepository(): AnnouncementRepository
fun staffRepository(): StaffRepository

@Component.Builder
interface Builder {
Expand All @@ -30,6 +32,7 @@ interface RepositoryComponent {
@BindsInstance fun database(database: SessionDatabase): Builder
@BindsInstance fun sponsorDatabase(database: SponsorDatabase): Builder
@BindsInstance fun announcementDatabase(database: AnnouncementDatabase): Builder
@BindsInstance fun staffDatabase(database: StaffDatabase): Builder

@BindsInstance fun firestore(firestore: Firestore): Builder

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ internal abstract class RepositoryModule {
@Binds
abstract fun announcementDatabase(impl: DataAnnouncementRepository): AnnouncementRepository

@Binds abstract fun staffDatabase(impl: DataStaffRepository): StaffRepository

@Module
internal object Providers
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.droidkaigi.confsched2019.data.repository

import io.github.droidkaigi.confsched2019.model.StaffContents

interface StaffRepository {
suspend fun staffContents(): StaffContents
suspend fun refresh()
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.droidkaigi.confsched2019.about.ui.item

import android.widget.Toast
import androidx.fragment.app.FragmentActivity
import androidx.navigation.Navigation
import com.google.android.gms.oss.licenses.OssLicensesMenuActivity
Expand Down Expand Up @@ -28,7 +27,8 @@ class AboutSection @Inject constructor(
name = R.string.about_staff_list,
description = R.string.about_check
) {
Toast.makeText(it, "FIXME!!", Toast.LENGTH_SHORT).show()
Navigation.findNavController(activity, R.id.root_nav_host_fragment)
.navigate(R.id.staff_search)
},
AboutItem(
name = R.string.about_privacy_policy,
Expand Down
Loading