Skip to content

Commit

Permalink
hive,Flutter local notification, connectivity
Browse files Browse the repository at this point in the history
  • Loading branch information
Moktadir authored and Moktadir committed Aug 8, 2023
1 parent bf23471 commit 3380f4d
Show file tree
Hide file tree
Showing 27 changed files with 942 additions and 501 deletions.
6 changes: 6 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ android {
ndkVersion flutter.ndkVersion

compileOptions {
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
Expand All @@ -52,6 +53,7 @@ android {
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
multiDexEnabled true
}

buildTypes {
Expand All @@ -69,4 +71,8 @@ flutter {

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.google.android.material:material:1.9.0'
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
implementation 'androidx.window:window:1.0.0'
implementation 'androidx.window:window-java:1.0.0'
}
3 changes: 3 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="notification_channel" />
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
public final class GeneratedPluginRegistrant {
private static final String TAG = "GeneratedPluginRegistrant";
public static void registerWith(@NonNull FlutterEngine flutterEngine) {
try {
flutterEngine.getPlugins().add(new me.carda.awesome_notifications.AwesomeNotificationsPlugin());
} catch(Exception e) {
Log.e(TAG, "Error registering plugin awesome_notifications, me.carda.awesome_notifications.AwesomeNotificationsPlugin", e);
}
try {
flutterEngine.getPlugins().add(new io.flutter.plugins.firebase.firestore.FlutterFirebaseFirestorePlugin());
} catch(Exception e) {
Expand Down Expand Up @@ -55,6 +50,11 @@ public static void registerWith(@NonNull FlutterEngine flutterEngine) {
} catch(Exception e) {
Log.e(TAG, "Error registering plugin firebase_messaging, io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingPlugin", e);
}
try {
flutterEngine.getPlugins().add(new com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin());
} catch(Exception e) {
Log.e(TAG, "Error registering plugin flutter_local_notifications, com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin", e);
}
try {
flutterEngine.getPlugins().add(new net.jonhanson.flutter_native_splash.FlutterNativeSplashPlugin());
} catch(Exception e) {
Expand All @@ -65,6 +65,11 @@ public static void registerWith(@NonNull FlutterEngine flutterEngine) {
} catch(Exception e) {
Log.e(TAG, "Error registering plugin flutter_plugin_android_lifecycle, io.flutter.plugins.flutter_plugin_android_lifecycle.FlutterAndroidLifecyclePlugin", e);
}
try {
flutterEngine.getPlugins().add(new net.wolverinebeach.flutter_timezone.FlutterTimezonePlugin());
} catch(Exception e) {
Log.e(TAG, "Error registering plugin flutter_timezone, net.wolverinebeach.flutter_timezone.FlutterTimezonePlugin", e);
}
try {
flutterEngine.getPlugins().add(new io.flutter.plugins.pathprovider.PathProviderPlugin());
} catch(Exception e) {
Expand Down
1 change: 1 addition & 0 deletions animations/image_loader.json

Large diffs are not rendered by default.

Binary file added assets/images/no_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 28 additions & 26 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,6 @@ PODS:
- abseil/base/base_internal
- abseil/base/config
- abseil/meta/type_traits
- awesome_notifications (0.7.5-dev.3):
- Flutter
- IosAwnCore (= 0.7.5-dev.3)
- BoringSSL-GRPC (0.0.24):
- BoringSSL-GRPC/Implementation (= 0.0.24)
- BoringSSL-GRPC/Interface (= 0.0.24)
Expand Down Expand Up @@ -703,7 +700,7 @@ PODS:
- Firebase/Messaging (= 10.10.0)
- firebase_core
- Flutter
- FirebaseAppCheckInterop (10.12.0)
- FirebaseAppCheckInterop (10.13.0)
- FirebaseAuth (10.10.0):
- FirebaseAppCheckInterop (~> 10.0)
- FirebaseCore (~> 10.0)
Expand All @@ -714,7 +711,7 @@ PODS:
- FirebaseCoreInternal (~> 10.0)
- GoogleUtilities/Environment (~> 7.8)
- GoogleUtilities/Logger (~> 7.8)
- FirebaseCoreInternal (10.12.0):
- FirebaseCoreInternal (10.13.0):
- "GoogleUtilities/NSData+zlib (~> 7.8)"
- FirebaseFirestore (10.10.0):
- abseil/algorithm (~> 1.20220623.0)
Expand All @@ -729,7 +726,7 @@ PODS:
- "gRPC-C++ (~> 1.50.1)"
- leveldb-library (~> 1.22)
- nanopb (< 2.30910.0, >= 2.30908.0)
- FirebaseInstallations (10.12.0):
- FirebaseInstallations (10.13.0):
- FirebaseCore (~> 10.0)
- GoogleUtilities/Environment (~> 7.8)
- GoogleUtilities/UserDefaults (~> 7.8)
Expand All @@ -744,31 +741,35 @@ PODS:
- GoogleUtilities/UserDefaults (~> 7.8)
- nanopb (< 2.30910.0, >= 2.30908.0)
- Flutter (1.0.0)
- flutter_local_notifications (0.0.1):
- Flutter
- flutter_native_splash (0.0.1):
- Flutter
- flutter_timezone (0.0.1):
- Flutter
- FMDB (2.7.5):
- FMDB/standard (= 2.7.5)
- FMDB/standard (2.7.5)
- GoogleDataTransport (9.2.3):
- GoogleDataTransport (9.2.5):
- GoogleUtilities/Environment (~> 7.7)
- nanopb (< 2.30910.0, >= 2.30908.0)
- PromisesObjC (< 3.0, >= 1.2)
- GoogleUtilities/AppDelegateSwizzler (7.11.4):
- GoogleUtilities/AppDelegateSwizzler (7.11.5):
- GoogleUtilities/Environment
- GoogleUtilities/Logger
- GoogleUtilities/Network
- GoogleUtilities/Environment (7.11.4):
- GoogleUtilities/Environment (7.11.5):
- PromisesObjC (< 3.0, >= 1.2)
- GoogleUtilities/Logger (7.11.4):
- GoogleUtilities/Logger (7.11.5):
- GoogleUtilities/Environment
- GoogleUtilities/Network (7.11.4):
- GoogleUtilities/Network (7.11.5):
- GoogleUtilities/Logger
- "GoogleUtilities/NSData+zlib"
- GoogleUtilities/Reachability
- "GoogleUtilities/NSData+zlib (7.11.4)"
- GoogleUtilities/Reachability (7.11.4):
- "GoogleUtilities/NSData+zlib (7.11.5)"
- GoogleUtilities/Reachability (7.11.5):
- GoogleUtilities/Logger
- GoogleUtilities/UserDefaults (7.11.4):
- GoogleUtilities/UserDefaults (7.11.5):
- GoogleUtilities/Logger
- "gRPC-C++ (1.50.1)":
- "gRPC-C++/Implementation (= 1.50.1)"
Expand Down Expand Up @@ -832,7 +833,6 @@ PODS:
- gRPC-Core/Interface (= 1.50.1)
- gRPC-Core/Interface (1.50.1)
- GTMSessionFetcher/Core (3.1.1)
- IosAwnCore (0.7.5-dev.3)
- leveldb-library (1.22.2)
- nanopb (2.30909.0):
- nanopb/decode (= 2.30909.0)
Expand All @@ -856,7 +856,6 @@ PODS:
- SwiftyGif (5.4.4)

DEPENDENCIES:
- awesome_notifications (from `.symlinks/plugins/awesome_notifications/ios`)
- cloud_firestore (from `.symlinks/plugins/cloud_firestore/ios`)
- connectivity_plus (from `.symlinks/plugins/connectivity_plus/ios`)
- device_info_plus (from `.symlinks/plugins/device_info_plus/ios`)
Expand All @@ -865,7 +864,9 @@ DEPENDENCIES:
- firebase_core (from `.symlinks/plugins/firebase_core/ios`)
- firebase_messaging (from `.symlinks/plugins/firebase_messaging/ios`)
- Flutter (from `Flutter`)
- flutter_local_notifications (from `.symlinks/plugins/flutter_local_notifications/ios`)
- flutter_native_splash (from `.symlinks/plugins/flutter_native_splash/ios`)
- flutter_timezone (from `.symlinks/plugins/flutter_timezone/ios`)
- path_provider_foundation (from `.symlinks/plugins/path_provider_foundation/darwin`)
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`)
- sqflite (from `.symlinks/plugins/sqflite/ios`)
Expand All @@ -890,7 +891,6 @@ SPEC REPOS:
- "gRPC-C++"
- gRPC-Core
- GTMSessionFetcher
- IosAwnCore
- leveldb-library
- nanopb
- PromisesObjC
Expand All @@ -899,8 +899,6 @@ SPEC REPOS:
- SwiftyGif

EXTERNAL SOURCES:
awesome_notifications:
:path: ".symlinks/plugins/awesome_notifications/ios"
cloud_firestore:
:path: ".symlinks/plugins/cloud_firestore/ios"
connectivity_plus:
Expand All @@ -917,8 +915,12 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/firebase_messaging/ios"
Flutter:
:path: Flutter
flutter_local_notifications:
:path: ".symlinks/plugins/flutter_local_notifications/ios"
flutter_native_splash:
:path: ".symlinks/plugins/flutter_native_splash/ios"
flutter_timezone:
:path: ".symlinks/plugins/flutter_timezone/ios"
path_provider_foundation:
:path: ".symlinks/plugins/path_provider_foundation/darwin"
shared_preferences_foundation:
Expand All @@ -928,7 +930,6 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
abseil: 926fb7a82dc6d2b8e1f2ed7f3a718bce691d1e46
awesome_notifications: 1b6393d6ce02878dc50d0ae87baf7bdbc9d802a2
BoringSSL-GRPC: 3175b25143e648463a56daeaaa499c6cb86dad33
cloud_firestore: 818ebb1a8235177a0dcf7005c14aed5408b8342c
connectivity_plus: 413a8857dd5d9f1c399a39130850d02fe0feaf7e
Expand All @@ -940,22 +941,23 @@ SPEC CHECKSUMS:
firebase_auth: 9905bc3d82328b5050a8b7cb410a959f150b6549
firebase_core: 85b6664038311940ad60584eaabc73103c61f5de
firebase_messaging: c55f70dd48a998dea00a29ccf94572e1e4d454b2
FirebaseAppCheckInterop: f95a4feb9089867aff1a4bdc2ce309137e07736a
FirebaseAppCheckInterop: 5e12dc623d443dedffcde9c6f3ed41510125d8ef
FirebaseAuth: 5ddbe23ebc4e647469261f5c59cd12a04f37c8e6
FirebaseCore: d027ff503d37edb78db98429b11f580a24a7df2a
FirebaseCoreInternal: 950500ad8a08963657f6d8c67b579740c06d6aa1
FirebaseCoreInternal: b342e37cd4f5b4454ec34308f073420e7920858e
FirebaseFirestore: b3bb12a497c9d13e80ec3158dbb75ded03592e8d
FirebaseInstallations: 7b99ef103f013624444c614397038219c45f8e63
FirebaseInstallations: b28af1b9f997f1a799efe818c94695a3728c352f
FirebaseMessaging: 8a3b9a8b98ce72a42d22e69865cf662e38d2d6f5
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
flutter_local_notifications: 0c0b1ae97e741e1521e4c1629a459d04b9aec743
flutter_native_splash: 52501b97d1c0a5f898d687f1646226c1f93c56ef
flutter_timezone: ffb07bdad3c6276af8dada0f11978d8a1f8a20bb
FMDB: 2ce00b547f966261cd18927a3ddb07cb6f3db82a
GoogleDataTransport: f0308f5905a745f94fb91fea9c6cbaf3831cb1bd
GoogleUtilities: c63691989bf362ba0505507da00eeb326192e83e
GoogleDataTransport: 54dee9d48d14580407f8f5fbf2f496e92437a2f2
GoogleUtilities: 13e2c67ede716b8741c7989e26893d151b2b2084
"gRPC-C++": 0968bace703459fd3e5dcb0b2bed4c573dbff046
gRPC-Core: 17108291d84332196d3c8466b48f016fc17d816d
GTMSessionFetcher: e8647203b65cee28c5f73d0f473d096653945e72
IosAwnCore: edf31913fd18960ebcde7c05912dc860ca2b63b2
leveldb-library: f03246171cce0484482ec291f88b6d563699ee06
nanopb: b552cce312b6c8484180ef47159bc0f65a1f0431
path_provider_foundation: eaf5b3e458fc0e5fbb9940fb09980e853fe058b8
Expand Down
3 changes: 3 additions & 0 deletions ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import Flutter
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as UNUserNotificationCenterDelegate
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
48 changes: 48 additions & 0 deletions lib/app/data/local/my_hive.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'package:getx_standard/app/modules/home-with-restAPI/model/posts.dart';
import 'package:hive_flutter/hive_flutter.dart';

class MyHive {
// Prevent making an instance of this class
MyHive._();

// Hive box to store movie data
static late Box<Posts> _postBox;

// Box name, it's like the table name
static const String _postBoxName = 'posts';

/// Initialize local db (HIVE)
/// Pass testPath only if you are testing hive
static Future<void> init(
{Function(HiveInterface)? registerAdapters, String? testPath}) async {
if (testPath != null) {
Hive.init(testPath);
} else {
await Hive.initFlutter();
}
await registerAdapters?.call(Hive);
await initPostsBox();
}

/// Initialize post box
static Future<void> initPostsBox() async {
_postBox = await Hive.openBox<Posts>(_postBoxName);
}

/// Save all posts to the database
static Future<void> saveAllPosts(List<Posts> posts) async {
try {
await _postBox.clear(); // Clear existing data
await _postBox.addAll(posts); // Add all posts to Hive
} catch (error) {
// Handle error
}
}

/// Get all posts from Hive
static List<Posts> getAllPosts() {
final posts = _postBox.values.toList();
return posts
.cast<Posts>(); // Cast the list to the correct type (List<Posts>)
}
}
54 changes: 29 additions & 25 deletions lib/app/modules/home-with-restAPI/controllers/home_controller.dart
Original file line number Diff line number Diff line change
@@ -1,49 +1,53 @@
import 'package:get/get.dart';
import 'package:getx_standard/app/components/custom_snackbar.dart';
import 'package:getx_standard/app/service/network_connectivity.dart';

import '../../../components/navbar/navbar_controller.dart';
import '../../../data/local/my_hive.dart';
import '../../../service/REST/api_urls.dart';
import '../../../service/REST/dio_client.dart';
import '../../../service/handler/exception_handler.dart';
import '../bindings/home_binding.dart';
import '../model/posts.dart';
import '../views/post_detail_view.dart';

class HomeController extends GetxController with ExceptionHandler {
final navController = Get.put(NavbarController());

///GET POST LIST
RxString title = "".obs;
RxString body = "".obs;

/// GET POST LIST
final postList = RxList<Posts>();

getPostList() async {
showLoading();

var response =
await DioClient().get(url: ApiUrl.allPosts).catchError(handleError);

if (response == null) return;
if (await NetworkConnectivity.isNetworkAvailable()) {
// Fetch posts from the API
var response =
await DioClient().get(url: ApiUrl.allPosts).catchError(handleError);

postList
.assignAll((response as List).map((e) => Posts.fromJson(e)).toList());
if (response == null) return;

hideLoading();
}
postList
.assignAll((response as List).map((e) => Posts.fromJson(e)).toList());

/// GET POST DETAIL
String title = "";
String body = "";
// Save fetched posts to Hive for future use
await MyHive.saveAllPosts(postList);

getPostDetail(int? id) async {
showLoading();
var response = await DioClient()
.get(url: "${ApiUrl.postDetail}$id")
.catchError(handleError);
hideLoading();
} else {
// If offline, try to load from Hive
CustomSnackBar.showCustomToast(message: "No network!");
var posts = MyHive.getAllPosts();

if (response == null) return;
if (posts.isNotEmpty) {
// Use posts from Hive if available
postList.assignAll(posts);

title = response["title"].toString();
body = response["body"].toString();
hideLoading();
Get.to(() => const PostDetailView(), binding: HomeBinding());
hideLoading();
return;
}
}
}

@override
Expand All @@ -53,4 +57,4 @@ class HomeController extends GetxController with ExceptionHandler {

super.onReady();
}
}
}
Loading

0 comments on commit 3380f4d

Please sign in to comment.