Skip to content

Commit

Permalink
Add & Fix: adding clickable challenge, tweaks on home screen, fixing …
Browse files Browse the repository at this point in the history
…daily streak logic'
  • Loading branch information
ahmrh committed Jun 5, 2024
1 parent 0c8606a commit e3220db
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 96 deletions.
43 changes: 17 additions & 26 deletions app/src/main/java/com/ahmrh/serene/common/utils/DateUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ object DateUtils {
return "Joined " + monthYearDateFormat.format(date)
}

fun formatSimpleDate(date: Date): String {
val format = SimpleDateFormat("MMM dd, yyyy")

return format.format(date)
}

fun getDayStreak(dates: List<Date>): Int {
if (dates.isEmpty()) {
return 0
Expand All @@ -42,38 +48,23 @@ object DateUtils {
var currentStreak = 1
var previousDate = sortedDates[0]

val calendar = Calendar.getInstance()

for (date in sortedDates.subList(1, sortedDates.size)) {
val daysDifference = daysBetween(previousDate, date)
if (daysDifference == 1L) {
calendar.time = previousDate
calendar.add(Calendar.DAY_OF_YEAR, 1)
val nextDay = calendar.time

if (isSameDay(date, nextDay)) {
currentStreak++
} else {
break
} else if (date.after(nextDay)) {
return 0 // Streak is broken
}
previousDate = date
}

// Check for broken streak even with previous entries
return if (currentStreak == 0 && sortedDates.size > 1) {
0 // No streak or broken streak
} else {
currentStreak
previousDate = date
}
}

fun calculateStreak(dateList: List<Date>): Int {
var streak = 1 // Start with 1 to count the first date in the streak
for (i in 1 until dateList.size) {
val previousDate = Calendar.getInstance().apply { time = dateList[i - 1] }
val currentDate = Calendar.getInstance().apply { time = dateList[i] }
previousDate.add(Calendar.DATE, -1)

if (DateUtils.isSameDay(previousDate.time, currentDate.time)) {
streak++
} else {
break
}
}
return streak
return currentStreak
}

// Function to calculate the difference in days between two dates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ class UserRepository @Inject constructor(
description =
if (language == Language.ID.code) achievementResponse.description?.id
else achievementResponse.description?.en,
category = achievementResponse.category
category = achievementResponse.category,
)
}

Expand Down Expand Up @@ -418,15 +418,15 @@ class UserRepository @Inject constructor(
.await()
.toObject<AchievementResponse>()!!

val achievement = achievementResponse.toMap()
val achievementMap = achievementResponse.toMap()

val userId = auth.currentUser?.uid ?: return

firestore.collection("users")
.document(userId)
.collection("achievements")
.document(achievementId)
.set(achievement)
.set(achievementMap)
.addOnCompleteListener {
onResult(it.exception)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.ahmrh.serene.data.source.remote.response

import com.ahmrh.serene.common.utils.DateUtils
import com.ahmrh.serene.domain.model.personalization.PersonalizationPoint
import com.google.gson.annotations.SerializedName
import java.util.Date

data class AchievementResponse(

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.ahmrh.serene.domain.model.gamification

import android.net.Uri
import com.ahmrh.serene.data.source.remote.response.AchievementResponse
import com.ahmrh.serene.data.source.remote.response.Description
import com.ahmrh.serene.domain.model.personalization.PersonalizationPoint
import com.google.gson.annotations.SerializedName
import java.util.Date

data class Achievement(
val id: String? = null,
Expand All @@ -18,6 +20,10 @@ data class Achievement(

val description: String? = null,

val category: String? = null
val category: String? = null,

val date: Date? = null
)



Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.util.Date

data class DailyStreak(
val count: Int,
val date: Date
val date: Date // ga kepake
)
fun DailyStreak.toMap(): Map<String, Any> {
val map = mutableMapOf<String, Any>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,15 @@ class PracticeSelfCareUseCase @Inject constructor(
val todayDate = Calendar.getInstance().time

if (DateUtils.isSameDay(todayDate, dateList[0])) {
// Calculate streak including today
val dayStreakCount = DateUtils.calculateStreak(dateList)
return DailyStreak(dayStreakCount, todayDate)
// If there is an entry for today, return null
return null
}
else {
// No self-care activity for today, return streak up to yesterday
val yesterdayDate = Calendar.getInstance().apply { add(Calendar.DATE, -1) }.time
if (DateUtils.isSameDay(yesterdayDate, dateList[0])) {
val dayStreakCount = DateUtils.calculateStreak(dateList)
return DailyStreak(dayStreakCount, yesterdayDate)
} else {
return DailyStreak(0, todayDate)
}
}
// if (dateList.size >= 2) {

// If there is no entry for today, calculate the streak
val dayStreakCount = DateUtils.getDayStreak(dateList)
return DailyStreak(dayStreakCount, todayDate)

// if (dateList.size >= 1) {
//
// val previousDate = dateList[1]
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ import com.ahmrh.serene.ui.component.textfield.SereneTextField
import com.ahmrh.serene.ui.navigation.Destination
import com.ahmrh.serene.ui.screen.main.activity.practice.LoadingContent
import com.ahmrh.serene.ui.theme.SereneTheme
import com.google.firebase.Firebase
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.auth
import com.google.firebase.auth.ktx.auth


@OptIn(ExperimentalMaterial3Api::class)
Expand Down Expand Up @@ -190,19 +194,24 @@ fun RegisterScreen(
)


TextButton(onClick = {
navController.navigate(
Destination.Auth.Login.route
) {
popUpTo(Destination.Auth.SetUpProfile.route) {
saveState = true
val firebaseUser = Firebase.auth.currentUser

if(firebaseUser == null){

TextButton(onClick = {
navController.navigate(
Destination.Auth.Login.route
) {
popUpTo(Destination.Auth.SetUpProfile.route) {
saveState = true
}
}
}) {
Text(
"Already have an account?",
fontWeight = FontWeight.Bold
)
}
}) {
Text(
"Already have an account?",
fontWeight = FontWeight.Bold
)
}
}

Expand All @@ -223,6 +232,8 @@ fun RegisterScreen(

}



var openErrorDialog by remember { mutableStateOf(false) }

when (uiState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@ fun AchievementDetailScreen(
},
actions = {

IconButton(onClick = {}) {

Icon(
painter = painterResource(
id = R.drawable.serene_icon_share
),
contentDescription = null,
)

}
// IconButton(onClick = {}) {
//
// Icon(
// painter = painterResource(
// id = R.drawable.serene_icon_share
// ),
// contentDescription = null,
// )
//
// }
})
}
) {
Expand Down Expand Up @@ -136,13 +136,13 @@ fun AchievementDetailScreen(
Text("${achievement.name}", style = MaterialTheme.typography.titleLarge)

Spacer(modifier = Modifier.height(16.dp))

ElevatedFilterChip(
selected = false,
onClick = { },
label = {
Text("Dec 19, 2023", style = MaterialTheme.typography.titleSmall)
})
//
// ElevatedFilterChip(
// selected = false,
// onClick = { },
// label = {
// Text("Dec 19, 2023", style = MaterialTheme.typography.titleSmall)
// })

Spacer(modifier = Modifier.height(16.dp))
Text("${achievement.description}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import coil.compose.SubcomposeAsyncImageContent
import coil.request.ImageRequest
import com.ahmrh.serene.R
import com.ahmrh.serene.common.state.UiState
import com.ahmrh.serene.common.utils.DateUtils
import com.ahmrh.serene.domain.model.gamification.Achievement
import com.ahmrh.serene.domain.model.gamification.DailyStreak
import com.ahmrh.serene.ui.navigation.Destination
Expand Down Expand Up @@ -144,16 +145,16 @@ fun DailyStreakEvent(
title = {},
actions = {

IconButton(onClick = {}) {

Icon(
painter = painterResource(
id = R.drawable.serene_icon_share
),
contentDescription = null,
)

}
// IconButton(onClick = {}) {
//
// Icon(
// painter = painterResource(
// id = R.drawable.serene_icon_share
// ),
// contentDescription = null,
// )
//
// }
})
}
) {
Expand Down Expand Up @@ -269,16 +270,16 @@ fun AchievementEvent(
title = {},
actions = {

IconButton(onClick = {}) {

Icon(
painter = painterResource(
id = R.drawable.serene_icon_share
),
contentDescription = null,
)

}
// IconButton(onClick = {}) {
//
// Icon(
// painter = painterResource(
// id = R.drawable.serene_icon_share
// ),
// contentDescription = null,
// )
//
// }
})
}
) {
Expand Down Expand Up @@ -336,12 +337,13 @@ fun AchievementEvent(

Spacer(modifier = Modifier.height(16.dp))

val date = Calendar.getInstance().time
ElevatedFilterChip(
selected = false,
onClick = { },
label = {
Text(
"Dec 19, 2023",
DateUtils.formatSimpleDate(date),
style = MaterialTheme.typography.titleSmall
)
})
Expand Down
Loading

0 comments on commit e3220db

Please sign in to comment.