Skip to content

Commit a27bf70

Browse files
author
Aaron Sarazan
committed
Java notepad
0 parents  commit a27bf70

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1309
-0
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
9+
.externalNativeBuild
10+
.idea

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 26
5+
buildToolsVersion '26.0.2'
6+
defaultConfig {
7+
applicationId "com.udacity.notepad"
8+
minSdkVersion 16
9+
targetSdkVersion 26
10+
versionCode 1
11+
versionName "1.0"
12+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13+
vectorDrawables.useSupportLibrary = true
14+
}
15+
buildTypes {
16+
release {
17+
minifyEnabled false
18+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19+
}
20+
}
21+
}
22+
23+
dependencies {
24+
implementation fileTree(dir: 'libs', include: ['*.jar'])
25+
implementation "com.android.support.constraint:constraint-layout:1.0.2"
26+
implementation "com.android.support:design:$support_version"
27+
implementation "com.android.support:appcompat-v7:$support_version"
28+
implementation "com.android.support:recyclerview-v7:$support_version"
29+
implementation "com.android.support:cardview-v7:$support_version"
30+
}

app/proguard-rules.pro

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/mek556/Library/Android/sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
18+
19+
# Uncomment this to preserve the line number information for
20+
# debugging stack traces.
21+
#-keepattributes SourceFile,LineNumberTable
22+
23+
# If you keep the line number information, uncomment this to
24+
# hide the original source file name.
25+
#-renamesourcefileattribute SourceFile

app/src/main/AndroidManifest.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.udacity.notepad">
4+
5+
<application
6+
android:name=".NotesApplication"
7+
android:allowBackup="true"
8+
android:icon="@mipmap/ic_launcher"
9+
android:label="@string/app_name"
10+
android:roundIcon="@mipmap/ic_launcher_round"
11+
android:supportsRtl="true"
12+
android:theme="@style/AppTheme">
13+
<activity
14+
android:name=".MainActivity"
15+
android:label="@string/app_name"
16+
android:theme="@style/AppTheme.NoActionBar">
17+
<intent-filter>
18+
<action android:name="android.intent.action.MAIN" />
19+
20+
<category android:name="android.intent.category.LAUNCHER" />
21+
</intent-filter>
22+
</activity>
23+
<activity android:name=".crud.CreateActivity"
24+
android:windowSoftInputMode="stateVisible|adjustPan"
25+
/>
26+
</application>
27+
28+
</manifest>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.udacity.notepad;
2+
3+
import android.os.Bundle;
4+
import android.support.design.widget.FloatingActionButton;
5+
import android.support.v7.app.AppCompatActivity;
6+
import android.support.v7.widget.LinearLayoutManager;
7+
import android.support.v7.widget.RecyclerView;
8+
import android.support.v7.widget.Toolbar;
9+
import android.view.Menu;
10+
import android.view.MenuItem;
11+
import android.view.View;
12+
13+
import com.udacity.notepad.crud.CreateActivity;
14+
import com.udacity.notepad.recycler.NotesAdapter;
15+
import com.udacity.notepad.util.SpaceItemDecoration;
16+
17+
public class MainActivity extends AppCompatActivity {
18+
19+
private RecyclerView recycler;
20+
21+
@Override
22+
public void onCreate(Bundle savedInstanceState) {
23+
super.onCreate(savedInstanceState);
24+
setContentView(R.layout.activity_main);
25+
26+
Toolbar toolbar = findViewById(R.id.toolbar);
27+
setSupportActionBar(toolbar);
28+
29+
FloatingActionButton fab = findViewById(R.id.fab);
30+
fab.setOnClickListener(new View.OnClickListener() {
31+
@Override
32+
public void onClick(View v) {
33+
startActivity(CreateActivity.get(MainActivity.this));
34+
}
35+
});
36+
37+
recycler = findViewById(R.id.recycler);
38+
recycler.setLayoutManager(new LinearLayoutManager(this));
39+
recycler.addItemDecoration(new SpaceItemDecoration(this, R.dimen.margin_small));
40+
recycler.setAdapter(new NotesAdapter(this));
41+
}
42+
43+
@Override
44+
protected void onResume() {
45+
super.onResume();
46+
refresh();
47+
}
48+
49+
@Override
50+
public void onDestroy() {
51+
super.onDestroy();
52+
recycler.setAdapter(null);
53+
}
54+
55+
@Override
56+
public boolean onCreateOptionsMenu(Menu menu) {
57+
// Inflate the menu; this adds items to the action bar if it is present.
58+
getMenuInflater().inflate(R.menu.menu_main, menu);
59+
return true;
60+
}
61+
62+
@Override
63+
public boolean onOptionsItemSelected(MenuItem item) {
64+
// Handle action bar item clicks here. The action bar will
65+
// automatically handle clicks on the Home/Up button, so long
66+
// as you specify a parent activity in AndroidManifest.xml.
67+
switch (item.getItemId()) {
68+
case R.id.action_settings:
69+
return true;
70+
default:
71+
return super.onOptionsItemSelected(item);
72+
}
73+
}
74+
75+
76+
private void refresh() {
77+
((NotesAdapter) recycler.getAdapter()).refresh();
78+
}
79+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.udacity.notepad;
2+
3+
import android.app.Application;
4+
5+
import com.udacity.notepad.data.DataStore;
6+
7+
public class NotesApplication extends Application {
8+
9+
@Override
10+
public void onCreate() {
11+
super.onCreate();
12+
DataStore.init(this);
13+
}
14+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.udacity.notepad.crud;
2+
3+
import android.content.Context;
4+
import android.content.Intent;
5+
import android.os.Bundle;
6+
import android.support.annotation.Nullable;
7+
import android.support.v7.app.AppCompatActivity;
8+
import android.view.Menu;
9+
import android.view.MenuItem;
10+
import android.widget.TextView;
11+
12+
import com.udacity.notepad.R;
13+
import com.udacity.notepad.data.DataStore;
14+
import com.udacity.notepad.data.Note;
15+
16+
import java.util.Date;
17+
18+
public class CreateActivity extends AppCompatActivity {
19+
20+
public static Intent get(Context context) {
21+
return new Intent(context, CreateActivity.class);
22+
}
23+
24+
private TextView editText;
25+
26+
@Override
27+
protected void onCreate(@Nullable Bundle savedInstanceState) {
28+
super.onCreate(savedInstanceState);
29+
setContentView(R.layout.activity_create);
30+
editText = findViewById(R.id.edit_text);
31+
}
32+
33+
@Override
34+
public boolean onCreateOptionsMenu(Menu menu) {
35+
getMenuInflater().inflate(R.menu.menu_accept, menu);
36+
return super.onCreateOptionsMenu(menu);
37+
}
38+
39+
@Override
40+
public boolean onOptionsItemSelected(MenuItem item) {
41+
switch (item.getItemId()) {
42+
case R.id.action_accept:
43+
save();
44+
finish();
45+
break;
46+
default:
47+
return super.onOptionsItemSelected(item);
48+
}
49+
return super.onOptionsItemSelected(item);
50+
}
51+
52+
private void save() {
53+
DataStore.execute(new Runnable() {
54+
@Override
55+
public void run() {
56+
Note note = updateNote();
57+
DataStore.getNotes().insert(note);
58+
}
59+
});
60+
}
61+
62+
private Note updateNote() {
63+
Note note = new Note();
64+
note.setText(editText.getText().toString());
65+
note.setUpdatedAt(new Date());
66+
return note;
67+
}
68+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.udacity.notepad.data;
2+
3+
import android.content.Context;
4+
5+
import java.util.concurrent.Executor;
6+
import java.util.concurrent.Executors;
7+
8+
public final class DataStore {
9+
10+
private DataStore() {}
11+
12+
public static final Executor EXEC = Executors.newSingleThreadExecutor();
13+
14+
private static NoteDatabase notes;
15+
16+
public static void init(Context context) {
17+
notes = new NoteDatabase(context);
18+
}
19+
20+
public static NoteDatabase getNotes() {
21+
return notes;
22+
}
23+
24+
public static void execute(Runnable runnable) {
25+
EXEC.execute(runnable);
26+
}
27+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.udacity.notepad.data;
2+
3+
import android.support.annotation.Nullable;
4+
5+
import java.util.Date;
6+
7+
public class Note {
8+
private int id = -1;
9+
@Nullable private String text;
10+
private boolean isPinned = false;
11+
private Date createdAt = new Date();
12+
private Date updatedAt;
13+
14+
public int getId() {
15+
return id;
16+
}
17+
18+
public void setId(int id) {
19+
this.id = id;
20+
}
21+
22+
@Nullable
23+
public String getText() {
24+
return text;
25+
}
26+
27+
public void setText(@Nullable String text) {
28+
this.text = text;
29+
}
30+
31+
public boolean isPinned() {
32+
return isPinned;
33+
}
34+
35+
public void setPinned(boolean pinned) {
36+
isPinned = pinned;
37+
}
38+
39+
public Date getCreatedAt() {
40+
return createdAt;
41+
}
42+
43+
public void setCreatedAt(Date createdAt) {
44+
this.createdAt = createdAt;
45+
}
46+
47+
public Date getUpdatedAt() {
48+
return updatedAt;
49+
}
50+
51+
public void setUpdatedAt(Date updatedAt) {
52+
this.updatedAt = updatedAt;
53+
}
54+
}

0 commit comments

Comments
 (0)