Skip to content

Commit

Permalink
Update: Init Setting - Main
Browse files Browse the repository at this point in the history
  • Loading branch information
easyhz committed Feb 20, 2024
1 parent 762a89a commit d15c686
Show file tree
Hide file tree
Showing 38 changed files with 743 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
.externalNativeBuild
.cxx
local.properties
app/google-services.json
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
id("org.jetbrains.kotlin.android")
id("kotlin-kapt")
id("dagger.hilt.android.plugin")
id("com.google.gms.google-services")
}

android {
Expand All @@ -11,7 +12,7 @@ android {

defaultConfig {
applicationId = "com.easyhz.pico"
minSdk = 24
minSdk = 26
targetSdk = 34
versionCode = 1
versionName = "1.0"
Expand Down Expand Up @@ -53,8 +54,21 @@ dependencies {
androidTestImplementation(libs.ext.junit)
androidTestImplementation(libs.espresso.core)

implementation(libs.activity.ktx)
implementation(libs.fragment.ktx)

// dagger hilt
implementation(libs.hilt.android)
kapt(libs.hilt.compiler)

// firebase
implementation(platform(libs.firebase.bom))
implementation(libs.firebase.analytics)
implementation(libs.firebase.auth)
implementation(libs.firebase.firestore)

// Glide
implementation(libs.glide)
annotationProcessor(libs.compiler)

}
71 changes: 69 additions & 2 deletions app/src/main/java/com/easyhz/pico/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,79 @@ package com.easyhz.pico

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import com.easyhz.pico.databinding.ActivityMainBinding
import com.easyhz.pico.view.album.AlbumFragment
import com.easyhz.pico.view.album.AlbumViewModel
import com.easyhz.pico.view.settings.SettingsFragment
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private val viewModel by viewModels<AlbumViewModel>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setUp()
}

private fun setUp() {
initDefaultFragment()
setUpBottomNavigation()
setUpToolbar()
fetchAlbums()
}

private fun initDefaultFragment() {
getFragment(R.id.page_album)?.let { replaceFragment(it) }
}

private fun setUpBottomNavigation() {
binding.bottomNavigation.setOnItemSelectedListener { menuItem ->
getFragment(menuItem.itemId)?.let {
replaceFragment(it)
setToolbarTitle(menuItem.itemId)
}
true
}
}

private fun setUpToolbar() {
setSupportActionBar(binding.mainToolbar)
supportActionBar?.setDisplayShowTitleEnabled(false)
}

private fun replaceFragment(fragment: Fragment) {
supportFragmentManager.beginTransaction().replace(R.id.mainFrameLayout, fragment).commit()
}

private fun getMenu(id: Int) = BottomNavigationItemType.entries.find { it.itemId == id }

private fun getFragment(id: Int) = getMenu(id)?.fragment

private fun setToolbarTitle(itemId: Int) {
getMenu(itemId)?.let {
binding.toolbarTitle.text = getString(it.title)
}
}

private fun fetchAlbums() {
viewModel.fetchAlbums()
}

enum class BottomNavigationItemType(val itemId : Int, val title: Int) {
ALBUM(R.id.page_album, R.string.app_title) {
override val fragment: Fragment
get() = AlbumFragment()
},
SETTINGS(R.id.page_settings, R.string.app_settings_title) {
override val fragment: Fragment
get() = SettingsFragment()
};
abstract val fragment: Fragment
}
}
}
25 changes: 25 additions & 0 deletions app/src/main/java/com/easyhz/pico/data/entity/album/Album.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.easyhz.pico.data.entity.album

import com.google.firebase.Timestamp
import com.google.firebase.firestore.PropertyName

data class Album(
@PropertyName("creationTime")
val creationTime: Timestamp = Timestamp.now(),
@PropertyName("expireTime")
val expireTime: Timestamp = Timestamp.now(),
@PropertyName("imageCount")
val imageCount: Int = 0,
@PropertyName("imageSizes")
val imageSizes: List<ImageSize> = listOf(),
@PropertyName("imageURLs")
val imageUrls: List<String> = listOf(),
@PropertyName("ownerID")
val ownerId: String = "",
@PropertyName("tags")
val tags: List<String> = listOf(),
@PropertyName("thumbnailURL")
val thumbnailUrl: String = "",
@PropertyName("viewCount")
val viewCount: Int = 0
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.easyhz.pico.data.entity.album

data class ImageSize(
val height: Int = 0,
val width: Int = 0
)
8 changes: 8 additions & 0 deletions app/src/main/java/com/easyhz/pico/data/firebase/Constants.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.easyhz.pico.data.firebase

object Constants {
const val ALBUMS = "Albums"

const val OWNER_ID = "ownerID"
const val CREATION_TIME = "creationTime"
}
15 changes: 15 additions & 0 deletions app/src/main/java/com/easyhz/pico/data/mapper/AlbumMapper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.easyhz.pico.data.mapper

import com.easyhz.pico.data.entity.album.Album
import com.easyhz.pico.domain.model.AlbumItem
import com.easyhz.pico.util.toDateFormat
import com.easyhz.pico.util.toDay

fun List<Album>.toAlbumItem(): List<AlbumItem> = this.map {
AlbumItem(
regDate = it.creationTime.toDateFormat(),
expireDate = it.expireTime.toDay(),
tag = it.tags[0],
thumbnailUrl = it.thumbnailUrl
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.easyhz.pico.data.repository.album

import com.easyhz.pico.data.entity.album.Album
import com.easyhz.pico.data.firebase.Constants.ALBUMS
import com.easyhz.pico.data.firebase.Constants.CREATION_TIME
import com.easyhz.pico.data.firebase.Constants.OWNER_ID
import com.easyhz.pico.domain.repository.album.AlbumRepository
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.Query
import com.google.firebase.firestore.toObjects
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.tasks.await
import javax.inject.Inject

class AlbumRepositoryImpl
@Inject constructor(
private val fireStore: FirebaseFirestore,
): AlbumRepository {
override fun fetchAlbums() : Flow<List<Album>> = flow {
try {
// TODO: OWNER_ID 관리
val result = fireStore.collection(ALBUMS)
.whereEqualTo(OWNER_ID, "19bIo1GzbzPmZsjoHEeOTKf8WAm1")
.orderBy(CREATION_TIME, Query.Direction.DESCENDING)
.get()
.await()

val albums = result.toObjects<Album>()
emit(albums)
} catch (e: Exception) {
// 예외 처리
println("Error fetching albums: ${e.message}")
}
}

}
22 changes: 22 additions & 0 deletions app/src/main/java/com/easyhz/pico/di/FirebaseModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.easyhz.pico.di

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


@Module
@InstallIn(SingletonComponent::class)
object FirebaseModule {

@Provides
@Singleton
fun provideFireStore(): FirebaseFirestore {
return FirebaseFirestore.getInstance()
}


}
20 changes: 20 additions & 0 deletions app/src/main/java/com/easyhz/pico/di/RepositoryModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.easyhz.pico.di

import com.easyhz.pico.data.repository.album.AlbumRepositoryImpl
import com.easyhz.pico.domain.repository.album.AlbumRepository
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
abstract class RepositoryModule {

@Binds
@Singleton
abstract fun bindAlbumRepository(
albumRepositoryImpl: AlbumRepositoryImpl
): AlbumRepository
}
21 changes: 21 additions & 0 deletions app/src/main/java/com/easyhz/pico/di/UseCaseModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.easyhz.pico.di

import com.easyhz.pico.domain.repository.album.AlbumRepository
import com.easyhz.pico.domain.usecase.album.AlbumUseCase
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object UseCaseModule {

@Provides
@Singleton
fun provideAlbumUseCase(
albumRepository: AlbumRepository
): AlbumUseCase = AlbumUseCase(albumRepository)

}
8 changes: 8 additions & 0 deletions app/src/main/java/com/easyhz/pico/domain/model/AlbumItem.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.easyhz.pico.domain.model

data class AlbumItem(
val regDate: String,
val tag: String,
val expireDate: Long,
val thumbnailUrl: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.easyhz.pico.domain.repository.album

import com.easyhz.pico.data.entity.album.Album
import kotlinx.coroutines.flow.Flow

interface AlbumRepository {
fun fetchAlbums() : Flow<List<Album>>

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.easyhz.pico.domain.usecase.album

import com.easyhz.pico.data.entity.album.Album
import com.easyhz.pico.domain.repository.album.AlbumRepository
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

class AlbumUseCase
@Inject constructor(
private val repository: AlbumRepository
) {
operator fun invoke() : Flow<List<Album>> = repository.fetchAlbums()

}
19 changes: 19 additions & 0 deletions app/src/main/java/com/easyhz/pico/util/Extensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.easyhz.pico.util

import com.google.firebase.Timestamp
import java.time.Duration
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.Locale

fun Timestamp.toDateFormat(): String =
LocalDateTime.ofInstant(this.toDate().toInstant(), ZoneId.systemDefault())
.format(DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm", Locale.getDefault()))

fun Timestamp.toDay(): Long {
val expireDateTime = this.toDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime()
val currentDateTime = LocalDateTime.now()

return Duration.between(currentDateTime, expireDateTime).toDays()
}
Loading

0 comments on commit d15c686

Please sign in to comment.