Skip to content

Commit

Permalink
Merge branch 'main' into performance-class
Browse files Browse the repository at this point in the history
# Conflicts:
#	app/src/main/java/com/google/android/samples/socialite/di/DatabaseModule.kt
#	app/src/main/java/com/google/android/samples/socialite/ui/home/timeline/TimelineMediaItem.kt
  • Loading branch information
SigmanZero committed Feb 15, 2024
2 parents 6b47c74 + 6c59a22 commit a4c2ad6
Show file tree
Hide file tree
Showing 27 changed files with 136 additions and 69 deletions.
21 changes: 21 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
root = true

[*]
charset = utf-8
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.{kt,kts}]
ij_kotlin_imports_layout = *
ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = true
ktlint_code_style = intellij_idea
ktlint_function_naming_ignore_when_annotated_with = Composable

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2
36 changes: 36 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Pull Request

on:
pull_request:
types: [ opened, synchronize, reopened ]

permissions:
contents: write
actions: read

jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 60

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@v2

- name: Setup JDK
uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: 17

- name: Setup Gradle
uses: gradle/gradle-build-action@v3

- name: Check spotless
run: ./gradlew spotlessCheck

- name: Build App
run: ./gradlew :app:assembleDebug
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
/.idea/deploymentTargetDropDown.xml
/.idea/deploymentTargetSelector.xml
/.idea/misc.xml
/.idea/migrations.xml
/.idea/gradle.xml
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Here are the screens that make up SociaLite:

## Reporting Issues

You can report an [issue with a sample](https://github.com/android/social-sample/issues) using
You can report an [issue with a sample](https://github.com/android/socialite/issues) using
this repository. When doing so, make sure to specify which sample you are referring to.

## Contributions
Expand Down
12 changes: 6 additions & 6 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ dependencies {
implementation(libs.camera.lifecycle)
implementation(libs.camera.view)

implementation (libs.media3.common)
implementation (libs.media3.effect)
implementation (libs.media3.exoplayer)
implementation (libs.media3.transformer)
implementation (libs.media3.ui)
implementation(libs.media3.common)
implementation(libs.media3.effect)
implementation(libs.media3.exoplayer)
implementation(libs.media3.transformer)
implementation(libs.media3.ui)

implementation(libs.hilt.android)
implementation(libs.hilt.navigation.compose)
Expand All @@ -134,7 +134,7 @@ dependencies {

androidTestImplementation(libs.turbine)

//For photopicker feature
// For photopicker feature
implementation(libs.activity)

implementation(libs.coil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class AppDatabaseTest {

private lateinit var _db: AppDatabase
private var _contactsSize = Contact.CONTACTS.size + 1
private lateinit var db: AppDatabase
private var contactsSize = Contact.CONTACTS.size + 1

@Before
fun setup() {
_db = createTestDatabase()
db = createTestDatabase()
}

@Test
fun populateInitialData_AssureDataIsInitialized() = runBlocking {
val contacts = _db.contactDao().loadAll()
val chatDetails = _db.chatDao().loadAllDetails()
val contacts = db.contactDao().loadAll()
val chatDetails = db.chatDao().loadAllDetails()

// Assert
assertThat(contacts).hasSize(_contactsSize)
assertThat(contacts).hasSize(contactsSize)
assertThat(chatDetails).hasSize(4)
for (detail in chatDetails) {
assertThat(detail.attendees).hasSize(1)
val messages = _db.messageDao().loadAll(detail.chatWithLastMessage.id)
val messages = db.messageDao().loadAll(detail.chatWithLastMessage.id)
assertThat(messages).hasSize(2)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import android.content.Context
import android.content.Intent
import com.google.android.samples.socialite.repository.ChatRepository
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
* Handles the "Reply" action in the chat notification.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.asCoroutineDispatcher
import java.util.concurrent.Executors
import javax.inject.Qualifier
import javax.inject.Singleton
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.asCoroutineDispatcher

@Qualifier
annotation class AppCoroutineScope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import com.google.android.samples.socialite.data.MessageDao
import com.google.android.samples.socialite.di.AppCoroutineScope
import com.google.android.samples.socialite.model.ChatDetail
import com.google.android.samples.socialite.model.Message
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class ChatRepository @Inject internal constructor(
Expand Down Expand Up @@ -67,7 +67,8 @@ class ChatRepository @Inject internal constructor(
Message(
id = 0L,
chatId = chatId,
senderId = 0L, // User
// User
senderId = 0L,
text = text,
mediaUri = mediaUri,
mediaMimeType = mediaMimeType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ fun Bubble(chatId: Long) {
chatId = chatId,
foreground = false,
onBackPressed = null,
onCameraClick = {}, // TODO (donovanfm): Hook up camera button in the Bubble composable
onVideoClick = {}, // TODO (jolandaverhoef): Hook up play video button in the Bubble composable
onPhotoPickerClick = {}, // TODO (mayurikhin): Hook up camera button in the Bubble composable
// TODO (donovanfm): Hook up camera button in the Bubble composable
onCameraClick = {},
// TODO (jolandaverhoef): Hook up play video button in the Bubble composable
onVideoClick = {},
// TODO (mayurikhin): Hook up camera button in the Bubble composable
onPhotoPickerClick = {},
modifier = Modifier.fillMaxSize(),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import kotlinx.coroutines.flow.stateIn
* use in UI.
*/
context(ViewModel)
fun <T> Flow<T>.stateInUi(initialValue: T): StateFlow<T> {
fun <T> Flow<T>.stateInUi(
initialValue: T,
): StateFlow<T> {
return stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000L), initialValue)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
Expand Down Expand Up @@ -55,9 +54,7 @@ fun Main(
) {
val modifier = Modifier.fillMaxSize()
SocialTheme {
Surface(modifier = modifier) {
MainNavigation(modifier, shortcutParams)
}
MainNavigation(modifier, shortcutParams)
}
}

Expand Down Expand Up @@ -198,19 +195,19 @@ data class ShortcutParams(
)

object AnimationConstants {
private const val enterMillis = 250
private const val exitMillis = 250
private const val ENTER_MILLIS = 250
private const val EXIT_MILLIS = 250

val enterTransition = fadeIn(
animationSpec = tween(
durationMillis = enterMillis,
durationMillis = ENTER_MILLIS,
easing = FastOutLinearInEasing,
),
)

val exitTransition = fadeOut(
animationSpec = tween(
durationMillis = exitMillis,
durationMillis = EXIT_MILLIS,
easing = FastOutSlowInEasing,
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ import androidx.window.layout.FoldingFeature
import androidx.window.layout.WindowInfoTracker
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.rememberMultiplePermissionsState
import kotlin.reflect.KFunction1
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asExecutor
import kotlin.reflect.KFunction1

@OptIn(ExperimentalPermissionsApi::class)
@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ import androidx.lifecycle.viewModelScope
import com.google.android.samples.socialite.repository.ChatRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.Locale
import javax.inject.Inject
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch

@HiltViewModel
class CameraViewModel @Inject constructor(
Expand Down Expand Up @@ -331,5 +331,7 @@ enum class CameraState {
}

enum class CaptureMode {
PHOTO, VIDEO_READY, VIDEO_RECORDING
PHOTO,
VIDEO_READY,
VIDEO_RECORDING,
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ import android.net.Uri
data class Media(var uri: Uri, var mediaType: MediaType)

enum class MediaType {
PHOTO, VIDEO
PHOTO,
VIDEO,
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,33 @@ import androidx.lifecycle.viewModelScope
import com.google.android.samples.socialite.repository.ChatRepository
import com.google.android.samples.socialite.ui.stateInUi
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class ChatViewModel @Inject constructor(
private val repository: ChatRepository,
) : ViewModel() {

private val _chatId = MutableStateFlow(0L)
private val chatId = MutableStateFlow(0L)

@OptIn(ExperimentalCoroutinesApi::class)
private val _chat = _chatId.flatMapLatest { id -> repository.findChat(id) }
private val _chat = chatId.flatMapLatest { id -> repository.findChat(id) }

private val _attendees = _chat.map { c -> (c?.attendees ?: emptyList()).associateBy { it.id } }
private val attendees = _chat.map { c -> (c?.attendees ?: emptyList()).associateBy { it.id } }

@OptIn(ExperimentalCoroutinesApi::class)
private val _messages = _chatId.flatMapLatest { id -> repository.findMessages(id) }
private val _messages = chatId.flatMapLatest { id -> repository.findMessages(id) }

val chat = _chat.stateInUi(null)

val messages = combine(_messages, _attendees) { messages, attendees ->
val messages = combine(_messages, attendees) { messages, attendees ->
// Build a list of `ChatMessage` from this list of `Message`.
buildList {
for (i in messages.indices) {
Expand Down Expand Up @@ -83,7 +83,7 @@ class ChatViewModel @Inject constructor(
* suppressing further notifications.
*/
fun setForeground(foreground: Boolean) {
val chatId = _chatId.value
val chatId = chatId.value
if (chatId != 0L) {
if (foreground) {
repository.activateChat(chatId)
Expand All @@ -94,7 +94,7 @@ class ChatViewModel @Inject constructor(
}

fun setChatId(chatId: Long) {
_chatId.value = chatId
this.chatId.value = chatId
}

fun updateInput(input: String) {
Expand All @@ -108,7 +108,7 @@ class ChatViewModel @Inject constructor(
}

fun send() {
val chatId = _chatId.value
val chatId = chatId.value
if (chatId <= 0) return
val input = _input.value
if (!isInputValid(input)) return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ fun Home(
onDestinationChanged = { destination = it },
)
},
containerColor = Color.Transparent,
) { innerPadding ->
val navController = rememberNavController()
HomeBackground(modifier = Modifier.fillMaxSize())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import com.google.android.samples.socialite.data.DatabaseManager
import com.google.android.samples.socialite.repository.ChatRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject

@HiltViewModel
class SettingsViewModel @Inject constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ data class TimelineMediaItem(
)

enum class TimelineMediaType {
PHOTO, VIDEO
PHOTO,
VIDEO,
}
Loading

0 comments on commit a4c2ad6

Please sign in to comment.