From 5bfc4cfe1c6a7db6602985eb37b8807910e51deb Mon Sep 17 00:00:00 2001 From: "egecan.serbester" Date: Sun, 22 Oct 2023 21:50:36 +0300 Subject: [PATCH] #341# Arranging Package Design # Creating new database in MVVM architecture # Using Dependency Injection # Using not hard-coded variable names # Using Coroutine # Using ViewModel #Add new dependencies on gradle files# --- .../DisasterResponsePlatform/app/build.gradle | 69 ++++++++++++++++--- .../app/src/main/AndroidManifest.xml | 1 + .../DarpApplication.kt | 13 ++++ .../disasterresponseplatform/MainActivity.kt | 22 ++++++ .../adapter/ActivityAdapter.kt | 9 +-- .../data/DummyActivity.kt | 5 -- .../data/database/DarpDB.kt | 39 +++++++++++ .../data/database/DatabaseInfo.kt | 16 +++++ .../data/database/need/Need.kt | 27 ++++++++ .../data/database/need/NeedCols.kt | 16 +++++ .../data/database/need/NeedDao.kt | 15 ++++ .../data/{ => enums}/ActivityEnum.kt | 2 +- .../data/enums/EndPoint.kt | 7 ++ .../data/{ => enums}/PredefinedTypes.kt | 2 +- .../data/enums/RequestType.kt | 5 ++ .../data/enums/Urgency.kt | 10 +++ .../data/models/DummyActivity.kt | 8 +++ .../data/repositories/NeedRepository.kt | 17 +++++ .../dependencyInjection/AppModule.kt | 44 ++++++++++++ .../dependencyInjection/ViewModelModule.kt | 29 ++++++++ .../managers/NetworkManager.kt | 4 +- .../models/enums/endPoint+enums.kt | 11 --- .../ui/HomePageFragment.kt | 34 ++++++--- .../ui/activity/need/NeedViewModel.kt | 28 ++++++++ .../ui/network/NetworkFragment.kt | 5 +- .../app/src/main/res/menu/nav_header_menu.xml | 4 ++ .../DisasterResponsePlatform/build.gradle | 23 +++++++ 27 files changed, 415 insertions(+), 50 deletions(-) create mode 100644 Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/DarpApplication.kt delete mode 100644 Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/DummyActivity.kt create mode 100644 Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/database/DarpDB.kt create mode 100644 Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/database/DatabaseInfo.kt create mode 100644 Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/database/need/Need.kt create mode 100644 Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/database/need/NeedCols.kt create mode 100644 Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/database/need/NeedDao.kt rename Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/{ => enums}/ActivityEnum.kt (68%) create mode 100644 Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/enums/EndPoint.kt rename Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/{ => enums}/PredefinedTypes.kt (68%) create mode 100644 Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/enums/RequestType.kt create mode 100644 Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/enums/Urgency.kt create mode 100644 Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/models/DummyActivity.kt create mode 100644 Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/repositories/NeedRepository.kt create mode 100644 Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/dependencyInjection/AppModule.kt create mode 100644 Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/dependencyInjection/ViewModelModule.kt delete mode 100644 Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/models/enums/endPoint+enums.kt create mode 100644 Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/ui/activity/need/NeedViewModel.kt diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/build.gradle b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/build.gradle index 710032e9..73099dca 100644 --- a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/build.gradle +++ b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/build.gradle @@ -1,6 +1,11 @@ plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' + id 'kotlin-android' + id 'kotlin-kapt' + id 'org.jetbrains.dokka' + id 'dagger.hilt.android.plugin' + id 'com.google.dagger.hilt.android' } // android { @@ -29,11 +34,11 @@ android { } } compileOptions { - sourceCompatibility JavaVersion.VERSION_1_8 - targetCompatibility JavaVersion.VERSION_1_8 + sourceCompatibility JavaVersion.VERSION_17 + targetCompatibility JavaVersion.VERSION_17 } kotlinOptions { - jvmTarget = '1.8' + jvmTarget = '17' } } @@ -61,13 +66,55 @@ dependencies { implementation("androidx.recyclerview:recyclerview-selection:1.1.0") - // ViewModel - implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2") - // ViewModel utilities for Compose - implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.6.2") - // LiveData - implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.6.2") - // Lifecycles only (without ViewModel or LiveData) - implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2") + //lifecycle for ViewModel and LiveData + def lifecycle_version = "2.6.1" + def coroutine_lifecycle_version = "2.6.2" + + implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version") // ViewModel + implementation("androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version") + implementation("androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version") + implementation("androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version") + + //lifecycle + implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' + implementation "androidx.lifecycle:lifecycle-common-java8:2.6.2" + + //noinspection LifecycleAnnotationProcessorWithJava8 + kapt("androidx.lifecycle:lifecycle-compiler:$lifecycle_version") + + // Room + def room_version = "2.6.0" + implementation("androidx.room:room-runtime:$room_version") + annotationProcessor("androidx.room:room-compiler:$room_version") + // To use Kotlin annotation processing tool (kapt) + kapt("androidx.room:room-compiler:$room_version") + + //androidx + implementation("androidx.concurrent:concurrent-futures-ktx:1.1.0") + + // Kotlin Extensions and Coroutines support for Room + implementation "androidx.room:room-ktx:$room_version" + + def coroutine_version = "1.6.4" + // Coroutines + implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutine_version" + implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutine_version" + + // Coroutine Lifecycle Scopes + implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$coroutine_lifecycle_version" + implementation "androidx.lifecycle:lifecycle-runtime-ktx:$coroutine_lifecycle_version" + + //apache for string utils + implementation 'org.apache.commons:commons-text:1.9' + + //dagger-hilt + implementation("com.google.dagger:hilt-android:2.46") + //because it gives an error with hilt-android 2.46 it's older version. I wastes my 2 hours.. + kapt("com.google.dagger:hilt-android-compiler:2.44") + //to add dagger-hilt by viewModel() + implementation "androidx.fragment:fragment-ktx:1.6.1" + + //Gson + implementation 'com.google.code.gson:gson:2.10.1' } \ No newline at end of file diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/AndroidManifest.xml b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/AndroidManifest.xml index 6166a282..1b85c8ff 100644 --- a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/AndroidManifest.xml +++ b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/AndroidManifest.xml @@ -5,6 +5,7 @@ replaceNavFragment(registrationFragment) R.id.miNetwork -> replaceNavFragment(networkFragment) R.id.miLogout -> Toast.makeText(this,"Logout",Toast.LENGTH_SHORT).show() + R.id.miAddNeed -> tryNeedViewModel() } binding.root.closeDrawer(GravityCompat.START) //whenever clicked item on drawer, closing it automatically true diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/adapter/ActivityAdapter.kt b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/adapter/ActivityAdapter.kt index dc5a9a6d..cc51a590 100644 --- a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/adapter/ActivityAdapter.kt +++ b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/adapter/ActivityAdapter.kt @@ -1,6 +1,5 @@ package com.example.disasterresponseplatform.adapter -import android.content.Intent import android.os.Looper import android.view.LayoutInflater import android.view.ViewGroup @@ -9,12 +8,10 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.recyclerview.widget.RecyclerView import com.example.disasterresponseplatform.R -import com.example.disasterresponseplatform.data.ActivityEnum -import com.example.disasterresponseplatform.data.DummyActivity -import com.example.disasterresponseplatform.data.PredefinedTypes +import com.example.disasterresponseplatform.data.enums.ActivityEnum +import com.example.disasterresponseplatform.data.models.DummyActivity +import com.example.disasterresponseplatform.data.enums.PredefinedTypes import com.example.disasterresponseplatform.databinding.ActivityItemBinding -import kotlinx.coroutines.delay -import java.util.logging.Handler class ActivityAdapter(private val activityList: MutableList): RecyclerView.Adapter() { diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/DummyActivity.kt b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/DummyActivity.kt deleted file mode 100644 index b923fa24..00000000 --- a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/DummyActivity.kt +++ /dev/null @@ -1,5 +0,0 @@ -package com.example.disasterresponseplatform.data - -class DummyActivity(var activityType: ActivityEnum, var predefinedTypes: PredefinedTypes, var location: String, -var date: String, var reliabilityScale: Double) { -} \ No newline at end of file diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/database/DarpDB.kt b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/database/DarpDB.kt new file mode 100644 index 00000000..cfecb5ce --- /dev/null +++ b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/database/DarpDB.kt @@ -0,0 +1,39 @@ +package com.example.disasterresponseplatform.data.database + +import android.content.Context +import androidx.room.AutoMigration +import androidx.room.Database +import androidx.room.Room +import androidx.room.RoomDatabase +import androidx.room.migration.Migration +import com.example.disasterresponseplatform.data.database.need.Need +import com.example.disasterresponseplatform.data.database.need.NeedDao + +@Database(entities = [Need::class], version = DatabaseInfo.DATABASE_VERSION, exportSchema = false) +abstract class DarpDB: RoomDatabase() { + abstract val needDao: NeedDao + + // it's static object in this way you can call getInstance method without any initialization + companion object{ + @Volatile //this makes this field visible to other threads + private var firstInstance: DarpDB? = null + + fun getInstance(context: Context): DarpDB { + synchronized(this) { + var instance = firstInstance + if (instance == null) { + instance = Room.databaseBuilder( + context.applicationContext, + DarpDB::class.java, DatabaseInfo.DATABASE + ) + .allowMainThreadQueries() + .fallbackToDestructiveMigration() // when migrate a new version delete the existing db + .build() + firstInstance = instance + } + return instance + } + } + } + +} \ No newline at end of file diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/database/DatabaseInfo.kt b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/database/DatabaseInfo.kt new file mode 100644 index 00000000..349c922c --- /dev/null +++ b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/database/DatabaseInfo.kt @@ -0,0 +1,16 @@ +package com.example.disasterresponseplatform.data.database + +/** + * This is for holding data in regular way. + */ +class DatabaseInfo { + companion object { + const val DATABASE: String = "DARP_DB" + const val ACTION: String = "ACTION" + const val EMERGENCY: String = "EMERGENCY" + const val EVENT: String = "EVENT" + const val NEED: String = "NEED" + const val RESOURCE: String = "RESOURCE" + const val DATABASE_VERSION: Int = 2 // you need to change that whenever you change any table on DB + } +} diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/database/need/Need.kt b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/database/need/Need.kt new file mode 100644 index 00000000..37cd663c --- /dev/null +++ b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/database/need/Need.kt @@ -0,0 +1,27 @@ +package com.example.disasterresponseplatform.data.database.need + +import androidx.room.ColumnInfo +import androidx.room.Entity +import androidx.room.PrimaryKey +import com.example.disasterresponseplatform.data.database.DatabaseInfo + +@Entity(tableName = DatabaseInfo.NEED) +data class Need( + @PrimaryKey(autoGenerate = true) + @ColumnInfo (name = NeedCols.id) + val ID: Int?, // if user does not enter ID, it generates it automatically + @ColumnInfo(name = NeedCols.creatorID) + val creatorID: String, + @ColumnInfo(name = NeedCols.type) + val type: String, + @ColumnInfo(name = NeedCols.subTypeList) + val subType: String, + @ColumnInfo(name = NeedCols.creationTime) + val creationTime: String?, + @ColumnInfo(name = NeedCols.quantity) + val quantity: Int?, + @ColumnInfo(name = NeedCols.location) + val location: String?, + @ColumnInfo(name = NeedCols.urgency) + val urgency: Int? +) diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/database/need/NeedCols.kt b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/database/need/NeedCols.kt new file mode 100644 index 00000000..823c49cb --- /dev/null +++ b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/database/need/NeedCols.kt @@ -0,0 +1,16 @@ +package com.example.disasterresponseplatform.data.database.need + +class NeedCols { + + companion object{ + const val id: String = "id" + const val type: String = "type" + const val subTypeList: String = "subTypeList" + const val creationTime: String = "creationTime" + const val creatorID: String = "creatorID" + const val location: String = "location" + const val quantity: String = "quantity" + const val urgency: String = "urgency" + + } +} \ No newline at end of file diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/database/need/NeedDao.kt b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/database/need/NeedDao.kt new file mode 100644 index 00000000..efd17806 --- /dev/null +++ b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/database/need/NeedDao.kt @@ -0,0 +1,15 @@ +package com.example.disasterresponseplatform.data.database.need + +import androidx.room.Dao +import androidx.room.Insert +import androidx.room.Query +import com.example.disasterresponseplatform.data.database.DatabaseInfo + +@Dao +interface NeedDao { + @Insert + suspend fun insertActivation(need: Need) + + @Query("SELECT ${NeedCols.location} FROM ${DatabaseInfo.NEED} WHERE ${NeedCols.creatorID} = :creatorID") + fun getLocation(creatorID: String): String +} \ No newline at end of file diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/ActivityEnum.kt b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/enums/ActivityEnum.kt similarity index 68% rename from Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/ActivityEnum.kt rename to Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/enums/ActivityEnum.kt index 7214d00b..0b0d0277 100644 --- a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/ActivityEnum.kt +++ b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/enums/ActivityEnum.kt @@ -1,4 +1,4 @@ -package com.example.disasterresponseplatform.data +package com.example.disasterresponseplatform.data.enums enum class ActivityEnum(val type: Int) { Action(1), diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/enums/EndPoint.kt b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/enums/EndPoint.kt new file mode 100644 index 00000000..a422ee92 --- /dev/null +++ b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/enums/EndPoint.kt @@ -0,0 +1,7 @@ +package com.example.disasterresponseplatform.data.enums + +enum class Endpoint(val path: String) { + DATA("joke/any"), + USER("user"), + PRODUCTS("products"); +} \ No newline at end of file diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/PredefinedTypes.kt b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/enums/PredefinedTypes.kt similarity index 68% rename from Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/PredefinedTypes.kt rename to Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/enums/PredefinedTypes.kt index 75e8397d..eafb5a51 100644 --- a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/PredefinedTypes.kt +++ b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/enums/PredefinedTypes.kt @@ -1,4 +1,4 @@ -package com.example.disasterresponseplatform.data +package com.example.disasterresponseplatform.data.enums enum class PredefinedTypes(val type: Int) { Clothes(1), diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/enums/RequestType.kt b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/enums/RequestType.kt new file mode 100644 index 00000000..131c5df5 --- /dev/null +++ b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/enums/RequestType.kt @@ -0,0 +1,5 @@ +package com.example.disasterresponseplatform.data.enums + +enum class RequestType { + GET, POST, PUT, DELETE // Add more HTTP methods as needed +} \ No newline at end of file diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/enums/Urgency.kt b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/enums/Urgency.kt new file mode 100644 index 00000000..34577027 --- /dev/null +++ b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/enums/Urgency.kt @@ -0,0 +1,10 @@ +package com.example.disasterresponseplatform.data.enums + +enum class Urgency(val type: Int) { + EMERGENCY(0), + CRITICAL(1), + URGENT(2), + HIGH_PRIORITY(3), + NORMAL_PRIORITY(4), + LOW_PRIORITY(5) +} \ No newline at end of file diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/models/DummyActivity.kt b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/models/DummyActivity.kt new file mode 100644 index 00000000..fc5e7dc2 --- /dev/null +++ b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/models/DummyActivity.kt @@ -0,0 +1,8 @@ +package com.example.disasterresponseplatform.data.models + +import com.example.disasterresponseplatform.data.enums.ActivityEnum +import com.example.disasterresponseplatform.data.enums.PredefinedTypes + +class DummyActivity(var activityType: ActivityEnum, var predefinedTypes: PredefinedTypes, var location: String, + var date: String, var reliabilityScale: Double) { +} \ No newline at end of file diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/repositories/NeedRepository.kt b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/repositories/NeedRepository.kt new file mode 100644 index 00000000..8b77c6b7 --- /dev/null +++ b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/data/repositories/NeedRepository.kt @@ -0,0 +1,17 @@ +package com.example.disasterresponseplatform.data.repositories + +import com.example.disasterresponseplatform.data.database.need.Need +import com.example.disasterresponseplatform.data.database.need.NeedDao +import javax.inject.Inject + +class NeedRepository @Inject constructor(private val needDao: NeedDao) { + + suspend fun insertNeed(need: Need){ + needDao.insertActivation(need) + } + + fun getLocation(creatorID: String): String{ + return needDao.getLocation(creatorID) + } + +} \ No newline at end of file diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/dependencyInjection/AppModule.kt b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/dependencyInjection/AppModule.kt new file mode 100644 index 00000000..3256aabc --- /dev/null +++ b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/dependencyInjection/AppModule.kt @@ -0,0 +1,44 @@ +package com.example.disasterresponseplatform.dependencyInjection + +import android.app.Application +import com.example.disasterresponseplatform.data.database.DarpDB +import com.example.disasterresponseplatform.data.repositories.NeedRepository +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent +import javax.inject.Singleton + +/** This is a Module for Hilt, Hilt has some modules to define some values. If you define those values there, you + * don't need to define them everytime to call those. The only thing to do is @Inject method before you call those values + * If you Inject them in constructor you need to @Inject constructor, else if in class first you should + * annotate that class with @AndroidEntryPoint then you specify its class and add @Inject annotation to head of that. + * Because there are different lifecycles in Android, in @InstallIn parameter you specify + * to get more details you can visit https://developer.android.com/training/dependency-injection/hilt-android + * lifecycle of values that those class defines + * This is SingletonComponent because those values should live as application does + */ +@Module +@InstallIn(SingletonComponent::class) +class AppModule { + + /** It returns a DarpDB instance + * @Provide is for providing dependency which class it returns. + * To do that without errors, You should specify only one provide method for each class + * otherwise you should named them and call them with their names. + * @Singleton is for making it single instance of this. If it doesn't exist, everytime we call this it creates new instance + * @param app is Application, as I mentioned in DarpDB class, it comes from there and returns ActivityContext + */ + @Provides + @Singleton + fun provideDatabase(app: Application): DarpDB = DarpDB.getInstance(app) + // Application comes from AppTemp thanks to annotation @HiltAndroidApp + + /** + * It returns Repository instance + * @param database is comes from provideDatabase method automatically + */ + @Provides + @Singleton + fun provideNeedRepository(database: DarpDB): NeedRepository = NeedRepository(database.needDao) +} \ No newline at end of file diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/dependencyInjection/ViewModelModule.kt b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/dependencyInjection/ViewModelModule.kt new file mode 100644 index 00000000..cbfc8859 --- /dev/null +++ b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/dependencyInjection/ViewModelModule.kt @@ -0,0 +1,29 @@ +package com.example.disasterresponseplatform.dependencyInjection + +import com.example.disasterresponseplatform.data.repositories.NeedRepository +import com.example.disasterresponseplatform.ui.activity.need.NeedViewModel +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.android.components.ViewModelComponent +import dagger.hilt.android.scopes.ViewModelScoped + +/** This is a module object for Dependency Injection with Hilt + * It defines the viewModel inside that object + * Because ViewModel's life is different than App's Life in lifecycle we have to define it another module + * @InstallIn(ViewModelComponent::class) means it's life as much as ViewModel's life + */ +@Module +@InstallIn(ViewModelComponent::class) +class ViewModelModule { + + + /** + * It defines our viewModel, app and repository are gotten from Hilt DI + */ + @Provides + @ViewModelScoped + fun provideNeedViewModel(repository: NeedRepository): NeedViewModel = + NeedViewModel(repository) + +} \ No newline at end of file diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/managers/NetworkManager.kt b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/managers/NetworkManager.kt index 439b116a..5d9d603e 100644 --- a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/managers/NetworkManager.kt +++ b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/managers/NetworkManager.kt @@ -1,7 +1,7 @@ package com.example.disasterresponseplatform.managers -import com.example.disasterresponseplatform.models.enums.Endpoint -import com.example.disasterresponseplatform.models.enums.RequestType +import com.example.disasterresponseplatform.data.enums.Endpoint +import com.example.disasterresponseplatform.data.enums.RequestType import retrofit2.Call import retrofit2.Callback import retrofit2.Retrofit diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/models/enums/endPoint+enums.kt b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/models/enums/endPoint+enums.kt deleted file mode 100644 index a08e96d8..00000000 --- a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/models/enums/endPoint+enums.kt +++ /dev/null @@ -1,11 +0,0 @@ -package com.example.disasterresponseplatform.models.enums - -enum class Endpoint(val path: String) { - DATA("joke/any"), - USER("user"), - PRODUCTS("products"); -} - -enum class RequestType { - GET, POST, PUT, DELETE // Add more HTTP methods as needed -} \ No newline at end of file diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/ui/HomePageFragment.kt b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/ui/HomePageFragment.kt index 388e516f..33cc5c1e 100644 --- a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/ui/HomePageFragment.kt +++ b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/ui/HomePageFragment.kt @@ -1,6 +1,5 @@ package com.example.disasterresponseplatform.ui -import android.icu.lang.UCharacter.VerticalOrientation import android.os.Bundle import androidx.fragment.app.Fragment import android.view.LayoutInflater @@ -13,9 +12,9 @@ import androidx.recyclerview.widget.LinearLayoutManager import com.example.disasterresponseplatform.MainActivity import com.example.disasterresponseplatform.R import com.example.disasterresponseplatform.adapter.ActivityAdapter -import com.example.disasterresponseplatform.data.ActivityEnum -import com.example.disasterresponseplatform.data.DummyActivity -import com.example.disasterresponseplatform.data.PredefinedTypes +import com.example.disasterresponseplatform.data.enums.ActivityEnum +import com.example.disasterresponseplatform.data.models.DummyActivity +import com.example.disasterresponseplatform.data.enums.PredefinedTypes import com.example.disasterresponseplatform.databinding.FragmentHomePageBinding import com.example.disasterresponseplatform.utils.DateUtil @@ -64,12 +63,27 @@ class HomePageFragment(private val mainActivity: MainActivity) : Fragment() { private fun prepareDummyList(): MutableList{ val list = mutableListOf() - list.add(DummyActivity(ActivityEnum.Need,PredefinedTypes.Food,"Gaziantep","${DateUtil.getDate("yyyy-MM-dd")} ${DateUtil.getTime("HH:mm:ss")}",0.89)) - list.add(DummyActivity(ActivityEnum.Resource,PredefinedTypes.Food,"İstanbul","${DateUtil.getDate("yyyy-MM-dd")} ${DateUtil.getTime("HH:mm:ss")}",0.92)) - list.add(DummyActivity(ActivityEnum.Resource,PredefinedTypes.Human,"Bursa","22.10.2023",0.88)) - list.add(DummyActivity(ActivityEnum.Need,PredefinedTypes.Clothes,"Hatay","22.10.2023",0.53)) - list.add(DummyActivity(ActivityEnum.Event,PredefinedTypes.Collapse,"Kahramanmaraş","20.10.2023",0.76)) - list.add(DummyActivity(ActivityEnum.Emergency,PredefinedTypes.Debris,"Kahramanmaraş","21.10.2023",1.00)) + list.add( + DummyActivity(ActivityEnum.Need, + PredefinedTypes.Food,"Gaziantep","${DateUtil.getDate("yyyy-MM-dd")} ${DateUtil.getTime("HH:mm:ss")}",0.89) + ) + list.add( + DummyActivity(ActivityEnum.Resource, + PredefinedTypes.Food,"İstanbul","${DateUtil.getDate("yyyy-MM-dd")} ${DateUtil.getTime("HH:mm:ss")}",0.92) + ) + list.add( + DummyActivity(ActivityEnum.Resource, + PredefinedTypes.Human,"Bursa","22.10.2023",0.88) + ) + list.add(DummyActivity(ActivityEnum.Need, PredefinedTypes.Clothes,"Hatay","22.10.2023",0.53)) + list.add( + DummyActivity(ActivityEnum.Event, + PredefinedTypes.Collapse,"Kahramanmaraş","20.10.2023",0.76) + ) + list.add( + DummyActivity(ActivityEnum.Emergency, + PredefinedTypes.Debris,"Kahramanmaraş","21.10.2023",1.00) + ) return list } diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/ui/activity/need/NeedViewModel.kt b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/ui/activity/need/NeedViewModel.kt new file mode 100644 index 00000000..62797bf0 --- /dev/null +++ b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/ui/activity/need/NeedViewModel.kt @@ -0,0 +1,28 @@ +package com.example.disasterresponseplatform.ui.activity.need + +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.example.disasterresponseplatform.data.database.need.Need +import com.example.disasterresponseplatform.data.repositories.NeedRepository +import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import javax.inject.Inject + +@HiltViewModel +class NeedViewModel@Inject constructor(private val needRepository: NeedRepository) : ViewModel() { + + /** + * It inserts need in a background (IO) thread + */ + fun insertNeed(need: Need){ + viewModelScope.launch(Dispatchers.IO){ + needRepository.insertNeed(need) + } + } + + fun getLocation(creatorID: String): String{ + return needRepository.getLocation(creatorID) + } + +} \ No newline at end of file diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/ui/network/NetworkFragment.kt b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/ui/network/NetworkFragment.kt index 57d75c2b..379a117c 100644 --- a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/ui/network/NetworkFragment.kt +++ b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/java/com/example/disasterresponseplatform/ui/network/NetworkFragment.kt @@ -7,12 +7,11 @@ import android.view.View import android.view.ViewGroup import android.widget.Toast import com.example.disasterresponseplatform.MainActivity -import com.example.disasterresponseplatform.R +import com.example.disasterresponseplatform.data.enums.Endpoint +import com.example.disasterresponseplatform.data.enums.RequestType import com.example.disasterresponseplatform.databinding.FragmentNetworkBinding import com.example.disasterresponseplatform.managers.DataResponse import com.example.disasterresponseplatform.managers.NetworkManager -import com.example.disasterresponseplatform.models.enums.Endpoint -import com.example.disasterresponseplatform.models.enums.RequestType import retrofit2.Call import retrofit2.Response diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/res/menu/nav_header_menu.xml b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/res/menu/nav_header_menu.xml index aa7ca837..bb39c5f9 100644 --- a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/res/menu/nav_header_menu.xml +++ b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/app/src/main/res/menu/nav_header_menu.xml @@ -17,5 +17,9 @@ android:id="@+id/miLogout" android:title="@string/logout"/> + + \ No newline at end of file diff --git a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/build.gradle b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/build.gradle index 25ad99de..2c1f6b5e 100644 --- a/Disaster-Response-Platform/mobile/DisasterResponsePlatform/build.gradle +++ b/Disaster-Response-Platform/mobile/DisasterResponsePlatform/build.gradle @@ -1,6 +1,29 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. + +buildscript { + + repositories { + google() + jcenter() + } + dependencies { + classpath 'com.android.tools.build:gradle:8.0.1' + classpath("org.jetbrains.dokka:dokka-gradle-plugin:1.7.10") + + // NOTE: Do not place your application dependencies here; they belong + // in the individual module build.gradle files + } +} + plugins { id 'com.android.application' version '8.0.1' apply false id 'com.android.library' version '8.0.1' apply false id 'org.jetbrains.kotlin.android' version '1.8.20' apply false + id("org.jetbrains.dokka") version "1.7.10" + id 'com.google.dagger.hilt.android' version '2.44' apply false +} + + +task clean(type: Delete) { + delete rootProject.buildDir } \ No newline at end of file