Skip to content

Commit

Permalink
made module fragment powered by backedn
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinav12k committed May 21, 2022
1 parent e4dc0ce commit a4e7e9c
Show file tree
Hide file tree
Showing 20 changed files with 217 additions and 122 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
id 'kotlin-parcelize'
id 'androidx.navigation.safeargs.kotlin'
id 'kotlin-kapt'
id 'com.google.firebase.crashlytics'
}

def rootConfiguration = rootProject.ext
Expand Down Expand Up @@ -66,11 +67,14 @@ dependencies {
implementation 'com.google.firebase:firebase-auth-ktx'
implementation 'com.google.firebase:firebase-analytics-ktx'
implementation 'com.google.firebase:firebase-firestore-ktx'
implementation 'com.google.firebase:firebase-crashlytics'
implementation 'com.google.firebase:firebase-analytics'

//navigation
implementation "androidx.navigation:navigation-fragment-ktx:${rootConfiguration.navVersion}"
implementation "androidx.navigation:navigation-ui-ktx:${rootConfiguration.navVersion}"

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.4.1"
implementation "io.noties.markwon:core:${rootConfiguration.markwonVersion}"
implementation("com.squareup.moshi:moshi-kotlin:${rootConfiguration.moshiVersion}")
kapt("com.squareup.moshi:moshi-kotlin-codegen:${rootConfiguration.moshiVersion}")
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/deepspace/hab/Stratofox.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.deepspace.hab

import android.app.Application
import com.deepspace.hab.screens.HomeRepository
import timber.log.Timber

/**
* Created by abhinav on 25/09/21.
*/
class Stratofox : Application() {

val homeRepository: HomeRepository by lazy { HomeRepository() }

override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.deepspace.hab.constants

object StratofoxFirebaseConstants {
const val MODULE_LIST_COLLECTION = "modules"
const val LESSON_LIST_COLLECTION = "lessons"
const val MODULE_LESSON_LIST_COLLECTION = "moduleSections"
}
41 changes: 33 additions & 8 deletions app/src/main/java/com/deepspace/hab/models/Module.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,44 @@ package com.deepspace.hab.models

import android.os.Parcelable
import com.deepspace.hab.models.BaseModel
import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.google.firebase.firestore.DocumentSnapshot
import kotlinx.parcelize.Parcelize
import timber.log.Timber
import java.lang.Exception

/**
* Created by abhinav on 26/09/21.
*/
@Parcelize
data class Module(
override var id: String? = "",
val description: String = "",
val moduleDuration: String = "",
val imageId: String = "",
val noOfLessons: Int = 0,
val rank: Int = 0,
val title: String = "",
val version: String = "",
): BaseModel(), Parcelable
val description: String? = "",
val moduleDuration: String? = "",
val imageId: String? = "",
val noOfLessons: Int? = 0,
val rank: Int? = 0, //to determine position
val title: String? = "",
val version: String? = "", //Not necessary
) : BaseModel(), Parcelable {
companion object {
fun DocumentSnapshot.toModule(): Module? {
try {
val description = getString("description")
val moduleDuration = getString("moduleDuration")
val imageId = getString("imageId")
val noOfLessons = getLong("noOfLessons")?.toInt()
val rank = getLong("rank")?.toInt()
val title = getString("title")
val version = getString("version")
return Module(id, description, moduleDuration, imageId, noOfLessons, rank, title, version)
} catch (e: Exception) {
Timber.e("Error converting module: $e")
FirebaseCrashlytics.getInstance().log("Error converting module")
FirebaseCrashlytics.getInstance().setCustomKey("moduleId", id)
FirebaseCrashlytics.getInstance().recordException(e)
return null
}
}
}
}
15 changes: 2 additions & 13 deletions app/src/main/java/com/deepspace/hab/screens/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.navigation.ui.NavigationUI
import com.deepspace.common.base.BaseActivity
import com.deepspace.hab.models.Module
import com.deepspace.hab.R
import com.deepspace.hab.Stratofox
import com.deepspace.hab.databinding.ActivityHomeBinding
import java.util.*
import kotlin.concurrent.schedule
Expand All @@ -17,25 +18,13 @@ class HomeActivity : BaseActivity<ActivityHomeBinding>() {

override fun getViewBinding(): ActivityHomeBinding = ActivityHomeBinding.inflate(layoutInflater)
private var isSecondBackPress = false
private val homeViewModel: HomeViewModel by viewModels()

override fun create(savedInstanceState: Bundle?) {
getModulesViaIntent()
if (homeViewModel.moduleList != null) {
binding.progressBar.visibility = View.INVISIBLE
binding.navHostFragment.visibility = View.VISIBLE
}
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
NavigationUI.setupWithNavController(binding.bottomNavigation, navController)
}

private fun getModulesViaIntent() {
homeViewModel.moduleList =
intent.getParcelableArrayListExtra<Module>(SplashScreenActivity.MODULE_LIST)
}

override fun onBackPressed() {
if (isSecondBackPress) {
super.onBackPressed()
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/java/com/deepspace/hab/screens/HomeRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.deepspace.hab.screens

import com.deepspace.hab.constants.StratofoxFirebaseConstants
import com.deepspace.hab.models.Module
import com.deepspace.hab.models.Module.Companion.toModule
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
import kotlinx.coroutines.tasks.await

class HomeRepository {

private val db = Firebase.firestore

suspend fun fetchModuleList(): MutableList<Module> {
val moduleList = mutableListOf<Module>()
db.collection(StratofoxFirebaseConstants.MODULE_LIST_COLLECTION)
.get().await().mapNotNull { snapshot ->
snapshot.toModule()?.let { moduleList.add(it) }
}
return moduleList
}

}
19 changes: 16 additions & 3 deletions app/src/main/java/com/deepspace/hab/screens/HomeViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
package com.deepspace.hab.screens

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.deepspace.hab.models.Module
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch

/**
* Created by abhinav on 26/09/21.
*/
class HomeViewModel: ViewModel() {
var moduleList: List<Module>? = null
class HomeViewModel(private val repo: HomeRepository): ViewModel() {

private val _moduleViewState = MutableLiveData<ModuleViewState>()
val moduleViewState: LiveData<ModuleViewState> = _moduleViewState

fun fetchModuleList() {
viewModelScope.launch {
_moduleViewState.value = ModuleViewState(showLoader = true)
_moduleViewState.value = ModuleViewState(showLoader = false, repo.fetchModuleList())
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.deepspace.hab.screens

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

class HomeViewModelFactory(private val repo: HomeRepository): ViewModelProvider.Factory {

override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return HomeViewModel(repo) as T
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.deepspace.hab.screens

import com.deepspace.hab.models.Module

data class ModuleViewState(val showLoader: Boolean = false, val moduleList: List<Module>? = null)
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ class SplashScreenActivity : AppCompatActivity() {

private fun shiftToHomeActivity(modules: List<Module>) {
val intent = Intent(this, HomeActivity::class.java)
intent.putParcelableArrayListExtra(MODULE_LIST, ArrayList(modules))
// intent.putParcelableArrayListExtra(MODULE_LIST, ArrayList(modules))
startActivity(intent)
finish()
}

private fun shiftToWelcomeActivity(modules: List<Module>) {
val welcomeIntent = Intent(this, WelcomeActivity::class.java)
welcomeIntent.putParcelableArrayListExtra(MODULE_LIST, ArrayList(modules))
// welcomeIntent.putParcelableArrayListExtra(MODULE_LIST, ArrayList(modules))
startActivity(welcomeIntent)
finish()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
package com.deepspace.hab.screens.guidelines

import android.os.Bundle
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import com.deepspace.common.base.BaseFragment
import com.deepspace.hab.R
import com.deepspace.hab.databinding.FragmentGuidelinesBinding
import com.deepspace.hab.screens.HomeViewModel
import com.deepspace.hab.screens.modules.ModuleAdapter

class GuidelinesFragment : BaseFragment<FragmentGuidelinesBinding>() {

override fun getViewBinding(): FragmentGuidelinesBinding =
FragmentGuidelinesBinding.inflate(layoutInflater)

private val viewModel: HomeViewModel by activityViewModels()
override fun getViewBinding(): FragmentGuidelinesBinding = FragmentGuidelinesBinding.inflate(layoutInflater)

override fun onViewCreated(savedInstanceState: Bundle?) {
val resourceAdapter = ResourceAdapter()
binding.rvResources.layoutManager =
GridLayoutManager(requireContext(), 2, GridLayoutManager.VERTICAL, false)
binding.rvResources.layoutManager = GridLayoutManager(requireContext(), 2, GridLayoutManager.VERTICAL, false)
binding.rvResources.adapter = resourceAdapter
resourceAdapter.submitList(getResourceList())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ class LessonListFragment : BaseFragment<FragmentLessonListBinding>() {
binding.tvModuleTitle.text = module.title
binding.tvModuleDescription.text = module.description
binding.tvModuleName.text = moduleName
setupLineColor(module.rank)
setupImage(module.rank)
module.rank?.let { setupLineColor(it) }
module.rank?.let { setupImage(it) }
}

private fun setupImage(moduleNo: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ class ModuleAdapter(private val onClick: (Module) -> Unit) :
}

private fun setupColors(module: Module) {
moduleImage.setImageResource(getImageDrawable(module.rank))
moduleImage.setImageResource(getImageDrawable(module.rank ?: 1))
moduleBgColor.setCardBackgroundColor(
ContextCompat.getColor(
itemView.context,
getBackgroundColorID(module.rank)
getBackgroundColorID(module.rank ?: 1)
)
)
ContextCompat.getColor(
itemView.context,
getTextColor(module.rank)
getTextColor(module.rank ?: 1)
).apply {
moduleTitle.setTextColor(this)
moduleNoInWords.setTextColor(this)
Expand All @@ -81,14 +81,14 @@ class ModuleAdapter(private val onClick: (Module) -> Unit) :
moduleNo.setTextColor(
ContextCompat.getColor(
itemView.context,
getModuleNoColor(module.rank)
getModuleNoColor(module.rank ?: 1)
)
)
startBtn.setBackgroundResource(getStartBtnBg(module.rank))
startBtn.setBackgroundResource(getStartBtnBg(module.rank ?: 1))
startBtn.setTextColor(
ContextCompat.getColor(
itemView.context,
getStartBtnTextColor(module.rank)
getStartBtnTextColor(module.rank ?: 1)
)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
package com.deepspace.hab.screens.modules

import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import com.deepspace.common.base.BaseFragment
import com.deepspace.hab.models.Module
import com.deepspace.hab.Stratofox
import com.deepspace.hab.databinding.FragmentModuleBinding
import com.deepspace.hab.models.Module
import com.deepspace.hab.screens.HomeViewModel
import com.deepspace.hab.screens.HomeViewModelFactory
import com.deepspace.hab.screens.ModuleViewState
import timber.log.Timber

class ModuleFragment : BaseFragment<FragmentModuleBinding>() {

override fun getViewBinding(): FragmentModuleBinding =
FragmentModuleBinding.inflate(layoutInflater)

private val viewModel: HomeViewModel by activityViewModels()
private val viewModel: HomeViewModel by activityViewModels {
HomeViewModelFactory((requireActivity().application as Stratofox).homeRepository)
}
private var moduleAdapter: ModuleAdapter? = null

override fun onViewCreated(savedInstanceState: Bundle?) {
val moduleAdapter = ModuleAdapter { module -> onModuleClicked(module) }
binding.rvModules.layoutManager =
GridLayoutManager(requireContext(), 2, GridLayoutManager.VERTICAL, false)
binding.rvModules.adapter = moduleAdapter
moduleAdapter.submitList(viewModel.moduleList)
observeViewState()
viewModel.fetchModuleList()
initAdapter()

binding.cardModule0.setOnClickListener {
findNavController().navigate(
Expand All @@ -38,6 +43,34 @@ class ModuleFragment : BaseFragment<FragmentModuleBinding>() {

}

private fun initAdapter() {
moduleAdapter = ModuleAdapter { module -> onModuleClicked(module) }
binding.rvModules.layoutManager =
GridLayoutManager(requireContext(), 2, GridLayoutManager.VERTICAL, false)
binding.rvModules.adapter = moduleAdapter
}

private fun observeViewState() {
viewModel.moduleViewState.observe(viewLifecycleOwner) {
handleViewState(it)
}
}

private fun handleViewState(viewState: ModuleViewState) {
if (viewState.showLoader) {
binding.progressBar.visibility = View.VISIBLE
binding.moduleRoot.visibility = View.GONE
} else {
binding.progressBar.visibility = View.GONE
binding.moduleRoot.visibility = View.VISIBLE
}

viewState.moduleList?.let {
Timber.tag("abhinav").d("$it")
moduleAdapter?.submitList(it)
}
}

private fun onModuleClicked(module: Module) {
Timber.d("Module Clicked: $module")
findNavController().navigate(
Expand Down
Loading

0 comments on commit a4e7e9c

Please sign in to comment.