Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

milestone_2 completed #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions .idea/misc.xml

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

3 changes: 3 additions & 0 deletions app/.idea/.gitignore

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

13 changes: 13 additions & 0 deletions app/.idea/gradle.xml

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

9 changes: 9 additions & 0 deletions app/.idea/misc.xml

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

8 changes: 8 additions & 0 deletions app/.idea/modules.xml

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

6 changes: 6 additions & 0 deletions app/.idea/vcs.xml

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

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.androidproject;


import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -10,8 +12,8 @@
import androidx.annotation.Nullable;
import java.util.ArrayList;

public class ArticleAdapter extends ArrayAdapter<ArticleModel> {

public class ArticleAdapter extends ArrayAdapter<ArticleModel>
{
private final ItemClick itemClick;

public interface ItemClick {
Expand All @@ -27,7 +29,7 @@ public ArticleAdapter(@NonNull Context context, ArrayList<ArticleModel> data, It
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ArticleModel data = getItem(position);
View view = LayoutInflater.from(getContext()).inflate(R.layout.item_article, parent, false);
@SuppressLint("ViewHolder") View view = LayoutInflater.from(getContext()).inflate(R.layout.item_article, parent, false);
view.findViewById(R.id.root).setOnClickListener(v -> itemClick.onClick(v, data));
((TextView) view.findViewById(R.id.web_title)).setText(data.getTitle());
((TextView) view.findViewById(R.id.web_url)).setText(data.getUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.os.Parcel;
import android.os.Parcelable;


public class ArticleModel implements Parcelable {
private String id;
private String title;
Expand All @@ -15,7 +16,6 @@ public ArticleModel(String id, String title, String url, String sectionName) {
this.url = url;
this.sectionName = sectionName;
}

public String getId() {
return id;
}
Expand Down
33 changes: 30 additions & 3 deletions app/src/main/java/com/example/androidproject/FavoriteActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import java.util.ArrayList;


public class FavoriteActivity extends AppCompatActivity implements ArticleAdapter.ItemClick {

public static final String KEY_IS_DELETE = "KEY_IS_DELETE";
Expand All @@ -19,7 +25,8 @@ public class FavoriteActivity extends AppCompatActivity implements ArticleAdapte
private ArticleAdapter arrayAdapter;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_favorite);
Expand All @@ -29,8 +36,11 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

setSupportActionBar(toolbar);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
if (getSupportActionBar() != null)
{
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}

articleModels.addAll(GDatabase.getInstance(this).get());
arrayAdapter = new ArticleAdapter(this, articleModels, this);
Expand All @@ -54,4 +64,21 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
arrayAdapter.notifyDataSetChanged();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == R.id.menu_help) {
utils.showDialogHelp(this, getString(R.string.help_item_favorite));
return true;
}
return super.onOptionsItemSelected(item);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public class FavoriteContract
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_URL = "url";
public static final String COLUMN_SECTION_NAME = "section_name";

}

Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,24 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
btnOpenInBrowser.setOnClickListener(v -> utils.openBrowser(v, articleModel.getUrl()));

FloatingActionButton fabSave = findViewById(R.id.fab_save);

fabSave.setOnClickListener(v -> {
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setTitle("Are you sure want to delete from favorites?");
builder.setTitle(R.string.dialog_title_delete_favorite);
builder.setCancelable(true);
builder.setPositiveButton("Yes", (dialog, id) -> {
builder.setPositiveButton(R.string.yes, (dialog, id) -> {
if (GDatabase.getInstance(getBaseContext()).delete(articleModel.getId())) {
Toast.makeText(v.getContext(), "Success Delete To Favorite", Toast.LENGTH_LONG).show();
Toast.makeText(v.getContext(), v.getContext().getString(R.string.success_delete_favorite), Toast.LENGTH_LONG).show();
Intent intent = new Intent();
intent.putExtra(FavoriteActivity.KEY_IS_DELETE, true);
setResult(Activity.RESULT_OK, intent);
finish();
} else {
Toast.makeText(v.getContext(), "Fail Delete To Favorite", Toast.LENGTH_LONG).show();
Toast.makeText(v.getContext(), v.getContext().getString(R.string.fail_delete_favorite), Toast.LENGTH_LONG).show();
}
dialog.cancel();
});
builder.setNegativeButton("No", (dialog, id) -> dialog.cancel());
builder.setNegativeButton(R.string.no,(dialog, id) -> dialog.cancel());
AlertDialog dialog = builder.create();
dialog.show();
});
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/com/example/androidproject/GDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ public static GDatabase getInstance(Context context) {
return guardianDatabase;
}


private static final String DATABASE_NAME = "guardian";

private GDatabase(Context context) {
private GDatabase(Context context)
{
super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
public void onCreate(SQLiteDatabase db)
{
db.execSQL(FavoriteContract.QUERY_CREATE_TABLE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ private static GuardianResponse jsonObjectToGuardianResponse(JSONObject jsonObje
);
}


public static GuardianResponse search(@Nullable String query) throws Throwable {
StringBuilder stringUrl = new StringBuilder(BASE_URL);
stringUrl.append("/search?api-key=1fb36b70-1588-4259-b703-2570ea1fac6a");
stringUrl.append("&page-size=50");
if (query !=null) stringUrl.append("&q=").append(query);
if (query !=null) stringUrl.append(query);

URL url = new URL(stringUrl.toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import android.os.Looper;
import androidx.annotation.Nullable;

public class GuardianAsyncTask extends AsyncTask<String, Void, GuardianResponse> {

public class GuardianAsyncTask extends AsyncTask<String, Void, GuardianResponse>
{
public interface Callback {
void onPreExecute();

Expand All @@ -17,6 +17,7 @@ public interface Callback {

private final Callback callback;


@SuppressWarnings("deprecation")
public GuardianAsyncTask(Callback callback) {
this.callback = callback;
Expand All @@ -28,6 +29,7 @@ protected void onPreExecute() {
callback.onPreExecute();
}


@Override
protected GuardianResponse doInBackground(String... strings) {
String query = strings.length > 0 ? strings[0] : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import androidx.annotation.NonNull;
import java.util.List;

public class GuardianResponse {
public class GuardianResponse
{
private String status;
private String userTier;
private int total;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public GuardianResult(String id, String type, String sectionId, String sectionNa
this.pillarName = pillarName;
}


public String getId() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ public HttpError(@Nullable String message)
super(message);
}
}

Loading