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

LoginPage #2

Merged
merged 6 commits into from
Apr 14, 2024
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
79 changes: 66 additions & 13 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,68 @@
# Gradle files
.gradle/
build/

# Log/OS Files
*.log

# Android Studio generated files and folders
captures/
.externalNativeBuild/
.cxx/
*.apk
output.json

# IntelliJ
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
.idea/
misc.xml
deploymentTargetDropDown.xml
render.experimental.xml

# Keystore files
*.jks
*.keystore

# Google Services (e.g. APIs or Firebase)
google-services.json

# Android Profiling
*.hprof

# Built application files
/*/build/

# Crashlytics configuations
com_crashlytics_export_strings.xml

# Local configuration file (sdk path, etc)
local.properties

# Signing files
.signing/

# User-specific configurations
.idea/libraries/
.idea/workspace.xml
.idea/tasks.xml
.idea/.name
.idea/compiler.xml
.idea/copyright/profiles_settings.xml
.idea/encodings.xml
.idea/misc.xml
.idea/modules.xml
.idea/scopes/scope_settings.xml
.idea/vcs.xml
.idea/assetWizardSettings.xml
.idea/caches
#*.iml

# OS-specific files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
/.idea/
2 changes: 1 addition & 1 deletion .idea/vcs.xml

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

8 changes: 7 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {

defaultConfig {
applicationId = "com.example.sharedcalendar"
minSdk = 24
minSdk = 26
targetSdk = 34
versionCode = 1
versionName = "1.0"
Expand All @@ -33,6 +33,9 @@ android {
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
viewBinding = true
}
}

dependencies {
Expand All @@ -41,6 +44,9 @@ dependencies {
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.11.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.annotation:annotation:1.6.0")
implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.6.1")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SharedCalendar"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
tools:targetApi="31">
<activity
android:name=".ui.login.LoginActivity"
android:exported="false"
android:label="@string/title_activity_login" />
<activity
android:name=".MainActivity"
android:exported="true">
Expand Down
33 changes: 33 additions & 0 deletions app/src/main/java/com/example/sharedcalendar/CalendarAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.sharedcalendar

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView


class CalendarAdapter(
private val daysOfMonth: ArrayList<String>,
private val onItemListener: OnItemListener
) :
RecyclerView.Adapter<CalendarViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CalendarViewHolder {
val inflater = LayoutInflater.from(parent.context)
val view: View = inflater.inflate(R.layout.calendar_cell,parent,false)
val layoutParams = view.layoutParams
layoutParams.height = (parent.height * 0.166666666).toInt()
return CalendarViewHolder(view, onItemListener)
}

override fun onBindViewHolder(holder: CalendarViewHolder, position: Int) {
holder.dayOfMonth.text = daysOfMonth[position]
}

override fun getItemCount(): Int {
return daysOfMonth.size
}

interface OnItemListener {
fun onItemClick(position: Int, dayText: String?)
}
}
22 changes: 22 additions & 0 deletions app/src/main/java/com/example/sharedcalendar/CalendarViewHolder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.sharedcalendar

import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView


class CalendarViewHolder(itemView: View, onItemListener: CalendarAdapter.OnItemListener) :
RecyclerView.ViewHolder(itemView), View.OnClickListener {
val dayOfMonth: TextView
private val onItemListener: CalendarAdapter.OnItemListener

init {
dayOfMonth = itemView.findViewById<TextView>(R.id.cellDayText)
this.onItemListener = onItemListener
itemView.setOnClickListener(this)
}

override fun onClick(view: View) {
onItemListener.onItemClick(adapterPosition, dayOfMonth.text as String)
}
}
83 changes: 81 additions & 2 deletions app/src/main/java/com/example/sharedcalendar/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,92 @@
package com.example.sharedcalendar

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.sharedcalendar.CalendarAdapter.OnItemListener
import com.example.sharedcalendar.data.LoginDataSource
import com.example.sharedcalendar.data.LoginRepository
import com.example.sharedcalendar.ui.login.LoginActivity
import java.time.LocalDate
import java.time.YearMonth
import java.time.format.DateTimeFormatter

class MainActivity : AppCompatActivity() {
// private val TAG = "MainActivity"
const val TAG = "MainActivity"

class MainActivity : AppCompatActivity(), OnItemListener {
private var monthYearText: TextView? = null
private var calendarRecyclerView: RecyclerView? = null
private var selectedDate: LocalDate? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initWidgets()
selectedDate = LocalDate.now()
setMonthView()

// Start Login Activity if not logged in
Log.i(TAG, LoginRepository(LoginDataSource()).isLoggedIn.toString())
if (!LoginRepository(LoginDataSource()).isLoggedIn) {
startActivity(Intent(this, LoginActivity::class.java))
}
}

private fun initWidgets() {
calendarRecyclerView = findViewById<RecyclerView>(R.id.calendarRecyclerView)
monthYearText = findViewById<TextView>(R.id.monthViewTV)
}

private fun setMonthView() {
monthYearText!!.text = monthYearFromDate(selectedDate)
val daysInMonth = daysInMonthArray(selectedDate)
val calendarAdapter = CalendarAdapter(daysInMonth, this)
val layoutManager: RecyclerView.LayoutManager = GridLayoutManager(applicationContext, 7)
calendarRecyclerView!!.layoutManager = layoutManager
calendarRecyclerView!!.adapter = calendarAdapter
}

private fun daysInMonthArray(date: LocalDate?): ArrayList<String> {
val daysInMonthArray = ArrayList<String>()
val yearMonth = YearMonth.from(date)
val daysInMonth = yearMonth.lengthOfMonth()
val firstOfMonth = selectedDate!!.withDayOfMonth(1)
val dayOfWeek = firstOfMonth.dayOfWeek.value
for (i in 1..42) {
if (i <= dayOfWeek || i > daysInMonth + dayOfWeek) {
daysInMonthArray.add("")
} else {
daysInMonthArray.add((i - dayOfWeek).toString())
}
}
return daysInMonthArray
}

private fun monthYearFromDate(date: LocalDate?): String {
val formatter = DateTimeFormatter.ofPattern("MMMM yyyy")
return date!!.format(formatter)
}

fun previousMonthAction(view: View?) {
selectedDate = selectedDate!!.minusMonths(1)
setMonthView()
}

fun nextMonthAction(view: View?) {
selectedDate = selectedDate!!.plusMonths(1)
setMonthView()
}

override fun onItemClick(position: Int, dayText: String?) {
if (dayText != "") {
val message = "Selected Date " + dayText + " " + monthYearFromDate(selectedDate)
Toast.makeText(this, message, Toast.LENGTH_LONG).show()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.sharedcalendar.data

import android.util.Log
import com.example.sharedcalendar.data.model.LoggedInUser
import java.io.IOException

const val TAG = "LoginDataSource"
/**
* Class that handles authentication w/ login credentials and retrieves user information.
*/
class LoginDataSource {

fun login(username: String, password: String): Result<LoggedInUser> {
try {
Log.i(TAG, username)
Log.i(TAG, password)
// TODO: handle loggedInUser authentication

val fakeUser = LoggedInUser(java.util.UUID.randomUUID().toString(), "Jane Doe")
return Result.Success(fakeUser)
} catch (e: Throwable) {
return Result.Error(IOException("Error logging in", e))
}
}

fun logout() {
// TODO: revoke authentication
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.example.sharedcalendar.data

import com.example.sharedcalendar.data.model.LoggedInUser

/**
* Class that requests authentication and user information from the remote data source and
* maintains an in-memory cache of login status and user credentials information.
*/

class LoginRepository(val dataSource: LoginDataSource) {

// in-memory cache of the loggedInUser object
var user: LoggedInUser? = null
private set

val isLoggedIn: Boolean
get() = user != null

init {
// If user credentials will be cached in local storage, it is recommended it be encrypted
// @see https://developer.android.com/training/articles/keystore
user = null
}

fun logout() {
user = null
dataSource.logout()
}

fun login(username: String, password: String): Result<LoggedInUser> {
// handle login
val result = dataSource.login(username, password)

if (result is Result.Success) {
// If Login is successful
setLoggedInUser(result.data)
}

return result
}

private fun setLoggedInUser(loggedInUser: LoggedInUser) {
this.user = loggedInUser
// If user credentials will be cached in local storage, it is recommended it be encrypted
// @see https://developer.android.com/training/articles/keystore
}
}
18 changes: 18 additions & 0 deletions app/src/main/java/com/example/sharedcalendar/data/Result.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.sharedcalendar.data

/**
* A generic class that holds a value with its loading status.
* @param <T>
*/
sealed class Result<out T : Any> {

data class Success<out T : Any>(val data: T) : Result<T>()
data class Error(val exception: Exception) : Result<Nothing>()

override fun toString(): String {
return when (this) {
is Success<*> -> "Success[data=$data]"
is Error -> "Error[exception=$exception]"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.sharedcalendar.data.model

/**
* Data class that captures user information for logged in users retrieved from LoginRepository
*/
data class LoggedInUser(
val userId: String,
val displayName: String
)
Loading