Skip to content

Commit cdc3510

Browse files
authored
feat: Add new card via QR code (#4)
* feat: Back to main activity after capturing QR You can't add a new card unless you refresh the app * Revert last commit * Fix back to main activity from QR Scanner * feat: Hardening design
1 parent d824e98 commit cdc3510

6 files changed

Lines changed: 13 additions & 60 deletions

File tree

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/com/aros/naufal/qrreader/data/database/CardRoomDatabase.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,41 +25,10 @@ public static CardRoomDatabase getDatabase(final Context context) {
2525
if (INSTANCE == null) {
2626
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
2727
CardRoomDatabase.class, "card_database")
28-
.addCallback(sRoomDatabaseCallback)
2928
.build();
3029
}
3130
}
3231
}
3332
return INSTANCE;
3433
}
35-
36-
private static RoomDatabase.Callback sRoomDatabaseCallback =
37-
new RoomDatabase.Callback() {
38-
39-
@Override
40-
public void onOpen(@NonNull SupportSQLiteDatabase db) {
41-
super.onOpen(db);
42-
new PopulateDbAsync(INSTANCE).execute();
43-
}
44-
};
45-
46-
private static class PopulateDbAsync extends AsyncTask<Void, Void, Void> {
47-
48-
private final CardDao mDao;
49-
50-
PopulateDbAsync(CardRoomDatabase db) {
51-
mDao = db.cardDao();
52-
}
53-
54-
@Override
55-
protected Void doInBackground(final Void... params) {
56-
Log.d("CardRoomDatabase", "Populating database");
57-
mDao.deleteAll();
58-
Card card = new Card("Naufal Aros");
59-
mDao.insert(card);
60-
card = new Card("Miguel Rojo");
61-
mDao.insert(card);
62-
return null;
63-
}
64-
}
6534
}

app/src/main/java/com/aros/naufal/qrreader/ui/activities/MainActivity.java

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public class MainActivity extends AppCompatActivity implements ZXingScannerView.
3030
private ZXingScannerView mScannerView;
3131
private CardViewModel mCardViewModel;
3232
private static final String TAG = "MainActivity";
33+
private RecyclerView recyclerView;
34+
private CardListAdapter adapter;
3335

3436
@Override
3537
protected void onCreate(Bundle savedInstanceState) {
@@ -66,8 +68,8 @@ public void onMenuClosed() {
6668
}
6769
});
6870

69-
RecyclerView recyclerView = findViewById(R.id.recycler_view);
70-
final CardListAdapter adapter = new CardListAdapter(this);
71+
recyclerView = findViewById(R.id.recycler_view);
72+
adapter = new CardListAdapter(this);
7173
recyclerView.setAdapter(adapter);
7274
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
7375
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
@@ -97,27 +99,11 @@ protected void onPause() {
9799

98100
@Override
99101
public void handleResult(final Result result) {
100-
// Do something with the result here
101-
System.out.println("RESULT ==>> " + result.getText());
102-
Log.d("handler", result.getText()); // Prints scan results
103-
Log.d("handler", result.getBarcodeFormat().toString()); // Prints the scan format (qrcode)
104-
// show the scanner result into dialog box.
105-
AlertDialog.Builder builder = new AlertDialog.Builder(this);
106-
builder.setTitle("Scan Result");
107-
builder.setNeutralButton("Añadir contacto", new DialogInterface.OnClickListener() {
108-
@Override
109-
public void onClick(DialogInterface dialog, int which) {
110-
mCardViewModel.instert(new Card(result.getText()));
111-
Toast.makeText(MainActivity.this, "Confirmado", Toast.LENGTH_LONG).show();
112-
}
113-
});
114-
115-
builder.setMessage(result.getText());
116-
AlertDialog alert1 = builder.create();
117-
118-
alert1.show();
119-
120-
// If you would like to resume scanning, call this method below:
121-
mScannerView.resumeCameraPreview(this);
102+
mCardViewModel.instert(new Card(result.getText()));
103+
Toast.makeText(MainActivity.this, "Confirmed", Toast.LENGTH_LONG).show();
104+
mScannerView.removeAllViews();
105+
mScannerView.stopCamera();
106+
finish();
107+
startActivity(getIntent());
122108
}
123109
}

app/src/main/res/layout/content_main.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
android:id="@+id/recycler_view"
1414
android:layout_width="match_parent"
1515
android:layout_height="match_parent"
16-
android:background="@android:color/white"
1716
tools:listitem="@layout/recyclerview_item" />
1817

1918
</android.support.constraint.ConstraintLayout>

app/src/main/res/values/colors.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3-
<color name="colorPrimary">#3F51B5</color>
4-
<color name="colorPrimaryDark">#303F9F</color>
3+
<color name="colorPrimary">#000000</color>
4+
<color name="colorPrimaryDark">#000000</color>
55
<color name="colorAccent">#FF4081</color>
66
<color name="color">#00FF00</color>
77
<color name="fabButton">#2196F3</color>

app/src/main/res/values/styles.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
<item name="android:layout_width">match_parent</item>
1313
<item name="android:layout_height">26dp</item>
1414
<item name="android:textSize">24sp</item>
15-
<item name="android:textStyle">bold</item>
1615
<item name="android:layout_marginBottom">6dp</item>
1716
<item name="android:paddingLeft">8dp</item>
1817
</style>

0 commit comments

Comments
 (0)