-
Notifications
You must be signed in to change notification settings - Fork 0
Added code of milestone4 #4
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
Open
falsefox13
wants to merge
2
commits into
master
Choose a base branch
from
milestone4
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
231 changes: 231 additions & 0 deletions
231
app/src/main/java/com/example/mobilelab/NewItemActivity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,231 @@ | ||
| package com.example.mobilelab; | ||
|
|
||
| import android.app.DatePickerDialog; | ||
| import android.content.ContentResolver; | ||
| import android.content.Intent; | ||
| import android.net.Uri; | ||
| import android.os.AsyncTask; | ||
| import android.os.Bundle; | ||
| import android.view.MenuItem; | ||
| import android.view.View; | ||
| import android.view.WindowManager; | ||
| import android.webkit.MimeTypeMap; | ||
| import android.widget.EditText; | ||
| import android.widget.ImageView; | ||
| import android.widget.ProgressBar; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.appcompat.app.AppCompatActivity; | ||
|
|
||
| import com.google.android.material.snackbar.Snackbar; | ||
| import com.google.android.material.textfield.TextInputLayout; | ||
| import com.google.auth.oauth2.ServiceAccountCredentials; | ||
| import com.google.cloud.storage.Blob; | ||
| import com.google.cloud.storage.BlobId; | ||
| import com.google.cloud.storage.BlobInfo; | ||
| import com.google.cloud.storage.Bucket; | ||
| import com.google.cloud.storage.Storage; | ||
| import com.google.cloud.storage.StorageOptions; | ||
| import com.squareup.picasso.Picasso; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.util.Calendar; | ||
| import java.util.Objects; | ||
|
|
||
| import retrofit2.Call; | ||
| import retrofit2.Callback; | ||
| import retrofit2.Response; | ||
|
|
||
| public class NewItemActivity extends AppCompatActivity { | ||
| private static final int IMAGE_REQUEST = 1; | ||
| private TextInputLayout titleField; | ||
| private TextInputLayout placeField; | ||
| private TextInputLayout dateField; | ||
| private TextInputLayout priceField; | ||
| private DatePickerDialog picker; | ||
| private ProgressBar progressBar; | ||
| private String imgDownloadLink; | ||
|
|
||
| @Override | ||
| protected void onCreate(Bundle savedInstanceState) { | ||
| super.onCreate(savedInstanceState); | ||
| setContentView(R.layout.activity_new_item); | ||
| Objects.requireNonNull(getSupportActionBar()).setTitle(getString(R.string.new_item)); | ||
| getSupportActionBar().setDisplayHomeAsUpEnabled(true); | ||
| initFields(); | ||
| } | ||
|
|
||
| private void initFields() { | ||
| titleField = findViewById(R.id.title_wrapper); | ||
| placeField = findViewById(R.id.place_wrapper); | ||
| dateField = findViewById(R.id.date_wrapper); | ||
| progressBar = findViewById(R.id.progress_bar); | ||
| priceField = findViewById(R.id.price_wrapper); | ||
| findViewById(R.id.new_image_button).setOnClickListener(v -> openImage()); | ||
| initButton(); | ||
| initDatePicker(); | ||
| } | ||
|
|
||
| private void initButton() { | ||
| findViewById(R.id.btn_create_item).setOnClickListener(v -> { | ||
| final String title = Objects.requireNonNull(titleField.getEditText()).getText().toString(); | ||
| final String name = Objects.requireNonNull(placeField.getEditText()).getText().toString(); | ||
| final String date = Objects.requireNonNull(dateField.getEditText()).getText().toString(); | ||
| final String price = Objects.requireNonNull(priceField.getEditText()).getText().toString(); | ||
| addItem(title, name, date, price, imgDownloadLink); | ||
| }); | ||
| } | ||
|
|
||
| private void initDatePicker() { | ||
| final EditText date = dateField.getEditText(); | ||
| Objects.requireNonNull(date).setOnClickListener(v -> { | ||
| final Calendar calendar = Calendar.getInstance(); | ||
| int day = calendar.get(Calendar.DAY_OF_MONTH); | ||
| int month = calendar.get(Calendar.MONTH); | ||
| int year = calendar.get(Calendar.YEAR); | ||
| picker = new DatePickerDialog(this, (view, year1, monthOfYear, dayOfMonth) -> | ||
| date.setText(String.format(getString(R.string.date_format), | ||
| dayOfMonth, monthOfYear + 1, year1)), year, month, day); | ||
| picker.show(); | ||
| }); | ||
| } | ||
|
|
||
| private void addItem(final String title, final String place, final String date, final String price, final String imgPath) { | ||
| if (!validate(title, place, date, price)) { | ||
| return; | ||
| } | ||
| final GoodsService service = getApplicationEx().getApiService(); | ||
| Good good = new Good(title, place, date, price, imgPath); | ||
| Call<Good> call = service.addGood(good); | ||
| progressBar.setVisibility(View.VISIBLE); | ||
| call.enqueue(new Callback<Good>() { | ||
| @Override | ||
| public void onResponse(Call<Good> call, Response<Good> response) { | ||
| progressBar.setVisibility(View.INVISIBLE); | ||
| if (response.isSuccessful()) { | ||
| openMainActivity(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onFailure(Call<Good> call, Throwable t) { | ||
| progressBar.setVisibility(View.INVISIBLE); | ||
| Snackbar.make(findViewById(R.id.item_view), R.string.post_failed, Snackbar.LENGTH_LONG).show(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private void openImage() { | ||
| final Intent intent = new Intent(); | ||
| intent.setType("image/*"); | ||
| intent.setAction(Intent.ACTION_GET_CONTENT); | ||
| startActivityForResult(intent, IMAGE_REQUEST); | ||
| } | ||
|
|
||
| @Override | ||
| public void onActivityResult(int requestCode, int resultCode, Intent data) { | ||
| super.onActivityResult(requestCode, resultCode, data); | ||
| if (requestCode == IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) { | ||
| new UploadImageTask().execute(data.getData()); | ||
| } | ||
| } | ||
|
|
||
| private void showImage(final String photoUrl) { | ||
| final int TARGET_WIDTH = 200; | ||
| final int TARGET_HEIGHT = 200; | ||
| if (!photoUrl.isEmpty()) { | ||
| Picasso.get().load(photoUrl).resize(TARGET_WIDTH, TARGET_HEIGHT).into((ImageView) findViewById(R.id.item_image)); | ||
| } | ||
| } | ||
|
|
||
| private String getFileExtension(Uri uri) { | ||
| ContentResolver contentResolver = Objects.requireNonNull(this).getContentResolver(); | ||
| MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton(); | ||
| return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri)); | ||
| } | ||
|
|
||
| public boolean validate(final String title, final String place, final String date, final String price) { | ||
| boolean valid = true; | ||
| if (Utils.validateString(title)) { | ||
| titleField.setError(null); | ||
| } else { | ||
| titleField.setError(getString(R.string.title_error)); | ||
| valid = false; | ||
| } | ||
|
|
||
| if (Utils.validateString(place)) { | ||
| placeField.setError(null); | ||
| } else { | ||
| placeField.setError(getString(R.string.place_error)); | ||
| valid = false; | ||
| } | ||
|
|
||
| if (!date.isEmpty()) { | ||
| dateField.setError(null); | ||
| } else { | ||
| dateField.setError(getString(R.string.date_error)); | ||
| valid = false; | ||
| } | ||
|
|
||
| if (Utils.validatePrice(price)) { | ||
| priceField.setError(null); | ||
| } else { | ||
| priceField.setError(getString(R.string.price_error)); | ||
| valid = false; | ||
| } | ||
|
|
||
| return valid; | ||
|
Comment on lines
+149
to
+178
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. завеликий методи виходить, варто розбити на менші валідатори |
||
| } | ||
|
|
||
| public boolean onOptionsItemSelected(@NonNull MenuItem item) { | ||
| openMainActivity(); | ||
| return true; | ||
| } | ||
|
|
||
| private void openMainActivity() { | ||
| final Intent myIntent = new Intent(getApplicationContext(), MainActivity.class); | ||
| startActivityForResult(myIntent, 0); | ||
| } | ||
|
|
||
| private App getApplicationEx() { | ||
| return ((App) Objects.requireNonNull(this.getApplication())); | ||
| } | ||
|
|
||
| private class UploadImageTask extends AsyncTask<Uri, Void, Void> { | ||
| protected void onPreExecute() { | ||
| progressBar.setVisibility(View.VISIBLE); | ||
| getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, | ||
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); | ||
| } | ||
|
|
||
| protected Void doInBackground(Uri... blobName) { | ||
falsefox13 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ServiceAccountCredentials credentials; | ||
| try { | ||
| credentials = ServiceAccountCredentials.fromStream(getResources().openRawResource(R.raw.bowlingsite)); | ||
| Storage storage = StorageOptions.newBuilder().setProjectId("bowlingsite") | ||
| .setCredentials(credentials) | ||
| .build() | ||
| .getService(); | ||
| final String bucketName = "www.bowling-iot.pp.ua"; | ||
| final Bucket bucket = storage.get(bucketName); | ||
| final String imgPath = "img/" + blobName[0].getLastPathSegment() + "." + getFileExtension(blobName[0]); | ||
| final BlobId blobId = BlobId.of(bucket.getName(), imgPath); | ||
| final InputStream imageStream = getContentResolver().openInputStream(blobName[0]); | ||
| final BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("image/jpeg").build(); | ||
| final Blob blob = storage.create(blobInfo, imageStream); | ||
| imgDownloadLink = blob.getMediaLink(); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| protected void onPostExecute(Void result) { | ||
| showImage(imgDownloadLink); | ||
| progressBar.setVisibility(View.INVISIBLE); | ||
| getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
цей метод троошки великий, треба подумати, якусь частину виокремити
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
чесно кажучи, не знайшла способу розділити цей метод. на мою думку 20 стрічок це не критично.