Skip to content
Open
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
1 change: 1 addition & 0 deletions .idea/gradle.xml

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

2 changes: 1 addition & 1 deletion .idea/misc.xml

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

78 changes: 70 additions & 8 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 30
buildToolsVersion "30.0.1"

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

packagingOptions {
exclude 'META-INF/atomicfu.kotlin_module'
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}

defaultConfig {
applicationId "com.example.androidpractice2020"
minSdkVersion 21
Expand All @@ -25,13 +39,61 @@ android {
}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
// implementation fileTree(dir: "libs", include: ["*.jar"])
// implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
// implementation 'androidx.core:core-ktx:1.3.1'
// implementation 'androidx.appcompat:appcompat:1.2.0'
// implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
// implementation 'androidx.room:room-runtime:2.2.5'
// implementation 'com.google.android.material:material:1.2.1'
// testImplementation 'junit:junit:4.12'
// androidTestImplementation 'androidx.test.ext:junit:1.1.2'
// androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

implementation "androidx.appcompat:appcompat:$rootProject.appCompatVersion"
implementation "androidx.activity:activity-ktx:$rootProject.activityVersion"

// Dependencies for working with Architecture components
// You'll probably have to update the version numbers in build.gradle (Project)

// Room components
implementation "androidx.room:room-ktx:$rootProject.roomVersion"
kapt "androidx.room:room-compiler:$rootProject.roomVersion"
androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"

// Lifecycle components
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$rootProject.lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$rootProject.lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-common-java8:$rootProject.lifecycleVersion"

// Kotlin components
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutines"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а тебе точно эт надо?

api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutines"

// UI
implementation "androidx.constraintlayout:constraintlayout:$rootProject.constraintLayoutVersion"
implementation "com.google.android.material:material:$rootProject.materialVersion"

// Testing
testImplementation "junit:junit:$rootProject.junitVersion"
androidTestImplementation "androidx.arch.core:core-testing:$rootProject.coreTestingVersion"
androidTestImplementation ("androidx.test.espresso:espresso-core:$rootProject.espressoVersion", {
exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestImplementation "androidx.test.ext:junit:$rootProject.androidxJunitVersion"

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutines"
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutines"
annotationProcessor 'androidx.room:room-compiler:2.2.5'

def lifecycle_version = "2.2.0"

// LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation 'androidx.fragment:fragment-ktx:1.1.0'


}
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
package="com.example.androidpractice2020">

<application
android:name=".database.NotesApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".ui.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Binary file added app/src/main/ic_add-playstore.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/ic_back-playstore.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 0 additions & 11 deletions app/src/main/java/com/example/androidpractice2020/MainActivity.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.androidpractice2020.database

import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.example.androidpractice2020.notes.Note
import kotlinx.coroutines.flow.Flow

interface NoteDao {
@Query("SELECT * FROM note")
fun getListNotes(): Flow<List<Note>>

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(note: Note)

@Query("DELETE FROM note")
suspend fun deleteAll()

@Update()
suspend fun update()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.androidpractice2020.database

import androidx.annotation.WorkerThread
import com.example.androidpractice2020.notes.Note
import kotlinx.coroutines.flow.Flow

class NoteRepository(private val noteDao: NoteDao) {

val allNotes: Flow<List<Note>> = noteDao.getListNotes()

@Suppress("RedundantSuspendModifier")
@WorkerThread

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А смысл?, у тебя дао само его уже на ио запустит, нет смысла указывать тред на верхние уровни

suspend fun insert(note: Note) {
noteDao.insert(note)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.example.androidpractice2020.database

import android.content.Context
import androidx.room.*
import androidx.sqlite.db.SupportSQLiteDatabase
import com.example.androidpractice2020.notes.Note
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@Database(entities = [Note::class], version = 1, exportSchema = false)
abstract class NoteRoomDataBase : RoomDatabase() {

abstract fun noteDao(): NoteDao

companion object {
@Volatile
private var INSTANCE: NoteRoomDataBase? = null

fun getDatabase(
context: Context,
scope: CoroutineScope
): NoteRoomDataBase {
return INSTANCE
?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
NoteRoomDataBase::class.java,
"note"
)
.fallbackToDestructiveMigration()
.addCallback(
NoteDatabaseCallback(
scope
)
)
.build()
INSTANCE = instance
instance
}
}

private class NoteDatabaseCallback(
private val scope: CoroutineScope
) : RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)

INSTANCE?.let { database ->
scope.launch(Dispatchers.IO) {
populateDatabase(
database.noteDao()
)
}
}
}
}

suspend fun populateDatabase(noteDao: NoteDao) {
noteDao.deleteAll()

var example = Note(0, "hqweqweo", "hqweqwe")
noteDao.insert(example)
example = Note(1, "qweqweqwe","qweqwqweqwewq00")
noteDao.insert(example)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.androidpractice2020.database

import androidx.lifecycle.*
import com.example.androidpractice2020.notes.Note
import kotlinx.coroutines.launch

class NoteViewModel(private val repository: NoteRepository): ViewModel() {

val allNotes: LiveData<List<Note>> = repository.allNotes.asLiveData()

fun insert(note: Note) = viewModelScope.launch {
repository.insert(note)
}
}

class NoteViewModelFactory(private val repository: NoteRepository) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(NoteViewModel::class.java)) {
@Suppress("UNCHECKED_CAST")
return NoteViewModel(
repository
) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.androidpractice2020.database

import android.app.Application
import com.example.androidpractice2020.database.NoteRepository
import com.example.androidpractice2020.database.NoteRoomDataBase
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob

class NotesApplication : Application() {

val applicationScope = CoroutineScope(SupervisorJob())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не оч понятно зачем это. Наверное, чтоб при первом запуске что-то было в бд, но выглядит как-то мега костыльно. Смысл скоупа в том что он зависит от жц элементов, а ты создаешь applicationScope на все приложение. Тогда мог бы и глобалскоуп использовать. Вообщем очень сомнительное решение, которое в принципе рушит идею самих скоупов



val database by lazy { NoteRoomDataBase.getDatabase(this, applicationScope) }
val repository by lazy {
NoteRepository(
database.noteDao()
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.androidpractice2020.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.androidpractice2020.R

class NoteEditAddFragment : Fragment() {

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

return inflater.inflate(R.layout.activity_note_details, container, false)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.androidpractice2020.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.androidpractice2020.R

class NoteListFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.activity_todolist, container, false)
}

interface OnSelectedButtonListener {
fun onButtonSelected(buttonIndex: Int)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.androidpractice2020.interfacecell

interface CellClickListener {
fun onCellClickListener()
fun onDeleteClickListener()
}
18 changes: 18 additions & 0 deletions app/src/main/java/com/example/androidpractice2020/notes/Note.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.androidpractice2020.notes

import android.os.Parcelable
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import kotlinx.android.parcel.Parcelize

@Parcelize
@Entity(tableName = "note")
data class Note(
@PrimaryKey(autoGenerate = true)
val id: Int = 0,
@ColumnInfo(name = "title")
var title: String,
@ColumnInfo(name = "description")
var description: String
) : Parcelable
Loading