commit a1089807b4dc78730366cad9287ef5d64c0454fb Author: Feyzu Date: Mon Apr 25 14:49:32 2022 +0200 Pendulum stand canvas steht keine funktionen diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa724b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +*.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 +local.properties diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..f006433 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +acaPendulum2.0 \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..fb7f4a8 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..526b4c2 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,20 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..8ab2039 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..ef83eef --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,84 @@ +plugins { + id 'com.android.application' + id 'kotlin-android' + id 'kotlin-android-extensions' + id 'kotlin-kapt' + id 'org.jetbrains.kotlin.android' + //id 'android.navigation.safeargs.kotlin' + id 'androidx.navigation.safeargs' +} + +android { + + compileSdk 31 + + defaultConfig { + applicationId "com.example.acapendulum20" + minSdk 26 + targetSdk 31 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + packagingOptions { + exclude 'META-INF/atomicfu.kotlin_module' + } + + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + kotlinOptions { + jvmTarget = '1.8' + } + + buildFeatures { + viewBinding = true + } + +} + +dependencies { + + implementation fileTree(dir: "libs", include: ["*.jar"]) + implementation 'androidx.core:core-ktx:1.7.0' + //implementation "org.jetbrians.kotlin:kotlin-stdlib:$kotlin_version" + implementation 'androidx.appcompat:appcompat:1.4.1' + implementation 'com.google.android.material:material:1.5.0' + implementation 'androidx.constraintlayout:constraintlayout:2.1.3' + implementation 'androidx.legacy:legacy-support-v4:1.0.0' + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'androidx.test.ext:junit:1.1.3' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' + + + // Navigation Component + implementation 'androidx.navigation:navigation-fragment-ktx:2.4.2' + implementation 'androidx.navigation:navigation-ui-ktx:2.4.2' + + // Room components + implementation "androidx.room:room-runtime:2.4.2" + kapt "androidx.room:room-compiler:2.4.2" + implementation "androidx.room:room-ktx:2.4.2" + androidTestImplementation "androidx.room:room-testing:2.4.2" + + // Lifecycle components + implementation "androidx.lifecycle:lifecycle-extensions:2.2.0" + implementation "androidx.lifecycle:lifecycle-common-java8:2.4.1" + implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1" + + // Kotlin components + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.20" + api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1" + api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2" + + +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/app/src/androidTest/java/com/example/acapendulum20/ExampleInstrumentedTest.kt b/app/src/androidTest/java/com/example/acapendulum20/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..8acd87d --- /dev/null +++ b/app/src/androidTest/java/com/example/acapendulum20/ExampleInstrumentedTest.kt @@ -0,0 +1,24 @@ +package com.example.acapendulum20 + +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertEquals("com.example.acapendulum20", appContext.packageName) + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..cbcbe74 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/acapendulum20/MainActivity.kt b/app/src/main/java/com/example/acapendulum20/MainActivity.kt new file mode 100644 index 0000000..e85c088 --- /dev/null +++ b/app/src/main/java/com/example/acapendulum20/MainActivity.kt @@ -0,0 +1,22 @@ +package com.example.acapendulum20 + +import android.graphics.Canvas +import androidx.appcompat.app.AppCompatActivity +import android.os.Bundle +import androidx.navigation.findNavController +import androidx.navigation.ui.setupActionBarWithNavController +import kotlinx.android.synthetic.main.activity_main.* + +class MainActivity : AppCompatActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + + setupActionBarWithNavController(findNavController(R.id.fragment)) + } + + override fun onSupportNavigateUp(): Boolean { + val navController = findNavController(R.id.fragment) + return navController.navigateUp() || super.onSupportNavigateUp() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/acapendulum20/canvas/Canvas.kt b/app/src/main/java/com/example/acapendulum20/canvas/Canvas.kt new file mode 100644 index 0000000..fd50bc2 --- /dev/null +++ b/app/src/main/java/com/example/acapendulum20/canvas/Canvas.kt @@ -0,0 +1,32 @@ +package com.example.acapendulum20.canvas + +import android.content.Context +import android.graphics.Canvas +import android.graphics.Paint +import android.util.AttributeSet +import android.view.View + +class CanvasView @JvmOverloads constructor( + context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 +) : View(context, attrs, defStyleAttr) { + + + val paint: Paint = Paint() + + override fun onDraw(canvas: Canvas?) { + super.onDraw(canvas) + + paint.strokeWidth = 9f + canvas?.drawLine(455f,0f,500f, 630f,paint) + + } + fun drawBob(canvas: Canvas?) { + paint.style = Paint.Style.STROKE + paint.strokeWidth = 10f + canvas?.drawCircle(500f, 700f, 70f, paint) + } + + fun drawBob() { + println("hallo") + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/acapendulum20/data/UserDao.kt b/app/src/main/java/com/example/acapendulum20/data/UserDao.kt new file mode 100644 index 0000000..76ae01f --- /dev/null +++ b/app/src/main/java/com/example/acapendulum20/data/UserDao.kt @@ -0,0 +1,25 @@ +package com.example.acapendulum20.data + +import androidx.lifecycle.LiveData +import androidx.room.* +import com.example.acapendulum20.model.User + +@Dao +interface UserDao { + + @Insert(onConflict = OnConflictStrategy.IGNORE ) + suspend fun addUser(user: User) + + @Update + suspend fun updateUser(user: User) + + @Delete + suspend fun deleteUser(user: User) + + @Query("DELETE FROM user_table") + suspend fun deleteAllUsers() + + @Query("SELECT * FROM user_table ORDER BY id ASC") + fun readAllData(): LiveData> + +} \ No newline at end of file diff --git a/app/src/main/java/com/example/acapendulum20/data/UserDatabase.kt b/app/src/main/java/com/example/acapendulum20/data/UserDatabase.kt new file mode 100644 index 0000000..ff15845 --- /dev/null +++ b/app/src/main/java/com/example/acapendulum20/data/UserDatabase.kt @@ -0,0 +1,33 @@ +package com.example.acapendulum20.data + +import android.content.Context +import androidx.room.Database +import androidx.room.Room +import androidx.room.RoomDatabase +import com.example.acapendulum20.model.User + +@Database(entities = [User::class], version = 1, exportSchema = false) +abstract class UserDatabase: RoomDatabase() { + abstract fun userDao(): UserDao + + companion object { + @Volatile + private var INSTANCE: UserDatabase? = null + + fun getDatabase(context: Context): UserDatabase{ + val tempInstance = INSTANCE + if(tempInstance != null){ + return tempInstance + } + synchronized(this){ + val instance = Room.databaseBuilder( + context.applicationContext, + UserDatabase::class.java, + "user_database" + ).build() + INSTANCE = instance + return instance + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/acapendulum20/fragments/add/AddFragment.kt b/app/src/main/java/com/example/acapendulum20/fragments/add/AddFragment.kt new file mode 100644 index 0000000..e09187b --- /dev/null +++ b/app/src/main/java/com/example/acapendulum20/fragments/add/AddFragment.kt @@ -0,0 +1,63 @@ +package com.example.acapendulum20.fragments.add + +import android.os.Bundle +import android.text.Editable +import android.text.TextUtils +import androidx.fragment.app.Fragment +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.Toast +import androidx.lifecycle.ViewModelProvider +import androidx.navigation.fragment.findNavController +import com.example.acapendulum20.R +import com.example.acapendulum20.model.User +import com.example.acapendulum20.viewmodel.UserViewmodel +import kotlinx.android.synthetic.main.fragment_add.* +import kotlinx.android.synthetic.main.fragment_add.view.* + +class AddFragment : Fragment() { + + private lateinit var mUserViewModel: UserViewmodel + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + // Inflate the layout for this fragment + val view = inflater.inflate(R.layout.fragment_add, container, false) + + mUserViewModel = ViewModelProvider(this).get(UserViewmodel::class.java) + + view.add_btn.setOnClickListener{ + insertDataToDatabase() + } + + return view + } + + private fun insertDataToDatabase() { + val firstName = addFirstName_et.text.toString() + val lastName = addLastName_et.text.toString() + val age = addAge_et.text + val sex = addSex_et.text.toString() + val handiness = addHandiness_et.text.toString() + + if(inputCheck(firstName, lastName, sex, handiness, age)){ + //Create User Objeect + val user = User(0, firstName, lastName,Integer.parseInt(age.toString()), sex, handiness) + //Add Data to Database + mUserViewModel.addUser(user) + Toast.makeText(requireContext(),"Successfully added!", Toast.LENGTH_LONG).show() + //Navigate Back + findNavController().navigate(R.id.action_addFragment_to_listFragment) + }else{ + Toast.makeText(requireContext(),"Please fill out all fields.", Toast.LENGTH_LONG).show() + } + } + + private fun inputCheck(firstName: String, lastName: String, sex: String, handiness:String, age: Editable): Boolean{ + return !(TextUtils.isEmpty(firstName) && TextUtils.isEmpty(lastName) && TextUtils.isEmpty(sex) && TextUtils.isEmpty(handiness) && age.isEmpty()) + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/example/acapendulum20/fragments/list/ListAdapter.kt b/app/src/main/java/com/example/acapendulum20/fragments/list/ListAdapter.kt new file mode 100644 index 0000000..4d6d5f4 --- /dev/null +++ b/app/src/main/java/com/example/acapendulum20/fragments/list/ListAdapter.kt @@ -0,0 +1,48 @@ +package com.example.acapendulum20.fragments.list + +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.navigation.findNavController +import androidx.recyclerview.widget.RecyclerView +import com.example.acapendulum20.R +import com.example.acapendulum20.model.User +import kotlinx.android.synthetic.main.custom_row.view.* + +class ListAdapter: RecyclerView.Adapter() { + + private var userList = emptyList() + + class MyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) { + + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { + return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.custom_row, parent, false)) + } + + override fun getItemCount(): Int { + return userList.size + } + + override fun onBindViewHolder(holder: MyViewHolder, position: Int) { + val currentItem = userList[position] + holder.itemView.id_txt.text = currentItem.id.toString() + holder.itemView.firstName_txt.text = currentItem.firstName + holder.itemView.lastName_txt.text = currentItem.lastName + holder.itemView.age_txt.text = currentItem.age.toString() + holder.itemView.sex_txt.text = currentItem.sex + holder.itemView.handiness_txt.text = currentItem.handiness + + holder.itemView.rowLayout.setOnClickListener{ + val action = ListFragmentDirections.actionListFragmentToUpdateFragment(currentItem) + holder.itemView.findNavController().navigate(action) + } + } + + fun setData(user: List){ + this.userList = user + notifyDataSetChanged() + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/example/acapendulum20/fragments/list/ListFragment.kt b/app/src/main/java/com/example/acapendulum20/fragments/list/ListFragment.kt new file mode 100644 index 0000000..842b09e --- /dev/null +++ b/app/src/main/java/com/example/acapendulum20/fragments/list/ListFragment.kt @@ -0,0 +1,77 @@ +package com.example.acapendulum20.fragments.list + +import android.app.AlertDialog +import android.os.Bundle +import android.view.* +import android.widget.Toast +import androidx.fragment.app.Fragment +import androidx.lifecycle.Observer +import androidx.lifecycle.ViewModelProvider +import androidx.navigation.fragment.findNavController +import androidx.recyclerview.widget.LinearLayoutManager +import com.example.acapendulum20.R +import com.example.acapendulum20.viewmodel.UserViewmodel +import kotlinx.android.synthetic.main.fragment_list.view.* + + +class ListFragment : Fragment() { + + private lateinit var mUserViewModel: UserViewmodel + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + // Inflate the layout for this fragment + val view = inflater.inflate(R.layout.fragment_list, container, false) + + //Recylerview + val adapter = ListAdapter() + val recyclerView = view.recyclerview + recyclerView.adapter = adapter + recyclerView.layoutManager = LinearLayoutManager(requireContext()) + + //UserViewModel + mUserViewModel = ViewModelProvider(this).get(UserViewmodel::class.java) + mUserViewModel.readAllData.observe(viewLifecycleOwner, Observer { user -> + adapter.setData(user) + }) + + + view.floatingActionButton.setOnClickListener { + findNavController().navigate(R.id.action_listFragment_to_addFragment) + } + + // Add menu + setHasOptionsMenu(true) + + return view + } + + override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { + inflater.inflate(R.menu.delete_menu, menu) + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + if (item.itemId == R.id.menu_delete) { + deleteAllUsers() + } + return super.onOptionsItemSelected(item) + } + + private fun deleteAllUsers() { + val builder = AlertDialog.Builder(requireContext()) + builder.setPositiveButton("Yes") { _, _ -> + mUserViewModel.deleteAllUsers() + Toast.makeText( + requireContext(), + "Succesfully removed everything!", + Toast.LENGTH_SHORT + ).show() + } + builder.setNegativeButton("No") { _, _ -> } + builder.setTitle("Delete everything?") + builder.setMessage("Are you sure you want to delete everything?") + builder.create().show() + } + } diff --git a/app/src/main/java/com/example/acapendulum20/fragments/pendulum/PendulumFragment.kt b/app/src/main/java/com/example/acapendulum20/fragments/pendulum/PendulumFragment.kt new file mode 100644 index 0000000..c447f0c --- /dev/null +++ b/app/src/main/java/com/example/acapendulum20/fragments/pendulum/PendulumFragment.kt @@ -0,0 +1,42 @@ +package com.example.acapendulum20.fragments.pendulum + +import android.content.Context +import android.graphics.* +import android.graphics.drawable.Drawable +import android.os.Bundle +import android.util.AttributeSet +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.ImageView +import androidx.core.content.res.ResourcesCompat +import androidx.fragment.app.Fragment +import androidx.lifecycle.ViewModelProvider +import androidx.navigation.fragment.findNavController +import com.example.acapendulum20.R +import com.example.acapendulum20.canvas.CanvasView +import com.example.acapendulum20.viewmodel.UserViewmodel +import kotlinx.android.synthetic.main.fragment_pendulum.* +import kotlinx.android.synthetic.main.fragment_pendulum.view.* +import kotlinx.android.synthetic.main.fragment_update.view.* + + +class PendulumFragment : Fragment() { + + private lateinit var mCanvasView: CanvasView + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + // Inflate the layout for this fragment + val view = inflater.inflate(R.layout.fragment_pendulum, container, false) + + //activate the magnet + view.activate_btn.setOnClickListener{ + mCanvasView.drawBob() + } + + return view + } +} diff --git a/app/src/main/java/com/example/acapendulum20/fragments/update/UpdateFragment.kt b/app/src/main/java/com/example/acapendulum20/fragments/update/UpdateFragment.kt new file mode 100644 index 0000000..9045556 --- /dev/null +++ b/app/src/main/java/com/example/acapendulum20/fragments/update/UpdateFragment.kt @@ -0,0 +1,103 @@ +package com.example.acapendulum20.fragments.update + +import android.app.AlertDialog +import android.os.Bundle +import android.text.Editable +import android.text.TextUtils +import android.view.* +import androidx.fragment.app.Fragment +import android.widget.Toast +import androidx.lifecycle.ViewModelProvider +import androidx.navigation.fragment.findNavController +import androidx.navigation.fragment.navArgs +import com.example.acapendulum20.R +import com.example.acapendulum20.model.User +import com.example.acapendulum20.viewmodel.UserViewmodel +import kotlinx.android.synthetic.main.fragment_update.* +import kotlinx.android.synthetic.main.fragment_update.view.* + +class UpdateFragment : Fragment() { + + private val args by navArgs() + + private lateinit var mUserViewModel: UserViewmodel + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + // Inflate the layout for this fragment + val view = inflater.inflate(R.layout.fragment_update, container, false) + + mUserViewModel = ViewModelProvider(this).get(UserViewmodel::class.java) + + view.updateFirstName_et.setText(args.currentUser.firstName) + view.updateLastName_et.setText(args.currentUser.lastName) + view.updateAge_et.setText(args.currentUser.age.toString()) + view.updateSex_et.setText(args.currentUser.sex) + view.updateHandiness_et.setText(args.currentUser.handiness) + + //navigate to fragment_pendulum + view.startTest_btn.setOnClickListener{ + findNavController().navigate(R.id.action_updateFragment_to_pendulumFragment) + } + + //navigate to fragment_update + view.update_btn.setOnClickListener{ + updateItem() + } + + //Add menu + setHasOptionsMenu(true) + + return view + } + + private fun updateItem(){ + val firstName = updateFirstName_et.text.toString() + val lastName = updateLastName_et.text.toString() + val age = Integer.parseInt(updateAge_et.text.toString()) + val sex = updateSex_et.text.toString() + val handiness = updateHandiness_et.text.toString() + + if(inputCheck(firstName, lastName, sex, handiness, updateAge_et.text)){ + // Create User Object + val updatedUser = User(args.currentUser.id, firstName, lastName, age, sex, handiness) + // update current User + mUserViewModel.updateUser(updatedUser) + Toast.makeText(requireContext(), "Updated Successfully!", Toast.LENGTH_SHORT).show() + // Navigate Back + findNavController().navigate(R.id.action_updateFragment_to_listFragment) + }else{ + Toast.makeText(requireContext(), "Please fill out all fields!", Toast.LENGTH_SHORT).show() + } + } + + private fun inputCheck(firstName: String, lastName: String, sex: String, handiness:String, age: Editable): Boolean{ + return !(TextUtils.isEmpty(firstName) && TextUtils.isEmpty(lastName) && TextUtils.isEmpty(sex) && TextUtils.isEmpty(handiness) && age.isEmpty()) + } + + override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { + inflater.inflate(R.menu.delete_menu, menu) + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + if(item.itemId == R.id.menu_delete){ + deleteUser() + } + return super.onOptionsItemSelected(item) + } + + private fun deleteUser() { + val builder = AlertDialog.Builder(requireContext()) + builder.setPositiveButton("Yes") { _, _ -> + mUserViewModel.deleteUser(args.currentUser) + Toast.makeText(requireContext(), "Succesfully removed : ${args.currentUser.firstName}", Toast.LENGTH_SHORT).show() + findNavController().navigate(R.id.action_updateFragment_to_listFragment) + } + builder.setNegativeButton("No") { _, _ ->} + builder.setTitle("Delete ${args.currentUser.firstName}?") + builder.setMessage("Are you sure you want to delete ${args.currentUser.firstName}?") + builder.create().show() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/acapendulum20/model/User.kt b/app/src/main/java/com/example/acapendulum20/model/User.kt new file mode 100644 index 0000000..b9eda8d --- /dev/null +++ b/app/src/main/java/com/example/acapendulum20/model/User.kt @@ -0,0 +1,19 @@ +package com.example.acapendulum20.model + +import android.os.Parcelable +import androidx.room.Entity +import androidx.room.PrimaryKey +import kotlinx.android.parcel.Parcelize + +@Parcelize +@Entity(tableName = "user_table") +data class User ( + @PrimaryKey(autoGenerate = true) + val id: Int, + val firstName: String, + val lastName: String, + val age: Int, + val sex: String, + val handiness: String, + +): Parcelable \ No newline at end of file diff --git a/app/src/main/java/com/example/acapendulum20/repository/UserRepository.kt b/app/src/main/java/com/example/acapendulum20/repository/UserRepository.kt new file mode 100644 index 0000000..6593240 --- /dev/null +++ b/app/src/main/java/com/example/acapendulum20/repository/UserRepository.kt @@ -0,0 +1,26 @@ +package com.example.acapendulum20.repository + +import androidx.lifecycle.LiveData +import com.example.acapendulum20.data.UserDao +import com.example.acapendulum20.model.User + +class UserRepository(private val userDao: UserDao) { + + val readAllData: LiveData> = userDao.readAllData() + + suspend fun addUser(user: User){ + userDao.addUser(user) + } + + suspend fun updateUser(user: User){ + userDao.updateUser(user) + } + + suspend fun deleteUser(user: User){ + userDao.deleteUser(user) + } + + suspend fun deleteAllUser(){ + userDao.deleteAllUsers() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/acapendulum20/viewmodel/UserViewmodel.kt b/app/src/main/java/com/example/acapendulum20/viewmodel/UserViewmodel.kt new file mode 100644 index 0000000..418e719 --- /dev/null +++ b/app/src/main/java/com/example/acapendulum20/viewmodel/UserViewmodel.kt @@ -0,0 +1,47 @@ +package com.example.acapendulum20.viewmodel + +import android.app.Application +import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.LiveData +import androidx.lifecycle.viewModelScope +import com.example.acapendulum20.data.UserDatabase +import com.example.acapendulum20.repository.UserRepository +import com.example.acapendulum20.model.User +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch + +class UserViewmodel(application: Application): AndroidViewModel(application) { + + val readAllData: LiveData> + private val repository: UserRepository + + init{ + val userDao = UserDatabase.getDatabase(application).userDao() + repository = UserRepository(userDao) + readAllData = repository.readAllData + } + + fun addUser(user: User){ + viewModelScope.launch(Dispatchers.IO){ + repository.addUser(user) + } + } + + fun updateUser(user: User){ + viewModelScope.launch(Dispatchers.IO) { + repository.updateUser(user) + } + } + + fun deleteUser(user: User){ + viewModelScope.launch(Dispatchers.IO) { + repository.deleteUser(user) + } + } + + fun deleteAllUsers(){ + viewModelScope.launch(Dispatchers.IO) { + repository.deleteAllUser() + } + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_add.xml b/app/src/main/res/drawable/ic_add.xml new file mode 100644 index 0000000..eb23254 --- /dev/null +++ b/app/src/main/res/drawable/ic_add.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/ic_baseline_delete_24.xml b/app/src/main/res/drawable/ic_baseline_delete_24.xml new file mode 100644 index 0000000..3c4030b --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_delete_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..865bd35 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,21 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/custom_row.xml b/app/src/main/res/layout/custom_row.xml new file mode 100644 index 0000000..9b85f4f --- /dev/null +++ b/app/src/main/res/layout/custom_row.xml @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_add.xml b/app/src/main/res/layout/fragment_add.xml new file mode 100644 index 0000000..dbbfc8e --- /dev/null +++ b/app/src/main/res/layout/fragment_add.xml @@ -0,0 +1,79 @@ + + + + + + + + + + + + + +